html.formadmin.class.php 13 KB

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