dolibarr  19.0.0-dev
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  if (is_numeric($showempty)) {
97  $out .= '<option value="0"';
98  } else {
99  $out .= '<option value="-1"';
100  }
101  if ($selected === '') {
102  $out .= ' selected';
103  }
104  $out .= '>';
105  if ($showempty != '1') {
106  $out .= $showempty;
107  } else {
108  $out .= '&nbsp;';
109  }
110  $out .= '</option>';
111  }
112  if ($showauto) {
113  $out .= '<option value="auto"';
114  if ($selected === 'auto') {
115  $out .= ' selected';
116  }
117  $out .= '>'.$langs->trans("AutoDetectLang").'</option>';
118  }
119 
120  asort($langs_available); // array('XX' => 'Language (Country)', ...)
121 
122  foreach ($langs_available as $key => $value) {
123  $valuetoshow = $value;
124  if ($showcode == 1) {
125  if ($mainlangonly) {
126  $valuetoshow = '<span class="opacitymedium">'.preg_replace('/[_-].*$/', '', $key).'</span> - '.$value;
127  } else {
128  $valuetoshow = '<span class="opacitymedium">'.$key.'</span> - '.$value;
129  }
130  }
131  if ($showcode == 2) {
132  if ($mainlangonly) {
133  $valuetoshow = $value.' <span class="opacitymedium">('.preg_replace('/[_-].*$/', '', $key).')</span>';
134  } else {
135  $valuetoshow = $value.' <span class="opacitymedium">('.$key.')</span>';
136  }
137  }
138 
139  $keytouse = $key;
140  if ($mainlangonly) {
141  $keytouse = preg_replace('/[_-].*$/', '', $key);
142  }
143 
144  if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) {
145  continue;
146  }
147  if ($onlykeys && is_array($onlykeys) && !array_key_exists($keytouse, $onlykeys)) {
148  continue;
149  }
150 
151  $valuetoshow = picto_from_langcode($key, 'class="saturatemedium"').' '.$valuetoshow;
152  if ((is_string($selected) && (string) $selected == (string) $keytouse) || (is_array($selected) && in_array($keytouse, $selected))) {
153  $out .= '<option value="'.$keytouse.'" selected data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
154  } else {
155  $out .= '<option value="'.$keytouse.'" data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
156  }
157  }
158  $out .= '</select>';
159 
160  // Make select dynamic
161  if (!$forcecombo) {
162  include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
163  $out .= ajax_combobox($htmlname);
164  }
165 
166  return $out;
167  }
168 
169  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
179  public function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib = '')
180  {
181  // phpcs:enable
182  global $langs, $conf;
183 
184  // Clean parameters
185 
186 
187  // Check parameters
188  if (!is_array($dirmenuarray)) {
189  return -1;
190  }
191 
192  $menuarray = array();
193  foreach ($conf->file->dol_document_root as $dirroot) {
194  foreach ($dirmenuarray as $dirtoscan) {
195  $dir = $dirroot.$dirtoscan;
196  //print $dir.'<br>';
197  if (is_dir($dir)) {
198  $handle = opendir($dir);
199  if (is_resource($handle)) {
200  while (($file = readdir($handle)) !== false) {
201  if (is_file($dir."/".$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && substr($file, 0, 5) != 'index') {
202  if (preg_match('/lib\.php$/i', $file)) {
203  continue; // We exclude library files
204  }
205  if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i', $file)) {
206  continue; // We exclude all menu manager files
207  }
208  if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i', $file)) {
209  continue; // We exclude all menu manager files
210  }
211  if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i', $file)) {
212  continue; // We exclude all menu manager files
213  }
214 
215  $filelib = preg_replace('/\.php$/i', '', $file);
216  $prefix = '';
217  // 0=Recommanded, 1=Experimental, 2=Developpement, 3=Other
218  if (preg_match('/^eldy/i', $file)) {
219  $prefix = '0';
220  } elseif (preg_match('/^smartphone/i', $file)) {
221  $prefix = '2';
222  } else {
223  $prefix = '3';
224  }
225 
226  if ($file == $selected) {
227  $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" selected>'.$filelib.'</option>';
228  } else {
229  $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'">'.$filelib.'</option>';
230  }
231  }
232  }
233  closedir($handle);
234  }
235  }
236  }
237  }
238  ksort($menuarray);
239 
240  // Output combo list of menus
241  print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
242  $oldprefix = '';
243  foreach ($menuarray as $key => $val) {
244  $tab = explode('_', $key);
245  $newprefix = $tab[0];
246  if ($newprefix == '1' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1)) {
247  continue;
248  }
249  if ($newprefix == '2' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2)) {
250  continue;
251  }
252  if ($newprefix != $oldprefix) { // Add separators
253  // Affiche titre
254  print '<option value="-1" disabled>';
255  if ($newprefix == '0') {
256  print '-- '.$langs->trans("VersionRecommanded").' --';
257  }
258  if ($newprefix == '1') {
259  print '-- '.$langs->trans("VersionExperimental").' --';
260  }
261  if ($newprefix == '2') {
262  print '-- '.$langs->trans("VersionDevelopment").' --';
263  }
264  if ($newprefix == '3') {
265  print '-- '.$langs->trans("Other").' --';
266  }
267  print '</option>';
268  $oldprefix = $newprefix;
269  }
270  print $val."\n"; // Show menu entry
271  }
272  print '</select>';
273  }
274 
275  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
284  public function select_menu_families($selected, $htmlname, $dirmenuarray)
285  {
286  // phpcs:enable
287  global $langs, $conf;
288 
289  //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
290  $expdevmenu = array();
291 
292  $menuarray = array();
293 
294  foreach ($dirmenuarray as $dirmenu) {
295  foreach ($conf->file->dol_document_root as $dirroot) {
296  $dir = $dirroot.$dirmenu;
297  if (is_dir($dir)) {
298  $handle = opendir($dir);
299  if (is_resource($handle)) {
300  while (($file = readdir($handle)) !== false) {
301  if (is_file($dir."/".$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS') {
302  $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file);
303  if (preg_match('/^index/i', $filelib)) {
304  continue;
305  }
306  if (preg_match('/^default/i', $filelib)) {
307  continue;
308  }
309  if (preg_match('/^empty/i', $filelib)) {
310  continue;
311  }
312  if (preg_match('/\.lib/i', $filelib)) {
313  continue;
314  }
315  if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) {
316  continue;
317  }
318 
319  $menuarray[$filelib] = 1;
320  }
321  $menuarray['all'] = 1;
322  }
323  closedir($handle);
324  }
325  }
326  }
327  }
328 
329  ksort($menuarray);
330 
331  // Affichage liste deroulante des menus
332  print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
333  $oldprefix = '';
334  foreach ($menuarray as $key => $val) {
335  $tab = explode('_', $key);
336  $newprefix = $tab[0];
337  print '<option value="'.$key.'"';
338  if ($key == $selected) {
339  print ' selected';
340  }
341  print '>';
342  if ($key == 'all') {
343  print $langs->trans("AllMenus");
344  } else {
345  print $key;
346  }
347  print '</option>'."\n";
348  }
349  print '</select>';
350 
351  print ajax_combobox($htmlname);
352  }
353 
354 
355  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
363  public function select_timezone($selected, $htmlname)
364  {
365  // phpcs:enable
366  global $langs, $conf;
367 
368  print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
369  print '<option value="-1">&nbsp;</option>';
370 
371  $arraytz = array(
372  "Pacific/Midway"=>"GMT-11:00",
373  "Pacific/Fakaofo"=>"GMT-10:00",
374  "America/Anchorage"=>"GMT-09:00",
375  "America/Los_Angeles"=>"GMT-08:00",
376  "America/Dawson_Creek"=>"GMT-07:00",
377  "America/Chicago"=>"GMT-06:00",
378  "America/Bogota"=>"GMT-05:00",
379  "America/Anguilla"=>"GMT-04:00",
380  "America/Araguaina"=>"GMT-03:00",
381  "America/Noronha"=>"GMT-02:00",
382  "Atlantic/Azores"=>"GMT-01:00",
383  "Africa/Abidjan"=>"GMT+00:00",
384  "Europe/Paris"=>"GMT+01:00",
385  "Europe/Helsinki"=>"GMT+02:00",
386  "Europe/Moscow"=>"GMT+03:00",
387  "Asia/Dubai"=>"GMT+04:00",
388  "Asia/Karachi"=>"GMT+05:00",
389  "Indian/Chagos"=>"GMT+06:00",
390  "Asia/Jakarta"=>"GMT+07:00",
391  "Asia/Hong_Kong"=>"GMT+08:00",
392  "Asia/Tokyo"=>"GMT+09:00",
393  "Australia/Sydney"=>"GMT+10:00",
394  "Pacific/Noumea"=>"GMT+11:00",
395  "Pacific/Auckland"=>"GMT+12:00",
396  "Pacific/Enderbury"=>"GMT+13:00"
397  );
398  foreach ($arraytz as $lib => $gmt) {
399  print '<option value="'.$lib.'"';
400  if ($selected == $lib || $selected == $gmt) {
401  print ' selected';
402  }
403  print '>'.$gmt.'</option>'."\n";
404  }
405  print '</select>';
406  }
407 
408 
409 
410  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
421  public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = 0, $showempty = 0, $forcecombo = 0)
422  {
423  // phpcs:enable
424  global $langs;
425 
426  $langs->load("dict");
427 
428  $sql = "SELECT code, label, width, height, unit";
429  $sql .= " FROM ".$this->db->prefix()."c_paper_format";
430  $sql .= " WHERE active=1";
431  if ($filter) {
432  $sql .= " AND code LIKE '%".$this->db->escape($filter)."%'";
433  }
434 
435  $resql = $this->db->query($sql);
436  if ($resql) {
437  $num = $this->db->num_rows($resql);
438  $i = 0;
439  while ($i < $num) {
440  $obj = $this->db->fetch_object($resql);
441  $unitKey = $langs->trans('SizeUnit'.$obj->unit);
442 
443  $paperformat[$obj->code] = $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey);
444 
445  $i++;
446  }
447  } else {
448  dol_print_error($this->db);
449  return '';
450  }
451  $out = '';
452 
453  $out .= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
454  if ($showempty) {
455  $out .= '<option value=""';
456  if ($selected == '') {
457  $out .= ' selected';
458  }
459  $out .= '>&nbsp;</option>';
460  }
461  foreach ($paperformat as $key => $value) {
462  if ($selected == $key) {
463  $out .= '<option value="'.$key.'" selected>'.$value.'</option>';
464  } else {
465  $out .= '<option value="'.$key.'">'.$value.'</option>';
466  }
467  }
468  $out .= '</select>';
469 
470  if (!$forcecombo) {
471  include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
472  $out .= ajax_combobox($htmlname);
473  }
474 
475  return $out;
476  }
477 }
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:449
Class to generate html code for admin pages.
select_timezone($selected, $htmlname)
Return a HTML select list of timezones.
select_menu($selected, $htmlname, $dirmenuarray, $moreattrib='')
Return list of available menus (eldy_backoffice, ...)
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)
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.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
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 dolibarr global constant int 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...