1<?php
2// must be run within DokuWiki
3if(!defined('DOKU_INC')) die();
4
5if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
6require_once DOKU_PLUGIN.'syntax.php';
7/**
8 * All DokuWiki plugins to extend the parser/rendering mechanism
9 * need to inherit from this class
10 */
11class syntax_plugin_bez_qlink extends DokuWiki_Syntax_Plugin {
12
13    function getType() { return 'substition'; }
14    function getSort() { return 34; }
15	function connectTo($mode) {
16		$this->Lexer->addSpecialPattern('#(?:z|zk|k)?[0-9]+',$mode,'plugin_bez_qlink');
17	}
18
19
20    function handle($match, $state, $pos, Doku_Handler $handler) {
21        preg_match('/#([a-z]*)([0-9]+)/', $match, $matches);
22        list(,$code, $id) = $matches;
23
24        $anchor = '';
25        $id_key = 'id';
26        switch ($code) {
27            case '':
28                $table = 'thread';
29                break;
30            case 'k':
31                $table = 'thread';
32                $anchor = '#k' . $id;
33
34                /** @var helper_plugin_sqlite $sqlite */
35                $sqlite = plugin_load('helper', 'bez_db')->getDB();
36                $res = $sqlite->query("SELECT thread_id FROM thread_comment WHERE id=?", $id);
37                $id = $res->fetchColumn();
38                break;
39            case 'z':
40                $table = 'task';
41                $id_key = 'tid';
42                break;
43            case 'zk':
44                $table = 'task';
45                $id_key = 'tid';
46                $anchor = '#zk' . $id;
47
48                /** @var helper_plugin_sqlite $sqlite */
49                $sqlite = plugin_load('helper', 'bez_db')->getDB();
50                $res = $sqlite->query("SELECT task_id FROM task_comment WHERE id=?", $id);
51                $id = $res->fetchColumn();
52                break;
53        }
54
55        return array($match, $table, $id_key, $id, $anchor);
56    }
57
58    function render($mode, Doku_Renderer $renderer, $data) {
59		if ($mode == 'xhtml') {
60			$id = $_GET['id'];
61			$ex = explode(':', $id);
62
63			$lang_code = '';
64			/*english namespace*/
65			switch($ex[0]) {
66				case 'en':
67					$lang_code = $ex[0].':';
68			}
69
70            list($match, $table, $id_key, $id, $anchor) = $data;
71            $renderer->doc .= '<a href="?id='.$lang_code.'bez:'.$table.':'.$id_key.':'.$id.$anchor.'">'.$match.'</a>';
72
73			return true;
74		}
75		return false;
76    }
77}
78