Completed
Push — master ( c622f7...33a06a )
by Richard
09:52 queued 02:32
created

SelectUser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 52.78%

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 19
cts 36
cp 0.5278
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 8

1 Method

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 71 7
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Form;
13
14
use Xoops\Core\Kernel\Criteria;
15
use Xoops\Core\Kernel\CriteriaCompo;
16
17
/**
18
 * SelectUser - select user form element
19
 *
20
 * limit: Only works with javascript enabled
21
 *
22
 * @category  Xoops\Form\SelectUser
23
 * @package   Xoops\Form
24
 * @author    Taiwen Jiang <[email protected]>
25
 * @copyright 2001-2016 XOOPS Project (http://xoops.org)
26
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
27
 * @link      http://xoops.org
28
 */
29
class SelectUser extends ElementTray
30
{
31
    /**
32
     * Constructor
33
     *
34
     * @param string $caption      caption
35
     * @param string $name         element name
36
     * @param bool   $include_anon Include user "anonymous"?
37
     * @param mixed  $value        Pre-selected value (or array of them).
38
     *                             For an item with massive members, such as "Registered Users",
39
     *                             "$value" should be used to store selected temporary users only
40
     *                             instead of all members of that item
41
     * @param int    $size         Number or rows. "1" makes a drop-down-list.
42
     * @param bool   $multiple     Allow multiple selections?
43
     */
44 1
    public function __construct($caption, $name, $include_anon = false, $value = null, $size = 1, $multiple = false)
45
    {
46 1
        $xoops = \Xoops::getInstance();
47 1
        $limit = 200;
48 1
        $select_element = new Select('', $name, $value, $size, $multiple);
49 1
        if ($include_anon) {
50
            $select_element->addOption(0, $xoops->getConfig('anonymous'));
51
        }
52 1
        $member_handler = $xoops->getHandlerMember();
53 1
        $user_count = $member_handler->getUserCount();
54 1
        $value = is_array($value) ? $value : (empty($value) ? array() : array($value));
55 1
        if ($user_count > $limit && count($value) > 0) {
56
            $criteria = new CriteriaCompo(new Criteria('uid', '(' . implode(',', $value) . ')', 'IN'));
57
        } else {
58 1
            $criteria = new CriteriaCompo();
59 1
            $criteria->setLimit($limit);
60
        }
61 1
        $criteria->setSort('uname');
62 1
        $criteria->setOrder('ASC');
63 1
        $users = $member_handler->getUserList($criteria);
64 1
        $select_element->addOptionArray($users);
65 1
        if ($user_count <= $limit) {
66 1
            parent::__construct($caption, "", $name);
67 1
            $this->addElement($select_element);
68 1
            return;
69
        }
70
71
        $js_addusers = "<script type='text/javascript'>
72
            function addusers(opts){
73
                var num = opts.substring(0, opts.indexOf(':'));
74
                opts = opts.substring(opts.indexOf(':')+1, opts.length);
75
                var sel = xoopsGetElementById('" . $name . "');
76
                var arr = new Array(num);
77
                for (var n=0; n < num; n++) {
78
                    var nm = opts.substring(0, opts.indexOf(':'));
79
                    opts = opts.substring(opts.indexOf(':')+1, opts.length);
80
                    var val = opts.substring(0, opts.indexOf(':'));
81
                    opts = opts.substring(opts.indexOf(':')+1, opts.length);
82
                    var txt = opts.substring(0, nm - val.length);
83
                    opts = opts.substring(nm - val.length, opts.length);
84
                    var added = false;
85
                    for (var k = 0; k < sel.options.length; k++) {
86
                        if(sel.options[k].value == val){
87
                            added = true;
88
                            break;
89
                        }
90
                    }
91
                    if (added == false) {
92
                        sel.options[k] = new Option(txt, val);
93
                        sel.options[k].selected = true;
94
                    }
95
                }
96
                return true;
97
            }
98
            </script>";
99
        $token = $xoops->security()->createToken();
100
        $action_tray = new Label(
101
            '',
102
            '<a href="#" onclick="var sel = xoopsGetElementById(\'' . $name
103
                . '\');for (var i = sel.options.length-1; i >= 0; i--) {if (!sel.options[i].selected) '
104
                .'{sel.options[i] = null;}}; return false;">' . \XoopsLocale::REMOVE_UNSELECTED_USERS . "</a>"
105
                . ' | '
106
                . '<a href="#" onclick="openWithSelfMain(\'' . \XoopsBaseConfig::get('url') . '/include/findusers.php?target='
107
                . $name . '&amp;multiple=' . $multiple . '&amp;token=' . $token
108
                . '\', \'userselect\', 800, 600, null); return false;" >'
109
                . \XoopsLocale::SEARCH_USERS . "</a>" . $js_addusers
110
        );
111
        parent::__construct($caption, '', $name);
112
        $this->addElement($select_element);
113
        $this->addElement($action_tray);
114
    }
115
}
116