1<?php
2/*
3 * DokuWiki ActiveCollab Ticket Plugin
4 * Copyright (C) 2011  Tobias Sarnowski
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21// must be run within Dokuwiki
22if (!defined('DOKU_INC')) die();
23
24if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
25if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
26if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
27
28require_once DOKU_PLUGIN.'syntax.php';
29require_once DOKU_INC.'inc/form.php';
30
31/**
32 * @author Tobias Sarnowski
33 */
34class syntax_plugin_actickets extends DokuWiki_Syntax_Plugin {
35
36    const PATTERN_TICKET = '\d+';
37    const PATTERN_TICKET_GRP = '(\d+)';
38    const PATTERN_FULLTICKET = '\d+-\d+';
39    const PATTERN_FULLTICKET_GRP = '(\d+)-(\d+)';
40    const PATTERN_PROJECT = '{{Project:\d+}}';
41    const PATTERN_PROJECT_GRP = '{{Project:(\d+)}}';
42
43    /**
44     * @var int use to globaly define the project's ID
45     */
46    private $projectId = null;
47
48
49    public function getType() {
50        return 'substition';
51    }
52
53    public function getPType() {
54        return 'normal';  // wtf? docs doent tell me. normal|block|stack
55    }
56
57    public function getSort() {
58        return 32; // http://www.dokuwiki.org/devel:parser:getsort_list
59    }
60
61    public function connectTo($mode) {
62        $this->Lexer->addSpecialPattern(
63            $this->getConf('actickets.hash').self::PATTERN_FULLTICKET.'|'.
64            $this->getConf('actickets.hash').self::PATTERN_TICKET.'|'.
65            self::PATTERN_PROJECT
66            ,$mode,'plugin_actickets');
67    }
68
69    public function handle($match, $state, $pos, &$handler){
70        $data = array();
71        if (preg_match('/'.self::PATTERN_PROJECT_GRP.'/', $match, $matches)) {
72            $this->projectId = $matches[1];
73        } else if (preg_match('/'.$this->getConf('actickets.hash').self::PATTERN_FULLTICKET_GRP.'/', $match, $matches)) {
74            $data['ticketId'] = $matches[1];
75            $data['projectId'] = $matches[2];
76        } else if (preg_match('/'.$this->getConf('actickets.hash').self::PATTERN_TICKET_GRP.'/', $match, $matches)) {
77            $data['ticketId'] = $matches[1];
78            $data['projectId'] = $this->projectId;
79            if (is_null($this->projectId)) {
80                $data['original'] = $match;
81            }
82        } else {
83            throw new Exception("This should not happen, blame the actickets plugin.");
84        }
85        return $data;
86    }
87
88    public function render($mode, &$renderer, $data) {
89        if($mode != 'xhtml') return false;
90
91        if (!empty($data)) {
92            if (!is_null($data['projectId'])) {
93                $url = $this->getConf('actickets.url');
94                if (substr($url, -1) != '/') {
95                    $url .= '/';
96                }
97                $url .= 'public/index.php/projects/'.$data['projectId'].'/tickets/'.$data['ticketId'];
98                $renderer->doc .= '<a href="'.$url.'" target="_blank" class="acticket" title="Status not yet resolved.">';
99                $renderer->doc .= $this->getConf('actickets.hash').$data['ticketId'];
100                $renderer->doc .= '</a>';
101            } else {
102                $renderer->doc .= $data['original'];
103            }
104        }
105
106        return true;
107    }
108}
109
110// vim:ts=4:sw=4:et:
111