From 0781fd2d80182760e441b43ba7a9817a02786fc2 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+github@gmail.com>
Date: Thu, 25 Aug 2022 14:40:50 +0200
Subject: [PATCH] fixes #35

---
 TracToGitlabPlugin.php                 |  4 ++
 assets/style.scss                      | 10 ++++
 controllers/cgmembers.php              | 72 ++++++++++++++++++++++++++
 migrations/3_setup_cg_members_view.php | 20 +++++++
 plugin.manifest                        |  2 +-
 views/cgmembers/index.php              | 54 +++++++++++++++++++
 6 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 controllers/cgmembers.php
 create mode 100644 migrations/3_setup_cg_members_view.php
 create mode 100644 views/cgmembers/index.php

diff --git a/TracToGitlabPlugin.php b/TracToGitlabPlugin.php
index f8477b9..5b17310 100644
--- a/TracToGitlabPlugin.php
+++ b/TracToGitlabPlugin.php
@@ -134,6 +134,10 @@ final class TracToGitlabPlugin extends StudIPPlugin implements StandardPlugin, S
             'merge-requests',
             new Navigation(_('Merge Requests'), PluginEngine::getURL($this, [], 'mergerequests'))
         );
+        $navigation->addSubNavigation(
+            'cg-members',
+            new Navigation(_('Übersicht Zuständigkeiten'), PluginEngine::getURL($this, [], 'cgmembers'))
+        );
 
         Navigation::addItem('/gitlab-dashboard', $navigation);
     }
diff --git a/assets/style.scss b/assets/style.scss
index 3b84cd8..965afa5 100644
--- a/assets/style.scss
+++ b/assets/style.scss
@@ -13,3 +13,13 @@
         color: $light-gray-color-80;
     }
 }
+
+.cgmembers-list {
+    .cg-label {
+        background-color: var(--light-gray-color-20);
+        border: 1px solid var(--black);
+        display: inline-block;
+        font-weight: normal;
+        padding: 0 0.5em;
+    }
+}
diff --git a/controllers/cgmembers.php b/controllers/cgmembers.php
new file mode 100644
index 0000000..d0c9e6a
--- /dev/null
+++ b/controllers/cgmembers.php
@@ -0,0 +1,72 @@
+<?php
+final class CgmembersController extends TracToGitlab\Controller
+{
+    public function before_filter(&$action, &$args)
+    {
+        parent::before_filter($action, $args);
+
+        $this->activateNavigation('cg-members');
+        PageLayout::setTitle(_('Übersicht der Zuständigkeiten in der CoreGroup'));
+    }
+
+    public function index_action()
+    {
+        $this->groups = $this->getGroups();
+
+        $this->labels = [
+            'Check' => _('Dieses Kriterium muss erfüllt sein müssen und ein Plus erhalten haben, bevor der Entwicklungsbranch in den Hauptbranch überführt werden darf'),
+            'Veto'  => _('Ein nicht behobenes Veto in Form eines Minus dieser Zuständigkeit kann dazu führen, dass die Entwicklung wieder aus dem Hauptbranch ausgebaut werden muss.'),
+        ];
+    }
+
+    public function mail_action(string $group_id)
+    {
+        $groups = $this->getGroups();
+        if (!isset($groups[$group_id])) {
+            $this->redirect($this->indexURL());
+            return;
+        }
+
+        $group = $groups[$group_id];
+        $_SESSION['sms_data'] = [
+            'p_rec' => array_column($group['members'], 'username'),
+        ];
+
+        $this->redirect(URLHelper::getURL('dispatch.php/messages/write'));
+    }
+
+    private function extractLabelsFromName(string &$name): array
+    {
+        if (!preg_match('/\s\((.+)\)$/', $name, $matches)) {
+            return [];
+        }
+
+        $name = trim(str_replace($matches[0], '', $name));
+
+        return array_map('trim', array_filter(explode(',', $matches[1])));
+    }
+
+    private function getGroups(): array
+    {
+        $course_id = Config::get()->TRAC2GITLAB_CG_MEMBERS_COURSE_ID;
+        $course = Course::find($course_id);
+
+        if (!$course) {
+            throw new RuntimeException(_('Die in TRAC2GITLAB_CG_MEMBERS_COURSE_ID konfigurierte Veranstaltung existiert nicht'));
+        }
+
+        $result = [];
+        $course->statusgruppen->each(function (Statusgruppen $group) use (&$result) {
+            $name = (string) $group->name;
+            $labels = $this->extractLabelsFromName($name);
+
+            $result[$group->id] = [
+                'name'    => $name,
+                'labels'  => $labels,
+                'members' => $group->members->pluck('user'),
+            ];
+        });
+
+        return $result;
+    }
+}
diff --git a/migrations/3_setup_cg_members_view.php b/migrations/3_setup_cg_members_view.php
new file mode 100644
index 0000000..9731bdc
--- /dev/null
+++ b/migrations/3_setup_cg_members_view.php
@@ -0,0 +1,20 @@
+<?php
+final class SetupCgMembersView extends Migration
+{
+    public function up()
+    {
+        Config::get()->create('TRAC2GITLAB_CG_MEMBERS_COURSE_ID', [
+            'value'       => '',
+            'type'        => 'string',
+            'range'       => 'global',
+            'section'     => 'Trac2Gitlab',
+            'description' => 'Veranstaltung für die Darstellung der CG-Zuständigkeiten',
+        ]);
+    }
+
+    public function down()
+    {
+        Config::get()->delete('TRAC2GITLAB_CG_MEMBERS_COURSE_ID');
+
+    }
+}
diff --git a/plugin.manifest b/plugin.manifest
index 921fb27..ca5ae14 100644
--- a/plugin.manifest
+++ b/plugin.manifest
@@ -1,5 +1,5 @@
 pluginname=Trac to gitlab converter
 pluginclassname=TracToGitlabPlugin
 origin=UOL
-version=1.1.2
+version=1.2
 studipMinVersion=5.0
diff --git a/views/cgmembers/index.php b/views/cgmembers/index.php
new file mode 100644
index 0000000..69adc76
--- /dev/null
+++ b/views/cgmembers/index.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * @var CgmembersController $controller
+ * @var array $groups
+ * @var array $labels
+ */
+?>
+<table class="default cgmembers-list">
+    <caption><?= _('Übersicht der Zuständigkeiten innerhalb der CoreGroup') ?></caption>
+    <colgroup>
+        <col>
+        <col style="width: 24px">
+    </colgroup>
+<? foreach ($groups as $id => $group): ?>
+    <tbody>
+        <tr>
+            <th>
+                <?= htmlReady($group['name']) ?>
+            <? foreach ($group['labels'] as $label): ?>
+                <abbr class="cg-label" title="<?= htmlReady($labels[$label]) ?>">
+                    <?= htmlReady($label) ?>
+                </abbr>
+            <? endforeach; ?>
+            </th>
+            <th>
+                <a href="<?= $controller->mail($id) ?>" data-dialog>
+                    <?= Icon::create('mail')->asImg(tooltip2(sprintf(
+                        _('Nachricht an alle Personen mit der Zuständigkeit %s schreiben'),
+                        $group['name']
+                    ))) ?>
+                </a>
+            </th>
+        </tr>
+    <? foreach ($group['members'] as $user): ?>
+        <tr>
+            <td>
+                <a href="<?= URLHelper::getLink('dispatch.php/profile', ['username' => $user->username]) ?>">
+                    <?= Avatar::getAvatar($user->id)->getImageTag(Avatar::SMALL) ?>
+                    <?= htmlReady($user->getFullName()) ?>
+                </a>
+            </td>
+            <td class="actions">
+                <a href="<?= URLHelper::getLink('dispatch.php/messages/write', ['rec_uname' => $user->username]) ?>" data-dialog>
+                    <?= Icon::create('mail')->asImg(tooltip2(sprintf(
+                        _('Nachricht an %s schreiben'),
+                        $user->getFullName()
+                    ))) ?>
+                </a>
+            </td>
+        </tr>
+    <? endforeach; ?>
+    </tbody>
+<? endforeach; ?>
+</table>
-- 
GitLab