html.formadmin.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/html.formadmin.class.php
  21. * \ingroup core
  22. * \brief File of class for html functions for admin pages
  23. */
  24. /**
  25. * Class to generate html code for admin pages
  26. */
  27. class FormAdmin
  28. {
  29. var $db;
  30. var $error;
  31. /**
  32. * Constructor
  33. *
  34. * @param DoliDB $db Database handler
  35. */
  36. function __construct($db)
  37. {
  38. $this->db = $db;
  39. }
  40. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  41. /**
  42. * Return html select list with available languages (key='en_US', value='United States' for example)
  43. *
  44. * @param string $selected Language pre-selected
  45. * @param string $htmlname Name of HTML select
  46. * @param int $showauto Show 'auto' choice
  47. * @param array $filter Array of keys to exclude in list
  48. * @param string $showempty '1'=Add empty value or string to show
  49. * @param int $showwarning Show a warning if language is not complete
  50. * @param int $disabled Disable edit of select
  51. * @param string $morecss Add more css styles
  52. * @param int $showcode 1=Add language code into label at begining, 2=Add language code into label at end
  53. * @param int $forcecombo Force to use combo box (so no ajax beautify effect)
  54. * @return string Return HTML select string with list of languages
  55. */
  56. function select_language($selected='', $htmlname='lang_id', $showauto=0, $filter=null, $showempty='', $showwarning=0, $disabled=0, $morecss='', $showcode=0, $forcecombo=0)
  57. {
  58. // phpcs:enable
  59. global $langs;
  60. $langs_available=$langs->get_available_languages(DOL_DOCUMENT_ROOT,12);
  61. $out='';
  62. $out.= '<select class="flat'.($morecss?' '.$morecss:'').'" id="'.$htmlname.'" name="'.$htmlname.'"'.($disabled?' disabled':'').'>';
  63. if ($showempty)
  64. {
  65. $out.= '<option value="0"';
  66. if ($selected == '') $out.= ' selected';
  67. $out.= '>';
  68. if ($showempty != '1') $out.=$showempty;
  69. else $out.='&nbsp;';
  70. $out.='</option>';
  71. }
  72. if ($showauto)
  73. {
  74. $out.= '<option value="auto"';
  75. if ($selected == 'auto') $out.= ' selected';
  76. $out.= '>'.$langs->trans("AutoDetectLang").'</option>';
  77. }
  78. asort($langs_available);
  79. foreach ($langs_available as $key => $value)
  80. {
  81. $valuetoshow=$value;
  82. if ($showcode == 1) $valuetoshow=$key.' - '.$value;
  83. if ($showcode == 2) $valuetoshow=$value.' ('.$key.')';
  84. if ($filter && is_array($filter))
  85. {
  86. if ( ! array_key_exists($key, $filter))
  87. {
  88. $out.= '<option value="'.$key.'">'.$valuetoshow.'</option>';
  89. }
  90. }
  91. else if ($selected == $key)
  92. {
  93. $out.= '<option value="'.$key.'" selected>'.$valuetoshow.'</option>';
  94. }
  95. else
  96. {
  97. $out.= '<option value="'.$key.'">'.$valuetoshow.'</option>';
  98. }
  99. }
  100. $out.= '</select>';
  101. // Make select dynamic
  102. if (! $forcecombo)
  103. {
  104. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  105. $out.= ajax_combobox($htmlname);
  106. }
  107. return $out;
  108. }
  109. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  110. /**
  111. * Return list of available menus (eldy_backoffice, ...)
  112. *
  113. * @param string $selected Preselected menu value
  114. * @param string $htmlname Name of html select
  115. * @param array $dirmenuarray Array of directories to scan
  116. * @param string $moreattrib More attributes on html select tag
  117. * @return integer|null
  118. */
  119. function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib='')
  120. {
  121. // phpcs:enable
  122. global $langs,$conf;
  123. // Clean parameters
  124. // Check parameters
  125. if (! is_array($dirmenuarray)) return -1;
  126. $menuarray=array();
  127. foreach ($conf->file->dol_document_root as $dirroot)
  128. {
  129. foreach($dirmenuarray as $dirtoscan)
  130. {
  131. $dir=$dirroot.$dirtoscan;
  132. //print $dir.'<br>';
  133. if (is_dir($dir))
  134. {
  135. $handle=opendir($dir);
  136. if (is_resource($handle))
  137. {
  138. while (($file = readdir($handle))!==false)
  139. {
  140. if (is_file($dir."/".$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && substr($file, 0, 5) != 'index')
  141. {
  142. if (preg_match('/lib\.php$/i',$file)) continue; // We exclude library files
  143. if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i',$file)) continue; // We exclude all menu manager files
  144. if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i',$file)) continue; // We exclude all menu manager files
  145. if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i',$file)) continue; // We exclude all menu manager files
  146. $filelib=preg_replace('/\.php$/i','',$file);
  147. $prefix='';
  148. // 0=Recommanded, 1=Experimental, 2=Developpement, 3=Other
  149. if (preg_match('/^eldy/i',$file)) $prefix='0';
  150. else if (preg_match('/^smartphone/i',$file)) $prefix='2';
  151. else $prefix='3';
  152. if ($file == $selected)
  153. {
  154. $menuarray[$prefix.'_'.$file]='<option value="'.$file.'" selected>'.$filelib.'</option>';
  155. }
  156. else
  157. {
  158. $menuarray[$prefix.'_'.$file]='<option value="'.$file.'">'.$filelib.'</option>';
  159. }
  160. }
  161. }
  162. closedir($handle);
  163. }
  164. }
  165. }
  166. }
  167. ksort($menuarray);
  168. // Output combo list of menus
  169. print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($moreattrib?' '.$moreattrib:'').'>';
  170. $oldprefix='';
  171. foreach ($menuarray as $key => $val)
  172. {
  173. $tab=explode('_',$key);
  174. $newprefix=$tab[0];
  175. if ($newprefix=='1' && ($conf->global->MAIN_FEATURES_LEVEL < 1)) continue;
  176. if ($newprefix=='2' && ($conf->global->MAIN_FEATURES_LEVEL < 2)) continue;
  177. if ($newprefix != $oldprefix) // Add separators
  178. {
  179. // Affiche titre
  180. print '<option value="-1" disabled>';
  181. if ($newprefix=='0') print '-- '.$langs->trans("VersionRecommanded").' --';
  182. if ($newprefix=='1') print '-- '.$langs->trans("VersionExperimental").' --';
  183. if ($newprefix=='2') print '-- '.$langs->trans("VersionDevelopment").' --';
  184. if ($newprefix=='3') print '-- '.$langs->trans("Other").' --';
  185. print '</option>';
  186. $oldprefix=$newprefix;
  187. }
  188. print $val."\n"; // Show menu entry
  189. }
  190. print '</select>';
  191. }
  192. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  193. /**
  194. * Return combo list of available menu families
  195. *
  196. * @param string $selected Menu pre-selected
  197. * @param string $htmlname Name of html select
  198. * @param string[] $dirmenuarray Directories to scan
  199. * @return void
  200. */
  201. function select_menu_families($selected, $htmlname, $dirmenuarray)
  202. {
  203. // phpcs:enable
  204. global $langs,$conf;
  205. //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
  206. $expdevmenu=array();
  207. $menuarray=array();
  208. foreach($dirmenuarray as $dirmenu)
  209. {
  210. foreach ($conf->file->dol_document_root as $dirroot)
  211. {
  212. $dir=$dirroot.$dirmenu;
  213. if (is_dir($dir))
  214. {
  215. $handle=opendir($dir);
  216. if (is_resource($handle))
  217. {
  218. while (($file = readdir($handle))!==false)
  219. {
  220. if (is_file($dir."/".$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS')
  221. {
  222. $filelib=preg_replace('/(_backoffice|_frontoffice)?\.php$/i','',$file);
  223. if (preg_match('/^index/i',$filelib)) continue;
  224. if (preg_match('/^default/i',$filelib)) continue;
  225. if (preg_match('/^empty/i',$filelib)) continue;
  226. if (preg_match('/\.lib/i',$filelib)) continue;
  227. if (empty($conf->global->MAIN_FEATURES_LEVEL) && in_array($file,$expdevmenu)) continue;
  228. $menuarray[$filelib]=1;
  229. }
  230. $menuarray['all']=1;
  231. }
  232. closedir($handle);
  233. }
  234. }
  235. }
  236. }
  237. ksort($menuarray);
  238. // Affichage liste deroulante des menus
  239. print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
  240. $oldprefix='';
  241. foreach ($menuarray as $key => $val)
  242. {
  243. $tab=explode('_',$key);
  244. $newprefix=$tab[0];
  245. print '<option value="'.$key.'"';
  246. if ($key == $selected)
  247. {
  248. print ' selected';
  249. }
  250. print '>';
  251. if ($key == 'all') print $langs->trans("AllMenus");
  252. else print $key;
  253. print '</option>'."\n";
  254. }
  255. print '</select>';
  256. }
  257. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  258. /**
  259. * Return a HTML select list of timezones
  260. *
  261. * @param string $selected Menu pre-selectionnee
  262. * @param string $htmlname Nom de la zone select
  263. * @return void
  264. */
  265. function select_timezone($selected,$htmlname)
  266. {
  267. // phpcs:enable
  268. global $langs,$conf;
  269. print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
  270. print '<option value="-1">&nbsp;</option>';
  271. $arraytz=array(
  272. "Pacific/Midway"=>"GMT-11:00",
  273. "Pacific/Fakaofo"=>"GMT-10:00",
  274. "America/Anchorage"=>"GMT-09:00",
  275. "America/Los_Angeles"=>"GMT-08:00",
  276. "America/Dawson_Creek"=>"GMT-07:00",
  277. "America/Chicago"=>"GMT-06:00",
  278. "America/Bogota"=>"GMT-05:00",
  279. "America/Anguilla"=>"GMT-04:00",
  280. "America/Araguaina"=>"GMT-03:00",
  281. "America/Noronha"=>"GMT-02:00",
  282. "Atlantic/Azores"=>"GMT-01:00",
  283. "Africa/Abidjan"=>"GMT+00:00",
  284. "Europe/Paris"=>"GMT+01:00",
  285. "Europe/Helsinki"=>"GMT+02:00",
  286. "Europe/Moscow"=>"GMT+03:00",
  287. "Asia/Dubai"=>"GMT+04:00",
  288. "Asia/Karachi"=>"GMT+05:00",
  289. "Indian/Chagos"=>"GMT+06:00",
  290. "Asia/Jakarta"=>"GMT+07:00",
  291. "Asia/Hong_Kong"=>"GMT+08:00",
  292. "Asia/Tokyo"=>"GMT+09:00",
  293. "Australia/Sydney"=>"GMT+10:00",
  294. "Pacific/Noumea"=>"GMT+11:00",
  295. "Pacific/Auckland"=>"GMT+12:00",
  296. "Pacific/Enderbury"=>"GMT+13:00"
  297. );
  298. foreach ($arraytz as $lib => $gmt)
  299. {
  300. print '<option value="'.$lib.'"';
  301. if ($selected == $lib || $selected == $gmt) print ' selected';
  302. print '>'.$gmt.'</option>'."\n";
  303. }
  304. print '</select>';
  305. }
  306. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  307. /**
  308. * Return html select list with available languages (key='en_US', value='United States' for example)
  309. *
  310. * @param string $selected Paper format pre-selected
  311. * @param string $htmlname Name of HTML select field
  312. * @param string $filter Value to filter on code
  313. * @param int $showempty Add empty value
  314. * @return string Return HTML output
  315. */
  316. function select_paper_format($selected='',$htmlname='paperformat_id',$filter=0,$showempty=0)
  317. {
  318. // phpcs:enable
  319. global $langs;
  320. $langs->load("dict");
  321. $sql = "SELECT code, label, width, height, unit";
  322. $sql.= " FROM ".MAIN_DB_PREFIX."c_paper_format";
  323. $sql.= " WHERE active=1";
  324. if ($filter) $sql.=" AND code LIKE '%".$this->db->escape($filter)."%'";
  325. $resql=$this->db->query($sql);
  326. if ($resql)
  327. {
  328. $num=$this->db->num_rows($resql);
  329. $i=0;
  330. while ($i < $num)
  331. {
  332. $obj=$this->db->fetch_object($resql);
  333. $unitKey = $langs->trans('SizeUnit'.$obj->unit);
  334. $paperformat[$obj->code]= $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey);
  335. $i++;
  336. }
  337. }
  338. else
  339. {
  340. dol_print_error($this->db);
  341. return '';
  342. }
  343. $out='';
  344. $out.= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
  345. if ($showempty)
  346. {
  347. $out.= '<option value=""';
  348. if ($selected == '') $out.= ' selected';
  349. $out.= '>&nbsp;</option>';
  350. }
  351. foreach ($paperformat as $key => $value)
  352. {
  353. if ($selected == $key)
  354. {
  355. $out.= '<option value="'.$key.'" selected>'.$value.'</option>';
  356. }
  357. else
  358. {
  359. $out.= '<option value="'.$key.'">'.$value.'</option>';
  360. }
  361. }
  362. $out.= '</select>';
  363. return $out;
  364. }
  365. }