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