dolibarr  16.0.5
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 
30 class FormAdmin
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 = null, $showempty = '', $showwarning = 0, $disabled = 0, $morecss = '', $showcode = 0, $forcecombo = 0, $multiselect = 0, $onlykeys = null, $mainlangonly = 0)
66  {
67  // phpcs:enable
68  global $conf, $langs;
69 
70  if (!empty($conf->global->MAIN_DEFAULT_LANGUAGE_FILTER)) {
71  $filter[$conf->global->MAIN_DEFAULT_LANGUAGE_FILTER] = 1;
72  }
73 
74  $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12, 0, $mainlangonly);
75 
76  // If empty value is not allowed and the language to select is not inside the list of available language and we must find
77  // an alternative of the language code to pre-select (to avoid to have first element in list pre-selected).
78  if ($selected && empty($showempty)) {
79  if (!is_array($selected) && !array_key_exists($selected, $langs_available)) {
80  $tmparray = explode('_', $selected);
81  if (!empty($tmparray[1])) {
82  $selected = getLanguageCodeFromCountryCode($tmparray[1]);
83  }
84  if (empty($selected)) {
85  $selected = $langs->defaultlang;
86  }
87  } else {
88  // If the preselected value is an array, we do not try to find alternative to preselect
89  }
90  }
91 
92  $out = '';
93 
94  $out .= '<select '.($multiselect ? 'multiple="multiple" ' : '').'class="flat'.($morecss ? ' '.$morecss : '').'" id="'.$htmlname.'" name="'.$htmlname.($multiselect ? '[]' : '').'"'.($disabled ? ' disabled' : '').'>';
95  if ($showempty && !$multiselect) {
96  $out .= '<option value="0"';
97  if ($selected === '') {
98  $out .= ' selected';
99  }
100  $out .= '>';
101  if ($showempty != '1') {
102  $out .= $showempty;
103  } else {
104  $out .= '&nbsp;';
105  }
106  $out .= '</option>';
107  }
108  if ($showauto) {
109  $out .= '<option value="auto"';
110  if ($selected === 'auto') {
111  $out .= ' selected';
112  }
113  $out .= '>'.$langs->trans("AutoDetectLang").'</option>';
114  }
115 
116  asort($langs_available); // array('XX' => 'Language (Country)', ...)
117 
118  foreach ($langs_available as $key => $value) {
119  $valuetoshow = $value;
120  if ($showcode == 1) {
121  if ($mainlangonly) {
122  $valuetoshow = '<span class="opacitymedium">'.preg_replace('/[_-].*$/', '', $key).'</span> - '.$value;
123  } else {
124  $valuetoshow = '<span class="opacitymedium">'.$key.'</span> - '.$value;
125  }
126  }
127  if ($showcode == 2) {
128  if ($mainlangonly) {
129  $valuetoshow = $value.' <span class="opacitymedium">('.preg_replace('/[_-].*$/', '', $key).')</span>';
130  } else {
131  $valuetoshow = $value.' <span class="opacitymedium">('.$key.')</span>';
132  }
133  }
134 
135  $keytouse = $key;
136  if ($mainlangonly) {
137  $keytouse = preg_replace('/[_-].*$/', '', $key);
138  }
139 
140  if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) {
141  continue;
142  }
143  if ($onlykeys && is_array($onlykeys) && !array_key_exists($keytouse, $onlykeys)) {
144  continue;
145  }
146 
147  $valuetoshow = picto_from_langcode($key, 'class="saturatemedium"').' '.$valuetoshow;
148  if ((is_string($selected) && (string) $selected == (string) $keytouse) || (is_array($selected) && in_array($keytouse, $selected))) {
149  $out .= '<option value="'.$keytouse.'" selected data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
150  } else {
151  $out .= '<option value="'.$keytouse.'" data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
152  }
153  }
154  $out .= '</select>';
155 
156  // Make select dynamic
157  if (!$forcecombo) {
158  include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
159  $out .= ajax_combobox($htmlname);
160  }
161 
162  return $out;
163  }
164 
165  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
175  public function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib = '')
176  {
177  // phpcs:enable
178  global $langs, $conf;
179 
180  // Clean parameters
181 
182 
183  // Check parameters
184  if (!is_array($dirmenuarray)) {
185  return -1;
186  }
187 
188  $menuarray = array();
189  foreach ($conf->file->dol_document_root as $dirroot) {
190  foreach ($dirmenuarray as $dirtoscan) {
191  $dir = $dirroot.$dirtoscan;
192  //print $dir.'<br>';
193  if (is_dir($dir)) {
194  $handle = opendir($dir);
195  if (is_resource($handle)) {
196  while (($file = readdir($handle)) !== false) {
197  if (is_file($dir."/".$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && substr($file, 0, 5) != 'index') {
198  if (preg_match('/lib\.php$/i', $file)) {
199  continue; // We exclude library files
200  }
201  if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i', $file)) {
202  continue; // We exclude all menu manager files
203  }
204  if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i', $file)) {
205  continue; // We exclude all menu manager files
206  }
207  if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i', $file)) {
208  continue; // We exclude all menu manager files
209  }
210 
211  $filelib = preg_replace('/\.php$/i', '', $file);
212  $prefix = '';
213  // 0=Recommanded, 1=Experimental, 2=Developpement, 3=Other
214  if (preg_match('/^eldy/i', $file)) {
215  $prefix = '0';
216  } elseif (preg_match('/^smartphone/i', $file)) {
217  $prefix = '2';
218  } else {
219  $prefix = '3';
220  }
221 
222  if ($file == $selected) {
223  $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" selected>'.$filelib.'</option>';
224  } else {
225  $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'">'.$filelib.'</option>';
226  }
227  }
228  }
229  closedir($handle);
230  }
231  }
232  }
233  }
234  ksort($menuarray);
235 
236  // Output combo list of menus
237  print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
238  $oldprefix = '';
239  foreach ($menuarray as $key => $val) {
240  $tab = explode('_', $key);
241  $newprefix = $tab[0];
242  if ($newprefix == '1' && ($conf->global->MAIN_FEATURES_LEVEL < 1)) {
243  continue;
244  }
245  if ($newprefix == '2' && ($conf->global->MAIN_FEATURES_LEVEL < 2)) {
246  continue;
247  }
248  if ($newprefix != $oldprefix) { // Add separators
249  // Affiche titre
250  print '<option value="-1" disabled>';
251  if ($newprefix == '0') {
252  print '-- '.$langs->trans("VersionRecommanded").' --';
253  }
254  if ($newprefix == '1') {
255  print '-- '.$langs->trans("VersionExperimental").' --';
256  }
257  if ($newprefix == '2') {
258  print '-- '.$langs->trans("VersionDevelopment").' --';
259  }
260  if ($newprefix == '3') {
261  print '-- '.$langs->trans("Other").' --';
262  }
263  print '</option>';
264  $oldprefix = $newprefix;
265  }
266  print $val."\n"; // Show menu entry
267  }
268  print '</select>';
269  }
270 
271  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
280  public function select_menu_families($selected, $htmlname, $dirmenuarray)
281  {
282  // phpcs:enable
283  global $langs, $conf;
284 
285  //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
286  $expdevmenu = array();
287 
288  $menuarray = array();
289 
290  foreach ($dirmenuarray as $dirmenu) {
291  foreach ($conf->file->dol_document_root as $dirroot) {
292  $dir = $dirroot.$dirmenu;
293  if (is_dir($dir)) {
294  $handle = opendir($dir);
295  if (is_resource($handle)) {
296  while (($file = readdir($handle)) !== false) {
297  if (is_file($dir."/".$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS') {
298  $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file);
299  if (preg_match('/^index/i', $filelib)) {
300  continue;
301  }
302  if (preg_match('/^default/i', $filelib)) {
303  continue;
304  }
305  if (preg_match('/^empty/i', $filelib)) {
306  continue;
307  }
308  if (preg_match('/\.lib/i', $filelib)) {
309  continue;
310  }
311  if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) {
312  continue;
313  }
314 
315  $menuarray[$filelib] = 1;
316  }
317  $menuarray['all'] = 1;
318  }
319  closedir($handle);
320  }
321  }
322  }
323  }
324 
325  ksort($menuarray);
326 
327  // Affichage liste deroulante des menus
328  print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
329  $oldprefix = '';
330  foreach ($menuarray as $key => $val) {
331  $tab = explode('_', $key);
332  $newprefix = $tab[0];
333  print '<option value="'.$key.'"';
334  if ($key == $selected) {
335  print ' selected';
336  }
337  print '>';
338  if ($key == 'all') {
339  print $langs->trans("AllMenus");
340  } else {
341  print $key;
342  }
343  print '</option>'."\n";
344  }
345  print '</select>';
346  }
347 
348 
349  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
357  public function select_timezone($selected, $htmlname)
358  {
359  // phpcs:enable
360  global $langs, $conf;
361 
362  print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
363  print '<option value="-1">&nbsp;</option>';
364 
365  $arraytz = array(
366  "Pacific/Midway"=>"GMT-11:00",
367  "Pacific/Fakaofo"=>"GMT-10:00",
368  "America/Anchorage"=>"GMT-09:00",
369  "America/Los_Angeles"=>"GMT-08:00",
370  "America/Dawson_Creek"=>"GMT-07:00",
371  "America/Chicago"=>"GMT-06:00",
372  "America/Bogota"=>"GMT-05:00",
373  "America/Anguilla"=>"GMT-04:00",
374  "America/Araguaina"=>"GMT-03:00",
375  "America/Noronha"=>"GMT-02:00",
376  "Atlantic/Azores"=>"GMT-01:00",
377  "Africa/Abidjan"=>"GMT+00:00",
378  "Europe/Paris"=>"GMT+01:00",
379  "Europe/Helsinki"=>"GMT+02:00",
380  "Europe/Moscow"=>"GMT+03:00",
381  "Asia/Dubai"=>"GMT+04:00",
382  "Asia/Karachi"=>"GMT+05:00",
383  "Indian/Chagos"=>"GMT+06:00",
384  "Asia/Jakarta"=>"GMT+07:00",
385  "Asia/Hong_Kong"=>"GMT+08:00",
386  "Asia/Tokyo"=>"GMT+09:00",
387  "Australia/Sydney"=>"GMT+10:00",
388  "Pacific/Noumea"=>"GMT+11:00",
389  "Pacific/Auckland"=>"GMT+12:00",
390  "Pacific/Enderbury"=>"GMT+13:00"
391  );
392  foreach ($arraytz as $lib => $gmt) {
393  print '<option value="'.$lib.'"';
394  if ($selected == $lib || $selected == $gmt) {
395  print ' selected';
396  }
397  print '>'.$gmt.'</option>'."\n";
398  }
399  print '</select>';
400  }
401 
402 
403 
404  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
415  public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = 0, $showempty = 0, $forcecombo = 0)
416  {
417  // phpcs:enable
418  global $langs;
419 
420  $langs->load("dict");
421 
422  $sql = "SELECT code, label, width, height, unit";
423  $sql .= " FROM ".$this->db->prefix()."c_paper_format";
424  $sql .= " WHERE active=1";
425  if ($filter) {
426  $sql .= " AND code LIKE '%".$this->db->escape($filter)."%'";
427  }
428 
429  $resql = $this->db->query($sql);
430  if ($resql) {
431  $num = $this->db->num_rows($resql);
432  $i = 0;
433  while ($i < $num) {
434  $obj = $this->db->fetch_object($resql);
435  $unitKey = $langs->trans('SizeUnit'.$obj->unit);
436 
437  $paperformat[$obj->code] = $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey);
438 
439  $i++;
440  }
441  } else {
442  dol_print_error($this->db);
443  return '';
444  }
445  $out = '';
446 
447  $out .= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
448  if ($showempty) {
449  $out .= '<option value=""';
450  if ($selected == '') {
451  $out .= ' selected';
452  }
453  $out .= '>&nbsp;</option>';
454  }
455  foreach ($paperformat as $key => $value) {
456  if ($selected == $key) {
457  $out .= '<option value="'.$key.'" selected>'.$value.'</option>';
458  } else {
459  $out .= '<option value="'.$key.'">'.$value.'</option>';
460  }
461  }
462  $out .= '</select>';
463 
464  if (!$forcecombo) {
465  include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
466  $out .= ajax_combobox($htmlname);
467  }
468 
469  return $out;
470  }
471 }
ajax_combobox
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:438
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
picto_from_langcode
picto_from_langcode($codelang, $moreatt='', $notitlealt=0)
Return img flag of country for a language code or country code.
Definition: functions.lib.php:8742
getLanguageCodeFromCountryCode
getLanguageCodeFromCountryCode($countrycode)
Return default language from country code.
Definition: functions.lib.php:8785
FormAdmin
Class to generate html code for admin pages.
Definition: html.formadmin.class.php:30
FormAdmin\select_timezone
select_timezone($selected, $htmlname)
Return a HTML select list of timezones.
Definition: html.formadmin.class.php:357
FormAdmin\select_menu
select_menu($selected, $htmlname, $dirmenuarray, $moreattrib='')
Return list of available menus (eldy_backoffice, ...)
Definition: html.formadmin.class.php:175
FormAdmin\select_paper_format
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)
Definition: html.formadmin.class.php:415
FormAdmin\select_menu_families
select_menu_families($selected, $htmlname, $dirmenuarray)
Return combo list of available menu families.
Definition: html.formadmin.class.php:280
FormAdmin\select_language
select_language($selected='', $htmlname='lang_id', $showauto=0, $filter=null, $showempty='', $showwarning=0, $disabled=0, $morecss='', $showcode=0, $forcecombo=0, $multiselect=0, $onlykeys=null, $mainlangonly=0)
Return html select list with available languages (key='en_US', value='United States' for example)
Definition: html.formadmin.class.php:65
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
FormAdmin\__construct
__construct($db)
Constructor.
Definition: html.formadmin.class.php:41
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93