1<?php
2/**
3 *
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Andreas Gohr <andi@splitbrain.org>
6 */
7// must be run within Dokuwiki
8if(!defined('DOKU_INC')) die();
9
10/**
11 * Class action_plugin_data
12 */
13class action_plugin_dataau extends DokuWiki_Action_Plugin {
14
15    /**
16     * will hold the dataau helper plugin
17     * @var helper_plugin_data
18     */
19    var $dthlp = null;
20
21    /**
22     * Constructor. Load helper plugin
23     */
24    function __construct(){
25        $this->dthlp = plugin_load('helper', 'dataau');
26    }
27
28    /**
29     * Registers a callback function for a given event
30     */
31    function register(Doku_Event_Handler $controller) {
32        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, '_handle');
33        $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, '_editbutton');
34        $controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, '_editform');
35        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_edit_post');
36        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_handle_ajax');
37    }
38
39    /**
40     * Handles the page write event and removes the database info
41     * when the plugin code is no longer in the source
42     *
43     * @param Doku_Event $event
44     * @param null       $param
45     */
46    function _handle(Doku_Event $event, $param){
47        $dataau = $event->dataau;
48        if(strpos($dataau[0][1],'dataentry') !== false) return; // plugin seems still to be there
49
50        $sqlite = $this->dthlp->_getDB();
51        if(!$sqlite) return;
52        $id = ltrim($dataau[1].':'.$dataau[2],':');
53
54        // get page id
55        $res = $sqlite->query('SELECT pid FROM pages WHERE page = ?',$id);
56        $pid = (int) $sqlite->res2single($res);
57        if(!$pid) return; // we have no dataau for this page
58
59        $sqlite->query('DELETE FROM dataau WHERE pid = ?',$pid);
60        $sqlite->query('DELETE FROM pages WHERE pid = ?',$pid);
61    }
62
63    /**
64     * @param Doku_Event $event
65     * @param null       $param
66     */
67    function _editbutton($event, $param) {
68        if ($event->dataau['target'] !== 'plugin_dataau') {
69            return;
70        }
71
72        $event->dataau['name'] = $this->getLang('dataentry');
73    }
74
75    /**
76     * @param Doku_Event $event
77     * @param null       $param
78     */
79    function _editform(Doku_Event $event, $param) {
80        global $TEXT;
81        if ($event->dataau['target'] !== 'plugin_dataau') {
82            // Not a data edit
83            return;
84        }
85
86        $event->stopPropagation();
87        $event->preventDefault();
88        unset($event->dataau['intro_locale']);
89        $event->dataau['media_manager'] = false;
90
91        echo $this->locale_xhtml('edit_intro' . ($this->getConf('edit_content_only') ? '_contentonly' : ''));
92
93        require_once 'renderer_dataau_edit.php';
94        $Renderer = new Doku_Renderer_plugin_dataau_edit();
95        $Renderer->form = $event->dataau['form'];
96
97        // Loop through the instructions
98        $instructions = p_get_instructions($TEXT);
99        foreach ( $instructions as $instruction ) {
100            // Execute the callback against the Renderer
101            call_user_func_array(array($Renderer, $instruction[0]),$instruction[1]);
102        }
103    }
104
105    /**
106     * @param Doku_Event $event
107     */
108    function _handle_edit_post(Doku_Event $event) {
109        if (!isset($_POST['dataau_edit'])) {
110            return;
111        }
112        global $TEXT;
113
114        require_once 'syntax/entry.php';
115        $TEXT = syntax_plugin_dataau_entry::editToWiki($_POST['dataau_edit']);
116    }
117
118    /**
119     * @param Doku_Event $event
120     */
121    function _handle_ajax(Doku_Event $event) {
122        if ($event->dataau !== 'dataau_page') {
123            return;
124        }
125
126        $event->stopPropagation();
127        $event->preventDefault();
128
129        $type = substr($_REQUEST['aliastype'], 10);
130        $aliases = $this->dthlp->_aliases();
131
132        if (!isset($aliases[$type])) {
133            echo 'Unknown type';
134            return;
135        }
136
137        if ($aliases[$type]['type'] !== 'page') {
138            echo 'AutoCompletion is only supported for page types';
139            return;
140        }
141
142        if (substr($aliases[$type]['postfix'], -1, 1) === ':') {
143            // Resolve namespace start page ID
144            global $conf;
145            $aliases[$type]['postfix'] .= $conf['start'];
146        }
147
148        $search = $_REQUEST['search'];
149
150        $c_search = $search;
151        $in_ns = false;
152        if (!$search) {
153            // No search given, so we just want all pages in the prefix
154            $c_search = $aliases[$type]['prefix'];
155            $in_ns = true;
156        }
157        $pages = ft_pageLookup($c_search, $in_ns, false);
158
159        $regexp = '/^';
160        if ($aliases[$type]['prefix'] !== '') {
161            $regexp .= preg_quote($aliases[$type]['prefix'], '/');
162        }
163        $regexp .= '([^:]+)';
164        if ($aliases[$type]['postfix'] !== '') {
165            $regexp .= preg_quote($aliases[$type]['postfix'], '/');
166        }
167        $regexp .= '$/';
168
169        $result = array();
170        foreach ($pages as $page => $title) {
171            $id = array();
172            if (!preg_match($regexp, $page, $id)) {
173                // Does not satisfy the postfix and prefix criteria
174                continue;
175            }
176
177            $id = $id[1];
178
179            if ($search !== '' &&
180                stripos($id, cleanID($search)) === false &&
181                stripos($title, $search) === false) {
182                // Search string is not in id part or title
183                continue;
184            }
185
186            if ($title === '') {
187                $title = utf8_ucwords(str_replace('_', ' ', $id));
188            }
189            $result[hsc($id)] = hsc($title);
190        }
191
192        $json = new JSON();
193        header('Content-Type: application/json');
194        echo $json->encode($result);
195    }
196}
197