html.formadmin.class.php 17 KB

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