SelectAlbumForm   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
c 0
b 0
f 0
dl 0
loc 227
rs 9.6
wmc 35

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 6
A renderValidationJS() 0 20 4
A addOptionArray() 0 5 3
A setValue() 0 9 4
A isMultiple() 0 3 1
A getValue() 0 11 4
A getSize() 0 3 1
A getOptions() 0 11 5
A render() 0 22 5
A addOption() 0 6 2
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Songlist\Form;
4
5
/*
6
Module: Xcenter
7
8
Version: 2.01
9
10
Description: Multilingual Content Module with tags and lists with search functions
11
12
Author: Written by Simon Roberts aka. Wishcraft ([email protected])
13
14
Owner: Chronolabs
15
16
License: See /docs - GPL 2.0
17
*/
18
19
/*
20
 You may not change or alter any portion of this comment or credits
21
 of supporting developers from this source code or any supporting source code
22
 which is considered copyrighted (c) material of the original comment or credit authors.
23
24
 This program is distributed in the hope that it will be useful,
25
 but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27
*/
28
29
use Criteria;
30
use Xmf\Request;
31
use XoopsFormElement;
32
use XoopsModules\Songlist\Helper;
33
34
/**
35
 *  Xoops Form Class Elements
36
 *
37
 * @copyright       XOOPS Project (https://xoops.org)
38
 * @license         https://www.fsf.org/copyleft/gpl.html GNU public license
39
 * @author          Kazumi Ono <[email protected]>
40
 * @author          Taiwen Jiang <[email protected]>
41
 * @author          John Neill <[email protected]>
42
 * @version         $Id: formselect.php 3988 2009-12-05 15:46:47Z trabis $
43
 */
44
\xoops_load('XoopsFormElement');
45
46
/**
47
 * A select field
48
 *
49
 * @author      Kazumi Ono <[email protected]>
50
 * @author      Taiwen Jiang <[email protected]>
51
 * @author      John Neill <[email protected]>
52
 * @copyright   XOOPS Project (https://xoops.org)
53
 */
54
class SelectAlbumForm extends XoopsFormElement
55
{
56
    /**
57
     * Options
58
     *
59
     * @var array
60
     */
61
    public $_options = [];
62
    /**
63
     * Allow multiple selections?
64
     *
65
     * @var bool
66
     */
67
    public $_multiple = false;
68
    /**
69
     * Number of rows. "1" makes a dropdown list.
70
     *
71
     * @var int
72
     */
73
    public $_size;
74
    /**
75
     * Pre-selcted values
76
     *
77
     * @var array
78
     */
79
    public $_value = [];
80
81
    /**
82
     * Constructor
83
     *
84
     * @param string $caption  Caption
85
     * @param string $name     "name" attribute
86
     * @param mixed  $value    Pre-selected value (or array of them).
87
     * @param int    $size     Number or rows. "1" makes a drop-down-list
88
     * @param bool   $multiple Allow multiple selections?
89
     * @param int    $ownid
90
     */
91
    public function __construct($caption, $name, $value = null, $size = 1, $multiple = false, $ownid = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $ownid is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

91
    public function __construct($caption, $name, $value = null, $size = 1, $multiple = false, /** @scrutinizer ignore-unused */ $ownid = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        global $_form_object_options;
94
        \xoops_loadLanguage('modinfo', 'songlist');
95
96
        $this->setCaption($caption);
97
        $this->setName($name);
98
        $this->_multiple = $multiple;
99
        $this->_size     = (int)$size;
100
        if (isset($value)) {
101
            $this->setValue($value);
102
        }
103
        $this->addOption('0', \_MI_SONGLIST_NONE);
104
        if (!isset($_form_object_options['albums'])) {
105
            $albumsHandler = Helper::getInstance()->getHandler('Albums');
106
            $criteria      = new Criteria('');
107
            $criteria->setSort('title');
108
            $criteria->setOrder('ASC');
109
            foreach ($albumsHandler->getObjects($criteria, true) as $id => $obj) {
110
                $_form_object_options['albums'][$id] = $obj->getVar('title');
111
            }
112
        }
113
        //        if (Request::hasVar('albums', 'form_object_options')) {
114
        if (isset($_form_object_options['albums'])) {
115
            foreach ($_form_object_options['albums'] as $id => $value) {
116
                $this->addOption($id, $value);
117
            }
118
        }
119
    }
120
121
    /**
122
     * Are multiple selections allowed?
123
     *
124
     * @return bool
125
     */
126
    public function isMultiple(): bool
127
    {
128
        return $this->_multiple;
129
    }
130
131
    /**
132
     * Get the size
133
     *
134
     * @return int
135
     */
136
    public function getSize(): int
137
    {
138
        return $this->_size;
139
    }
140
141
    /**
142
     * Get an array of pre-selected values
143
     *
144
     * @param bool $encode To sanitizer the text?
145
     * @return array
146
     */
147
    public function getValue($encode = false): array
148
    {
149
        if (!$encode) {
150
            return $this->_value;
151
        }
152
        $value = [];
153
        foreach ($this->_value as $val) {
154
            $value[] = $val ? \htmlspecialchars($val, \ENT_QUOTES) : $val;
155
        }
156
157
        return $value;
158
    }
159
160
    /**
161
     * Set pre-selected values
162
     *
163
     * @param mixed $value
164
     */
165
    public function setValue($value): void
166
    {
167
        if (\is_array($value)) {
168
            foreach ($value as $v) {
169
                $this->_value[] = (int)$v;
170
            }
171
//            $this->_value[] = array_values($value);
172
        } elseif (isset($value)) {
173
            $this->_value[] = $value;
174
        }
175
    }
176
177
    /**
178
     * Add an option
179
     *
180
     * @param string $value "value" attribute
181
     * @param string $name  "name" attribute
182
     */
183
    public function addOption($value, $name = ''): void
184
    {
185
        if ('' != $name) {
186
            $this->_options[$value] = $name;
187
        } else {
188
            $this->_options[$value] = $value;
189
        }
190
    }
191
192
    /**
193
     * Add multiple options
194
     *
195
     * @param array $options Associative array of value->name pairs
196
     */
197
    public function addOptionArray($options): void
198
    {
199
        if (\is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
200
            foreach ($options as $k => $v) {
201
                $this->addOption($k, $v);
202
            }
203
        }
204
    }
205
206
    /**
207
     * Get an array with all the options
208
     *
209
     * Note: both name and value should be sanitized. However for backward compatibility, only value is sanitized for now.
210
     *
211
     * @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name
212
     * @return array Associative array of value->name pairs
213
     */
214
    public function getOptions($encode = false): array
215
    {
216
        if (!$encode) {
217
            return $this->_options;
218
        }
219
        $value = [];
220
        foreach ($this->_options as $val => $name) {
221
            $value[$encode ? \htmlspecialchars($val, \ENT_QUOTES) : $val] = ($encode > 1) ? \htmlspecialchars($name, \ENT_QUOTES) : $name;
222
        }
223
224
        return $value;
225
    }
226
227
    /**
228
     * Prepare HTML for output
229
     *
230
     * @return string HTML
231
     */
232
    public function render(): string
233
    {
234
        $ele_name    = $this->getName();
235
        $ele_title   = $this->getTitle();
236
        $ele_value   = $this->getValue();
237
        $ele_options = $this->getOptions();
238
        $ret         = '<select size="' . $this->getSize() . '"' . $this->getExtra();
239
        if ($this->isMultiple()) {
240
            $ret .= ' name="' . $ele_name . '[]" id="' . $ele_name . '" title="' . $ele_title . '" multiple="multiple">';
241
        } else {
242
            $ret .= ' name="' . $ele_name . '" id="' . $ele_name . '" title="' . $ele_title . '">';
243
        }
244
        foreach ($ele_options as $value => $name) {
245
            $ret .= '<option value="' . \htmlspecialchars((string)$value, \ENT_QUOTES) . '"';
246
            if (\count($ele_value) > 0 && \in_array($value, $ele_value, true)) {
247
                $ret .= ' selected';
248
            }
249
            $ret .= '>' . $name . '</option>';
250
        }
251
        $ret .= '</select>';
252
253
        return $ret;
254
    }
255
256
    /**
257
     * Render custom javascript validation code
258
     *
259
     * @seealso XoopsForm::renderValidationJS
260
     */
261
    public function renderValidationJS()
262
    {
263
        // render custom validation code if any
264
        if (!empty($this->customValidationCode)) {
265
            return \implode("\n", $this->customValidationCode);
266
            // generate validation code if required
267
        }
268
269
        if ($this->isRequired()) {
270
            $eltname    = $this->getName();
271
            $eltcaption = $this->getCaption();
272
            $eltmsg     = empty($eltcaption) ? \sprintf(_FORM_ENTER, $eltname) : \sprintf(_FORM_ENTER, $eltcaption);
273
            $eltmsg     = \str_replace('"', '\"', \stripslashes($eltmsg));
274
275
            return "\nvar hasSelected = false; var selectBox = myform.{$eltname};"
276
                   . 'for (i = 0; i < selectBox.options.length; i++ ) { if (selectBox.options[i].selected === true) { hasSelected = true; break; } }'
277
                   . "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
278
        }
279
280
        return '';
281
    }
282
}
283