summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/bureaucracy/helper/fielduser.php
blob: 49608e1474c534e57895327fb8c537c133e67a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
 * Class helper_plugin_bureaucracy_fielduser
 *
 * Create single user input, with autocompletion
 */
class helper_plugin_bureaucracy_fielduser extends helper_plugin_bureaucracy_fieldtextbox {

    /**
     * Arguments:
     *  - cmd
     *  - label
     *  - ^ (optional)
     *
     * @param array $args The tokenized definition, only split at spaces
     */
    public function initialize($args) {
        parent::initialize($args);
        $this->tpl['class'] .= ' userpicker';
    }

    /**
     * Allow receiving user attributes by ".". Ex. user.name
     * You can pass an optional argument to user.grps enclosed in brackets, used as an groups delimiter Ex. user.grps(, )
     *
     * @return string
     */
    public function getReplacementPattern() {
        $label = $this->opt['label'];

        return '/(@@|##)' . preg_quote($label, '/') .
            '(?:\.(.*?))?' .    //match attribute after "."
            '(?:\((.*?)\))?' .  //match parameter enclosed in "()". Used for grps separator
            '\1/si';
    }

    /**
     * Used as an callback for preg_replace_callback
     *
     * @param $matches
     * @return string
     */
    public function replacementValueCallback($matches) {
        /** @var DokuWiki_Auth_Plugin $auth */
        global $auth;

        $value = $this->opt['value'];
        //attr doesn't exists
        if (!isset($matches[2])) {
            return is_null($value) || $value === false ? '' : $value;
        }
        $attr = $matches[2];

        $udata = $auth->getUserData($value);
        //no such user
        if ($udata === false) {
            return $matches[0];
        }

        switch($attr) {
            case 'name':
            case 'mail':
                return $udata[$attr];
            case 'grps':
                $delitmiter = ', ';
                if (isset($matches[3])) {
                    $delitmiter = $matches[3];
                }
                return implode($delitmiter, $udata['grps']);
            default:
                return $matches[0];
        }
    }

    /**
     * Return the callback for user replacement
     *
     * @return array
     */
    public function getReplacementValue() {
        return array($this, 'replacementValueCallback');
    }

    /**
     * Validate value of field
     *
     * @throws Exception when user not exists
     */
    protected function _validate() {
        parent::_validate();

        /** @var DokuWiki_Auth_Plugin $auth */
        global $auth;
        $value = $this->getParam('value');
        if (!is_null($value) && $auth->getUserData($value) === false) {
            throw new Exception(sprintf($this->getLang('e_user'),hsc($this->getParam('display'))));
        }
    }
}