1<?php
2/**
3 * DokuWiki Plugin youtrack (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Anika Henke <anika@zopa.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14class syntax_plugin_youtrack_list extends DokuWiki_Syntax_Plugin {
15    /**
16     * @return string Syntax mode type
17     */
18    public function getType() {
19        return 'substition';
20    }
21    /**
22     * @return string Paragraph type
23     */
24    public function getPType() {
25        return 'block';
26    }
27    /**
28     * @return int Sort order - Low numbers go before high numbers
29     */
30    public function getSort() {
31        return 200;
32    }
33
34    /**
35     * Connect lookup pattern to lexer.
36     *
37     * @param string $mode Parser mode
38     */
39    public function connectTo($mode) {
40        $this->Lexer->addSpecialPattern('{{youtrack-list>.*?}}',$mode,'plugin_youtrack_list');
41    }
42
43    /**
44     * Handle matches of the youtrack syntax
45     *
46     * @param string $match The match of the syntax
47     * @param int    $state The state of the handler
48     * @param int    $pos The position in the document
49     * @param Doku_Handler    $handler The handler
50     * @return array Data for the renderer
51     */
52    public function handle($match, $state, $pos, Doku_Handler &$handler){
53        $ytissues = array();
54        $issues = array();
55
56        list($tmp, $match) = explode('>', substr($match, 0, -2), 2); // strip markup
57        list($filter, $cols) = explode('|', $match, 2); // split filter and columns from rest of match
58        if (empty($filter) || empty($cols)) return false;
59        $cols = array_map('hsc', array_map('trim', explode(',', $cols)));
60
61        $yt = $this->loadHelper('youtrack');
62
63        if($yt) {
64            $ytissues = $yt->getIssues($filter);
65        }
66        if ($ytissues === false) {
67            return false;
68        }
69
70        foreach ($ytissues as $issue) {
71            $issueData = array();
72
73            foreach ($cols as $col) {
74                $fieldFound = false;
75                $id = (string) $issue->attributes()->id;
76
77                foreach($issue as $field) {
78                    if ($field->attributes()->name == $col) {
79                        $fieldFound = true;
80                        $value = (string) $field->value;
81                        $fullname = (string) $field->value->attributes()->fullName;
82
83                        // if 13 digit number, very likely to be date (timestamp in milliseconds)
84                        if(preg_match('/^\d{13}$/', $value)) {
85                            $issueData[$col] = date($this->getConf('date_format'), $value/1000);
86                        // if a field has a fullName attribute, it is better to use it
87                        } elseif($fullname) {
88                            $issueData[$col] = $fullname;
89                        } else {
90                            $issueData[$col] = $value;
91                        }
92                    }
93                }
94
95                if ($col == 'ID') {
96                    $issueData[$col] = $id;
97                } elseif (!$fieldFound) {
98                    msg('Field "'.$col.'" not found for issue "'.$id.'"', -1);
99                }
100            }
101
102            $issues[] = $issueData;
103        }
104
105        return array($issues, $cols);
106    }
107
108    /**
109     * Render xhtml output or metadata
110     *
111     * @param string         $mode      Renderer mode (supported modes: xhtml)
112     * @param Doku_Renderer  $renderer  The renderer
113     * @param array          $data      The data from the handler() function
114     * @return bool If rendering was successful.
115     */
116    public function render($mode, Doku_Renderer &$renderer, $data) {
117        list($issues, $cols) = $data;
118
119        if (count($issues) == 0) {
120            global $lang;
121            $renderer->p_open();
122            $renderer->cdata($lang['nothingfound']);
123            $renderer->p_close();
124            return true;
125        }
126
127        $yt = $this->loadHelper('youtrack');
128        $yt->renderIssueTable($renderer, $issues, $cols);
129
130        return true;
131    }
132}
133
134// vim:ts=4:sw=4:et:
135