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 Xmf\Request; |
30
|
|
|
use XoopsModules\Songlist\Helper; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Xoops Form Class Elements |
34
|
|
|
* |
35
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
36
|
|
|
* @license https://www.fsf.org/copyleft/gpl.html GNU public license |
37
|
|
|
* @author Kazumi Ono <[email protected]> |
38
|
|
|
* @author Taiwen Jiang <[email protected]> |
39
|
|
|
* @author John Neill <[email protected]> |
40
|
|
|
* @version $Id: formselect.php 3988 2009-12-05 15:46:47Z trabis $ |
41
|
|
|
*/ |
42
|
|
|
\xoops_load('XoopsFormElement'); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* A select field |
46
|
|
|
* |
47
|
|
|
* @author Kazumi Ono <[email protected]> |
48
|
|
|
* @author Taiwen Jiang <[email protected]> |
49
|
|
|
* @author John Neill <[email protected]> |
50
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
51
|
|
|
*/ |
52
|
|
|
class SelectArtistForm extends \XoopsFormElement |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* Options |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
public $_options = []; |
60
|
|
|
/** |
61
|
|
|
* Allow multiple selections? |
62
|
|
|
* |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
public $_multiple = false; |
66
|
|
|
/** |
67
|
|
|
* Number of rows. "1" makes a dropdown list. |
68
|
|
|
* |
69
|
|
|
* @var int |
70
|
|
|
*/ |
71
|
|
|
public $_size; |
72
|
|
|
/** |
73
|
|
|
* Pre-selcted values |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
public $_value = []; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Constructor |
81
|
|
|
* |
82
|
|
|
* @param string $caption Caption |
83
|
|
|
* @param string $name "name" attribute |
84
|
|
|
* @param mixed $value Pre-selected value (or array of them). |
85
|
|
|
* @param int $size Number or rows. "1" makes a drop-down-list |
86
|
|
|
* @param bool $multiple Allow multiple selections? |
87
|
|
|
* @param int $ownid |
88
|
|
|
*/ |
89
|
|
|
public function __construct($caption, $name, $value = null, $size = 1, $multiple = false, $ownid = 0) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
global $_form_object_options; |
92
|
|
|
\xoops_loadLanguage('modinfo', 'songlist'); |
93
|
|
|
|
94
|
|
|
$this->setCaption($caption); |
95
|
|
|
$this->setName($name); |
96
|
|
|
$this->_multiple = $multiple; |
97
|
|
|
$this->_size = (int)$size; |
98
|
|
|
if (isset($value)) { |
99
|
|
|
$this->setValue($value); |
100
|
|
|
} |
101
|
|
|
$this->addOption('0', \_MI_SONGLIST_NONE); |
102
|
|
|
if (!isset($_form_object_options['artists'])) { |
103
|
|
|
$artistsHandler = Helper::getInstance()->getHandler('Artists'); |
104
|
|
|
$criteria = new \Criteria(''); |
105
|
|
|
$criteria->setSort('name'); |
106
|
|
|
$criteria->setOrder('ASC'); |
107
|
|
|
foreach ($artistsHandler->getObjects($criteria, true) as $id => $obj) { |
108
|
|
|
$_form_object_options['artists'][$id] = $obj->getVar('name'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
// if (Request::hasVar('artists', 'form_object_options')) { |
112
|
|
|
if (isset($_form_object_options['artists'])) { |
113
|
|
|
foreach ($_form_object_options['artists'] as $id => $value) { |
114
|
|
|
$this->addOption($id, $value); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Are multiple selections allowed? |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isMultiple(): bool |
125
|
|
|
{ |
126
|
|
|
return $this->_multiple; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the size |
131
|
|
|
* |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
|
|
public function getSize(): int |
135
|
|
|
{ |
136
|
|
|
return $this->_size; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get an array of pre-selected values |
141
|
|
|
* |
142
|
|
|
* @param bool $encode To sanitizer the text? |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getValue($encode = false): array |
146
|
|
|
{ |
147
|
|
|
if (!$encode) { |
148
|
|
|
return $this->_value; |
149
|
|
|
} |
150
|
|
|
$value = []; |
151
|
|
|
foreach ($this->_value as $val) { |
152
|
|
|
$value[] = $val ? \htmlspecialchars($val, \ENT_QUOTES) : $val; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set pre-selected values |
160
|
|
|
* |
161
|
|
|
* @param mixed $value |
162
|
|
|
*/ |
163
|
|
|
public function setValue($value): void |
164
|
|
|
{ |
165
|
|
|
if (\is_array($value)) { |
166
|
|
|
foreach ($value as $v) { |
167
|
|
|
$this->_value[] = (int)$v; |
168
|
|
|
} |
169
|
|
|
// $this->_value[] = array_values($value); |
170
|
|
|
} elseif (isset($value)) { |
171
|
|
|
$this->_value[] = $value; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Add an option |
177
|
|
|
* |
178
|
|
|
* @param string $value "value" attribute |
179
|
|
|
* @param string $name "name" attribute |
180
|
|
|
*/ |
181
|
|
|
public function addOption($value, $name = ''): void |
182
|
|
|
{ |
183
|
|
|
if ('' != $name) { |
184
|
|
|
$this->_options[$value] = $name; |
185
|
|
|
} else { |
186
|
|
|
$this->_options[$value] = $value; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add multiple options |
192
|
|
|
* |
193
|
|
|
* @param array $options Associative array of value->name pairs |
194
|
|
|
*/ |
195
|
|
|
public function addOptionArray($options): void |
196
|
|
|
{ |
197
|
|
|
if (\is_array($options)) { |
|
|
|
|
198
|
|
|
foreach ($options as $k => $v) { |
199
|
|
|
$this->addOption($k, $v); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get an array with all the options |
206
|
|
|
* |
207
|
|
|
* Note: both name and value should be sanitized. However for backward compatibility, only value is sanitized for now. |
208
|
|
|
* |
209
|
|
|
* @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name |
210
|
|
|
* @return array Associative array of value->name pairs |
211
|
|
|
*/ |
212
|
|
|
public function getOptions($encode = false): array |
213
|
|
|
{ |
214
|
|
|
if (!$encode) { |
215
|
|
|
return $this->_options; |
216
|
|
|
} |
217
|
|
|
$value = []; |
218
|
|
|
foreach ($this->_options as $val => $name) { |
219
|
|
|
$value[$encode ? \htmlspecialchars($val, \ENT_QUOTES) : $val] = ($encode > 1) ? \htmlspecialchars($name, \ENT_QUOTES) : $name; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $value; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Prepare HTML for output |
227
|
|
|
* |
228
|
|
|
* @return string HTML |
229
|
|
|
*/ |
230
|
|
|
public function render(): string |
231
|
|
|
{ |
232
|
|
|
$ele_name = $this->getName(); |
233
|
|
|
$ele_title = $this->getTitle(); |
234
|
|
|
$ele_value = $this->getValue(); |
235
|
|
|
$ele_options = $this->getOptions(); |
236
|
|
|
$ret = '<select size="' . $this->getSize() . '"' . $this->getExtra(); |
237
|
|
|
if ($this->isMultiple()) { |
238
|
|
|
$ret .= ' name="' . $ele_name . '[]" id="' . $ele_name . '" title="' . $ele_title . '" multiple="multiple">'; |
239
|
|
|
} else { |
240
|
|
|
$ret .= ' name="' . $ele_name . '" id="' . $ele_name . '" title="' . $ele_title . '">'; |
241
|
|
|
} |
242
|
|
|
foreach ($ele_options as $value => $name) { |
243
|
|
|
$ret .= '<option value="' . \htmlspecialchars((string)$value, \ENT_QUOTES) . '"'; |
244
|
|
|
if (\count($ele_value) > 0 && \in_array($value, $ele_value, true)) { |
245
|
|
|
$ret .= ' selected'; |
246
|
|
|
} |
247
|
|
|
$ret .= '>' . $name . '</option>'; |
248
|
|
|
} |
249
|
|
|
$ret .= '</select>'; |
250
|
|
|
|
251
|
|
|
return $ret; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Render custom javascript validation code |
256
|
|
|
* |
257
|
|
|
* @seealso XoopsForm::renderValidationJS |
258
|
|
|
*/ |
259
|
|
|
public function renderValidationJS() |
260
|
|
|
{ |
261
|
|
|
// render custom validation code if any |
262
|
|
|
if (!empty($this->customValidationCode)) { |
263
|
|
|
return \implode("\n", $this->customValidationCode); |
264
|
|
|
// generate validation code if required |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
if ($this->isRequired()) { |
268
|
|
|
$eltname = $this->getName(); |
269
|
|
|
$eltcaption = $this->getCaption(); |
270
|
|
|
$eltmsg = empty($eltcaption) ? \sprintf(_FORM_ENTER, $eltname) : \sprintf(_FORM_ENTER, $eltcaption); |
271
|
|
|
$eltmsg = \str_replace('"', '\"', \stripslashes($eltmsg)); |
272
|
|
|
|
273
|
|
|
return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" |
274
|
|
|
. 'for (i = 0; i < selectBox.options.length; i++ ) { if (selectBox.options[i].selected === true) { hasSelected = true; break; } }' |
275
|
|
|
. "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return ''; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.