html.formadmin.class.php 15 KB

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