dolibarr 21.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 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 = '', $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 pre-select (to avoid to have first element in list pre-selected).
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 $out .= '>'.$langs->trans("AutoDetectLang").'</option>';
130 }
131
132 asort($langs_available); // array('XX' => 'Language (Country)', ...)
133
134 foreach ($langs_available as $key => $value) {
135 $valuetoshow = $value;
136 if ($showcode == 1) {
137 if ($mainlangonly) {
138 $valuetoshow = '<span class="opacitymedium">'.preg_replace('/[_-].*$/', '', $key).'</span> - '.$value;
139 } else {
140 $valuetoshow = '<span class="opacitymedium">'.$key.'</span> - '.$value;
141 }
142 }
143 if ($showcode == 2) {
144 if ($mainlangonly) {
145 $valuetoshow = $value.' <span class="opacitymedium">('.preg_replace('/[_-].*$/', '', $key).')</span>';
146 } else {
147 $valuetoshow = $value.' <span class="opacitymedium">('.$key.')</span>';
148 }
149 }
150
151 $keytouse = $key;
152 if ($mainlangonly) {
153 $keytouse = preg_replace('/[_-].*$/', '', $key);
154 }
155
156 if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) {
157 continue;
158 }
159 if ($onlykeys && is_array($onlykeys) && !array_key_exists($keytouse, $onlykeys)) {
160 continue;
161 }
162
163 $valuetoshow = picto_from_langcode($key, 'class="saturatemedium"').' '.$valuetoshow;
164 if ((is_string($selected) && (string) $selected == (string) $keytouse) || (is_array($selected) && in_array($keytouse, $selected))) {
165 $out .= '<option value="'.$keytouse.'" selected data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
166 } else {
167 $out .= '<option value="'.$keytouse.'" data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
168 }
169 }
170 $out .= '</select>';
171
172 // Make select dynamic
173 if (!$forcecombo) {
174 include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
175 $out .= ajax_combobox($htmlname);
176 }
177
178 return $out;
179 }
180
181 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
191 public function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib = '')
192 {
193 // phpcs:enable
194 global $langs, $conf;
195
196 // Clean parameters
197
198
199 // Check parameters
200 if (!is_array($dirmenuarray)) {
201 return -1;
202 }
203
204 $menuarray = array();
205 foreach ($conf->file->dol_document_root as $dirroot) {
206 foreach ($dirmenuarray as $dirtoscan) {
207 $dir = $dirroot.$dirtoscan;
208 //print $dir.'<br>';
209 if (is_dir($dir)) {
210 $handle = opendir($dir);
211 if (is_resource($handle)) {
212 while (($file = readdir($handle)) !== false) {
213 if (is_file($dir."/".$file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS' && substr($file, 0, 5) != 'index') {
214 if (preg_match('/lib\.php$/i', $file)) {
215 continue; // We exclude library files
216 }
217 if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i', $file)) {
218 continue; // We exclude all menu manager files
219 }
220 if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i', $file)) {
221 continue; // We exclude all menu manager files
222 }
223 if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i', $file)) {
224 continue; // We exclude all menu manager files
225 }
226
227 $filetoshow = preg_replace('/\.php$/i', '', $file);
228 $filetoshow = ucfirst(preg_replace('/_menu$/i', '', $filetoshow));
229 $prefix = '';
230 // 0=Recommended, 1=Experimental, 2=Development, 3=Other
231 if (preg_match('/^eldy/i', $file)) {
232 $prefix = '0';
233 } elseif (preg_match('/^smartphone/i', $file)) {
234 $prefix = '2';
235 } else {
236 $prefix = '3';
237 }
238
239 $morelabel = '';
240 if (preg_match('/^auguria/i', $file)) {
241 $morelabel .= ' <span class="opacitymedium">('.$langs->trans("Unstable").')</span>';
242 }
243 if ($file == $selected) {
244 $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" selected data-html="'.dol_escape_htmltag($filetoshow.$morelabel).'">';
245 $menuarray[$prefix.'_'.$file] .= $filetoshow.$morelabel;
246 $menuarray[$prefix.'_'.$file] .= '</option>';
247 } else {
248 $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" data-html="'.dol_escape_htmltag($filetoshow.$morelabel).'">';
249 $menuarray[$prefix.'_'.$file] .= $filetoshow.$morelabel;
250 $menuarray[$prefix.'_'.$file] .= '</option>';
251 }
252 }
253 }
254 closedir($handle);
255 }
256 }
257 }
258 }
259 ksort($menuarray);
260
261 // Output combo list of menus
262 print '<select class="flat minwidth150" id="'.$htmlname.'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
263 $oldprefix = '';
264 foreach ($menuarray as $key => $val) {
265 $tab = explode('_', $key);
266 $newprefix = $tab[0];
267
268 if ($newprefix == '1' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1)) {
269 continue;
270 }
271 if ($newprefix == '2' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2)) {
272 continue;
273 }
274 if ($newprefix != $oldprefix) { // Add separators
275 // Affiche titre
276 print '<option value="-2" disabled>';
277 if ($newprefix == '0') {
278 print '-- '.$langs->trans("VersionRecommanded").' --';
279 }
280 if ($newprefix == '1') {
281 print '-- '.$langs->trans("VersionExperimental").' --';
282 }
283 if ($newprefix == '2') {
284 print '-- '.$langs->trans("VersionDevelopment").' --';
285 }
286 if ($newprefix == '3') {
287 print '-- '.$langs->trans("Other").' --';
288 }
289 print '</option>';
290 $oldprefix = $newprefix;
291 }
292
293 print $val."\n"; // Show menu entry ($val contains the <option> tags)
294 }
295 print '</select>';
296
297 print ajax_combobox($htmlname);
298
299 return;
300 }
301
302 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
311 public function select_menu_families($selected, $htmlname, $dirmenuarray)
312 {
313 // phpcs:enable
314 global $langs, $conf;
315
316 //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
317 $expdevmenu = array();
318
319 $menuarray = array();
320
321 foreach ($dirmenuarray as $dirmenu) {
322 foreach ($conf->file->dol_document_root as $dirroot) {
323 $dir = $dirroot.$dirmenu;
324 if (is_dir($dir)) {
325 $handle = opendir($dir);
326 if (is_resource($handle)) {
327 while (($file = readdir($handle)) !== false) {
328 if (is_file($dir."/".$file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS') {
329 $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file);
330 if (preg_match('/^index/i', $filelib)) {
331 continue;
332 }
333 if (preg_match('/^default/i', $filelib)) {
334 continue;
335 }
336 if (preg_match('/^empty/i', $filelib)) {
337 continue;
338 }
339 if (preg_match('/\.lib/i', $filelib)) {
340 continue;
341 }
342 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) {
343 continue;
344 }
345
346 $menuarray[$filelib] = 1;
347 }
348 $menuarray['all'] = 1;
349 }
350 closedir($handle);
351 }
352 }
353 }
354 }
355
356 ksort($menuarray);
357
358 // Show combo list of menu handlers
359 print '<select class="flat maxwidth150" id="'.$htmlname.'" name="'.$htmlname.'">';
360 foreach ($menuarray as $key => $val) {
361 $tab = explode('_', $key);
362 print '<option value="'.$key.'"';
363 if ($key == $selected) {
364 print ' selected';
365 }
366 print '>';
367 if ($key == 'all') {
368 print $langs->trans("AllMenus");
369 } else {
370 print $key;
371 }
372 print '</option>'."\n";
373 }
374 print '</select>';
375
376 print ajax_combobox($htmlname);
377 }
378
379
380 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
388 public function select_timezone($selected, $htmlname)
389 {
390 // phpcs:enable
391 print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
392 print '<option value="-1">&nbsp;</option>';
393
394 $arraytz = array(
395 "Pacific/Midway" => "GMT-11:00",
396 "Pacific/Fakaofo" => "GMT-10:00",
397 "America/Anchorage" => "GMT-09:00",
398 "America/Los_Angeles" => "GMT-08:00",
399 "America/Dawson_Creek" => "GMT-07:00",
400 "America/Chicago" => "GMT-06:00",
401 "America/Bogota" => "GMT-05:00",
402 "America/Anguilla" => "GMT-04:00",
403 "America/Araguaina" => "GMT-03:00",
404 "America/Noronha" => "GMT-02:00",
405 "Atlantic/Azores" => "GMT-01:00",
406 "Africa/Abidjan" => "GMT+00:00",
407 "Europe/Paris" => "GMT+01:00",
408 "Europe/Helsinki" => "GMT+02:00",
409 "Europe/Moscow" => "GMT+03:00",
410 "Asia/Dubai" => "GMT+04:00",
411 "Asia/Karachi" => "GMT+05:00",
412 "Indian/Chagos" => "GMT+06:00",
413 "Asia/Jakarta" => "GMT+07:00",
414 "Asia/Hong_Kong" => "GMT+08:00",
415 "Asia/Tokyo" => "GMT+09:00",
416 "Australia/Sydney" => "GMT+10:00",
417 "Pacific/Noumea" => "GMT+11:00",
418 "Pacific/Auckland" => "GMT+12:00",
419 "Pacific/Enderbury" => "GMT+13:00"
420 );
421 foreach ($arraytz as $lib => $gmt) {
422 print '<option value="'.$lib.'"';
423 if ($selected == $lib || $selected == $gmt) {
424 print ' selected';
425 }
426 print '>'.$gmt.'</option>'."\n";
427 }
428 print '</select>';
429 }
430
431
432
433 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
444 public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = '', $showempty = 0, $forcecombo = 0)
445 {
446 // phpcs:enable
447 global $langs;
448
449 $langs->load("dict");
450
451 $sql = "SELECT code, label, width, height, unit";
452 $sql .= " FROM ".$this->db->prefix()."c_paper_format";
453 $sql .= " WHERE active=1";
454 if ($filter) {
455 $sql .= " AND code LIKE '%".$this->db->escape($filter)."%'";
456 }
457
458 $paperformat = array();
459
460 $resql = $this->db->query($sql);
461 if ($resql) {
462 $num = $this->db->num_rows($resql);
463 $i = 0;
464 while ($i < $num) {
465 $obj = $this->db->fetch_object($resql);
466 $unitKey = $langs->trans('SizeUnit'.$obj->unit);
467
468 $paperformat[$obj->code] = $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey);
469
470 $i++;
471 }
472 } else {
473 dol_print_error($this->db);
474 return '';
475 }
476 $out = '';
477
478 $out .= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
479 if ($showempty) {
480 $out .= '<option value=""';
481 if ($selected == '') {
482 $out .= ' selected';
483 }
484 $out .= '>&nbsp;</option>';
485 }
486 foreach ($paperformat as $key => $value) {
487 if ($selected == $key) {
488 $out .= '<option value="'.$key.'" selected>'.$value.'</option>';
489 } else {
490 $out .= '<option value="'.$key.'">'.$value.'</option>';
491 }
492 }
493 $out .= '</select>';
494
495 if (!$forcecombo) {
496 include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
497 $out .= ajax_combobox($htmlname);
498 }
499
500 return $out;
501 }
502
503
512 public function selectTypeOfFields($htmlname, $type, $typewecanchangeinto = array())
513 {
514 global $type2label; // TODO Remove this global
515
516 $out = '';
517
518 $out .= '<select class="flat type" id="'.$htmlname.'" name="'.$htmlname.'">';
519 foreach ($type2label as $key => $val) {
520 $selected = '';
521 if ($key == $type) {
522 $selected = ' selected="selected"';
523 }
524
525 // Set $valhtml with the picto for the type
526 $valhtml = ($key ? getPictoForType($key) : '').$val;
527
528 if (empty($typewecanchangeinto) || in_array($key, $typewecanchangeinto[$type])) {
529 $out .= '<option value="'.$key.'"'.$selected.' data-html="'.dol_escape_htmltag($valhtml).'">'.($val ? $val : '&nbsp;').'</option>';
530 } else {
531 $out .= '<option value="'.$key.'" disabled="disabled"'.$selected.' data-html="'.dol_escape_htmltag($valhtml).'">'.($val ? $val : '&nbsp;').'</option>';
532 }
533 }
534 $out .= '</select>';
535 $out .= ajax_combobox('type');
536
537 return $out;
538 }
539}
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:459
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 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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79