dolibarr 24.0.0-beta
html.formadmin.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
33{
37 public $db;
38
42 public $error;
43
44
50 public function __construct($db)
51 {
52 $this->db = $db;
53 }
54
55 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
74 public function select_language($selected = '', $htmlname = 'lang_id', $showauto = 0, $filter = array(), $showempty = '', $showwarning = 0, $disabled = 0, $morecss = 'minwidth100', $showcode = 0, $forcecombo = 0, $multiselect = 0, $onlykeys = array(), $mainlangonly = 0)
75 {
76 // phpcs:enable
77 global $langs;
78
79 if (getDolGlobalString('MAIN_DEFAULT_LANGUAGE_FILTER')) {
80 if (!is_array($filter)) {
81 $filter = array();
82 }
83 $filter[getDolGlobalString('MAIN_DEFAULT_LANGUAGE_FILTER')] = 1;
84 }
85
86 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12, 0, $mainlangonly);
87
88 // If empty value is not allowed and the language to select is not inside the list of available language and we must find
89 // an alternative of the language code to preselect (to avoid to have first element in list preselected).
90 if ($selected && empty($showempty)) {
91 if (!is_array($selected) && !array_key_exists($selected, $langs_available)) {
92 $tmparray = explode('_', $selected);
93 if (!empty($tmparray[1])) {
94 $selected = getLanguageCodeFromCountryCode($tmparray[1]);
95 }
96 if (empty($selected)) {
97 $selected = $langs->defaultlang;
98 }
99 } else {
100 // If the preselected value is an array, we do not try to find alternative to preselect
101 }
102 }
103
104 $out = '';
105
106 $out .= '<select '.($multiselect ? 'multiple="multiple" ' : '').'class="flat'.($morecss ? ' '.$morecss : '').'" id="'.$htmlname.'" name="'.$htmlname.($multiselect ? '[]' : '').'"'.($disabled ? ' disabled' : '').'>';
107 if ($showempty && !$multiselect) {
108 if (is_numeric($showempty)) {
109 $out .= '<option value="0"';
110 } else {
111 $out .= '<option value="-1"';
112 }
113 if ($selected === '') {
114 $out .= ' selected';
115 }
116 $out .= '>';
117 if ($showempty != '1') {
118 $out .= $showempty;
119 } else {
120 $out .= '&nbsp;';
121 }
122 $out .= '</option>';
123 }
124 if ($showauto) {
125 $out .= '<option value="auto"';
126 if ($selected === 'auto') {
127 $out .= ' selected';
128 }
129 if ($showcode > 0) {
130 $out .= '>'.$langs->trans("AutoDetectLang").'</option>';
131 } else {
132 $out .= '>'.$langs->trans("AutoDetectLangShort").'</option>';
133 }
134 }
135
136 asort($langs_available); // array('XX' => 'Language (Country)', ...)
137
138 foreach ($langs_available as $key => $value) {
139 $valuetoshow = $value;
140 if ($showcode == 1) {
141 if ($mainlangonly) {
142 $valuetoshow = '<span class="opacitymedium">'.preg_replace('/[_-].*$/', '', $key).'</span> - '.$value;
143 } else {
144 $valuetoshow = '<span class="opacitymedium">'.$key.'</span> - '.$value;
145 }
146 }
147 if ($showcode == 2 || $showcode == 3) {
148 if ($mainlangonly) {
149 $valuetoshow = $value.' <span class="opacitymedium">'.($showcode == 3 ? '' : '(').preg_replace('/[_-].*$/', '', $key).($showcode == 3 ? '' : ')').'</span>';
150 } else {
151 $valuetoshow = $value.' <span class="opacitymedium">'.($showcode == 3 ? '' : '(').$key.($showcode == 3 ? '' : ')').'</span>';
152 }
153 }
154
155 $keytouse = $key;
156 if ($mainlangonly) {
157 $keytouse = preg_replace('/[_-].*$/', '', $key);
158 }
159
160 if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) {
161 continue;
162 }
163 if ($onlykeys && is_array($onlykeys) && !array_key_exists($keytouse, $onlykeys)) {
164 continue;
165 }
166
167 $valuetoshow = picto_from_langcode($key, 'class="saturatemedium"').' '.$valuetoshow;
168
169 if ((is_string($selected) && (string) $selected == (string) $keytouse) || (is_array($selected) && in_array($keytouse, $selected))) {
170 $out .= '<option value="'.$keytouse.'" selected data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
171 } else {
172 $out .= '<option value="'.$keytouse.'" data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
173 }
174 }
175 $out .= '</select>';
176
177 // Make autocompletion
178 if (!$forcecombo) {
179 include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
180 $out .= ajax_combobox($htmlname);
181 }
182
183 return $out;
184 }
185
186 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
196 public function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib = '')
197 {
198 // phpcs:enable
199 global $langs, $conf;
200
201 // Clean parameters
202
203
204 // Check parameters
205 if (!is_array($dirmenuarray)) {
206 return -1;
207 }
208
209 $menuarray = array();
210 foreach ($conf->file->dol_document_root as $dirroot) {
211 foreach ($dirmenuarray as $dirtoscan) {
212 $dir = $dirroot.$dirtoscan;
213 //print $dir.'<br>';
214 if (is_dir($dir)) {
215 $handle = opendir($dir);
216 if (is_resource($handle)) {
217 while (($file = readdir($handle)) !== false) {
218 if (is_file($dir."/".$file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS' && substr($file, 0, 5) != 'index') {
219 if (preg_match('/lib\.php$/i', $file)) {
220 continue; // We exclude library files
221 }
222 if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i', $file)) {
223 continue; // We exclude all menu manager files
224 }
225 if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i', $file)) {
226 continue; // We exclude all menu manager files
227 }
228 if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i', $file)) {
229 continue; // We exclude all menu manager files
230 }
231
232 $filetoshow = preg_replace('/\.php$/i', '', $file);
233 $filetoshow = ucfirst(preg_replace('/_menu$/i', '', $filetoshow));
234 $prefix = '';
235 // 0=Recommended, 1=Experimental, 2=Development, 3=Other
236 if (preg_match('/^eldy/i', $file)) {
237 $prefix = '0';
238 } elseif (preg_match('/^smartphone/i', $file)) {
239 $prefix = '2';
240 } else {
241 $prefix = '3';
242 }
243
244 $morelabel = '';
245 if (preg_match('/^auguria/i', $file)) {
246 $morelabel .= ' <span class="opacitymedium">('.$langs->trans("Unstable").')</span>';
247 }
248 if ($file == $selected) {
249 $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" selected data-html="'.dol_escape_htmltag($filetoshow.$morelabel).'">';
250 $menuarray[$prefix.'_'.$file] .= $filetoshow.$morelabel;
251 $menuarray[$prefix.'_'.$file] .= '</option>';
252 } else {
253 $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" data-html="'.dol_escape_htmltag($filetoshow.$morelabel).'">';
254 $menuarray[$prefix.'_'.$file] .= $filetoshow.$morelabel;
255 $menuarray[$prefix.'_'.$file] .= '</option>';
256 }
257 }
258 }
259 closedir($handle);
260 }
261 }
262 }
263 }
264 ksort($menuarray);
265
266 // Output combo list of menus
267 print '<select class="flat minwidth150" id="'.$htmlname.'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
268 $oldprefix = '';
269 foreach ($menuarray as $key => $val) {
270 $tab = explode('_', $key);
271 $newprefix = $tab[0];
272
273 if ($newprefix == '1' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1)) {
274 continue;
275 }
276 if ($newprefix == '2' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2)) {
277 continue;
278 }
279 if ($newprefix != $oldprefix) { // Add separators
280 // Affiche titre
281 print '<option value="-2" disabled>';
282 if ($newprefix == '0') {
283 print '-- '.$langs->trans("VersionRecommanded").' --';
284 }
285 if ($newprefix == '1') {
286 print '-- '.$langs->trans("VersionExperimental").' --';
287 }
288 if ($newprefix == '2') {
289 print '-- '.$langs->trans("VersionDevelopment").' --';
290 }
291 if ($newprefix == '3') {
292 print '-- '.$langs->trans("Other").' --';
293 }
294 print '</option>';
295 $oldprefix = $newprefix;
296 }
297
298 print $val."\n"; // Show menu entry ($val contains the <option> tags)
299 }
300 print '</select>';
301
302 print ajax_combobox($htmlname);
303
304 return;
305 }
306
307 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
316 public function select_menu_families($selected, $htmlname, $dirmenuarray)
317 {
318 // phpcs:enable
319 global $langs, $conf;
320
321 //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
322 $expdevmenu = array();
323
324 $menuarray = array();
325
326 foreach ($dirmenuarray as $dirmenu) {
327 foreach ($conf->file->dol_document_root as $dirroot) {
328 $dir = $dirroot.$dirmenu;
329 if (is_dir($dir)) {
330 $handle = opendir($dir);
331 if (is_resource($handle)) {
332 while (($file = readdir($handle)) !== false) {
333 if (is_file($dir."/".$file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS') {
334 $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file);
335 if (preg_match('/^index/i', $filelib)) {
336 continue;
337 }
338 if (preg_match('/^default/i', $filelib)) {
339 continue;
340 }
341 if (preg_match('/^empty/i', $filelib)) {
342 continue;
343 }
344 if (preg_match('/\.lib/i', $filelib)) {
345 continue;
346 }
347 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) {
348 continue;
349 }
350
351 $menuarray[$filelib] = 1;
352 }
353 $menuarray['all'] = 1;
354 }
355 closedir($handle);
356 }
357 }
358 }
359 }
360
361 ksort($menuarray);
362
363 // Show combo list of menu handlers
364 print '<select class="flat width150" id="'.$htmlname.'" name="'.$htmlname.'">';
365 foreach ($menuarray as $key => $val) {
366 $tab = explode('_', $key);
367 print '<option value="'.$key.'"';
368 if ($key == $selected) {
369 print ' selected';
370 }
371 print '>';
372 if ($key == 'all') {
373 print $langs->trans("AllMenus");
374 } else {
375 print $key;
376 }
377 print '</option>'."\n";
378 }
379 print '</select>';
380
381 print ajax_combobox($htmlname);
382 }
383
384
385 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
393 public function select_timezone($selected, $htmlname)
394 {
395 // phpcs:enable
396 print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
397 print '<option value="-1">&nbsp;</option>';
398
399 $arraytz = array(
400 "Pacific/Midway" => "GMT-11:00",
401 "Pacific/Fakaofo" => "GMT-10:00",
402 "America/Anchorage" => "GMT-09:00",
403 "America/Los_Angeles" => "GMT-08:00",
404 "America/Dawson_Creek" => "GMT-07:00",
405 "America/Chicago" => "GMT-06:00",
406 "America/Bogota" => "GMT-05:00",
407 "America/Anguilla" => "GMT-04:00",
408 "America/Araguaina" => "GMT-03:00",
409 "America/Noronha" => "GMT-02:00",
410 "Atlantic/Azores" => "GMT-01:00",
411 "Africa/Abidjan" => "GMT+00:00",
412 "Europe/Paris" => "GMT+01:00",
413 "Europe/Helsinki" => "GMT+02:00",
414 "Europe/Moscow" => "GMT+03:00",
415 "Asia/Dubai" => "GMT+04:00",
416 "Asia/Karachi" => "GMT+05:00",
417 "Indian/Chagos" => "GMT+06:00",
418 "Asia/Jakarta" => "GMT+07:00",
419 "Asia/Hong_Kong" => "GMT+08:00",
420 "Asia/Tokyo" => "GMT+09:00",
421 "Australia/Sydney" => "GMT+10:00",
422 "Pacific/Noumea" => "GMT+11:00",
423 "Pacific/Auckland" => "GMT+12:00",
424 "Pacific/Enderbury" => "GMT+13:00"
425 );
426 foreach ($arraytz as $lib => $gmt) {
427 print '<option value="'.$lib.'"';
428 if ($selected == $lib || $selected == $gmt) {
429 print ' selected';
430 }
431 print '>'.$gmt.'</option>'."\n";
432 }
433 print '</select>';
434 }
435
436
437
438 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
449 public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = '', $showempty = 0, $forcecombo = 0)
450 {
451 // phpcs:enable
452 global $langs;
453
454 $langs->load("dict");
455
456 $sql = "SELECT code, label, width, height, unit";
457 $sql .= " FROM ".$this->db->prefix()."c_paper_format";
458 $sql .= " WHERE active=1";
459 if ($filter) {
460 $sql .= " AND code LIKE '%".$this->db->escape($filter)."%'";
461 }
462
463 $paperformat = array();
464
465 $resql = $this->db->query($sql);
466 if ($resql) {
467 $num = $this->db->num_rows($resql);
468 $i = 0;
469 while ($i < $num) {
470 $obj = $this->db->fetch_object($resql);
471 $unitKey = $langs->trans('SizeUnit'.$obj->unit);
472
473 $paperformat[$obj->code] = $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey);
474
475 $i++;
476 }
477 } else {
478 dol_print_error($this->db);
479 return '';
480 }
481 $out = '';
482
483 $out .= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
484 if ($showempty) {
485 $out .= '<option value=""';
486 if ($selected == '') {
487 $out .= ' selected';
488 }
489 $out .= '>&nbsp;</option>';
490 }
491 foreach ($paperformat as $key => $value) {
492 if ($selected == $key) {
493 $out .= '<option value="'.$key.'" selected>'.$value.'</option>';
494 } else {
495 $out .= '<option value="'.$key.'">'.$value.'</option>';
496 }
497 }
498 $out .= '</select>';
499
500 if (!$forcecombo) {
501 include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
502 $out .= ajax_combobox($htmlname);
503 }
504
505 return $out;
506 }
507
508
517 public function selectTypeOfFields($htmlname, $type, $typewecanchangeinto = array())
518 {
519 $type2label = ExtraFields::getListOfTypesLabels();
520
521 $out = '';
522
523 $out .= '<!-- combo with type of extrafields -->'."\n";
524 $out .= '<select class="flat type" id="'.$htmlname.'" name="'.$htmlname.'">';
525 foreach ($type2label as $key => $val) {
526 $selected = '';
527 if ($key == $type) {
528 $selected = ' selected="selected"';
529 }
530
531 // Set $valhtml with the picto for the type
532 $valhtml = ($key ? getPictoForType($key) : '').$val;
533
534 if (empty($typewecanchangeinto) || in_array($key, $typewecanchangeinto[$type])) {
535 $out .= '<option value="'.$key.'"'.$selected.' data-html="'.dol_escape_htmltag($valhtml).'">'.($val ? $val : '&nbsp;').'</option>';
536 } else {
537 $out .= '<option value="'.$key.'" disabled="disabled"'.$selected.' data-html="'.dol_escape_htmltag($valhtml).'">'.($val ? $val : '&nbsp;').'</option>';
538 }
539 }
540 $out .= '</select>';
541 $out .= ajax_combobox('type');
542
543 return $out;
544 }
545}
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:476
static getListOfTypesLabels()
Return array with all possible types and labels of extrafields.
Class to generate html code for admin pages.
select_language($selected='', $htmlname='lang_id', $showauto=0, $filter=array(), $showempty='', $showwarning=0, $disabled=0, $morecss='minwidth100', $showcode=0, $forcecombo=0, $multiselect=0, $onlykeys=array(), $mainlangonly=0)
Return html select list with available languages (key='en_US', value='United States' for example)
select_timezone($selected, $htmlname)
Return a HTML select list of timezones.
select_menu($selected, $htmlname, $dirmenuarray, $moreattrib='')
Return list of available menus (eldy_backoffice, ...)
selectTypeOfFields($htmlname, $type, $typewecanchangeinto=array())
Function to show the combo select to chose a type of field (varchar, int, email, ....
select_menu_families($selected, $htmlname, $dirmenuarray)
Return combo list of available menu families.
__construct($db)
Constructor.
select_paper_format($selected='', $htmlname='paperformat_id', $filter='', $showempty=0, $forcecombo=0)
Return html select list with available languages (key='en_US', value='United States' for example)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
getLanguageCodeFromCountryCode($countrycode)
Return default language from country code.
picto_from_langcode($codelang, $moreatt='', $notitlealt=0)
Return img flag of country for a language code or country code.
getPictoForType($key, $morecss='')
Return the picto for a data type.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...