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