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