| Total Complexity | 97 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FormResource 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 FormResource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class FormResource |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var DoliDB Database handler. |
||
| 48 | */ |
||
| 49 | public $db; |
||
| 50 | |||
| 51 | public $substit = array(); |
||
| 52 | |||
| 53 | public $param = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string Error code (or message) |
||
| 57 | */ |
||
| 58 | public $error = ''; |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param DoliDB $db Database handler |
||
| 65 | */ |
||
| 66 | public function __construct($db) |
||
| 67 | { |
||
| 68 | $this->db = $db; |
||
| 69 | } |
||
| 70 | |||
| 71 | |||
| 72 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 73 | /** |
||
| 74 | * Output html form to select a resource |
||
| 75 | * |
||
| 76 | * @param int $selected Preselected resource id |
||
| 77 | * @param string $htmlname Name of field in form |
||
| 78 | * @param array $filter Optional filters criteria (example: 's.rowid <> x') |
||
| 79 | * @param int $showempty Add an empty field |
||
| 80 | * @param int $showtype Show third party type in combo list (customer, prospect or supplier) |
||
| 81 | * @param int $forcecombo Force to use combo box |
||
| 82 | * @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled'))) |
||
| 83 | * @param array $filterkey Filter on key value |
||
| 84 | * @param int $outputmode 0=HTML select string, 1=Array, 2=without form tag |
||
| 85 | * @param int $limit Limit number of answers, 0 for no limit |
||
| 86 | * @param string $morecss More css |
||
| 87 | * @param bool $multiple add [] in the name of element and add 'multiple' attribute |
||
| 88 | * @return string|array HTML string with |
||
| 89 | */ |
||
| 90 | public function select_resource_list($selected = 0, $htmlname = 'fk_resource', array $filter = [], $showempty = 0, $showtype = 0, $forcecombo = 0, $event = [], $filterkey = [], $outputmode = 0, $limit = 20, $morecss = 'minwidth100', $multiple = false) |
||
| 91 | { |
||
| 92 | // phpcs:enable |
||
| 93 | global $conf, $langs; |
||
| 94 | |||
| 95 | $out = ''; |
||
| 96 | $outarray = array(); |
||
| 97 | |||
| 98 | $resourcestat = new Dolresource($this->db); |
||
| 99 | |||
| 100 | $resources_used = $resourcestat->fetchAll('ASC', 't.rowid', $limit, 0, $filter); |
||
| 101 | |||
| 102 | if (!empty($selected) && !is_array($selected)) { |
||
| 103 | $selected = array($selected); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($outputmode != 2) { |
||
| 107 | $out = '<form action="' . $_SERVER["PHP_SELF"] . '" method="POST">'; |
||
| 108 | $out .= '<input type="hidden" name="token" value="' . newToken() . '">'; |
||
| 109 | } |
||
| 110 | |||
| 111 | if ($resourcestat) { |
||
| 112 | // Construct $out and $outarray |
||
| 113 | $out .= '<select id="' . $htmlname . '" class="flat' . ($morecss ? ' ' . $morecss : '') . '" name="' . $htmlname . ($multiple ? '[]' : '') . '" ' . ($multiple ? 'multiple' : '') . '>' . "\n"; |
||
| 114 | if ($showempty) { |
||
| 115 | $out .= '<option value="-1"> </option>' . "\n"; |
||
| 116 | } |
||
| 117 | |||
| 118 | $num = 0; |
||
| 119 | if (is_array($resourcestat->lines)) { |
||
| 120 | $num = count($resourcestat->lines); |
||
| 121 | } |
||
| 122 | |||
| 123 | //var_dump($resourcestat->lines); |
||
| 124 | $i = 0; |
||
| 125 | if ($num) { |
||
| 126 | while ($i < $num) { |
||
| 127 | $resourceclass = ucfirst($resourcestat->lines[$i]->element); |
||
| 128 | |||
| 129 | $label = $resourcestat->lines[$i]->ref ? $resourcestat->lines[$i]->ref : '' . $resourcestat->lines[$i]->label; |
||
| 130 | if ($resourceclass != 'Dolresource') { |
||
| 131 | $label .= ' (' . $langs->trans($resourceclass) . ')'; |
||
| 132 | } |
||
| 133 | |||
| 134 | // Test if entry is the first element of $selected. |
||
| 135 | if ((isset($selected[0]) && is_object($selected[0]) && $selected[0]->id == $resourcestat->lines[$i]->id) || ((!isset($selected[0]) || !is_object($selected[0])) && !empty($selected) && in_array($resourcestat->lines[$i]->id, $selected))) { |
||
| 136 | $out .= '<option value="' . $resourcestat->lines[$i]->id . '" selected>' . $label . '</option>'; |
||
| 137 | } else { |
||
| 138 | $out .= '<option value="' . $resourcestat->lines[$i]->id . '">' . $label . '</option>'; |
||
| 139 | } |
||
| 140 | |||
| 141 | array_push($outarray, array('key' => $resourcestat->lines[$i]->id, 'value' => $resourcestat->lines[$i]->id, 'label' => $label)); |
||
| 142 | |||
| 143 | $i++; |
||
| 144 | if (($i % 10) == 0) { |
||
| 145 | $out .= "\n"; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | $out .= '</select>' . "\n"; |
||
| 150 | |||
| 151 | if (!empty($conf->use_javascript_ajax) && getDolGlobalString('RESOURCE_USE_SEARCH_TO_SELECT') && !$forcecombo) { |
||
| 152 | //$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2); |
||
| 153 | $out .= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT); |
||
| 154 | } else { |
||
| 155 | $out .= ajax_combobox($htmlname); |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($outputmode != 2) { |
||
| 159 | $out .= '<input type="submit" class="button" value="' . $langs->trans("Search") . '"> '; |
||
| 160 | |||
| 161 | $out .= '</form>'; |
||
| 162 | } |
||
| 163 | } else { |
||
| 164 | dol_print_error($this->db); |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($outputmode && $outputmode != 2) { |
||
| 168 | return $outarray; |
||
| 169 | } |
||
| 170 | return $out; |
||
| 171 | } |
||
| 172 | |||
| 173 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 174 | /** |
||
| 175 | * Return html list of tickets type |
||
| 176 | * |
||
| 177 | * @param string $selected Id du type pre-selectionne |
||
| 178 | * @param string $htmlname Nom de la zone select |
||
| 179 | * @param string $filtertype To filter on field type in llx_c_ticket_type (array('code'=>xx,'label'=>zz)) |
||
| 180 | * @param int $format 0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code |
||
| 181 | * @param int $empty 1=peut etre vide, 0 sinon |
||
| 182 | * @param int $noadmininfo 0=Add admin info, 1=Disable admin info |
||
| 183 | * @param int $maxlength Max length of label |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | public function select_types_resource($selected = '', $htmlname = 'type_resource', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0) |
||
| 187 | { |
||
| 188 | // phpcs:enable |
||
| 189 | global $langs, $user; |
||
| 190 | |||
| 191 | $resourcestat = new Dolresource($this->db); |
||
| 192 | |||
| 193 | dol_syslog(get_class($this) . "::select_types_resource " . $selected . ", " . $htmlname . ", " . $filtertype . ", " . $format, LOG_DEBUG); |
||
| 194 | |||
| 195 | $filterarray = array(); |
||
| 196 | |||
| 197 | if ($filtertype != '' && $filtertype != '-1') { |
||
| 198 | $filterarray = explode(',', $filtertype); |
||
| 199 | } |
||
| 200 | |||
| 201 | $resourcestat->loadCacheCodeTypeResource(); |
||
| 202 | print '<select id="select' . $htmlname . '" class="flat maxwidthonsmartphone select_' . $htmlname . '" name="' . $htmlname . '">'; |
||
| 203 | if ($empty) { |
||
| 204 | print '<option value=""> </option>'; |
||
| 205 | } |
||
| 206 | if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource)) { |
||
| 207 | foreach ($resourcestat->cache_code_type_resource as $id => $arraytypes) { |
||
| 208 | // We discard empty line if showempty is on because an empty line has already been output. |
||
| 209 | if ($empty && empty($arraytypes['code'])) { |
||
| 210 | continue; |
||
| 211 | } |
||
| 212 | |||
| 213 | if ($format == 0) { |
||
| 214 | print '<option value="' . $id . '"'; |
||
| 215 | } elseif ($format == 1) { |
||
| 216 | print '<option value="' . $arraytypes['code'] . '"'; |
||
| 217 | } elseif ($format == 2) { |
||
| 218 | print '<option value="' . $arraytypes['code'] . '"'; |
||
| 219 | } elseif ($format == 3) { |
||
| 220 | print '<option value="' . $id . '"'; |
||
| 221 | } |
||
| 222 | // Si selected est text, on compare avec code, sinon avec id |
||
| 223 | if (!empty($selected) && preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code']) { |
||
| 224 | print ' selected'; |
||
| 225 | } elseif ($selected == $id) { |
||
| 226 | print ' selected'; |
||
| 227 | } |
||
| 228 | print '>'; |
||
| 229 | if ($format == 0) { |
||
| 230 | $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']); |
||
| 231 | } elseif ($format == 1) { |
||
| 232 | $value = $arraytypes['code']; |
||
| 233 | } elseif ($format == 2) { |
||
| 234 | $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']); |
||
| 235 | } elseif ($format == 3) { |
||
| 236 | $value = $arraytypes['code']; |
||
| 237 | } |
||
| 238 | if (empty($value)) { |
||
| 239 | $value = ' '; |
||
| 240 | } |
||
| 241 | print $value; |
||
| 242 | print '</option>'; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | print '</select>'; |
||
| 246 | if ($user->admin && !$noadmininfo) { |
||
| 247 | print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 252 | /** |
||
| 253 | * Return a select list with zip codes and their town |
||
| 254 | * |
||
| 255 | * @param string $selected Preselected value |
||
| 256 | * @param string $htmlname HTML select name |
||
| 257 | * @param array $fields Array with key of fields to refresh after selection |
||
| 258 | * @param int $fieldsize Field size |
||
| 259 | * @param int $disableautocomplete 1 To disable ajax autocomplete features (browser autocomplete may still occurs) |
||
| 260 | * @param string $moreattrib Add more attribute on HTML input field |
||
| 261 | * @param string $morecss More css |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function select_ziptown($selected = '', $htmlname = 'zipcode', $fields = array(), $fieldsize = 0, $disableautocomplete = 0, $moreattrib = '', $morecss = '') |
||
| 283 | } |
||
| 284 | |||
| 285 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 286 | /** |
||
| 287 | * Returns the drop-down list of departments/provinces/cantons for all countries or for a given country. |
||
| 288 | * In the case of an all-country list, the display breaks on the country. |
||
| 289 | * The key of the list is the code (there can be several entries for a given code but in this case, the country field differs). |
||
| 290 | * Thus the links with the departments are done on a department independently of its name. |
||
| 291 | * |
||
| 292 | * @param int $selected Code state preselected (mus be state id) |
||
| 293 | * @param integer $country_codeid Country code or id: 0=list for all countries, otherwise country code or country rowid to show |
||
| 294 | * @param string $htmlname Id of department. If '', we want only the string with <option> |
||
| 295 | * @param string $morecss Add more css |
||
| 296 | * @return string String with HTML select |
||
| 297 | * @see select_country() |
||
| 298 | */ |
||
| 299 | public function select_state($selected = 0, $country_codeid = 0, $htmlname = 'state_id', $morecss = 'maxwidth200onsmartphone minwidth300') |
||
| 395 | } |
||
| 396 | } |
||
| 397 |