Total Complexity | 95 |
Total Lines | 386 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Field 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 Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Field extends \XoopsObject |
||
32 | { |
||
33 | public function __construct() |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Extra treatment dealing with non latin encoding |
||
72 | * Tricky solution |
||
73 | * @param string $key |
||
74 | * @param mixed $value |
||
75 | * @param bool $not_gpc |
||
76 | */ |
||
77 | |||
78 | public function setVar($key, $value, $not_gpc = false) |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $key |
||
91 | * @param string $format |
||
92 | * |
||
93 | * @return mixed |
||
94 | */ |
||
95 | |||
96 | public function getVar($key, $format = 's') |
||
97 | { |
||
98 | $value = parent::getVar($key, $format); |
||
99 | |||
100 | if ('field_options' === $key && !empty($value)) { |
||
101 | foreach (\array_keys($value) as $idx) { |
||
|
|||
102 | $value[$idx] = \base64_decode($value[$idx]); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | return $value; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Returns a {@link XoopsFormElement} for editing the value of this field |
||
111 | * |
||
112 | * @param XoopsUser $user {@link XoopsUser} object to edit the value of |
||
113 | * @param ProfileProfile $profile {@link ProfileProfile} object to edit the value of |
||
114 | * |
||
115 | * @return \XoopsModules\Yogurt\XoopsFormCheckBox|\XoopsModules\Yogurt\XoopsFormDatetime|\XoopsModules\Yogurt\XoopsFormDhtmlTextArea|\XoopsModules\Yogurt\XoopsFormLabel|\XoopsModules\Yogurt\XoopsFormRadio|\XoopsModules\Yogurt\XoopsFormRadioYN|\XoopsModules\Yogurt\XoopsFormSelect|\XoopsModules\Yogurt\XoopsFormSelectGroup|\XoopsModules\Yogurt\XoopsFormSelectLang|\XoopsModules\Yogurt\XoopsFormSelectTheme|\XoopsModules\Yogurt\XoopsFormSelectTimezone|\XoopsModules\Yogurt\XoopsFormText|\XoopsModules\Yogurt\XoopsFormTextArea|\XoopsModules\Yogurt\XoopsFormTextDateSelect |
||
116 | */ |
||
117 | |||
118 | public function getEditElement($user, $profile) |
||
119 | { |
||
120 | $value = \in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e'); |
||
121 | |||
122 | $caption = $this->getVar('field_title'); |
||
123 | |||
124 | $caption = \defined($caption) ? \constant($caption) : $caption; |
||
125 | |||
126 | $name = $this->getVar('field_name', 'e'); |
||
127 | |||
128 | $options = $this->getVar('field_options'); |
||
129 | |||
130 | if (\is_array($options)) { |
||
131 | //asort($options); |
||
132 | |||
133 | foreach (\array_keys($options) as $key) { |
||
134 | $optval = \defined($options[$key]) ? \constant($options[$key]) : $options[$key]; |
||
135 | |||
136 | $optkey = \defined($key) ? \constant($key) : $key; |
||
137 | |||
138 | unset($options[$key]); |
||
139 | |||
140 | $options[$optkey] = $optval; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | include_once $GLOBALS['xoops']->path('class/xoopsformloader.php'); |
||
145 | |||
146 | switch ($this->getVar('field_type')) { |
||
147 | default: |
||
148 | case 'autotext': |
||
149 | //autotext is not for editing |
||
150 | $element = new \XoopsFormLabel($caption, $this->getOutputValue($user, $profile)); |
||
151 | break; |
||
152 | case 'textbox': |
||
153 | $element = new \XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value); |
||
154 | break; |
||
155 | case 'textarea': |
||
156 | $element = new \XoopsFormTextArea($caption, $name, $value, 4, 30); |
||
157 | break; |
||
158 | case 'dhtml': |
||
159 | $element = new \XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30); |
||
160 | break; |
||
161 | case 'select': |
||
162 | $element = new \XoopsFormSelect($caption, $name, $value); |
||
163 | // If options do not include an empty element, then add a blank option to prevent any default selection |
||
164 | // if (!in_array('', array_keys($options))) { |
||
165 | if (!\array_key_exists('', $options)) { |
||
166 | $element->addOption('', \_NONE); |
||
167 | |||
168 | $eltmsg = empty($caption) ? \sprintf(\_FORM_ENTER, $name) : \sprintf(\_FORM_ENTER, $caption); |
||
169 | |||
170 | $eltmsg = \str_replace('"', '\"', \stripslashes($eltmsg)); |
||
171 | |||
172 | $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" |
||
173 | . "for (i = 0; i < selectBox.options.length; i++) { if (selectBox.options[i].selected == true && selectBox.options[i].value != '') { hasSelected = true; break; } }" |
||
174 | . "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
||
175 | } |
||
176 | $element->addOptionArray($options); |
||
177 | break; |
||
178 | case 'select_multi': |
||
179 | $element = new \XoopsFormSelect($caption, $name, $value, 5, true); |
||
180 | $element->addOptionArray($options); |
||
181 | break; |
||
182 | case 'radio': |
||
183 | $element = new \XoopsFormRadio($caption, $name, $value); |
||
184 | $element->addOptionArray($options); |
||
185 | break; |
||
186 | case 'checkbox': |
||
187 | $element = new \XoopsFormCheckBox($caption, $name, $value); |
||
188 | $element->addOptionArray($options); |
||
189 | break; |
||
190 | case 'yesno': |
||
191 | $element = new \XoopsFormRadioYN($caption, $name, $value); |
||
192 | break; |
||
193 | case 'group': |
||
194 | $element = new \XoopsFormSelectGroup($caption, $name, true, $value); |
||
195 | break; |
||
196 | case 'group_multi': |
||
197 | $element = new \XoopsFormSelectGroup($caption, $name, true, $value, 5, true); |
||
198 | break; |
||
199 | case 'language': |
||
200 | $element = new \XoopsFormSelectLang($caption, $name, $value); |
||
201 | break; |
||
202 | case 'date': |
||
203 | $element = new \XoopsFormTextDateSelect($caption, $name, 15, $value); |
||
204 | break; |
||
205 | case 'longdate': |
||
206 | $element = new \XoopsFormTextDateSelect($caption, $name, 15, \str_replace('-', '/', $value)); |
||
207 | break; |
||
208 | case 'datetime': |
||
209 | $element = new \XoopsFormDatetime($caption, $name, 15, $value); |
||
210 | break; |
||
211 | case 'timezone': |
||
212 | $element = new \XoopsFormSelectTimezone($caption, $name, $value); |
||
213 | $element->setExtra("style='width: 280px;'"); |
||
214 | break; |
||
215 | case 'rank': |
||
216 | $element = new \XoopsFormSelect($caption, $name, $value); |
||
217 | |||
218 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
219 | $ranks = \XoopsLists::getUserRankList(); |
||
220 | $element->addOption(0, '--------------'); |
||
221 | $element->addOptionArray($ranks); |
||
222 | break; |
||
223 | case 'theme': |
||
224 | $element = new \XoopsFormSelectTheme($caption, $name, $value, 1, true); |
||
225 | break; |
||
226 | } |
||
227 | |||
228 | if ('' != $this->getVar('field_description')) { |
||
229 | $element->setDescription($this->getVar('field_description')); |
||
230 | } |
||
231 | |||
232 | return $element; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Returns a value for output of this field |
||
237 | * |
||
238 | * @param XoopsUser $user {@link XoopsUser} object to get the value of |
||
239 | * @param profileProfile $profile object to get the value of |
||
240 | * |
||
241 | * @return mixed |
||
242 | **/ |
||
243 | |||
244 | public function getOutputValue($user, $profile) |
||
245 | { |
||
246 | if (\file_exists($file = $GLOBALS['xoops']->path('modules/yogurt/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) { |
||
247 | include_once $file; |
||
248 | } else { |
||
249 | include_once $GLOBALS['xoops']->path('modules/yogurt/language/english/modinfo.php'); |
||
250 | } |
||
251 | |||
252 | $value = \in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name')); |
||
253 | |||
254 | switch ($this->getVar('field_type')) { |
||
255 | default: |
||
256 | case 'textbox': |
||
257 | $value = \is_array($value) ? $value[0] : $value; |
||
258 | if ('url' === $this->getVar('field_name') && '' !== $value) { |
||
259 | return '<a href="' . \formatURL($value) . '" rel="external">' . $value . '</a>'; |
||
260 | } |
||
261 | |||
262 | return $value; |
||
263 | break; |
||
264 | case 'textarea': |
||
265 | case 'dhtml': |
||
266 | case 'theme': |
||
267 | case 'language': |
||
268 | return $value; |
||
269 | break; |
||
270 | case 'select': |
||
271 | case 'radio': |
||
272 | $value = \is_array($value) ? $value[0] : $value; |
||
273 | $options = $this->getVar('field_options'); |
||
274 | if (isset($options[$value])) { |
||
275 | $value = \htmlspecialchars(\defined($options[$value]) ? \constant($options[$value]) : $options[$value]); |
||
276 | } else { |
||
277 | $value = ''; |
||
278 | } |
||
279 | |||
280 | return $value; |
||
281 | break; |
||
282 | case 'select_multi': |
||
283 | case 'checkbox': |
||
284 | $options = $this->getVar('field_options'); |
||
285 | $ret = []; |
||
286 | if (\count($options) > 0) { |
||
287 | foreach (\array_keys($options) as $key) { |
||
288 | if (\in_array($key, $value)) { |
||
289 | $ret[$key] = \htmlspecialchars(\defined($options[$key]) ? \constant($options[$key]) : $options[$key]); |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | |||
294 | return $ret; |
||
295 | break; |
||
296 | case 'group': |
||
297 | /* @var XoopsMemberHandler $memberHandler */ $memberHandler = \xoops_getHandler('member'); |
||
298 | $options = $memberHandler->getGroupList(); |
||
299 | $ret = $options[$value] ?? ''; |
||
300 | |||
301 | return $ret; |
||
302 | break; |
||
303 | case 'group_multi': |
||
304 | /* @var XoopsMemberHandler $memberHandler */ $memberHandler = \xoops_getHandler('member'); |
||
305 | $options = $memberHandler->getGroupList(); |
||
306 | $ret = []; |
||
307 | foreach (\array_keys($options) as $key) { |
||
308 | if (\in_array($key, $value)) { |
||
309 | $ret[$key] = \htmlspecialchars($options[$key]); |
||
310 | } |
||
311 | } |
||
312 | |||
313 | return $ret; |
||
314 | break; |
||
315 | case 'longdate': |
||
316 | //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that |
||
317 | //when we cannot convert it to a UNIX timestamp? |
||
318 | return \str_replace('-', '/', $value); |
||
319 | case 'date': |
||
320 | return \formatTimestamp($value, 's'); |
||
321 | break; |
||
322 | case 'datetime': |
||
323 | if (!empty($value)) { |
||
324 | return \formatTimestamp($value, 'm'); |
||
325 | } |
||
326 | |||
327 | return $value = _MI_YOGURT_NEVER_LOGGED_IN; |
||
328 | break; |
||
329 | case 'autotext': |
||
330 | $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it |
||
331 | $value = \str_replace('{X_UID}', $user->getVar('uid'), $value); |
||
332 | $value = \str_replace('{X_URL}', XOOPS_URL, $value); |
||
333 | $value = \str_replace('{X_UNAME}', $user->getVar('uname'), $value); |
||
334 | |||
335 | return $value; |
||
336 | break; |
||
337 | case 'rank': |
||
338 | $userrank = $user->rank(); |
||
339 | $user_rankimage = ''; |
||
340 | if (isset($userrank['image']) && '' !== $userrank['image']) { |
||
341 | $user_rankimage = '<img src="' . \XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /> '; |
||
342 | } |
||
343 | |||
344 | return $user_rankimage . $userrank['title']; |
||
345 | break; |
||
346 | case 'yesno': |
||
347 | return $value ? \_YES : \_NO; |
||
348 | break; |
||
349 | case 'timezone': |
||
350 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
351 | $timezones = \XoopsLists::getTimeZoneList(); |
||
352 | $value = empty($value) ? '0' : (string)$value; |
||
353 | |||
354 | return $timezones[\str_replace('.0', '', $value)]; |
||
355 | break; |
||
356 | } |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Returns a value ready to be saved in the database |
||
361 | * |
||
362 | * @param mixed $value Value to format |
||
363 | * |
||
364 | * @return mixed |
||
365 | */ |
||
366 | |||
367 | public function getValueForSave($value) |
||
401 | } |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Get names of user variables |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | |||
410 | public function getUserVars() |
||
417 | } |
||
418 | } |
||
419 |