xref: /plugin/struct/types/Page.php (revision 650e9493401c8f9f1230c886e395fd0eafde9638)
1<?php
2namespace plugin\struct\types;
3
4/**
5 * Class Page
6 *
7 * Represents a single page in the wiki. Will be linked in output.
8 *
9 * @package plugin\struct\types
10 */
11class Page extends AbstractMultiBaseType {
12
13    protected $config = array(
14        'autocomplete' => array(
15            'mininput' => 2,
16            'maxresult' => 5,
17        ),
18    );
19
20    /**
21     * Output the stored data
22     *
23     * @param string|int $value the value stored in the database
24     * @param \Doku_Renderer $R the renderer currently used to render the data
25     * @param string $mode The mode the output is rendered in (eg. XHTML)
26     * @return bool true if $mode could be satisfied
27     */
28    public function renderValue($value, \Doku_Renderer $R, $mode) {
29        $link = cleanID($this->config['prefix'] . $value . $this->config['postfix']);
30        if(!$link) return true;
31
32        $R->internallink(":$link");
33        return true;
34    }
35
36    /**
37     * Autocompletion support for pages
38     *
39     * @return array
40     */
41    public function handleAjax() {
42        global $INPUT;
43
44        // check minimum length
45        $lookup = trim($INPUT->str('search'));
46        if(utf8_strlen($lookup) < $this->config['autocomplete']['mininput']) return array();
47
48        // results wanted?
49        $max = $this->config['autocomplete']['maxresult'];
50        if($max <= 0) return array();
51
52        $data = ft_pageLookup($lookup, true, useHeading('navigation'));
53        if(!count($data)) return array();
54
55        // this basically duplicates what we do in ajax_qsearch()
56        $result = array();
57        $counter = 0;
58        foreach($data as $id => $title){
59            if (useHeading('navigation')) {
60                $name = $title;
61            } else {
62                $ns = getNS($id);
63                if($ns){
64                    $name = noNS($id).' ('.$ns.')';
65                }else{
66                    $name = $id;
67                }
68            }
69            $result[] = array(
70                'label' => $name,
71                'value' => $id
72            );
73
74            $counter ++;
75            if($counter > $max) break;
76        }
77
78        return $result;
79    }
80
81}
82