Passed
Pull Request — master (#2)
by Michael
07:08 queued 02:34
created

SonglistFormSelectSong   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 232
rs 9.36
c 0
b 0
f 0
wmc 38

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 10 5
A setValue() 0 8 4
A getValue() 0 10 4
A getSize() 0 3 1
A render() 0 21 5
A renderValidationJS() 0 14 4
A addOptionArray() 0 5 3
A isMultiple() 0 3 1
A addOption() 0 6 2
B SonglistFormSelectSong() 0 37 9
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')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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) { 
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

117
				foreach($songs_handler->/** @scrutinizer ignore-call */ getObjects($criteria, true) as $id => $obj) { 
Loading history...
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()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
143
    {
144
        return $this->_multiple;
145
    }
146
147
    /**
148
     * Get the size
149
     *
150
     * @return int
151
     */
152
    function getSize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 = '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
212
    {
213
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
229
    {
230
        if (! $encode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $encode of type false|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...