html.formadmin.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. /**
  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. public $db;
  30. public $error;
  31. /**
  32. * Constructor
  33. *
  34. * @param DoliDB $db Database handler
  35. */
  36. public function __construct($db)
  37. {
  38. $this->db = $db;
  39. }
  40. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  41. /**
  42. * Return html select list with available languages (key='en_US', value='United States' for example)
  43. *
  44. * @param string|array $selected Language pre-selected. Can be an array if $multiselect is 1.
  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 (opposite of $onlykeys)
  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. * @param int $multiselect Make the combo a multiselect
  55. * @param array $onlykeys Array of language keys to restrict list with the following keys (opposite of $filter). Example array('fr', 'es', ...)
  56. * @param int $mainlangonly 1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...)
  57. * @return string Return HTML select string with list of languages
  58. */
  59. 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)
  60. {
  61. // phpcs:enable
  62. global $conf, $langs;
  63. if (!empty($conf->global->MAIN_DEFAULT_LANGUAGE_FILTER)) {
  64. $filter[$conf->global->MAIN_DEFAULT_LANGUAGE_FILTER] = 1;
  65. }
  66. $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12, 0, $mainlangonly);
  67. // If empty value is not allowed and the language to select is not inside the list of available language and we must find
  68. // an alternative of the language code to pre-select (to avoid to have first element in list pre-selected).
  69. if ($selected && empty($showempty)) {
  70. if (!is_array($selected) && !array_key_exists($selected, $langs_available)) {
  71. $tmparray = explode('_', $selected);
  72. if (!empty($tmparray[1])) {
  73. $selected = getLanguageCodeFromCountryCode($tmparray[1]);
  74. }
  75. if (empty($selected)) {
  76. $selected = $langs->defaultlang;
  77. }
  78. } else {
  79. // If the preselected value is an array, we do not try to find alternative to preselect
  80. }
  81. }
  82. $out = '';
  83. $out .= '<select '.($multiselect ? 'multiple="multiple" ' : '').'class="flat'.($morecss ? ' '.$morecss : '').'" id="'.$htmlname.'" name="'.$htmlname.($multiselect ? '[]' : '').'"'.($disabled ? ' disabled' : '').'>';
  84. if ($showempty && !$multiselect) {
  85. $out .= '<option value="0"';
  86. if ($selected === '') {
  87. $out .= ' selected';
  88. }
  89. $out .= '>';
  90. if ($showempty != '1') {
  91. $out .= $showempty;
  92. } else {
  93. $out .= '&nbsp;';
  94. }
  95. $out .= '</option>';
  96. }
  97. if ($showauto) {
  98. $out .= '<option value="auto"';
  99. if ($selected === 'auto') {
  100. $out .= ' selected';
  101. }
  102. $out .= '>'.$langs->trans("AutoDetectLang").'</option>';
  103. }
  104. asort($langs_available); // array('XX' => 'Language (Country)', ...)
  105. foreach ($langs_available as $key => $value) {
  106. $valuetoshow = $value;
  107. if ($showcode == 1) {
  108. if ($mainlangonly) {
  109. $valuetoshow = '<span class="opacitymedium">'.preg_replace('/[_-].*$/', '', $key).'</span> - '.$value;
  110. } else {
  111. $valuetoshow = '<span class="opacitymedium">'.$key.'</span> - '.$value;
  112. }
  113. }
  114. if ($showcode == 2) {
  115. if ($mainlangonly) {
  116. $valuetoshow = $value.' <span class="opacitymedium">('.preg_replace('/[_-].*$/', '', $key).')</span>';
  117. } else {
  118. $valuetoshow = $value.' <span class="opacitymedium">('.$key.')</span>';
  119. }
  120. }
  121. $keytouse = $key;
  122. if ($mainlangonly) {
  123. $keytouse = preg_replace('/[_-].*$/', '', $key);
  124. }
  125. if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) {
  126. continue;
  127. }
  128. if ($onlykeys && is_array($onlykeys) && !array_key_exists($keytouse, $onlykeys)) {
  129. continue;
  130. }
  131. $valuetoshow = picto_from_langcode($key, 'class="saturatemedium"').' '.$valuetoshow;
  132. if ((is_string($selected) && (string) $selected == (string) $keytouse) || (is_array($selected) && in_array($keytouse, $selected))) {
  133. $out .= '<option value="'.$keytouse.'" selected data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
  134. } else {
  135. $out .= '<option value="'.$keytouse.'" data-html="'.dol_escape_htmltag($valuetoshow).'">'.$valuetoshow.'</option>';
  136. }
  137. }
  138. $out .= '</select>';
  139. // Make select dynamic
  140. if (!$forcecombo) {
  141. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  142. $out .= ajax_combobox($htmlname);
  143. }
  144. return $out;
  145. }
  146. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  147. /**
  148. * Return list of available menus (eldy_backoffice, ...)
  149. *
  150. * @param string $selected Preselected menu value
  151. * @param string $htmlname Name of html select
  152. * @param array $dirmenuarray Array of directories to scan
  153. * @param string $moreattrib More attributes on html select tag
  154. * @return integer|null
  155. */
  156. public function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib = '')
  157. {
  158. // phpcs:enable
  159. global $langs, $conf;
  160. // Clean parameters
  161. // Check parameters
  162. if (!is_array($dirmenuarray)) {
  163. return -1;
  164. }
  165. $menuarray = array();
  166. foreach ($conf->file->dol_document_root as $dirroot) {
  167. foreach ($dirmenuarray as $dirtoscan) {
  168. $dir = $dirroot.$dirtoscan;
  169. //print $dir.'<br>';
  170. if (is_dir($dir)) {
  171. $handle = opendir($dir);
  172. if (is_resource($handle)) {
  173. while (($file = readdir($handle)) !== false) {
  174. if (is_file($dir."/".$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && substr($file, 0, 5) != 'index') {
  175. if (preg_match('/lib\.php$/i', $file)) {
  176. continue; // We exclude library files
  177. }
  178. if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i', $file)) {
  179. continue; // We exclude all menu manager files
  180. }
  181. if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i', $file)) {
  182. continue; // We exclude all menu manager files
  183. }
  184. if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i', $file)) {
  185. continue; // We exclude all menu manager files
  186. }
  187. $filelib = preg_replace('/\.php$/i', '', $file);
  188. $prefix = '';
  189. // 0=Recommanded, 1=Experimental, 2=Developpement, 3=Other
  190. if (preg_match('/^eldy/i', $file)) {
  191. $prefix = '0';
  192. } elseif (preg_match('/^smartphone/i', $file)) {
  193. $prefix = '2';
  194. } else {
  195. $prefix = '3';
  196. }
  197. if ($file == $selected) {
  198. $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'" selected>'.$filelib.'</option>';
  199. } else {
  200. $menuarray[$prefix.'_'.$file] = '<option value="'.$file.'">'.$filelib.'</option>';
  201. }
  202. }
  203. }
  204. closedir($handle);
  205. }
  206. }
  207. }
  208. }
  209. ksort($menuarray);
  210. // Output combo list of menus
  211. print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
  212. $oldprefix = '';
  213. foreach ($menuarray as $key => $val) {
  214. $tab = explode('_', $key);
  215. $newprefix = $tab[0];
  216. if ($newprefix == '1' && ($conf->global->MAIN_FEATURES_LEVEL < 1)) {
  217. continue;
  218. }
  219. if ($newprefix == '2' && ($conf->global->MAIN_FEATURES_LEVEL < 2)) {
  220. continue;
  221. }
  222. if ($newprefix != $oldprefix) { // Add separators
  223. // Affiche titre
  224. print '<option value="-1" disabled>';
  225. if ($newprefix == '0') {
  226. print '-- '.$langs->trans("VersionRecommanded").' --';
  227. }
  228. if ($newprefix == '1') {
  229. print '-- '.$langs->trans("VersionExperimental").' --';
  230. }
  231. if ($newprefix == '2') {
  232. print '-- '.$langs->trans("VersionDevelopment").' --';
  233. }
  234. if ($newprefix == '3') {
  235. print '-- '.$langs->trans("Other").' --';
  236. }
  237. print '</option>';
  238. $oldprefix = $newprefix;
  239. }
  240. print $val."\n"; // Show menu entry
  241. }
  242. print '</select>';
  243. }
  244. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  245. /**
  246. * Return combo list of available menu families
  247. *
  248. * @param string $selected Menu pre-selected
  249. * @param string $htmlname Name of html select
  250. * @param string[] $dirmenuarray Directories to scan
  251. * @return void
  252. */
  253. public function select_menu_families($selected, $htmlname, $dirmenuarray)
  254. {
  255. // phpcs:enable
  256. global $langs, $conf;
  257. //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php'); // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
  258. $expdevmenu = array();
  259. $menuarray = array();
  260. foreach ($dirmenuarray as $dirmenu) {
  261. foreach ($conf->file->dol_document_root as $dirroot) {
  262. $dir = $dirroot.$dirmenu;
  263. if (is_dir($dir)) {
  264. $handle = opendir($dir);
  265. if (is_resource($handle)) {
  266. while (($file = readdir($handle)) !== false) {
  267. if (is_file($dir."/".$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS') {
  268. $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file);
  269. if (preg_match('/^index/i', $filelib)) {
  270. continue;
  271. }
  272. if (preg_match('/^default/i', $filelib)) {
  273. continue;
  274. }
  275. if (preg_match('/^empty/i', $filelib)) {
  276. continue;
  277. }
  278. if (preg_match('/\.lib/i', $filelib)) {
  279. continue;
  280. }
  281. if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) {
  282. continue;
  283. }
  284. $menuarray[$filelib] = 1;
  285. }
  286. $menuarray['all'] = 1;
  287. }
  288. closedir($handle);
  289. }
  290. }
  291. }
  292. }
  293. ksort($menuarray);
  294. // Affichage liste deroulante des menus
  295. print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
  296. $oldprefix = '';
  297. foreach ($menuarray as $key => $val) {
  298. $tab = explode('_', $key);
  299. $newprefix = $tab[0];
  300. print '<option value="'.$key.'"';
  301. if ($key == $selected) {
  302. print ' selected';
  303. }
  304. print '>';
  305. if ($key == 'all') {
  306. print $langs->trans("AllMenus");
  307. } else {
  308. print $key;
  309. }
  310. print '</option>'."\n";
  311. }
  312. print '</select>';
  313. print ajax_combobox($htmlname);
  314. }
  315. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  316. /**
  317. * Return a HTML select list of timezones
  318. *
  319. * @param string $selected Menu pre-selectionnee
  320. * @param string $htmlname Nom de la zone select
  321. * @return void
  322. */
  323. public function select_timezone($selected, $htmlname)
  324. {
  325. // phpcs:enable
  326. global $langs, $conf;
  327. print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
  328. print '<option value="-1">&nbsp;</option>';
  329. $arraytz = array(
  330. "Pacific/Midway"=>"GMT-11:00",
  331. "Pacific/Fakaofo"=>"GMT-10:00",
  332. "America/Anchorage"=>"GMT-09:00",
  333. "America/Los_Angeles"=>"GMT-08:00",
  334. "America/Dawson_Creek"=>"GMT-07:00",
  335. "America/Chicago"=>"GMT-06:00",
  336. "America/Bogota"=>"GMT-05:00",
  337. "America/Anguilla"=>"GMT-04:00",
  338. "America/Araguaina"=>"GMT-03:00",
  339. "America/Noronha"=>"GMT-02:00",
  340. "Atlantic/Azores"=>"GMT-01:00",
  341. "Africa/Abidjan"=>"GMT+00:00",
  342. "Europe/Paris"=>"GMT+01:00",
  343. "Europe/Helsinki"=>"GMT+02:00",
  344. "Europe/Moscow"=>"GMT+03:00",
  345. "Asia/Dubai"=>"GMT+04:00",
  346. "Asia/Karachi"=>"GMT+05:00",
  347. "Indian/Chagos"=>"GMT+06:00",
  348. "Asia/Jakarta"=>"GMT+07:00",
  349. "Asia/Hong_Kong"=>"GMT+08:00",
  350. "Asia/Tokyo"=>"GMT+09:00",
  351. "Australia/Sydney"=>"GMT+10:00",
  352. "Pacific/Noumea"=>"GMT+11:00",
  353. "Pacific/Auckland"=>"GMT+12:00",
  354. "Pacific/Enderbury"=>"GMT+13:00"
  355. );
  356. foreach ($arraytz as $lib => $gmt) {
  357. print '<option value="'.$lib.'"';
  358. if ($selected == $lib || $selected == $gmt) {
  359. print ' selected';
  360. }
  361. print '>'.$gmt.'</option>'."\n";
  362. }
  363. print '</select>';
  364. }
  365. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  366. /**
  367. * Return html select list with available languages (key='en_US', value='United States' for example)
  368. *
  369. * @param string $selected Paper format pre-selected
  370. * @param string $htmlname Name of HTML select field
  371. * @param string $filter Value to filter on code
  372. * @param int $showempty Add empty value
  373. * @param int $forcecombo Force to load all values and output a standard combobox (with no beautification)
  374. * @return string Return HTML output
  375. */
  376. public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = 0, $showempty = 0, $forcecombo = 0)
  377. {
  378. // phpcs:enable
  379. global $langs;
  380. $langs->load("dict");
  381. $sql = "SELECT code, label, width, height, unit";
  382. $sql .= " FROM ".$this->db->prefix()."c_paper_format";
  383. $sql .= " WHERE active=1";
  384. if ($filter) {
  385. $sql .= " AND code LIKE '%".$this->db->escape($filter)."%'";
  386. }
  387. $resql = $this->db->query($sql);
  388. if ($resql) {
  389. $num = $this->db->num_rows($resql);
  390. $i = 0;
  391. while ($i < $num) {
  392. $obj = $this->db->fetch_object($resql);
  393. $unitKey = $langs->trans('SizeUnit'.$obj->unit);
  394. $paperformat[$obj->code] = $langs->trans('PaperFormat'.strtoupper($obj->code)).' - '.round($obj->width).'x'.round($obj->height).' '.($unitKey == 'SizeUnit'.$obj->unit ? $obj->unit : $unitKey);
  395. $i++;
  396. }
  397. } else {
  398. dol_print_error($this->db);
  399. return '';
  400. }
  401. $out = '';
  402. $out .= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'">';
  403. if ($showempty) {
  404. $out .= '<option value=""';
  405. if ($selected == '') {
  406. $out .= ' selected';
  407. }
  408. $out .= '>&nbsp;</option>';
  409. }
  410. foreach ($paperformat as $key => $value) {
  411. if ($selected == $key) {
  412. $out .= '<option value="'.$key.'" selected>'.$value.'</option>';
  413. } else {
  414. $out .= '<option value="'.$key.'">'.$value.'</option>';
  415. }
  416. }
  417. $out .= '</select>';
  418. if (!$forcecombo) {
  419. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  420. $out .= ajax_combobox($htmlname);
  421. }
  422. return $out;
  423. }
  424. }