Total Complexity | 110 |
Total Lines | 534 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FormAdmin 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 FormAdmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class FormAdmin |
||
38 | { |
||
39 | /** |
||
40 | * @var DoliDB Database handler. |
||
|
|||
41 | */ |
||
42 | public $db; |
||
43 | |||
44 | /** |
||
45 | * @var string error message |
||
46 | */ |
||
47 | public $error; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * Constructor |
||
52 | * |
||
53 | * @param DoliDB|null $db Database handler |
||
54 | */ |
||
55 | public function __construct($db) |
||
58 | } |
||
59 | |||
60 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
61 | |||
62 | /** |
||
63 | * Return html select list with available languages (key='en_US', value='United States' for example) |
||
64 | * |
||
65 | * @param string|array $selected Language pre-selected. Can be an array if $multiselect is 1. |
||
66 | * @param string $htmlname Name of HTML select |
||
67 | * @param int $showauto Show 'auto' choice |
||
68 | * @param array $filter Array of keys to exclude in list (opposite of $onlykeys) |
||
69 | * @param int|string $showempty '1'=Add empty value or 'string to show' |
||
70 | * @param int $showwarning Show a warning if language is not complete |
||
71 | * @param int $disabled Disable edit of select |
||
72 | * @param string $morecss Add more css styles |
||
73 | * @param int $showcode 1=Add language code into label at beginning, 2=Add language code into label at |
||
74 | * end |
||
75 | * @param int $forcecombo Force to use combo box (so no ajax beautify effect) |
||
76 | * @param int $multiselect Make the combo a multiselect |
||
77 | * @param array $onlykeys Array of language keys to restrict list with the following keys (opposite of |
||
78 | * $filter). Example array('fr', 'es', ...) |
||
79 | * @param int $mainlangonly 1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...) |
||
80 | * |
||
81 | * @return string Return HTML select string with list of languages |
||
82 | */ |
||
83 | public function select_language($selected = 'auto', $htmlname = 'lang_id', $showauto = 0, $filter = [], $showempty = '', $showwarning = 0, $disabled = 0, $morecss = '', $showcode = 0, $forcecombo = 0, $multiselect = 0, $onlykeys = [], $mainlangonly = 0) |
||
84 | { |
||
85 | // phpcs:enable |
||
86 | global $conf, $langs; |
||
87 | |||
88 | $langs = Config::getLangs(); |
||
89 | $langs->setDefaultLang($selected); |
||
90 | $langs->loadLangs(['main', 'admin']); |
||
91 | |||
92 | if (getDolGlobalString('MAIN_DEFAULT_LANGUAGE_FILTER')) { |
||
93 | if (!is_array($filter)) { |
||
94 | $filter = []; |
||
95 | } |
||
96 | $filter[getDolGlobalString('MAIN_DEFAULT_LANGUAGE_FILTER')] = 1; |
||
97 | } |
||
98 | |||
99 | $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12, 0, $mainlangonly); |
||
100 | |||
101 | // If empty value is not allowed and the language to select is not inside the list of available language and we must find |
||
102 | // an alternative of the language code to pre-select (to avoid to have first element in list pre-selected). |
||
103 | if ($selected && empty($showempty)) { |
||
104 | if (!is_array($selected) && !array_key_exists($selected, $langs_available)) { |
||
105 | $tmparray = explode('_', $selected); |
||
106 | if (!empty($tmparray[1])) { |
||
107 | $selected = getLanguageCodeFromCountryCode($tmparray[1]); |
||
108 | } |
||
109 | if (empty($selected)) { |
||
110 | $selected = $langs->defaultlang; |
||
111 | } |
||
112 | } else { |
||
113 | // If the preselected value is an array, we do not try to find alternative to preselect |
||
114 | } |
||
115 | } |
||
116 | |||
117 | $out = ''; |
||
118 | |||
119 | $out .= '<select ' . ($multiselect ? 'multiple="multiple" ' : '') . 'class="flat' . ($morecss ? ' ' . $morecss : '') . '" id="' . $htmlname . '" name="' . $htmlname . ($multiselect ? '[]' : '') . '"' . ($disabled ? ' disabled' : '') . '>'; |
||
120 | if ($showempty && !$multiselect) { |
||
121 | if (is_numeric($showempty)) { |
||
122 | $out .= '<option value="0"'; |
||
123 | } else { |
||
124 | $out .= '<option value="-1"'; |
||
125 | } |
||
126 | if ($selected === '') { |
||
127 | $out .= ' selected'; |
||
128 | } |
||
129 | $out .= '>'; |
||
130 | if ($showempty != '1') { |
||
131 | $out .= $showempty; |
||
132 | } else { |
||
133 | $out .= ' '; |
||
134 | } |
||
135 | $out .= '</option>'; |
||
136 | } |
||
137 | if ($showauto) { |
||
138 | $out .= '<option value="auto"'; |
||
139 | if ($selected === 'auto') { |
||
140 | $out .= ' selected'; |
||
141 | } |
||
142 | $out .= '>' . $langs->trans("AutoDetectLang") . '</option>'; |
||
143 | } |
||
144 | |||
145 | asort($langs_available); // array('XX' => 'Language (Country)', ...) |
||
146 | |||
147 | foreach ($langs_available as $key => $value) { |
||
148 | $valuetoshow = $value; |
||
149 | if ($showcode == 1) { |
||
150 | if ($mainlangonly) { |
||
151 | $valuetoshow = '<span class="opacitymedium">' . preg_replace('/[_-].*$/', '', $key) . '</span> - ' . $value; |
||
152 | } else { |
||
153 | $valuetoshow = '<span class="opacitymedium">' . $key . '</span> - ' . $value; |
||
154 | } |
||
155 | } |
||
156 | if ($showcode == 2) { |
||
157 | if ($mainlangonly) { |
||
158 | $valuetoshow = $value . ' <span class="opacitymedium">(' . preg_replace('/[_-].*$/', '', $key) . ')</span>'; |
||
159 | } else { |
||
160 | $valuetoshow = $value . ' <span class="opacitymedium">(' . $key . ')</span>'; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | $keytouse = $key; |
||
165 | if ($mainlangonly) { |
||
166 | $keytouse = preg_replace('/[_-].*$/', '', $key); |
||
167 | } |
||
168 | |||
169 | if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) { |
||
170 | continue; |
||
171 | } |
||
172 | if ($onlykeys && is_array($onlykeys) && !array_key_exists($keytouse, $onlykeys)) { |
||
173 | continue; |
||
174 | } |
||
175 | |||
176 | $valuetoshow = picto_from_langcode($key, 'class="saturatemedium"') . ' ' . $valuetoshow; |
||
177 | if ((is_string($selected) && (string)$selected == (string)$keytouse) || (is_array($selected) && in_array($keytouse, $selected))) { |
||
178 | $out .= '<option value="' . $keytouse . '" selected data-html="' . dol_escape_htmltag($valuetoshow) . '">' . $valuetoshow . '</option>'; |
||
179 | } else { |
||
180 | $out .= '<option value="' . $keytouse . '" data-html="' . dol_escape_htmltag($valuetoshow) . '">' . $valuetoshow . '</option>'; |
||
181 | } |
||
182 | } |
||
183 | $out .= '</select>'; |
||
184 | |||
185 | // Make select dynamic |
||
186 | if (!$forcecombo) { |
||
187 | include_once BASE_PATH . '/../Dolibarr/Lib/Ajax.php'; |
||
188 | $out .= ajax_combobox($htmlname); |
||
189 | } |
||
190 | |||
191 | return $out; |
||
192 | } |
||
193 | |||
194 | public function select_theme($selected = 'adminlte') |
||
213 | } |
||
214 | |||
215 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
216 | |||
217 | /** |
||
218 | * Return list of available menus (eldy_backoffice, ...) |
||
219 | * |
||
220 | * @param string $selected Preselected menu value |
||
221 | * @param string $htmlname Name of html select |
||
222 | * @param array $dirmenuarray Array of directories to scan |
||
223 | * @param string $moreattrib More attributes on html select tag |
||
224 | * |
||
225 | * @return integer|void |
||
226 | */ |
||
227 | public function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib = '') |
||
228 | { |
||
229 | // phpcs:enable |
||
230 | global $langs, $conf; |
||
231 | |||
232 | // Clean parameters |
||
233 | |||
234 | |||
235 | // Check parameters |
||
236 | if (!is_array($dirmenuarray)) { |
||
237 | return -1; |
||
238 | } |
||
239 | |||
240 | $menuarray = []; |
||
241 | foreach ($conf->file->dol_document_root as $dirroot) { |
||
242 | foreach ($dirmenuarray as $dirtoscan) { |
||
243 | $dir = $dirroot . $dirtoscan; |
||
244 | //print $dir.'<br>'; |
||
245 | if (is_dir($dir)) { |
||
246 | $handle = opendir($dir); |
||
247 | if (is_resource($handle)) { |
||
248 | while (($file = readdir($handle)) !== false) { |
||
249 | if (is_file($dir . "/" . $file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS' && substr($file, 0, 5) != 'index') { |
||
250 | if (preg_match('/lib\.php$/i', $file)) { |
||
251 | continue; // We exclude library files |
||
252 | } |
||
253 | if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i', $file)) { |
||
254 | continue; // We exclude all menu manager files |
||
255 | } |
||
256 | if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i', $file)) { |
||
257 | continue; // We exclude all menu manager files |
||
258 | } |
||
259 | if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i', $file)) { |
||
260 | continue; // We exclude all menu manager files |
||
261 | } |
||
262 | |||
263 | $filelib = preg_replace('/\.php$/i', '', $file); |
||
264 | $prefix = ''; |
||
265 | // 0=Recommended, 1=Experimental, 2=Development, 3=Other |
||
266 | if (preg_match('/^eldy/i', $file)) { |
||
267 | $prefix = '0'; |
||
268 | } elseif (preg_match('/^smartphone/i', $file)) { |
||
269 | $prefix = '2'; |
||
270 | } else { |
||
271 | $prefix = '3'; |
||
272 | } |
||
273 | |||
274 | if ($file == $selected) { |
||
275 | $menuarray[$prefix . '_' . $file] = '<option value="' . $file . '" selected>' . $filelib . '</option>'; |
||
276 | } else { |
||
277 | $menuarray[$prefix . '_' . $file] = '<option value="' . $file . '">' . $filelib . '</option>'; |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | closedir($handle); |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | ksort($menuarray); |
||
287 | |||
288 | // Output combo list of menus |
||
289 | print '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '"' . ($moreattrib ? ' ' . $moreattrib : '') . '>'; |
||
290 | $oldprefix = ''; |
||
291 | foreach ($menuarray as $key => $val) { |
||
292 | $tab = explode('_', $key); |
||
293 | $newprefix = $tab[0]; |
||
294 | if ($newprefix == '1' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1)) { |
||
295 | continue; |
||
296 | } |
||
297 | if ($newprefix == '2' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2)) { |
||
298 | continue; |
||
299 | } |
||
300 | if ($newprefix != $oldprefix) { // Add separators |
||
301 | // Affiche titre |
||
302 | print '<option value="-1" disabled>'; |
||
303 | if ($newprefix == '0') { |
||
304 | print '-- ' . $langs->trans("VersionRecommanded") . ' --'; |
||
305 | } |
||
306 | if ($newprefix == '1') { |
||
307 | print '-- ' . $langs->trans("VersionExperimental") . ' --'; |
||
308 | } |
||
309 | if ($newprefix == '2') { |
||
310 | print '-- ' . $langs->trans("VersionDevelopment") . ' --'; |
||
311 | } |
||
312 | if ($newprefix == '3') { |
||
313 | print '-- ' . $langs->trans("Other") . ' --'; |
||
314 | } |
||
315 | print '</option>'; |
||
316 | $oldprefix = $newprefix; |
||
317 | } |
||
318 | print $val . "\n"; // Show menu entry |
||
319 | } |
||
320 | print '</select>'; |
||
321 | |||
322 | return; |
||
323 | } |
||
324 | |||
325 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
326 | |||
327 | /** |
||
328 | * Return combo list of available menu families |
||
329 | * |
||
330 | * @param string $selected Menu pre-selected |
||
331 | * @param string $htmlname Name of html select |
||
332 | * @param string[] $dirmenuarray Directories to scan |
||
333 | * |
||
334 | * @return void |
||
335 | */ |
||
336 | public function select_menu_families($selected, $htmlname, $dirmenuarray) |
||
337 | { |
||
338 | // phpcs:enable |
||
339 | global $langs, $conf; |
||
340 | |||
341 | //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set |
||
342 | $expdevmenu = []; |
||
343 | |||
344 | $menuarray = []; |
||
345 | |||
346 | foreach ($dirmenuarray as $dirmenu) { |
||
347 | foreach ($conf->file->dol_document_root as $dirroot) { |
||
348 | $dir = $dirroot . $dirmenu; |
||
349 | if (is_dir($dir)) { |
||
350 | $handle = opendir($dir); |
||
351 | if (is_resource($handle)) { |
||
352 | while (($file = readdir($handle)) !== false) { |
||
353 | if (is_file($dir . "/" . $file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS') { |
||
354 | $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file); |
||
355 | if (preg_match('/^index/i', $filelib)) { |
||
356 | continue; |
||
357 | } |
||
358 | if (preg_match('/^default/i', $filelib)) { |
||
359 | continue; |
||
360 | } |
||
361 | if (preg_match('/^empty/i', $filelib)) { |
||
362 | continue; |
||
363 | } |
||
364 | if (preg_match('/\.lib/i', $filelib)) { |
||
365 | continue; |
||
366 | } |
||
367 | if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) { |
||
368 | continue; |
||
369 | } |
||
370 | |||
371 | $menuarray[$filelib] = 1; |
||
372 | } |
||
373 | $menuarray['all'] = 1; |
||
374 | } |
||
375 | closedir($handle); |
||
376 | } |
||
377 | } |
||
378 | } |
||
379 | } |
||
380 | |||
381 | ksort($menuarray); |
||
382 | |||
383 | // Affichage liste deroulante des menus |
||
384 | print '<select class="flat maxwidth100" id="' . $htmlname . '" name="' . $htmlname . '">'; |
||
385 | $oldprefix = ''; |
||
386 | foreach ($menuarray as $key => $val) { |
||
387 | $tab = explode('_', $key); |
||
388 | $newprefix = $tab[0]; |
||
389 | print '<option value="' . $key . '"'; |
||
390 | if ($key == $selected) { |
||
391 | print ' selected'; |
||
392 | } |
||
393 | print '>'; |
||
394 | if ($key == 'all') { |
||
395 | print $langs->trans("AllMenus"); |
||
396 | } else { |
||
397 | print $key; |
||
398 | } |
||
399 | print '</option>' . "\n"; |
||
400 | } |
||
401 | print '</select>'; |
||
402 | |||
403 | print ajax_combobox($htmlname); |
||
404 | } |
||
405 | |||
406 | |||
407 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
408 | |||
409 | /** |
||
410 | * Return a HTML select list of timezones |
||
411 | * |
||
412 | * @param string $selected Menu pre-selectionnee |
||
413 | * @param string $htmlname Nom de la zone select |
||
414 | * |
||
415 | * @return void |
||
416 | */ |
||
417 | public function select_timezone($selected, $htmlname) |
||
418 | { |
||
419 | // phpcs:enable |
||
420 | print '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '">'; |
||
421 | print '<option value="-1"> </option>'; |
||
422 | |||
423 | $arraytz = [ |
||
424 | "Pacific/Midway" => "GMT-11:00", |
||
425 | "Pacific/Fakaofo" => "GMT-10:00", |
||
426 | "America/Anchorage" => "GMT-09:00", |
||
427 | "America/Los_Angeles" => "GMT-08:00", |
||
428 | "America/Dawson_Creek" => "GMT-07:00", |
||
429 | "America/Chicago" => "GMT-06:00", |
||
430 | "America/Bogota" => "GMT-05:00", |
||
431 | "America/Anguilla" => "GMT-04:00", |
||
432 | "America/Araguaina" => "GMT-03:00", |
||
433 | "America/Noronha" => "GMT-02:00", |
||
434 | "Atlantic/Azores" => "GMT-01:00", |
||
435 | "Africa/Abidjan" => "GMT+00:00", |
||
436 | "Europe/Paris" => "GMT+01:00", |
||
437 | "Europe/Helsinki" => "GMT+02:00", |
||
438 | "Europe/Moscow" => "GMT+03:00", |
||
439 | "Asia/Dubai" => "GMT+04:00", |
||
440 | "Asia/Karachi" => "GMT+05:00", |
||
441 | "Indian/Chagos" => "GMT+06:00", |
||
442 | "Asia/Jakarta" => "GMT+07:00", |
||
443 | "Asia/Hong_Kong" => "GMT+08:00", |
||
444 | "Asia/Tokyo" => "GMT+09:00", |
||
445 | "Australia/Sydney" => "GMT+10:00", |
||
446 | "Pacific/Noumea" => "GMT+11:00", |
||
447 | "Pacific/Auckland" => "GMT+12:00", |
||
448 | "Pacific/Enderbury" => "GMT+13:00", |
||
449 | ]; |
||
450 | foreach ($arraytz as $lib => $gmt) { |
||
451 | print '<option value="' . $lib . '"'; |
||
452 | if ($selected == $lib || $selected == $gmt) { |
||
453 | print ' selected'; |
||
454 | } |
||
455 | print '>' . $gmt . '</option>' . "\n"; |
||
456 | } |
||
457 | print '</select>'; |
||
458 | } |
||
459 | |||
460 | |||
461 | |||
462 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
463 | |||
464 | /** |
||
465 | * Return html select list with available languages (key='en_US', value='United States' for example) |
||
466 | * |
||
467 | * @param string $selected Paper format pre-selected |
||
468 | * @param string $htmlname Name of HTML select field |
||
469 | * @param string $filter Value to filter on code |
||
470 | * @param int $showempty Add empty value |
||
471 | * @param int $forcecombo Force to load all values and output a standard combobox (with no beautification) |
||
472 | * |
||
473 | * @return string Return HTML output |
||
474 | */ |
||
475 | public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = '', $showempty = 0, $forcecombo = 0) |
||
476 | { |
||
477 | // phpcs:enable |
||
478 | global $langs; |
||
479 | |||
480 | $langs->load("dict"); |
||
481 | |||
482 | $sql = "SELECT code, label, width, height, unit"; |
||
483 | $sql .= " FROM " . $this->db->prefix() . "c_paper_format"; |
||
484 | $sql .= " WHERE active=1"; |
||
485 | if ($filter) { |
||
486 | $sql .= " AND code LIKE '%" . $this->db->escape($filter) . "%'"; |
||
487 | } |
||
488 | |||
489 | $paperformat = []; |
||
490 | |||
491 | $resql = $this->db->query($sql); |
||
492 | if ($resql) { |
||
493 | $num = $this->db->num_rows($resql); |
||
494 | $i = 0; |
||
495 | while ($i < $num) { |
||
496 | $obj = $this->db->fetch_object($resql); |
||
497 | $unitKey = $langs->trans('SizeUnit' . $obj->unit); |
||
498 | |||
499 | $paperformat[$obj->code] = $langs->trans('PaperFormat' . strtoupper($obj->code)) . ' - ' . round($obj->width) . 'x' . round($obj->height) . ' ' . ($unitKey == 'SizeUnit' . $obj->unit ? $obj->unit : $unitKey); |
||
500 | |||
501 | $i++; |
||
502 | } |
||
503 | } else { |
||
504 | dol_print_error($this->db); |
||
505 | return ''; |
||
506 | } |
||
507 | $out = ''; |
||
508 | |||
509 | $out .= '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '">'; |
||
510 | if ($showempty) { |
||
511 | $out .= '<option value=""'; |
||
512 | if ($selected == '') { |
||
513 | $out .= ' selected'; |
||
514 | } |
||
515 | $out .= '> </option>'; |
||
516 | } |
||
517 | foreach ($paperformat as $key => $value) { |
||
518 | if ($selected == $key) { |
||
519 | $out .= '<option value="' . $key . '" selected>' . $value . '</option>'; |
||
520 | } else { |
||
521 | $out .= '<option value="' . $key . '">' . $value . '</option>'; |
||
522 | } |
||
523 | } |
||
524 | $out .= '</select>'; |
||
525 | |||
526 | if (!$forcecombo) { |
||
527 | include_once BASE_PATH . '/../Dolibarr/Lib/Ajax.php'; |
||
528 | $out .= ajax_combobox($htmlname); |
||
529 | } |
||
530 | |||
531 | return $out; |
||
532 | } |
||
533 | |||
534 | |||
535 | /** |
||
536 | * Function to show the combo select to chose a type of field (varchar, int, email, ...) |
||
537 | * |
||
538 | * @param string $htmlname Name of HTML select component |
||
539 | * @param string $type Type preselected |
||
540 | * @param array $typewecanchangeinto Array of possible switch combination from 1 type to another one. This will |
||
541 | * grey not possible combinations. |
||
542 | * |
||
543 | * @return string The combo HTML select component |
||
544 | */ |
||
545 | public function selectTypeOfFields($htmlname, $type, $typewecanchangeinto = []) |
||
571 | } |
||
572 | } |
||
573 |