dolibarr  20.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 
31 class FormAdmin
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 $conf, $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  if ($file == $selected) {
238  $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" selected>'.$filelib.'</option>';
239  } else {
240  $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'">'.$filelib.'</option>';
241  }
242  }
243  }
244  closedir($handle);
245  }
246  }
247  }
248  }
249  ksort($menuarray);
250 
251  // Output combo list of menus
252  print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
253  $oldprefix = '';
254  foreach ($menuarray as $key => $val) {
255  $tab = explode('_', $key);
256  $newprefix = $tab[0];
257  if ($newprefix == '1' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1)) {
258  continue;
259  }
260  if ($newprefix == '2' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2)) {
261  continue;
262  }
263  if ($newprefix != $oldprefix) { // Add separators
264  // Affiche titre
265  print '<option value="-1" disabled>';
266  if ($newprefix == '0') {
267  print '-- '.$langs->trans("VersionRecommanded").' --';
268  }
269  if ($newprefix == '1') {
270  print '-- '.$langs->trans("VersionExperimental").' --';
271  }
272  if ($newprefix == '2') {
273  print '-- '.$langs->trans("VersionDevelopment").' --';
274  }
275  if ($newprefix == '3') {
276  print '-- '.$langs->trans("Other").' --';
277  }
278  print '</option>';
279  $oldprefix = $newprefix;
280  }
281  print $val."\n"; // Show menu entry
282  }
283  print '</select>';
284 
285  return;
286  }
287 
288  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
297  public function select_menu_families($selected, $htmlname, $dirmenuarray)
298  {
299  // phpcs:enable
300  global $langs, $conf;
301 
302  //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
303  $expdevmenu = array();
304 
305  $menuarray = array();
306 
307  foreach ($dirmenuarray as $dirmenu) {
308  foreach ($conf->file->dol_document_root as $dirroot) {
309  $dir = $dirroot.$dirmenu;
310  if (is_dir($dir)) {
311  $handle = opendir($dir);
312  if (is_resource($handle)) {
313  while (($file = readdir($handle)) !== false) {
314  if (is_file($dir."/".$file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS') {
315  $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file);
316  if (preg_match('/^index/i', $filelib)) {
317  continue;
318  }
319  if (preg_match('/^default/i', $filelib)) {
320  continue;
321  }
322  if (preg_match('/^empty/i', $filelib)) {
323  continue;
324  }
325  if (preg_match('/\.lib/i', $filelib)) {
326  continue;
327  }
328  if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) {
329  continue;
330  }
331 
332  $menuarray[$filelib] = 1;
333  }
334  $menuarray['all'] = 1;
335  }
336  closedir($handle);
337  }
338  }
339  }
340  }
341 
342  ksort($menuarray);
343 
344  // Affichage liste deroulante des menus
345  print '<select class="flat maxwidth100" id="'.$htmlname.'" name="'.$htmlname.'">';
346  $oldprefix = '';
347  foreach ($menuarray as $key => $val) {
348  $tab = explode('_', $key);
349  $newprefix = $tab[0];
350  print '<option value="'.$key.'"';
351  if ($key == $selected) {
352  print ' selected';
353  }
354  print '>';
355  if ($key == 'all') {
356  print $langs->trans("AllMenus");
357  } else {
358  print $key;
359  }
360  print '</option>'."\n";
361  }
362  print '</select>';
363 
364  print ajax_combobox($htmlname);
365  }
366 
367 
368  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
376  public function select_timezone($selected, $htmlname)
377  {
378  // phpcs:enable
379  print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
380  print '<option value="-1">&nbsp;</option>';
381 
382  $arraytz = array(
383  "Pacific/Midway" => "GMT-11:00",
384  "Pacific/Fakaofo" => "GMT-10:00",
385  "America/Anchorage" => "GMT-09:00",
386  "America/Los_Angeles" => "GMT-08:00",
387  "America/Dawson_Creek" => "GMT-07:00",
388  "America/Chicago" => "GMT-06:00",
389  "America/Bogota" => "GMT-05:00",
390  "America/Anguilla" => "GMT-04:00",
391  "America/Araguaina" => "GMT-03:00",
392  "America/Noronha" => "GMT-02:00",
393  "Atlantic/Azores" => "GMT-01:00",
394  "Africa/Abidjan" => "GMT+00:00",
395  "Europe/Paris" => "GMT+01:00",
396  "Europe/Helsinki" => "GMT+02:00",
397  "Europe/Moscow" => "GMT+03:00",
398  "Asia/Dubai" => "GMT+04:00",
399  "Asia/Karachi" => "GMT+05:00",
400  "Indian/Chagos" => "GMT+06:00",
401  "Asia/Jakarta" => "GMT+07:00",
402  "Asia/Hong_Kong" => "GMT+08:00",
403  "Asia/Tokyo" => "GMT+09:00",
404  "Australia/Sydney" => "GMT+10:00",
405  "Pacific/Noumea" => "GMT+11:00",
406  "Pacific/Auckland" => "GMT+12:00",
407  "Pacific/Enderbury" => "GMT+13:00"
408  );
409  foreach ($arraytz as $lib => $gmt) {
410  print '<option value="'.$lib.'"';
411  if ($selected == $lib || $selected == $gmt) {
412  print ' selected';
413  }
414  print '>'.$gmt.'</option>'."\n";
415  }
416  print '</select>';
417  }
418 
419 
420 
421  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
432  public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = '', $showempty = 0, $forcecombo = 0)
433  {
434  // phpcs:enable
435  global $langs;
436 
437  $langs->load("dict");
438 
439  $sql = "SELECT code, label, width, height, unit";
440  $sql .= " FROM ".$this->db->prefix()."c_paper_format";
441  $sql .= " WHERE active=1";
442  if ($filter) {
443  $sql .= " AND code LIKE '%".$this->db->escape($filter)."%'";
444  }
445 
446  $paperformat = array();
447 
448  $resql = $this->db->query($sql);
449  if ($resql) {
450  $num = $this->db->num_rows($resql);
451  $i = 0;
452  while ($i < $num) {
453  $obj = $this->db->fetch_object($resql);
454  $unitKey = $langs->trans('SizeUnit'.$obj->unit);
455 
456  $paperformat[$obj->code] = $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey);
457 
458  $i++;
459  }
460  } else {
461  dol_print_error($this->db);
462  return '';
463  }
464  $out = '';
465 
466  $out .= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
467  if ($showempty) {
468  $out .= '<option value=""';
469  if ($selected == '') {
470  $out .= ' selected';
471  }
472  $out .= '>&nbsp;</option>';
473  }
474  foreach ($paperformat as $key => $value) {
475  if ($selected == $key) {
476  $out .= '<option value="'.$key.'" selected>'.$value.'</option>';
477  } else {
478  $out .= '<option value="'.$key.'">'.$value.'</option>';
479  }
480  }
481  $out .= '</select>';
482 
483  if (!$forcecombo) {
484  include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
485  $out .= ajax_combobox($htmlname);
486  }
487 
488  return $out;
489  }
490 
491 
500  public function selectTypeOfFields($htmlname, $type, $typewecanchangeinto = array())
501  {
502  global $type2label; // TODO Remove this
503 
504  $out = '';
505 
506  $out .= '<select class="flat type" id="'.$htmlname.'" name="'.$htmlname.'">';
507  foreach ($type2label as $key => $val) {
508  $selected = '';
509  if ($key == $type) {
510  $selected = ' selected="selected"';
511  }
512 
513  // Set $valhtml with the picto for the type
514  $valhtml = ($key ? getPictoForType($key) : '').$val;
515 
516  if (empty($typewecanchangeinto) || in_array($key, $typewecanchangeinto[$type])) {
517  $out .= '<option value="'.$key.'"'.$selected.' data-html="'.dol_escape_htmltag($valhtml).'">'.($val ? $val : '&nbsp;').'</option>';
518  } else {
519  $out .= '<option value="'.$key.'" disabled="disabled"'.$selected.' data-html="'.dol_escape_htmltag($valhtml).'">'.($val ? $val : '&nbsp;').'</option>';
520  }
521  }
522  $out .= '</select>';
523  $out .= ajax_combobox('type');
524 
525  return $out;
526  }
527 }
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:463
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)
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getPictoForType($key)
Return the picto for a data type.
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...