1<?php
2/**
3 * Redissue Syntax Plugin: Insert a link to redmine's issue
4 *
5 * @author Algorys
6 */
7
8if (!defined('DOKU_INC')) die();
9require 'vendor/php-redmine-api/lib/autoload.php';
10
11class DokuwikiRedmine {
12    const RI_IMPERSONATE = 4;
13    public $client;
14
15    function connect($url, $apiKey, $impersonate, $user){
16        $this->client = new Redmine\Client($url, $apiKey);
17        if ($impersonate == self::RI_IMPERSONATE) {
18            $this->client->setImpersonateUser($user);
19        }
20
21    }
22
23    function getProjectIdentifier($project_name) {
24            $project_id = $this->client->project->getIdByName($project_name);
25            $project = $this->client->project->show($project_id);
26            return $project['project']['identifier'];
27    }
28
29    function getIssue($issue_id){
30        return $this->client->issue->show($issue_id);
31    }
32
33    function getStatuses(){
34        return $this->client->issue_status->all();
35    }
36
37    function getIdByUsername($username){
38        $all_users = $this->client->user->all(['limit' => '500']);
39        for ($i = 0; $i < count($all_users['users']); $i++) {
40            $cur_name = $all_users['users'][$i]['firstname'] . ' ' . $all_users['users'][$i]['lastname'];
41            $pos = strpos($cur_name, $username);
42            if ($pos !== false)
43                return $all_users['users'][$i]['id'];
44        }
45        return null;
46    }
47    function getDateAndTime($dateTime){
48        $dateTimeExploded = explode('T', $dateTime);
49        $timeExploded = explode('Z', $dateTimeExploded[1]);
50        return ['date' => $dateTimeExploded[0], 'time' => $timeExploded[0]];
51    }
52
53    function getDatesTimesIssue($issue){
54        $created = $issue['issue']['created_on'];
55        $updated = $issue['issue']['updated_on'];
56        $closed = $issue['issue']['closed_on'];
57        if($closed){
58            $closedTime = $this->getDateAndTime($closed);
59        } else {
60            $closedTime = $closed;
61        }
62        $dates_times = [
63            'created' => $this->getDateAndTime($created),
64            'updated' => $this->getDateAndTime($updated),
65            'closed' => $closedTime,
66        ];
67        return $dates_times;
68    }
69
70    function getPriorityColor($id_priority) {
71        $all_prio = $this->client->issue_priority->all();
72        $normal_prio = 0;
73        // Get the normal index and current index
74        for ($i = 0; $i < count($all_prio['issue_priorities']); $i++) {
75            $current_prio = $all_prio['issue_priorities'][$i];
76            if ($current_prio['is_default'] == 1) {
77                $normal_prio = $i;
78            }
79            if($current_prio['id'] == $id_priority){
80                $index_prio = $i;
81            }
82        }
83        $min_prio = 0;
84        $low_prio = $normal_prio - 1;
85        $high_prio = $normal_prio + 1;
86        $critical_prio = count($all_prio['issue_priorities']) - 1;
87        if($index_prio == $normal_prio) {
88           $color_prio = 'success';
89        }
90        elseif($index_prio == $min_prio) {
91            $color_prio = 'info';
92        }
93        elseif($index_prio < $normal_prio && $index_prio > $min_prio) {
94            $color_prio = 'primary';
95        }
96        elseif($index_prio > $normal_prio && $index_prio < $critical_prio) {
97            $color_prio = 'warning';
98        }
99        else {
100            $color_prio = 'danger';
101        }
102        return $color_prio;
103    }
104
105    function getIsClosedValue($statusId){
106        $statuses = $this->getStatuses();
107        for($i = 0; $i < count($statuses['issue_statuses']); $i++) {
108            if($statuses['issue_statuses'][$i]['id'] == $statusId) {
109                $isClosed = $statuses['issue_statuses'][$i]['is_closed'];
110            }
111        }
112        return $isClosed;
113    }
114}
115
116