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 SelectGenreForm 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
|
|
|
*/ |
90
|
|
|
public function __construct($caption, $name, $value = null, $size = 1, $multiple = false) |
91
|
|
|
{ |
92
|
|
|
global $_form_object_options; |
93
|
|
|
\xoops_loadLanguage('modinfo', 'songlist'); |
94
|
|
|
|
95
|
|
|
$this->setCaption($caption); |
96
|
|
|
$this->setName($name); |
97
|
|
|
$this->_multiple = $multiple; |
98
|
|
|
$this->_size = (int)$size; |
99
|
|
|
if (isset($value)) { |
100
|
|
|
$this->setValue($value); |
101
|
|
|
} |
102
|
|
|
$this->addOption('0', \_MI_SONGLIST_ALL); |
103
|
|
|
if (!isset($_form_object_options['genre'])) { |
104
|
|
|
$genreHandler = Helper::getInstance()->getHandler('Genre'); |
105
|
|
|
$criteria = new Criteria(''); |
106
|
|
|
$criteria->setSort('name'); |
107
|
|
|
$criteria->setOrder('ASC'); |
108
|
|
|
foreach ($genreHandler->getObjects($criteria, true) as $id => $obj) { |
109
|
|
|
$_form_object_options['genre'][$id] = $obj->getVar('name'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
// if (Request::hasVar('genre', 'form_object_options')) { |
113
|
|
|
if (isset($_form_object_options['genre'])) { |
114
|
|
|
foreach ($_form_object_options['genre'] as $id => $value) { |
115
|
|
|
$this->addOption($id, $value); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Are multiple selections allowed? |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function isMultiple(): bool |
126
|
|
|
{ |
127
|
|
|
return $this->_multiple; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the size |
132
|
|
|
* |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
|
|
public function getSize(): int |
136
|
|
|
{ |
137
|
|
|
return $this->_size; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get an array of pre-selected values |
142
|
|
|
* |
143
|
|
|
* @param bool $encode To sanitizer the text? |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function getValue($encode = false): array |
147
|
|
|
{ |
148
|
|
|
if (!$encode) { |
149
|
|
|
return $this->_value; |
150
|
|
|
} |
151
|
|
|
$value = []; |
152
|
|
|
foreach ($this->_value as $val) { |
153
|
|
|
$value[] = $val ? \htmlspecialchars($val, \ENT_QUOTES) : $val; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $value; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set pre-selected values |
161
|
|
|
* |
162
|
|
|
* @param mixed $value |
163
|
|
|
*/ |
164
|
|
|
public function setValue($value): void |
165
|
|
|
{ |
166
|
|
|
if (\is_array($value)) { |
167
|
|
|
foreach ($value as $v) { |
168
|
|
|
$this->_value[] = (int)$v; |
169
|
|
|
} |
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)) { |
|
|
|
|
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
|
|
|
|