dolibarr 21.0.0-alpha
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
32{
36 public $db;
37
41 public $error;
42
43
49 public function __construct($db)
50 {
51 $this->db = $db;
52 }
53
54 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
73 public function select_language($selected = '', $htmlname = 'lang_id', $showauto = 0, $filter = array(), $showempty = '', $showwarning = 0, $disabled = 0, $morecss = '', $showcode = 0, $forcecombo = 0, $multiselect = 0, $onlykeys = array(), $mainlangonly = 0)
74 {
75 // phpcs:enable
76 global $langs;
77
78 if (getDolGlobalString('MAIN_DEFAULT_LANGUAGE_FILTER')) {
79 if (!is_array($filter)) {
80 $filter = array();
81 }
82 $filter[getDolGlobalString('MAIN_DEFAULT_LANGUAGE_FILTER')] = 1;
83 }
84
85 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12, 0, $mainlangonly);
86
87 // If empty value is not allowed and the language to select is not inside the list of available language and we must find
88 // an alternative of the language code to pre-select (to avoid to have first element in list pre-selected).
89 if ($selected && empty($showempty)) {
90 if (!is_array($selected) && !array_key_exists($selected, $langs_available)) {
91 $tmparray = explode('_', $selected);
92 if (!empty($tmparray[1])) {
93 $selected = getLanguageCodeFromCountryCode($tmparray[1]);
94 }
95 if (empty($selected)) {
96 $selected = $langs->defaultlang;
97 }
98 } else {
99 // If the preselected value is an array, we do not try to find alternative to preselect
100 }
101 }
102
103 $out = '';
104
105 $out .= '<select '.($multiselect ? 'multiple="multiple" ' : '').'class="flat'.($morecss ? ' '.$morecss : '').'" id="'.$htmlname.'" name="'.$htmlname.($multiselect ? '[]' : '').'"'.($disabled ? ' disabled' : '').'>';
106 if ($showempty && !$multiselect) {
107 if (is_numeric($showempty)) {
108 $out .= '<option value="0"';
109 } else {
110 $out .= '<option value="-1"';
111 }
112 if ($selected === '') {
113 $out .= ' selected';
114 }
115 $out .= '>';
116 if ($showempty != '1') {
117 $out .= $showempty;
118 } else {
119 $out .= '&nbsp;';
120 }
121 $out .= '</option>';
122 }
123 if ($showauto) {
124 $out .= '<option value="auto"';
125 if ($selected === 'auto') {
126 $out .= ' selected';
127 }
128 $out .= '>'.$langs->trans("AutoDetectLang").'</option>';
129 }
130
131 asort($langs_available); // array('XX' => 'Language (Country)', ...)
132
133 foreach ($langs_available as $key => $value) {
134 $valuetoshow = $value;
135 if ($showcode == 1) {
136 if ($mainlangonly) {
137 $valuetoshow = '<span class="opacitymedium">'.preg_replace('/[_-].*$/', '', $key).'</span> - '.$value;
138 } else {
139 $valuetoshow = '<span class="opacitymedium">'.$key.'</span> - '.$value;
140 }
141 }
142 if ($showcode == 2) {
143 if ($mainlangonly) {
144 $valuetoshow = $value.' <span class="opacitymedium">('.preg_replace('/[_-].*$/', '', $key).')</span>';
145 } else {
146 $valuetoshow = $value.' <span class="opacitymedium">('.$key.')</span>';
147 }
148 }
149
150 $keytouse = $key;
151 if ($mainlangonly) {
152 $keytouse = preg_replace('/[_-].*$/', '', $key);
153 }
154
155 if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) {
156 continue;
157 }
158 if ($onlykeys && is_array($onlykeys) && !array_key_exists($keytouse, $onlykeys)) {
159 continue;
160 }
161
162 $valuetoshow = picto_from_langcode($key, 'class="saturatemedium"').' '.$valuetoshow;
163 if ((is_string($selected) && (string) $selected == (string) $keytouse) || (is_array($selected) && in_array($keytouse, $selected))) {
164 $out .= '<option value="'.$keytouse.'" selected data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
165 } else {
166 $out .= '<option value="'.$keytouse.'" data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
167 }
168 }
169 $out .= '</select>';
170
171 // Make select dynamic
172 if (!$forcecombo) {
173 include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
174 $out .= ajax_combobox($htmlname);
175 }
176
177 return $out;
178 }
179
180 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
190 public function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib = '')
191 {
192 // phpcs:enable
193 global $langs, $conf;
194
195 // Clean parameters
196
197
198 // Check parameters
199 if (!is_array($dirmenuarray)) {
200 return -1;
201 }
202
203 $menuarray = array();
204 foreach ($conf->file->dol_document_root as $dirroot) {
205 foreach ($dirmenuarray as $dirtoscan) {
206 $dir = $dirroot.$dirtoscan;
207 //print $dir.'<br>';
208 if (is_dir($dir)) {
209 $handle = opendir($dir);
210 if (is_resource($handle)) {
211 while (($file = readdir($handle)) !== false) {
212 if (is_file($dir."/".$file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS' && substr($file, 0, 5) != 'index') {
213 if (preg_match('/lib\.php$/i', $file)) {
214 continue; // We exclude library files
215 }
216 if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i', $file)) {
217 continue; // We exclude all menu manager files
218 }
219 if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i', $file)) {
220 continue; // We exclude all menu manager files
221 }
222 if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i', $file)) {
223 continue; // We exclude all menu manager files
224 }
225
226 $filelib = preg_replace('/\.php$/i', '', $file);
227 $prefix = '';
228 // 0=Recommended, 1=Experimental, 2=Development, 3=Other
229 if (preg_match('/^eldy/i', $file)) {
230 $prefix = '0';
231 } elseif (preg_match('/^smartphone/i', $file)) {
232 $prefix = '2';
233 } else {
234 $prefix = '3';
235 }
236
237 $morelabel = '';
238 if (preg_match('/^auguria/i', $file)) {
239 $morelabel .= ' <span class="opacitymedium">('.$langs->trans("Unstable").')</span>';
240 }
241 if ($file == $selected) {
242 $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" selected data-html="'.dol_escape_htmltag($filelib.$morelabel).'">'.$filelib.$morelabel;
243 $menuarray[$prefix.'_'.$file] .= '</option>';
244 } else {
245 $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" data-html="'.dol_escape_htmltag($filelib.$morelabel).'">'.$filelib.$morelabel;
246 $menuarray[$prefix.'_'.$file] .= '</option>';
247 }
248 }
249 }
250 closedir($handle);
251 }
252 }
253 }
254 }
255 ksort($menuarray);
256
257 // Output combo list of menus
258 print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
259 $oldprefix = '';
260 foreach ($menuarray as $key => $val) {
261 $tab = explode('_', $key);
262 $newprefix = $tab[0];
263
264 if ($newprefix == '1' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1)) {
265 continue;
266 }
267 if ($newprefix == '2' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2)) {
268 continue;
269 }
270 if ($newprefix != $oldprefix) { // Add separators
271 // Affiche titre
272 print '<option value="-2" disabled>';
273 if ($newprefix == '0') {
274 print '-- '.$langs->trans("VersionRecommanded").' --';
275 }
276 if ($newprefix == '1') {
277 print '-- '.$langs->trans("VersionExperimental").' --';
278 }
279 if ($newprefix == '2') {
280 print '-- '.$langs->trans("VersionDevelopment").' --';
281 }
282 if ($newprefix == '3') {
283 print '-- '.$langs->trans("Other").' --';
284 }
285 print '</option>';
286 $oldprefix = $newprefix;
287 }
288
289 print $val."\n"; // Show menu entry ($val contains the <option> tags
290 }
291 print '</select>';
292
293 print ajax_combobox($htmlname);
294
295 return;
296 }
297
298 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
307 public function select_menu_families($selected, $htmlname, $dirmenuarray)
308 {
309 // phpcs:enable
310 global $langs, $conf;
311
312 //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
313 $expdevmenu = array();
314
315 $menuarray = array();
316
317 foreach ($dirmenuarray as $dirmenu) {
318 foreach ($conf->file->dol_document_root as $dirroot) {
319 $dir = $dirroot.$dirmenu;
320 if (is_dir($dir)) {
321 $handle = opendir($dir);
322 if (is_resource($handle)) {
323 while (($file = readdir($handle)) !== false) {
324 if (is_file($dir."/".$file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS') {
325 $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file);
326 if (preg_match('/^index/i', $filelib)) {
327 continue;
328 }
329 if (preg_match('/^default/i', $filelib)) {
330 continue;
331 }
332 if (preg_match('/^empty/i', $filelib)) {
333 continue;
334 }
335 if (preg_match('/\.lib/i', $filelib)) {
336 continue;
337 }
338 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) {
339 continue;
340 }
341
342 $menuarray[$filelib] = 1;
343 }
344 $menuarray['all'] = 1;
345 }
346 closedir($handle);
347 }
348 }
349 }
350 }
351
352 ksort($menuarray);
353
354 // Show combo list of menu handlers
355 print '<select class="flat maxwidth150" id="'.$htmlname.'" name="'.$htmlname.'">';
356 foreach ($menuarray as $key => $val) {
357 $tab = explode('_', $key);
358 print '<option value="'.$key.'"';
359 if ($key == $selected) {
360 print ' selected';
361 }
362 print '>';
363 if ($key == 'all') {
364 print $langs->trans("AllMenus");
365 } else {
366 print $key;
367 }
368 print '</option>'."\n";
369 }
370 print '</select>';
371
372 print ajax_combobox($htmlname);
373 }
374
375
376 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
384 public function select_timezone($selected, $htmlname)
385 {
386 // phpcs:enable
387 print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
388 print '<option value="-1">&nbsp;</option>';
389
390 $arraytz = array(
391 "Pacific/Midway" => "GMT-11:00",
392 "Pacific/Fakaofo" => "GMT-10:00",
393 "America/Anchorage" => "GMT-09:00",
394 "America/Los_Angeles" => "GMT-08:00",
395 "America/Dawson_Creek" => "GMT-07:00",
396 "America/Chicago" => "GMT-06:00",
397 "America/Bogota" => "GMT-05:00",
398 "America/Anguilla" => "GMT-04:00",
399 "America/Araguaina" => "GMT-03:00",
400 "America/Noronha" => "GMT-02:00",
401 "Atlantic/Azores" => "GMT-01:00",
402 "Africa/Abidjan" => "GMT+00:00",
403 "Europe/Paris" => "GMT+01:00",
404 "Europe/Helsinki" => "GMT+02:00",
405 "Europe/Moscow" => "GMT+03:00",
406 "Asia/Dubai" => "GMT+04:00",
407 "Asia/Karachi" => "GMT+05:00",
408 "Indian/Chagos" => "GMT+06:00",
409 "Asia/Jakarta" => "GMT+07:00",
410 "Asia/Hong_Kong" => "GMT+08:00",
411 "Asia/Tokyo" => "GMT+09:00",
412 "Australia/Sydney" => "GMT+10:00",
413 "Pacific/Noumea" => "GMT+11:00",
414 "Pacific/Auckland" => "GMT+12:00",
415 "Pacific/Enderbury" => "GMT+13:00"
416 );
417 foreach ($arraytz as $lib => $gmt) {
418 print '<option value="'.$lib.'"';
419 if ($selected == $lib || $selected == $gmt) {
420 print ' selected';
421 }
422 print '>'.$gmt.'</option>'."\n";
423 }
424 print '</select>';
425 }
426
427
428
429 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
440 public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = '', $showempty = 0, $forcecombo = 0)
441 {
442 // phpcs:enable
443 global $langs;
444
445 $langs->load("dict");
446
447 $sql = "SELECT code, label, width, height, unit";
448 $sql .= " FROM ".$this->db->prefix()."c_paper_format";
449 $sql .= " WHERE active=1";
450 if ($filter) {
451 $sql .= " AND code LIKE '%".$this->db->escape($filter)."%'";
452 }
453
454 $paperformat = array();
455
456 $resql = $this->db->query($sql);
457 if ($resql) {
458 $num = $this->db->num_rows($resql);
459 $i = 0;
460 while ($i < $num) {
461 $obj = $this->db->fetch_object($resql);
462 $unitKey = $langs->trans('SizeUnit'.$obj->unit);
463
464 $paperformat[$obj->code] = $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey);
465
466 $i++;
467 }
468 } else {
469 dol_print_error($this->db);
470 return '';
471 }
472 $out = '';
473
474 $out .= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
475 if ($showempty) {
476 $out .= '<option value=""';
477 if ($selected == '') {
478 $out .= ' selected';
479 }
480 $out .= '>&nbsp;</option>';
481 }
482 foreach ($paperformat as $key => $value) {
483 if ($selected == $key) {
484 $out .= '<option value="'.$key.'" selected>'.$value.'</option>';
485 } else {
486 $out .= '<option value="'.$key.'">'.$value.'</option>';
487 }
488 }
489 $out .= '</select>';
490
491 if (!$forcecombo) {
492 include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
493 $out .= ajax_combobox($htmlname);
494 }
495
496 return $out;
497 }
498
499
508 public function selectTypeOfFields($htmlname, $type, $typewecanchangeinto = array())
509 {
510 global $type2label; // TODO Remove this
511
512 $out = '';
513
514 $out .= '<select class="flat type" id="'.$htmlname.'" name="'.$htmlname.'">';
515 foreach ($type2label as $key => $val) {
516 $selected = '';
517 if ($key == $type) {
518 $selected = ' selected="selected"';
519 }
520
521 // Set $valhtml with the picto for the type
522 $valhtml = ($key ? getPictoForType($key) : '').$val;
523
524 if (empty($typewecanchangeinto) || in_array($key, $typewecanchangeinto[$type])) {
525 $out .= '<option value="'.$key.'"'.$selected.' data-html="'.dol_escape_htmltag($valhtml).'">'.($val ? $val : '&nbsp;').'</option>';
526 } else {
527 $out .= '<option value="'.$key.'" disabled="disabled"'.$selected.' data-html="'.dol_escape_htmltag($valhtml).'">'.($val ? $val : '&nbsp;').'</option>';
528 }
529 }
530 $out .= '</select>';
531 $out .= ajax_combobox('type');
532
533 return $out;
534 }
535}
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:456
Class to generate html code for admin pages.
select_language($selected='', $htmlname='lang_id', $showauto=0, $filter=array(), $showempty='', $showwarning=0, $disabled=0, $morecss='', $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)
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 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...