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