| Total Complexity | 334 |
| Total Lines | 1325 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FormWebPortal 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 FormWebPortal, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class FormWebPortal extends Form |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var DoliDB Database |
||
| 45 | */ |
||
| 46 | public $db; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array $infofiles Array of file info |
||
| 50 | */ |
||
| 51 | public $infofiles; // Used to return information by function getDocumentsLink |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor |
||
| 56 | * |
||
| 57 | * @param DoliDB $db Database handler |
||
| 58 | */ |
||
| 59 | public function __construct($db) |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Html for input with label |
||
| 66 | * |
||
| 67 | * @param string $type Type of input : button, checkbox, color, email, hidden, month, number, password, radio, range, tel, text, time, url, week |
||
| 68 | * @param string $name Name |
||
| 69 | * @param string $value [=''] Value |
||
| 70 | * @param string $id [=''] Id |
||
| 71 | * @param string $morecss [=''] Class |
||
| 72 | * @param string $moreparam [=''] Add attributes (checked, required, etc) |
||
| 73 | * @param string $label [=''] Label |
||
| 74 | * @param string $addInputLabel [=''] Add label for input |
||
| 75 | * @return string Html for input with label |
||
| 76 | */ |
||
| 77 | public function inputType($type, $name, $value = '', $id = '', $morecss = '', $moreparam = '', $label = '', $addInputLabel = '') |
||
| 78 | { |
||
| 79 | $out = ''; |
||
| 80 | if ($label != '') { |
||
| 81 | $out .= '<label for="' . $id . '">'; |
||
| 82 | } |
||
| 83 | $out .= '<input type="' . $type . '"'; |
||
| 84 | $out .= ($morecss ? ' class="' . $morecss . '"' : ''); |
||
| 85 | if ($id != '') { |
||
| 86 | $out .= ' id="' . $id . '"'; |
||
| 87 | } |
||
| 88 | $out .= ' name="' . $name . '"'; |
||
| 89 | $out .= ' value="' . $value . '"'; |
||
| 90 | $out .= ($moreparam ? ' ' . $moreparam : ''); |
||
| 91 | $out .= ' />' . $addInputLabel; |
||
| 92 | if ($label != '') { |
||
| 93 | $out .= $label . '</label>'; |
||
| 94 | } |
||
| 95 | |||
| 96 | return $out; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Input for date |
||
| 101 | * |
||
| 102 | * @param string $name Name of html input |
||
| 103 | * @param string $value [=''] Value of input (format : YYYY-MM-DD) |
||
| 104 | * @param string $placeholder [=''] Placeholder for input (keep empty for no label) |
||
| 105 | * @param string $id [=''] Id |
||
| 106 | * @param string $morecss [=''] Class |
||
| 107 | * @param string $moreparam [=''] Add attributes (checked, required, etc) |
||
| 108 | * @return string Html for input date |
||
| 109 | */ |
||
| 110 | public function inputDate($name, $value = '', $placeholder = '', $id = '', $morecss = '', $moreparam = '') |
||
| 111 | { |
||
| 112 | $out = ''; |
||
| 113 | |||
| 114 | $out .= '<input'; |
||
| 115 | if ($placeholder != '' && $value == '') { |
||
| 116 | // to show a placeholder on date input |
||
| 117 | $out .= ' type="text" placeholder="' . $placeholder . '" onfocus="(this.type=\'date\')"'; |
||
| 118 | } else { |
||
| 119 | $out .= ' type="date"'; |
||
| 120 | } |
||
| 121 | $out .= ($morecss ? ' class="' . $morecss . '"' : ''); |
||
| 122 | if ($id != '') { |
||
| 123 | $out .= ' id="' . $id . '"'; |
||
| 124 | } |
||
| 125 | $out .= ' name="' . $name . '"'; |
||
| 126 | $out .= ' value="' . $value . '"'; |
||
| 127 | $out .= ($moreparam ? ' ' . $moreparam : ''); |
||
| 128 | |||
| 129 | $out .= '>'; |
||
| 130 | |||
| 131 | return $out; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Return a HTML select string, built from an array of key+value. |
||
| 136 | * Note: Do not apply langs->trans function on returned content, content may be entity encoded twice. |
||
| 137 | * |
||
| 138 | * @param string $htmlname Name of html select area. |
||
| 139 | * @param array $array Array like array(key => value) or array(key=>array('label'=>..., 'data-...'=>..., 'disabled'=>..., 'css'=>...)) |
||
| 140 | * @param string|string[] $id Preselected key or preselected keys for multiselect. Use 'ifone' to autoselect record if there is only one record. |
||
| 141 | * @param int|string $show_empty 0 no empty value allowed, 1 or string to add an empty value into list (If 1: key is -1 and value is '' or ' ', If placeholder string: key is -1 and value is the string), <0 to add an empty value with key that is this value. |
||
| 142 | * @param int $key_in_label 1 to show key into label with format "[key] value" |
||
| 143 | * @param int $value_as_key 1 to use value as key |
||
| 144 | * @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container |
||
| 145 | * @param int $translate 1=Translate and encode value |
||
| 146 | * @param int $maxlen Length maximum for labels |
||
| 147 | * @param int $disabled Html select box is disabled |
||
| 148 | * @param string $sort 'ASC' or 'DESC' = Sort on label, '' or 'NONE' or 'POS' = Do not sort, we keep original order |
||
| 149 | * @param string $morecss Add more class to css styles |
||
| 150 | * @param int $addjscombo Add js combo |
||
| 151 | * @param string $moreparamonempty Add more param on the empty option line. Not used if show_empty not set |
||
| 152 | * @param int $disablebademail 1=Check if a not valid email, 2=Check string '---', and if found into value, disable and colorize entry |
||
| 153 | * @param int $nohtmlescape No html escaping. |
||
| 154 | * @return string HTML select string. |
||
| 155 | */ |
||
| 156 | public static function selectarray($htmlname, $array, $id = '', $show_empty = 0, $key_in_label = 0, $value_as_key = 0, $moreparam = '', $translate = 0, $maxlen = 0, $disabled = 0, $sort = '', $morecss = 'minwidth75', $addjscombo = 1, $moreparamonempty = '', $disablebademail = 0, $nohtmlescape = 0) |
||
| 157 | { |
||
| 158 | global $langs; |
||
| 159 | |||
| 160 | if ($value_as_key) { |
||
| 161 | $array = array_combine($array, $array); |
||
| 162 | } |
||
| 163 | |||
| 164 | $out = ''; |
||
| 165 | |||
| 166 | $idname = str_replace(array('[', ']'), array('', ''), $htmlname); |
||
| 167 | $out .= '<select id="' . preg_replace('/^\./', '', $idname) . '"' . ($disabled ? ' disabled="disabled"' : '') . ' class="' . ($morecss ? ' ' . $morecss : '') . '"'; |
||
| 168 | $out .= ' name="' . preg_replace('/^\./', '', $htmlname) . '"' . ($moreparam ? ' ' . $moreparam : ''); |
||
| 169 | $out .= '>' . "\n"; |
||
| 170 | |||
| 171 | if ($show_empty) { |
||
| 172 | $textforempty = ' '; |
||
| 173 | if (!is_numeric($show_empty)) { |
||
| 174 | $textforempty = $show_empty; |
||
| 175 | } |
||
| 176 | $out .= '<option value="' . ($show_empty < 0 ? $show_empty : -1) . '"' . ($id == $show_empty ? ' selected' : '') . '>' . $textforempty . '</option>' . "\n"; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (is_array($array)) { |
||
| 180 | // Translate |
||
| 181 | if ($translate) { |
||
| 182 | foreach ($array as $key => $value) { |
||
| 183 | if (!is_array($value)) { |
||
| 184 | $array[$key] = $langs->trans($value); |
||
| 185 | } else { |
||
| 186 | $array[$key]['label'] = $langs->trans($value['label']); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | // Sort |
||
| 192 | if ($sort == 'ASC') { |
||
| 193 | asort($array); |
||
| 194 | } elseif ($sort == 'DESC') { |
||
| 195 | arsort($array); |
||
| 196 | } |
||
| 197 | |||
| 198 | foreach ($array as $key => $tmpvalue) { |
||
| 199 | if (is_array($tmpvalue)) { |
||
| 200 | $value = $tmpvalue['label']; |
||
| 201 | $disabled = empty($tmpvalue['disabled']) ? '' : ' disabled'; |
||
| 202 | } else { |
||
| 203 | $value = $tmpvalue; |
||
| 204 | $disabled = ''; |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($key_in_label) { |
||
| 208 | $selectOptionValue = dol_escape_htmltag($key . ' - ' . ($maxlen ? dol_trunc($value, $maxlen) : $value)); |
||
| 209 | } else { |
||
| 210 | $selectOptionValue = dol_escape_htmltag($maxlen ? dol_trunc($value, $maxlen) : $value); |
||
| 211 | if ($value == '' || $value == '-') { |
||
| 212 | $selectOptionValue = ' '; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | $out .= '<option value="' . $key . '"'; |
||
| 217 | $out .= $disabled; |
||
| 218 | if (is_array($id)) { |
||
| 219 | if (in_array($key, $id) && !$disabled) { |
||
| 220 | $out .= ' selected'; // To preselect a value |
||
| 221 | } |
||
| 222 | } else { |
||
| 223 | $id = (string) $id; // if $id = 0, then $id = '0' |
||
| 224 | if ($id != '' && ($id == $key || ($id == 'ifone' && count($array) == 1)) && !$disabled) { |
||
| 225 | $out .= ' selected'; // To preselect a value |
||
| 226 | } |
||
| 227 | } |
||
| 228 | if (is_array($tmpvalue)) { |
||
| 229 | foreach ($tmpvalue as $keyforvalue => $valueforvalue) { |
||
| 230 | if (preg_match('/^data-/', $keyforvalue)) { |
||
| 231 | $out .= ' ' . $keyforvalue . '="' . dol_escape_htmltag($valueforvalue) . '"'; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | $out .= '>'; |
||
| 236 | $out .= $selectOptionValue; |
||
| 237 | $out .= "</option>\n"; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | $out .= "</select>"; |
||
| 242 | |||
| 243 | return $out; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Show a Document icon with link(s) |
||
| 248 | * You may want to call this into a div like this: |
||
| 249 | * print '<div class="inline-block valignmiddle">'.$formfile->getDocumentsLink($element_doc, $filename, $filedir).'</div>'; |
||
| 250 | * |
||
| 251 | * @param string $modulepart 'propal', 'facture', 'facture_fourn', ... |
||
| 252 | * @param string $modulesubdir Sub-directory to scan (Example: '0/1/10', 'FA/DD/MM/YY/9999'). Use '' if file is not into subdir of module. |
||
| 253 | * @param string $filedir Full path to directory to scan |
||
| 254 | * @param string $filter Filter filenames on this regex string (Example: '\.pdf$') |
||
| 255 | * @param string $morecss Add more css to the download picto |
||
| 256 | * @param int $allfiles 0=Only generated docs, 1=All files |
||
| 257 | * @return string Output string with HTML link of documents (might be empty string). This also fill the array ->infofiles |
||
| 258 | */ |
||
| 259 | public function getDocumentsLink($modulepart, $modulesubdir, $filedir, $filter = '', $morecss = '', $allfiles = 0) |
||
| 260 | { |
||
| 261 | include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
| 262 | |||
| 263 | $out = ''; |
||
| 264 | |||
| 265 | $context = Context::getInstance(); |
||
| 266 | if (!$context) { |
||
| 267 | return ''; |
||
| 268 | } |
||
| 269 | |||
| 270 | $this->infofiles = array('nboffiles' => 0, 'extensions' => array(), 'files' => array()); |
||
| 271 | |||
| 272 | $entity = 1; // Without multicompany |
||
| 273 | |||
| 274 | // Get object entity |
||
| 275 | if (isModEnabled('multicompany')) { |
||
| 276 | $regs = array(); |
||
| 277 | preg_match('/\/([0-9]+)\/[^\/]+\/' . preg_quote($modulesubdir, '/') . '$/', $filedir, $regs); |
||
| 278 | $entity = ((!empty($regs[1]) && $regs[1] > 1) ? $regs[1] : 1); // If entity id not found in $filedir this is entity 1 by default |
||
| 279 | } |
||
| 280 | |||
| 281 | // Get list of files starting with name of ref (Note: files with '^ref\.extension' are generated files, files with '^ref-...' are uploaded files) |
||
| 282 | if ($allfiles || getDolGlobalString('MAIN_SHOW_ALL_FILES_ON_DOCUMENT_TOOLTIP')) { |
||
| 283 | $filterforfilesearch = '^' . preg_quote(basename($modulesubdir), '/'); |
||
| 284 | } else { |
||
| 285 | $filterforfilesearch = '^' . preg_quote(basename($modulesubdir), '/') . '\.'; |
||
| 286 | } |
||
| 287 | $file_list = dol_dir_list($filedir, 'files', 0, $filterforfilesearch, '\.meta$|\.png$'); // We also discard .meta and .png preview |
||
| 288 | |||
| 289 | //var_dump($file_list); |
||
| 290 | // For ajax treatment |
||
| 291 | $out .= '<!-- html.formwebportal::getDocumentsLink -->' . "\n"; |
||
| 292 | if (!empty($file_list)) { |
||
| 293 | $tmpout = ''; |
||
| 294 | |||
| 295 | // Loop on each file found |
||
| 296 | $found = 0; |
||
| 297 | $i = 0; |
||
| 298 | foreach ($file_list as $file) { |
||
| 299 | $i++; |
||
| 300 | if ($filter && !preg_match('/' . $filter . '/i', $file["name"])) { |
||
| 301 | continue; // Discard this. It does not match provided filter. |
||
| 302 | } |
||
| 303 | |||
| 304 | $found++; |
||
| 305 | // Define relative path for download link (depends on module) |
||
| 306 | $relativepath = $file["name"]; // Cas general |
||
| 307 | if ($modulesubdir) { |
||
| 308 | $relativepath = $modulesubdir . "/" . $file["name"]; // Cas propal, facture... |
||
| 309 | } |
||
| 310 | // Autre cas |
||
| 311 | if ($modulepart == 'donation') { |
||
| 312 | $relativepath = get_exdir($modulesubdir, 2, 0, 0, null, 'donation') . $file["name"]; |
||
| 313 | } |
||
| 314 | if ($modulepart == 'export') { |
||
| 315 | $relativepath = $file["name"]; |
||
| 316 | } |
||
| 317 | |||
| 318 | $this->infofiles['nboffiles']++; |
||
| 319 | $this->infofiles['files'][] = $file['fullname']; |
||
| 320 | $ext = pathinfo($file["name"], PATHINFO_EXTENSION); |
||
| 321 | if (empty($this->infofiles[$ext])) { |
||
| 322 | $this->infofiles['extensions'][$ext] = 1; |
||
| 323 | } else { |
||
| 324 | $this->infofiles['extensions'][$ext]++; |
||
| 325 | } |
||
| 326 | |||
| 327 | // Download |
||
| 328 | $url = $context->getControllerUrl('document') . '&modulepart=' . $modulepart . '&entity=' . $entity . '&file=' . urlencode($relativepath) . '&soc_id=' . $context->logged_thirdparty->id; |
||
| 329 | $tmpout .= '<a href="' . $url . '"' . ($morecss ? ' class="' . $morecss . '"' : '') . ' role="downloadlink"'; |
||
| 330 | $mime = dol_mimetype($relativepath, '', 0); |
||
| 331 | if (preg_match('/text/', $mime)) { |
||
| 332 | $tmpout .= ' target="_blank" rel="noopener noreferrer"'; |
||
| 333 | } |
||
| 334 | $tmpout .= '>'; |
||
| 335 | $tmpout .= img_mime($relativepath, $file["name"]); |
||
| 336 | $tmpout .= strtoupper($ext); |
||
| 337 | $tmpout .= '</a>'; |
||
| 338 | } |
||
| 339 | |||
| 340 | if ($found) { |
||
| 341 | $out .= $tmpout; |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | return $out; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Show a Signature icon with link |
||
| 350 | * You may want to call this into a div like this: |
||
| 351 | * print '<div class="inline-block valignmiddle">'.$formfile->getDocumentsLink($element_doc, $filename, $filedir).'</div>'; |
||
| 352 | * |
||
| 353 | * @param string $modulepart 'proposal', 'facture', 'facture_fourn', ... |
||
| 354 | * @param Object $object Object linked to the document to be signed |
||
| 355 | * @param string $morecss Add more css to the download picto |
||
| 356 | * @return string Output string with HTML link of signature (might be empty string). |
||
| 357 | */ |
||
| 358 | public function getSignatureLink($modulepart, $object, $morecss = '') |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Generic method to select a component from a combo list. |
||
| 375 | * Can use autocomplete with ajax after x key pressed or a full combo, depending on setup. |
||
| 376 | * This is the generic method that will replace all specific existing methods. |
||
| 377 | * |
||
| 378 | * @param string $objectdesc 'ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]'. For hard coded custom needs. Try to prefer method using $objectfield instead of $objectdesc. |
||
| 379 | * @param string $htmlname Name of HTML select component |
||
| 380 | * @param int $preselectedvalue Preselected value (ID of element) |
||
| 381 | * @param string $showempty ''=empty values not allowed, 'string'=value show if we allow empty values (for example 'All', ...) |
||
| 382 | * @param string $searchkey Search criteria |
||
| 383 | * @param string $placeholder Place holder |
||
| 384 | * @param string $morecss More CSS |
||
| 385 | * @param string $moreparams More params provided to ajax call |
||
| 386 | * @param int $forcecombo Force to load all values and output a standard combobox (with no beautification) |
||
| 387 | * @param int $disabled 1=Html component is disabled |
||
| 388 | * @param string $selected_input_value Value of preselected input text (for use with ajax) |
||
| 389 | * @param string $objectfield Object:Field that contains the definition (in table $fields or $extrafields). Example: 'Object:xxx' or 'Module_Object:xxx' or 'Object:options_xxx' or 'Module_Object:options_xxx' |
||
| 390 | * @return string Return HTML string |
||
| 391 | * @see selectForFormsList(), select_thirdparty_list() |
||
| 392 | */ |
||
| 393 | public function selectForForms($objectdesc, $htmlname, $preselectedvalue, $showempty = '', $searchkey = '', $placeholder = '', $morecss = '', $moreparams = '', $forcecombo = 0, $disabled = 0, $selected_input_value = '', $objectfield = '') |
||
| 394 | { |
||
| 395 | global $conf; |
||
| 396 | |||
| 397 | $objecttmp = null; |
||
| 398 | |||
| 399 | // Example of value for $objectdec: |
||
| 400 | // Bom:bom/class/bom.class.php:0:t.status=1 |
||
| 401 | // Bom:bom/class/bom.class.php:0:t.status=1:ref |
||
| 402 | // Bom:bom/class/bom.class.php:0:(t.status:=:1):ref |
||
| 403 | $InfoFieldList = explode(":", $objectdesc, 4); |
||
| 404 | $vartmp = (empty($InfoFieldList[3]) ? '' : $InfoFieldList[3]); |
||
| 405 | $reg = array(); |
||
| 406 | if (preg_match('/^.*:(\w*)$/', $vartmp, $reg)) { |
||
| 407 | $InfoFieldList[4] = $reg[1]; // take the sort field |
||
| 408 | } |
||
| 409 | $InfoFieldList[3] = preg_replace('/:\w*$/', '', $vartmp); // take the filter field |
||
| 410 | |||
| 411 | $classname = $InfoFieldList[0]; |
||
| 412 | $classpath = $InfoFieldList[1]; |
||
| 413 | $filter = empty($InfoFieldList[3]) ? '' : $InfoFieldList[3]; |
||
| 414 | $sortfield = empty($InfoFieldList[4]) ? '' : $InfoFieldList[4]; |
||
| 415 | |||
| 416 | if (!empty($classpath)) { |
||
| 417 | dol_include_once($classpath); |
||
| 418 | |||
| 419 | if ($classname && class_exists($classname)) { |
||
| 420 | $objecttmp = new $classname($this->db); |
||
| 421 | |||
| 422 | // Make some replacement |
||
| 423 | $sharedentities = getEntity(strtolower($classname)); |
||
| 424 | $filter = str_replace( |
||
| 425 | array('__ENTITY__', '__SHARED_ENTITIES__'), |
||
| 426 | array($conf->entity, $sharedentities), |
||
| 427 | $filter |
||
| 428 | ); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | if (!is_object($objecttmp)) { |
||
| 432 | dol_syslog('Error bad setup of type for field ' . implode(',', $InfoFieldList), LOG_WARNING); |
||
| 433 | return 'Error bad setup of type for field ' . implode(',', $InfoFieldList); |
||
| 434 | } |
||
| 435 | |||
| 436 | dol_syslog(__METHOD__ . ' filter=' . $filter, LOG_DEBUG); |
||
| 437 | $out = ''; |
||
| 438 | // Immediate load of table record. |
||
| 439 | $out .= $this->selectForFormsList($objecttmp, $htmlname, $preselectedvalue, $showempty, $searchkey, $placeholder, $morecss, $moreparams, $forcecombo, 0, $disabled, $sortfield, $filter); |
||
| 440 | |||
| 441 | return $out; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Output html form to select an object. |
||
| 446 | * Note, this function is called by selectForForms or by ajax selectobject.php |
||
| 447 | * |
||
| 448 | * @param Object $objecttmp Object to know the table to scan for combo. |
||
| 449 | * @param string $htmlname Name of HTML select component |
||
| 450 | * @param int $preselectedvalue Preselected value (ID of element) |
||
| 451 | * @param string $showempty ''=empty values not allowed, 'string'=value show if we allow empty values (for example 'All', ...) |
||
| 452 | * @param string $searchkey Search value |
||
| 453 | * @param string $placeholder Place holder |
||
| 454 | * @param string $morecss More CSS |
||
| 455 | * @param string $moreparams More params provided to ajax call |
||
| 456 | * @param int $forcecombo Force to load all values and output a standard combobox (with no beautification) |
||
| 457 | * @param int $outputmode 0=HTML select string, 1=Array |
||
| 458 | * @param int $disabled 1=Html component is disabled |
||
| 459 | * @param string $sortfield Sort field |
||
| 460 | * @param string $filter Add more filter (Universal Search Filter) |
||
| 461 | * @return string|array Return HTML string |
||
| 462 | * @see selectForForms() |
||
| 463 | */ |
||
| 464 | public function selectForFormsList($objecttmp, $htmlname, $preselectedvalue, $showempty = '', $searchkey = '', $placeholder = '', $morecss = '', $moreparams = '', $forcecombo = 0, $outputmode = 0, $disabled = 0, $sortfield = '', $filter = '') |
||
| 465 | { |
||
| 466 | global $conf, $langs, $hookmanager; |
||
| 467 | |||
| 468 | $prefixforautocompletemode = $objecttmp->element; |
||
| 469 | if ($prefixforautocompletemode == 'societe') { |
||
| 470 | $prefixforautocompletemode = 'company'; |
||
| 471 | } |
||
| 472 | $confkeyforautocompletemode = strtoupper($prefixforautocompletemode) . '_USE_SEARCH_TO_SELECT'; // For example COMPANY_USE_SEARCH_TO_SELECT |
||
| 473 | |||
| 474 | if (in_array($objecttmp->element, array('adherent_type'))) { |
||
| 475 | $fieldstoshow = 't.libelle'; |
||
| 476 | } |
||
| 477 | if (!empty($objecttmp->fields)) { // For object that declare it, it is better to use declared fields (like societe, contact, ...) |
||
| 478 | $tmpfieldstoshow = ''; |
||
| 479 | foreach ($objecttmp->fields as $key => $val) { |
||
| 480 | if (! (int) dol_eval($val['enabled'], 1, 1, '1')) { |
||
| 481 | continue; |
||
| 482 | } |
||
| 483 | if (!empty($val['showoncombobox'])) { |
||
| 484 | $tmpfieldstoshow .= ($tmpfieldstoshow ? ',' : '') . 't.' . $key; |
||
| 485 | } |
||
| 486 | } |
||
| 487 | if ($tmpfieldstoshow) { |
||
| 488 | $fieldstoshow = $tmpfieldstoshow; |
||
| 489 | } |
||
| 490 | } elseif (!in_array($objecttmp->element, array('adherent_type'))) { |
||
| 491 | // For backward compatibility |
||
| 492 | $objecttmp->fields['ref'] = array('type' => 'varchar(30)', 'label' => 'Ref', 'showoncombobox' => 1); |
||
| 493 | } |
||
| 494 | |||
| 495 | if (empty($fieldstoshow)) { |
||
| 496 | if (isset($objecttmp->fields['ref'])) { |
||
| 497 | $fieldstoshow = 't.ref'; |
||
| 498 | } else { |
||
| 499 | $langs->load("errors"); |
||
| 500 | $this->error = $langs->trans("ErrorNoFieldWithAttributeShowoncombobox"); |
||
| 501 | return $langs->trans('ErrorNoFieldWithAttributeShowoncombobox'); |
||
| 502 | } |
||
| 503 | } |
||
| 504 | |||
| 505 | $out = ''; |
||
| 506 | $outarray = array(); |
||
| 507 | $tmparray = array(); |
||
| 508 | |||
| 509 | $num = 0; |
||
| 510 | |||
| 511 | // Search data |
||
| 512 | $sql = "SELECT t.rowid, " . $fieldstoshow . " FROM " . $this->db->prefix() . $objecttmp->table_element . " as t"; |
||
| 513 | if (isset($objecttmp->ismultientitymanaged)) { |
||
| 514 | if (!is_numeric($objecttmp->ismultientitymanaged)) { |
||
| 515 | $tmparray = explode('@', $objecttmp->ismultientitymanaged); |
||
| 516 | $sql .= " INNER JOIN " . $this->db->prefix() . $tmparray[1] . " as parenttable ON parenttable.rowid = t." . $tmparray[0]; |
||
| 517 | } |
||
| 518 | } |
||
| 519 | |||
| 520 | // Add where from hooks |
||
| 521 | $parameters = array( |
||
| 522 | 'object' => $objecttmp, |
||
| 523 | 'htmlname' => $htmlname, |
||
| 524 | 'filter' => $filter, |
||
| 525 | 'searchkey' => $searchkey |
||
| 526 | ); |
||
| 527 | |||
| 528 | $reshook = $hookmanager->executeHooks('selectForFormsListWhere', $parameters); // Note that $action and $object may have been modified by hook |
||
| 529 | if (!empty($hookmanager->resPrint)) { |
||
| 530 | $sql .= $hookmanager->resPrint; |
||
| 531 | } else { |
||
| 532 | $sql .= " WHERE 1=1"; |
||
| 533 | if (isset($objecttmp->ismultientitymanaged)) { |
||
| 534 | if ($objecttmp->ismultientitymanaged == 1) { |
||
| 535 | $sql .= " AND t.entity IN (" . getEntity($objecttmp->table_element) . ")"; |
||
| 536 | } |
||
| 537 | if (!is_numeric($objecttmp->ismultientitymanaged)) { |
||
| 538 | $sql .= " AND parenttable.entity = t." . $tmparray[0]; |
||
| 539 | } |
||
| 540 | } |
||
| 541 | if ($searchkey != '') { |
||
| 542 | $sql .= natural_search(explode(',', $fieldstoshow), $searchkey); |
||
| 543 | } |
||
| 544 | |||
| 545 | if ($filter) { // Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 546 | $errormessage = ''; |
||
| 547 | $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage); |
||
| 548 | if ($errormessage) { |
||
| 549 | return 'Error forging a SQL request from an universal criteria: ' . $errormessage; |
||
| 550 | } |
||
| 551 | } |
||
| 552 | } |
||
| 553 | $sql .= $this->db->order($sortfield ? $sortfield : $fieldstoshow, "ASC"); |
||
| 554 | |||
| 555 | // Build output string |
||
| 556 | $resql = $this->db->query($sql); |
||
| 557 | if ($resql) { |
||
| 558 | // Construct $out and $outarray |
||
| 559 | $out .= '<select id="' . $htmlname . '" class="' . ($morecss ? ' ' . $morecss : '') . '"' . ($disabled ? ' disabled="disabled"' : '') . ($moreparams ? ' ' . $moreparams : '') . ' name="' . $htmlname . '">' . "\n"; |
||
| 560 | |||
| 561 | // Warning: Do not use textifempty = ' ' or ' ' here, or search on key will search on ' key'. Seems it is no more true with selec2 v4 |
||
| 562 | $textifempty = ' '; |
||
| 563 | |||
| 564 | //if (!empty($conf->use_javascript_ajax) || $forcecombo) $textifempty=''; |
||
| 565 | if (!empty($conf->global->$confkeyforautocompletemode)) { |
||
| 566 | if ($showempty && !is_numeric($showempty)) { |
||
| 567 | $textifempty = $langs->trans($showempty); |
||
| 568 | } else { |
||
| 569 | $textifempty .= $langs->trans("All"); |
||
| 570 | } |
||
| 571 | } |
||
| 572 | if ($showempty) { |
||
| 573 | $out .= '<option value="-1">' . $textifempty . '</option>' . "\n"; |
||
| 574 | } |
||
| 575 | |||
| 576 | $num = $this->db->num_rows($resql); |
||
| 577 | $i = 0; |
||
| 578 | if ($num) { |
||
| 579 | while ($i < $num) { |
||
| 580 | $obj = $this->db->fetch_object($resql); |
||
| 581 | $label = ''; |
||
| 582 | $labelhtml = ''; |
||
| 583 | $tmparray = explode(',', $fieldstoshow); |
||
| 584 | $oldvalueforshowoncombobox = 0; |
||
| 585 | foreach ($tmparray as $key => $val) { |
||
| 586 | $val = preg_replace('/t\./', '', $val); |
||
| 587 | $label .= (($label && $obj->$val) ? ($oldvalueforshowoncombobox != $objecttmp->fields[$val]['showoncombobox'] ? ' - ' : ' ') : ''); |
||
| 588 | $labelhtml .= (($label && $obj->$val) ? ($oldvalueforshowoncombobox != $objecttmp->fields[$val]['showoncombobox'] ? ' - ' : ' ') : ''); |
||
| 589 | $label .= $obj->$val; |
||
| 590 | $labelhtml .= $obj->$val; |
||
| 591 | |||
| 592 | $oldvalueforshowoncombobox = empty($objecttmp->fields[$val]['showoncombobox']) ? 0 : $objecttmp->fields[$val]['showoncombobox']; |
||
| 593 | } |
||
| 594 | if (empty($outputmode)) { |
||
| 595 | if ($preselectedvalue > 0 && $preselectedvalue == $obj->rowid) { |
||
| 596 | $out .= '<option value="' . $obj->rowid . '" selected data-html="' . dol_escape_htmltag($labelhtml, 0, 0, '', 0, 1) . '">' . dol_escape_htmltag($label, 0, 0, '', 0, 1) . '</option>'; |
||
| 597 | } else { |
||
| 598 | $out .= '<option value="' . $obj->rowid . '" data-html="' . dol_escape_htmltag($labelhtml, 0, 0, '', 0, 1) . '">' . dol_escape_htmltag($label, 0, 0, '', 0, 1) . '</option>'; |
||
| 599 | } |
||
| 600 | } else { |
||
| 601 | array_push($outarray, array('key' => $obj->rowid, 'value' => $label, 'label' => $label)); |
||
| 602 | } |
||
| 603 | |||
| 604 | $i++; |
||
| 605 | if (($i % 10) == 0) { |
||
| 606 | $out .= "\n"; |
||
| 607 | } |
||
| 608 | } |
||
| 609 | } |
||
| 610 | |||
| 611 | $out .= '</select>' . "\n"; |
||
| 612 | } else { |
||
| 613 | dol_print_error($this->db); |
||
| 614 | } |
||
| 615 | |||
| 616 | $this->result = array('nbofelement' => $num); |
||
| 617 | |||
| 618 | if ($outputmode) { |
||
| 619 | return $outarray; |
||
| 620 | } |
||
| 621 | |||
| 622 | return $out; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Return HTML string to put an input field into a page |
||
| 627 | * Code very similar with showInputField for common object |
||
| 628 | * |
||
| 629 | * @param array|null $val Array of properties for field to show |
||
| 630 | * @param string $key Key of attribute |
||
| 631 | * @param string|array $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) |
||
| 632 | * @param string $moreparam To add more parameters on html input tag |
||
| 633 | * @param string $keysuffix Prefix string to add into name and id of field (can be used to avoid duplicate names) |
||
| 634 | * @param string $keyprefix Suffix string to add into name and id of field (can be used to avoid duplicate names) |
||
| 635 | * @param string $morecss Value for css to define style/length of field. May also be a numeric. |
||
| 636 | * @return string |
||
| 637 | */ |
||
| 638 | public function showInputField($val, $key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '') |
||
| 976 | } |
||
| 977 | |||
| 978 | /** |
||
| 979 | * Return HTML string to show a field into a page |
||
| 980 | * |
||
| 981 | * @param CommonObject $object Common object |
||
| 982 | * @param array $val Array of properties of field to show |
||
| 983 | * @param string $key Key of attribute |
||
| 984 | * @param string|string[] $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value) |
||
| 985 | * @param string $moreparam To add more parameters on html input tag |
||
| 986 | * @param string $keysuffix Prefix string to add into name and id of field (can be used to avoid duplicate names) |
||
| 987 | * @param string $keyprefix Suffix string to add into name and id of field (can be used to avoid duplicate names) |
||
| 988 | * @param mixed $morecss Value for css to define size. May also be a numeric. |
||
| 989 | * @return string |
||
| 990 | */ |
||
| 991 | public function showOutputFieldForObject($object, $val, $key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '') |
||
| 1366 | } |
||
| 1367 | } |
||
| 1368 |