xref: /plugin/bez/struct/BezType.php (revision dad2347cece7a8aa1058e56084fa8a76a92644f0)
1<?php
2
3namespace dokuwiki\plugin\bez\struct;
4
5use dokuwiki\plugin\struct\meta\ValidationException;
6use dokuwiki\plugin\struct\types\AbstractMultiBaseType;
7
8class BezType extends AbstractMultiBaseType {
9
10    protected $config = array(
11        'autocomplete' => array(
12            'maxresult' => 10
13        ),
14    );
15
16    /**
17     * Output the stored data
18     *
19     * @param string $value the value stored in the database
20     * @param \Doku_Renderer $R the renderer currently used to render the data
21     * @param string $mode The mode the output is rendered in (eg. XHTML)
22     * @return bool true if $mode could be satisfied
23     */
24    public function renderValue($value, \Doku_Renderer $R, $mode) {
25        $title = $value;
26        $id = substr($value, 1);//remove #
27        if ($id[0] == 'z') {
28            $id = substr($id, 1);
29            $wl = wl("bez:task:tid:$id");
30        } else {
31            $wl = wl("bez:thread:id:$id");
32        }
33
34        $R->doc .= '<a href="'.$wl.'">'.$title.'</a>';
35
36
37        return true;
38    }
39
40    /**
41     * Cleans the link
42     *
43     * @param string $rawvalue
44     * @return string
45     */
46    public function validate($rawvalue) {
47        $value = trim($rawvalue);
48
49        $id = substr($value, 1);//remove #
50        if ($id[0] == 'z') {
51            $id = substr($id, 1);
52            $table = 'task';
53        } else {
54            $table = 'thread';
55        }
56
57        if (!is_numeric($id)) {
58            throw new ValidationException('Invalid BEZ reference');
59        }
60
61
62        /** @var helper_plugin_sqlite $sqlite */
63        $sqlite = plugin_load('helper', 'bez_db')->getDB();
64        $res = $sqlite->query("SELECT COUNT(*) FROM $table WHERE id=?", $id);
65        $count = $res->fetchColumn();
66        if ($count == 0) {
67            throw new ValidationException(ucfirst($table) . " with id: $id doesn't exists.");
68        }
69
70        return $rawvalue;
71    }
72
73    /**
74     * Autocompletion support for pages
75     *
76     * @return array
77     */
78    public function handleAjax() {
79        global $INPUT;
80
81        // check minimum length
82        $lookup = trim($INPUT->str('search'));
83        if(utf8_strlen($lookup) < 1) return array();
84        if ($lookup[0] != '#') return array();
85
86        $id = substr($lookup, 1);
87        if ($id[0] == 'z') {
88            $id = substr($id, 1);
89            $table = 'task';
90        } else {
91            $table = 'thread';
92        }
93
94        if (!is_numeric($id)) return array();
95
96
97        // results wanted?
98        $max = (int)$this->config['autocomplete']['maxresult'];
99        if($max <= 0) return array();
100
101        $bez_db_helper = plugin_load('helper', 'bez_db');
102
103        /** @var helper_plugin_sqlite $sqlite */
104        $sqlite = $bez_db_helper->getDB();
105        $sql = "SELECT id, state FROM $table WHERE id LIKE ? LIMIT $max";
106        $res = $sqlite->query($sql, $id . '%');
107
108        $result = array();
109        while ($row = $res->fetch(\PDO::FETCH_ASSOC)) {
110            $state = $bez_db_helper->getLang('state_' . $row['state']);
111
112            $name = $row['id'] . ' (' . $state . ')';
113            if ($table == 'task') {
114                $name = '#z' . $name;
115                $value = '#z' . $row['id'];
116            } else {
117                $name = '#' . $name;
118                $value = '#' . $row['id'];
119            }
120
121            $result[] = array(
122                'label' => $name,
123                'value' => $value
124            );
125        }
126
127        return $result;
128    }
129}