1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
Module: Xcenter |
5
|
|
|
|
6
|
|
|
Version: 2.01 |
7
|
|
|
|
8
|
|
|
Description: Multilingual Content Module with tags and lists with search functions |
9
|
|
|
|
10
|
|
|
Author: Written by Simon Roberts aka. Wishcraft ([email protected]) |
11
|
|
|
|
12
|
|
|
Owner: Chronolabs |
13
|
|
|
|
14
|
|
|
License: See /docs - GPL 2.0 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/* |
18
|
|
|
You may not change or alter any portion of this comment or credits |
19
|
|
|
of supporting developers from this source code or any supporting source code |
20
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
21
|
|
|
|
22
|
|
|
This program is distributed in the hope that it will be useful, |
23
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
24
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Xoops Form Class Elements |
29
|
|
|
* |
30
|
|
|
* @copyright The XOOPS Project http://sourceforge.net/projects/xoops/ |
31
|
|
|
* @license http://www.fsf.org/copyleft/gpl.html GNU public license |
32
|
|
|
* @package class |
33
|
|
|
* @package kernel |
34
|
|
|
* @subpackage form |
35
|
|
|
* @author Kazumi Ono <[email protected]> |
36
|
|
|
* @author Taiwen Jiang <[email protected]> |
37
|
|
|
* @author John Neill <[email protected]> |
38
|
|
|
* @version $Id: formselect.php 3988 2009-12-05 15:46:47Z trabis $ |
39
|
|
|
*/ |
40
|
|
|
defined('XOOPS_ROOT_PATH') or die('Restricted access'); |
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 The XOOPS Project http://sourceforge.net/projects/xoops/ |
51
|
|
|
* @package kernel |
52
|
|
|
* @subpackage form |
53
|
|
|
* @access public |
54
|
|
|
*/ |
55
|
|
|
class SonglistFormSelectSong extends XoopsFormElement |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* Options |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
* @access private |
62
|
|
|
*/ |
63
|
|
|
var $_options = array(); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Allow multiple selections? |
67
|
|
|
* |
68
|
|
|
* @var bool |
69
|
|
|
* @access private |
70
|
|
|
*/ |
71
|
|
|
var $_multiple = false; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Number of rows. "1" makes a dropdown list. |
75
|
|
|
* |
76
|
|
|
* @var int |
77
|
|
|
* @access private |
78
|
|
|
*/ |
79
|
|
|
var $_size; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Pre-selcted values |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
* @access private |
86
|
|
|
*/ |
87
|
|
|
var $_value = array(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Constructor |
91
|
|
|
* |
92
|
|
|
* @param string $caption Caption |
93
|
|
|
* @param string $name "name" attribute |
94
|
|
|
* @param mixed $value Pre-selected value (or array of them). |
95
|
|
|
* @param int $size Number or rows. "1" makes a drop-down-list |
96
|
|
|
* @param bool $multiple Allow multiple selections? |
97
|
|
|
*/ |
98
|
|
|
function SonglistFormSelectSong($caption, $name, $value = null, $size = 1, $multiple = false, $id=-1, $field = 'cid') |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
global $_form_object_options; |
101
|
|
|
xoops_loadLanguage('modinfo', 'songlist'); |
102
|
|
|
|
103
|
|
|
$this->setCaption($caption); |
104
|
|
|
$this->setName($name); |
105
|
|
|
$this->_multiple = $multiple; |
106
|
|
|
$this->_size = intval($size); |
107
|
|
|
if (isset($value)) { |
108
|
|
|
$this->setValue($value); |
109
|
|
|
} |
110
|
|
|
$this->addOption('0', _MI_SONGLIST_NONE); |
111
|
|
|
if ($id==-1) { |
112
|
|
|
if (!isset($_form_object_options['songs'][$field][$id])) { |
113
|
|
|
$songs_handler =& xoops_getmodulehandler('songs', 'songlist'); |
114
|
|
|
$criteria = new Criteria('1', '1'); |
115
|
|
|
$criteria->setSort('`title`'); |
116
|
|
|
$criteria->setOrder('ASC'); |
117
|
|
|
foreach($songs_handler->getObjects($criteria, true) as $id => $obj) { |
|
|
|
|
118
|
|
|
$_form_object_options['songs'][$field][$id][$id] = $obj->getVar('title'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} else { |
122
|
|
|
if (!isset($_form_object_options['songs'][$field][$id])) { |
123
|
|
|
$songs_handler =& xoops_getmodulehandler('songs', 'songlist'); |
124
|
|
|
$criteria = new Criteria('`'.$field.'`', $id); |
125
|
|
|
$criteria->setSort('`title`'); |
126
|
|
|
$criteria->setOrder('ASC'); |
127
|
|
|
foreach($songs_handler->getObjects($criteria) as $id => $obj) { |
128
|
|
|
$_form_object_options['songs'][$field][$id][$id] = $obj->getVar('title'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
if (isset($_form_object_options['songs'][$field][$id])) |
133
|
|
|
foreach($_form_object_options['songs'][$field][$id] as $id => $value) |
134
|
|
|
$this->addOption($id, $value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Are multiple selections allowed? |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
function isMultiple() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
return $this->_multiple; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the size |
149
|
|
|
* |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
|
|
function getSize() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
return $this->_size; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get an array of pre-selected values |
159
|
|
|
* |
160
|
|
|
* @param bool $encode To sanitizer the text? |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
function getValue($encode = false) |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
if (! $encode) { |
166
|
|
|
return $this->_value; |
167
|
|
|
} |
168
|
|
|
$value = array(); |
169
|
|
|
foreach($this->_value as $val) { |
170
|
|
|
$value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val; |
171
|
|
|
} |
172
|
|
|
return $value; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set pre-selected values |
177
|
|
|
* |
178
|
|
|
* @param $value mixed |
179
|
|
|
*/ |
180
|
|
|
function setValue($value) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
if (is_array($value)) { |
183
|
|
|
foreach($value as $v) { |
184
|
|
|
$this->_value[] = $v; |
185
|
|
|
} |
186
|
|
|
} elseif (isset($value)) { |
187
|
|
|
$this->_value[] = $value; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Add an option |
193
|
|
|
* |
194
|
|
|
* @param string $value "value" attribute |
195
|
|
|
* @param string $name "name" attribute |
196
|
|
|
*/ |
197
|
|
|
function addOption($value, $name = '') |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
if ($name != '') { |
200
|
|
|
$this->_options[$value] = $name; |
201
|
|
|
} else { |
202
|
|
|
$this->_options[$value] = $value; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Add multiple options |
208
|
|
|
* |
209
|
|
|
* @param array $options Associative array of value->name pairs |
210
|
|
|
*/ |
211
|
|
|
function addOptionArray($options) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
if (is_array($options)) { |
|
|
|
|
214
|
|
|
foreach($options as $k => $v) { |
215
|
|
|
$this->addOption($k, $v); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get an array with all the options |
222
|
|
|
* |
223
|
|
|
* Note: both name and value should be sanitized. However for backward compatibility, only value is sanitized for now. |
224
|
|
|
* |
225
|
|
|
* @param int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name |
226
|
|
|
* @return array Associative array of value->name pairs |
227
|
|
|
*/ |
228
|
|
|
function getOptions($encode = false) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
if (! $encode) { |
|
|
|
|
231
|
|
|
return $this->_options; |
232
|
|
|
} |
233
|
|
|
$value = array(); |
234
|
|
|
foreach($this->_options as $val => $name) { |
235
|
|
|
$value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name; |
236
|
|
|
} |
237
|
|
|
return $value; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Prepare HTML for output |
242
|
|
|
* |
243
|
|
|
* @return string HTML |
244
|
|
|
*/ |
245
|
|
|
function render() |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$ele_name = $this->getName(); |
248
|
|
|
$ele_title = $this->getTitle(); |
249
|
|
|
$ele_value = $this->getValue(); |
250
|
|
|
$ele_options = $this->getOptions(); |
251
|
|
|
$ret = '<select size="'.$this->getSize().'"'.$this->getExtra(); |
252
|
|
|
if ($this->isMultiple() != false) { |
|
|
|
|
253
|
|
|
$ret .= ' name="'.$ele_name.'[]" id="'.$ele_name.'" title="'. $ele_title. '" multiple="multiple">' ; |
254
|
|
|
} else { |
255
|
|
|
$ret .= ' name="'.$ele_name.'" id="'.$ele_name.'" title="'. $ele_title. '">' ; |
256
|
|
|
} |
257
|
|
|
foreach($ele_options as $value => $name) { |
258
|
|
|
$ret .= '<option value="'.htmlspecialchars($value, ENT_QUOTES).'"'; |
259
|
|
|
if (count($ele_value) > 0 && in_array($value, $ele_value)) { |
260
|
|
|
$ret .= ' selected="selected"'; |
261
|
|
|
} |
262
|
|
|
$ret .= '>'.$name.'</option>' ; |
263
|
|
|
} |
264
|
|
|
$ret .= '</select>'; |
265
|
|
|
return $ret; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Render custom javascript validation code |
270
|
|
|
* |
271
|
|
|
* @seealso XoopsForm::renderValidationJS |
272
|
|
|
*/ |
273
|
|
|
function renderValidationJS() |
|
|
|
|
274
|
|
|
{ |
275
|
|
|
// render custom validation code if any |
276
|
|
|
if (! empty($this->customValidationCode)) { |
277
|
|
|
return implode("\n", $this->customValidationCode); |
278
|
|
|
// generate validation code if required |
279
|
|
|
} elseif ($this->isRequired()) { |
280
|
|
|
$eltname = $this->getName(); |
281
|
|
|
$eltcaption = $this->getCaption(); |
282
|
|
|
$eltmsg = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption); |
283
|
|
|
$eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
284
|
|
|
return "\nvar hasSelected = false; var selectBox = myform.{$eltname};"."for (i = 0; i < selectBox.options.length; i++ ) { if (selectBox.options[i].selected == true) { hasSelected = true; break; } }"."if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
285
|
|
|
} |
286
|
|
|
return ''; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
?> |
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.