1<?php
2/**
3 * Jirainfo action plugin for DokuWiki
4 *
5 * @author     Vadim Balabin <vadikflint@gmail.com>
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 */
8class action_plugin_jirainfo extends DokuWiki_Action_Plugin {
9
10    protected $fields = ['status', 'priority', 'issuetype', 'comment']; // Default fields for viewing in popover
11
12    function register(Doku_Event_Handler $controller) {
13        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,'_ajax_call');
14        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hookjs');
15        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'setConf');
16    }
17
18    /**
19     * handle ajax requests
20     */
21    public function _ajax_call(Doku_Event $event, $param)
22    {
23        if ($event->data !== 'plugin_jirainfo') {
24            return;
25        }
26        //no other ajax call handlers needed
27        $event->stopPropagation();
28        $event->preventDefault();
29        echo json_encode($this->fillDataTask());
30    }
31
32    /**
33     * request - get data a task
34     *
35     * @param string $key
36     *
37     * @return json
38     */
39    public function request(String $url)
40    {
41        $http = new DokuHTTPClient();
42        return $http->get($url);
43    }
44
45    public function getJiraApiUrl(String $key) {
46
47        $uri = parse_url($this->getConf('apiUrl'));
48        //Jira API URL endpoint must include the trailing slash
49        $path = (preg_match('/\w+\/$/', $uri['path'])) ? $uri['path'] : $uri['path'] . '/';
50
51        return sprintf('%s://%s:%s@%s/%s?fields=summary,%s',
52            $uri['scheme'],
53            $this->getConf('apiUser'),
54            $this->getConf('apiPass'),
55            $uri['host'].$path.'issue',
56            $key,
57            implode(",", $this->getFieldsRequest())
58        );
59    }
60
61    /**
62     * getFieldsRequest - returned fields for request
63     *
64     * @return array
65     */
66    public function getFieldsRequest() {
67        $arrFields = explode(",", $this->getConf('taskHideField'));
68        return array_diff($this->fields, $arrFields);
69    }
70
71    /**
72     * fillDataTask - to parse data about a task and returned in array
73     *
74     * @return Array
75     */
76    public function fillDataTask()
77    {
78        $res    = [];
79        $task   = trim($_POST['key']);
80        $fields = $this->getFieldsRequest();
81        $url    = $this->getJiraApiUrl($task);
82        $data   = $this->request($url);
83        $arr    = json_decode($data, true);
84
85        // If task not found or does not exist
86        if (!$arr) return ['errors' => sprintf($this->getlang('taskNotFound'), $task)];
87
88        $res = [
89            'key'      => $arr['key'],
90            'summary'  => $arr['fields']['summary'],
91            'issueUrl' => $this->getTaskUrl($task),
92        ];
93
94        if (in_array('status', $fields) && isset($arr['fields']['status'])) {
95            $res['status'] = [
96                'name'  => $arr['fields']['status']['name'],
97                'color' => $arr['fields']['status']['statusCategory']['colorName']
98            ];
99        }
100        if (in_array('priority', $fields) && isset($arr['fields']['priority'])) {
101            $res['priority'] = [
102                'name'    => $arr['fields']['priority']['name'],
103                'iconUrl' => $arr['fields']['priority']['iconUrl']
104            ];
105        }
106        if (in_array('issuetype', $fields) && isset($arr['fields']['issuetype'])) {
107            $res['issuetype'] = [
108                'name'    => $arr['fields']['issuetype']['name'],
109                'iconUrl' => $arr['fields']['issuetype']['iconUrl']
110            ];
111        }
112        if (in_array('comment', $fields) && isset($arr['fields']['comment'])) {
113            $res['totalComments'] = $arr['fields']['comment']['total'];
114        }
115        return $res;
116    }
117
118    /**
119     * getTaskUrl
120     *
121     * @param  String $key task key
122     *
123     * @return String
124     */
125    public function getTaskUrl (String $key = null)
126    {
127        $arrURL = parse_url($this->getConf('apiUrl'));
128        return $arrURL['scheme'] .'://'. $arrURL['host'] .'/browse/'. $key;
129    }
130
131    public function _hookjs(Doku_Event $event)
132    {
133        $event->data['script'][] = array(
134            'type'    => 'text/javascript',
135            'charset' => 'utf-8',
136            '_data'   => '',
137            'src'     => DOKU_BASE."lib/plugins/jirainfo/src/popper.min.js");
138    }
139
140    /**
141     * setConf - set config options in $JSINFO
142     *
143     */
144    public function setConf()
145    {
146        global $JSINFO;
147
148        $JSINFO['jirainfo'] = [
149            'placement' => $this->getConf('popoverPlacement'),
150            'trigger'   => $this->getConf('popoverTrigger'),
151            'animation' => $this->getConf('popoverAnimation')
152        ];
153    }
154 }
155