1<?php
2/**
3 * Bureaucracy Plugin: Allows flexible creation of forms
4 *
5 * This plugin allows definition of forms in wiki pages. The forms can be
6 * submitted via email or used to create new pages from templates.
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Andreas Gohr <andi@splitbrain.org>
10 * @author     Adrian Lang <dokuwiki@cosmocode.de>
11 */
12// must be run within Dokuwiki
13if (!defined('DOKU_INC')) die();
14
15/**
16 * Class action_plugin_bureaucracy
17 */
18class action_plugin_bureaucracy extends DokuWiki_Action_Plugin {
19
20    /**
21     * Registers a callback function for a given event
22     */
23    public function register(Doku_Event_Handler $controller) {
24        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax');
25    }
26
27    /**
28     * @param Doku_Event$event
29     * @param $param
30     */
31    public function ajax(Doku_Event $event, $param) {
32        if ($event->data !== 'bureaucracy_user_field') {
33            return;
34        }
35        $event->stopPropagation();
36        $event->preventDefault();
37
38        $search = $_REQUEST['search'];
39
40        /** @var DokuWiki_Auth_Plugin $auth */
41        global $auth;
42        $users = array();
43        foreach($auth->retrieveUsers() as $username => $data) {
44            if ($search === '' || // No search
45                stripos($username, $search) === 0 || // Username (prefix)
46                stripos($data['name'], $search) !== false) { // Full name
47                $users[$username] = $data['name'];
48            }
49            if (count($users) === 10) {
50                break;
51            }
52        }
53
54        if (count($users) === 1 && key($users) === $search) {
55            $users = array();
56        }
57
58        echo json_encode($users);
59    }
60}
61