xref: /plugin/data/action.php (revision 894aba355e0f1bcffe0374be566b1c3c46c60d97)
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();
9require_once(DOKU_PLUGIN.'action.php');
10
11require_once DOKU_PLUGIN.'data/bureaucracy_field.php';
12
13class action_plugin_data extends DokuWiki_Action_Plugin {
14
15    /**
16     * will hold the data helper plugin
17     */
18    var $dthlp = null;
19
20    /**
21     * Constructor. Load helper plugin
22     */
23    function action_plugin_data(){
24        $this->dthlp =& plugin_load('helper', 'data');
25    }
26
27    /**
28     * Registers a callback function for a given event
29     */
30    function register(&$controller) {
31        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, '_handle');
32        $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, '_editbutton');
33        $controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, '_editform');
34        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_edit_post');
35        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_handle_ajax');
36    }
37
38    /**
39     * Handles the page write event and removes the database info
40     * when the plugin code is no longer in the source
41     */
42    function _handle(&$event, $param){
43        $data = $event->data;
44        if(strpos($data[0][1],'dataentry') !== false) return; // plugin seems still to be there
45
46        $sqlite = $this->dthlp->_getDB();
47        if(!$sqlite) return;
48        $id = ltrim($data[1].':'.$data[2],':');
49
50        // get page id
51        $res = $sqlite->query('SELECT pid FROM pages WHERE page = ?',$id);
52        $pid = (int) sqlite_fetch_single($res);
53        if(!$pid) return; // we have no data for this page
54
55        $sqlite->query('DELETE FROM data WHERE pid = ?',$pid);
56        $sqlite->query('DELETE FROM pages WHERE pid = ?',$pid);
57    }
58
59    function _editbutton(&$event, $param) {
60        if ($event->data['target'] !== 'plugin_data') {
61            return;
62        }
63
64        $event->data['name'] = $this->getLang('dataentry');
65    }
66
67    function _editform(&$event, $param) {
68        global $TEXT;
69        if ($event->data['target'] !== 'plugin_data') {
70            // Not a data edit
71            return;
72        }
73
74        $event->stopPropagation();
75        $event->preventDefault();
76        unset($event->data['intro_locale']);
77        $event->data['media_manager'] = false;
78
79        echo $this->locale_xhtml('edit_intro' . ($this->getConf('edit_content_only') ? '_contentonly' : ''));
80
81        require_once 'renderer_data_edit.php';
82        $Renderer = new Doku_Renderer_plugin_data_edit();
83        $Renderer->form = $event->data['form'];
84
85        // Loop through the instructions
86        $instructions = p_get_instructions($TEXT);
87        foreach ( $instructions as $instruction ) {
88            // Execute the callback against the Renderer
89            call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
90        }
91    }
92
93    function _handle_edit_post($event) {
94        if (!isset($_POST['data_edit'])) {
95            return;
96        }
97        global $TEXT;
98
99        require_once 'syntax/entry.php';
100        $TEXT = syntax_plugin_data_entry::editToWiki($_POST['data_edit']);
101    }
102
103    function _handle_ajax($event) {
104        if (strpos($event->data, 'data_page_') !== 0) {
105            return;
106        }
107        $event->preventDefault();
108
109        $type = substr($event->data, 10);
110        $aliases = $this->dthlp->_aliases();
111        if (!isset($aliases[$type])) {
112            echo 'Unknown type';
113            return;
114        }
115        if ($aliases[$type]['type'] !== 'page') {
116            echo 'AutoCompletion is only supported for page types';
117            return;
118        }
119
120        if (substr($aliases[$type]['postfix'], -1, 1) === ':') {
121            // Resolve namespace start page ID
122            global $conf;
123            $aliases[$type]['postfix'] .= $conf['start'];
124        }
125
126        require_once(DOKU_INC.'inc/fulltext.php');
127        $search = cleanID($_POST['search']);
128        $pages = ft_pageLookup($search, false);
129        $result = array();
130        foreach ($pages as $page) {
131            if (($aliases[$type]['prefix'] !== '' &&
132                 stripos($page, $aliases[$type]['prefix']) !== 0) ||
133                ($aliases[$type]['postfix'] !== '' &&
134                 strripos($page, $aliases[$type]['postfix']) !== strlen($page) -
135                  strlen($aliases[$type]['postfix']))) {
136                continue;
137            }
138
139            $rtrim = -strlen($aliases[$type]['postfix']);
140            if ($rtrim === 0) {
141                // trimming with -0 gives the empty string, not the untrimmed
142                // string
143                $id = substr($page, strlen($aliases[$type]['prefix']));
144            } else {
145                $id = substr($page, strlen($aliases[$type]['prefix']), $rtrim);
146            }
147
148            if (useHeading('content')) {
149                $heading = p_get_first_heading($page,true);
150            } else {
151                $heading = '';
152            }
153
154            if ($search !== '' &&
155                (stripos($id, $search) === false &&
156                stripos($heading, $search) === false) ||
157                strpos($id, ':') !== false) {
158                continue;
159            }
160
161            $id = utf8_ucwords(str_replace('_', ' ', $id));
162
163            if ($heading === '') {
164                $heading = $id;
165            }
166            $result[hsc($id)] = hsc($heading);
167        }
168
169        require_once DOKU_INC . 'inc/JSON.php';
170        $json = new JSON();
171        echo '(' . $json->encode($result) . ')';
172    }
173}
174