Total Complexity | 46 |
Total Lines | 256 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SonglistFormSelectCategory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SonglistFormSelectCategory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class SonglistFormSelectCategory 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 SonglistFormSelectCategory($caption, $name, $value = null, $size = 1, $multiple = false, $ownid=0) |
||
116 | } |
||
117 | |||
118 | function populateList($vars, $ownid = 0) { |
||
119 | foreach($vars as $previd => $cats) { |
||
120 | if ($previd!=$ownid||$ownid==0) { |
||
121 | foreach($cats as $catid => $title) { |
||
122 | if ($catid!=$ownid||$ownid==0) { |
||
123 | $this->addOption($catid, $title['item']); |
||
124 | if (isset($title['sub'])) { |
||
125 | $this->populateList($title['sub'], $ownid); |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | function GetCategory($ownid){ |
||
133 | |||
134 | $category_handler =& xoops_getmodulehandler('category', 'songlist'); |
||
135 | $criteria = new Criteria('pid', '0'); |
||
136 | $criteria->setSort('`name`'); |
||
137 | $criteria->setOrder('ASC'); |
||
138 | $categories = $category_handler->getObjects($criteria, true); |
||
139 | $langs_array = $this->TreeMenu(array(), $categories, -1, $ownid); |
||
140 | return $langs_array; |
||
141 | } |
||
142 | |||
143 | function TreeMenu($langs_array, $categories, $level, $ownid, $previd=0) { |
||
144 | $level++; |
||
145 | $category_handler =& xoops_getmodulehandler('category', 'songlist'); |
||
146 | foreach($categories as $catid => $category) { |
||
147 | if ($catid!=$ownid) { |
||
148 | $langs_array[$previd][$catid]['item'] = str_repeat('--', $level).$category->getVar('name'); |
||
149 | $criteria = new Criteria('pid', $catid); |
||
150 | $criteria->setSort('`name`'); |
||
151 | $criteria->setOrder('ASC'); |
||
152 | if ($categoriesb = $category_handler->getObjects($criteria, true)){ |
||
153 | $langs_array[$previd][$catid]['sub'] = $this->TreeMenu($langs_array, $categoriesb, $level, $ownid, $catid); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | $level--; |
||
158 | return ($langs_array); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Are multiple selections allowed? |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | function isMultiple() |
||
167 | { |
||
168 | return $this->_multiple; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get the size |
||
173 | * |
||
174 | * @return int |
||
175 | */ |
||
176 | function getSize() |
||
177 | { |
||
178 | return $this->_size; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Get an array of pre-selected values |
||
183 | * |
||
184 | * @param bool $encode To sanitizer the text? |
||
185 | * @return array |
||
186 | */ |
||
187 | function getValue($encode = false) |
||
188 | { |
||
189 | if (! $encode) { |
||
190 | return $this->_value; |
||
191 | } |
||
192 | $value = array(); |
||
193 | foreach($this->_value as $val) { |
||
194 | $value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val; |
||
195 | } |
||
196 | return $value; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Set pre-selected values |
||
201 | * |
||
202 | * @param $value mixed |
||
203 | */ |
||
204 | function setValue($value) |
||
205 | { |
||
206 | if (is_array($value)) { |
||
207 | foreach($value as $v) { |
||
208 | $this->_value[] = $v; |
||
209 | } |
||
210 | } elseif (isset($value)) { |
||
211 | $this->_value[] = $value; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Add an option |
||
217 | * |
||
218 | * @param string $value "value" attribute |
||
219 | * @param string $name "name" attribute |
||
220 | */ |
||
221 | function addOption($value, $name = '') |
||
222 | { |
||
223 | if ($name != '') { |
||
224 | $this->_options[$value] = $name; |
||
225 | } else { |
||
226 | $this->_options[$value] = $value; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Add multiple options |
||
232 | * |
||
233 | * @param array $options Associative array of value->name pairs |
||
234 | */ |
||
235 | function addOptionArray($options) |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Get an array with all the options |
||
246 | * |
||
247 | * Note: both name and value should be sanitized. However for backward compatibility, only value is sanitized for now. |
||
248 | * |
||
249 | * @param int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name |
||
250 | * @return array Associative array of value->name pairs |
||
251 | */ |
||
252 | function getOptions($encode = false) |
||
253 | { |
||
254 | if (! $encode) { |
||
255 | return $this->_options; |
||
256 | } |
||
257 | $value = array(); |
||
258 | foreach($this->_options as $val => $name) { |
||
259 | $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name; |
||
260 | } |
||
261 | return $value; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Prepare HTML for output |
||
266 | * |
||
267 | * @return string HTML |
||
268 | */ |
||
269 | function render() |
||
270 | { |
||
271 | $ele_name = $this->getName(); |
||
272 | $ele_title = $this->getTitle(); |
||
273 | $ele_value = $this->getValue(); |
||
274 | $ele_options = $this->getOptions(); |
||
275 | $ret = '<select size="'.$this->getSize().'"'.$this->getExtra(); |
||
276 | if ($this->isMultiple() != false) { |
||
277 | $ret .= ' name="'.$ele_name.'[]" id="'.$ele_name.'" title="'. $ele_title. '" multiple="multiple">' ; |
||
278 | } else { |
||
279 | $ret .= ' name="'.$ele_name.'" id="'.$ele_name.'" title="'. $ele_title. '">' ; |
||
280 | } |
||
281 | foreach($ele_options as $value => $name) { |
||
282 | $ret .= '<option value="'.htmlspecialchars($value, ENT_QUOTES).'"'; |
||
283 | if (count($ele_value) > 0 && in_array($value, $ele_value)) { |
||
284 | $ret .= ' selected="selected"'; |
||
285 | } |
||
286 | $ret .= '>'.$name.'</option>' ; |
||
287 | } |
||
288 | $ret .= '</select>'; |
||
289 | return $ret; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Render custom javascript validation code |
||
294 | * |
||
295 | * @seealso XoopsForm::renderValidationJS |
||
296 | */ |
||
297 | function renderValidationJS() |
||
311 | } |
||
312 | } |
||
313 | |||
314 | ?> |
||
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.