1<?php
2
3/**
4 * This file is part of the Froxlor project.
5 * Copyright (c) 2010 the Froxlor Team (see authors).
6 *
7 * For the full copyright and license information, please view the COPYING
8 * file that was distributed with this source code. You can also view the
9 * COPYING file online at http://files.froxlor.org/misc/COPYING.txt
10 *
11 * @copyright  (c) the authors
12 * @author     Froxlor team <team@froxlor.org> (2010-)
13 * @license    GPLv2 http://files.froxlor.org/misc/COPYING.txt
14 * @package    Classes
15 *
16 */
17
18class htmlform
19{
20	/**
21	 * internal tmp-variable to store form
22	 * @var string
23	 */
24	private static $_form = '';
25	private static $_filename = '';
26
27	public static function genHTMLForm($data = array())
28	{
29		global $lng, $theme;
30		$nob = false;
31
32		self::$_form = '';
33
34		foreach($data as $fdata)
35		{
36			$sections = $fdata['sections'];
37
38			foreach($sections as $section)
39			{
40				/*
41				 * here be section title & image
42				*/
43				$title = $section['title'];
44				$image = $section['image'];
45
46				if(isset($section['visible']) && $section['visible'] === false)
47				{
48					continue;
49				}
50
51				if (!isset($section['nobuttons']) || $section['nobuttons'] == false) {
52					eval("self::\$_form .= \"" . getTemplate("misc/form/table_section", "1") . "\";");
53				} else {
54					$nob = true;
55				}
56
57				$nexto = false;
58				foreach($section['fields'] as $fieldname => $fielddata)
59				{
60					if(isset($fielddata['visible']) && $fielddata['visible'] === false)
61					{
62						continue;
63					}
64
65					if ($nexto === false || (isset($fielddata['next_to']) && $nexto['field'] != $fielddata['next_to'])) {
66						$label = $fielddata['label'];
67						$desc = (isset($fielddata['desc']) ? $fielddata['desc'] : '');
68						$style = (isset($fielddata['style']) ? ' class="'.$fielddata['style'].'"' : '');
69						$mandatory = self::_getMandatoryFlag($fielddata);
70						$data_field = self::_parseDataField($fieldname, $fielddata);
71						if (isset($fielddata['has_nextto'])) {
72							$nexto = array('field' => $fieldname);
73							$data_field.='{NEXTTOFIELD_'.$fieldname.'}';
74						} else {
75							$nexto = false;
76						}
77						eval("self::\$_form .= \"" . getTemplate("misc/form/table_row", "1") . "\";");
78					} else {
79						$data_field = self::_parseDataField($fieldname, $fielddata);
80						$data_field = str_replace("\t", "", $data_field);
81						$data_field = $fielddata['next_to_prefix'].$data_field;
82						self::$_form = str_replace(
83								'{NEXTTOFIELD_'.$fielddata['next_to'].'}',
84								$data_field,
85								self::$_form
86						);
87						$nexto = false;
88					}
89				}
90			}
91		}
92
93		// add save/reset buttons at the end of the form
94		if (!$nob) {
95			eval("self::\$_form .= \"" . getTemplate("misc/form/table_end", "1") . "\";");
96		}
97
98		return self::$_form;
99	}
100
101	private static function _parseDataField($fieldname, $data = array())
102	{
103		switch($data['type'])
104		{
105			case 'text':
106				return self::_textBox($fieldname, $data); break;
107			case 'textul':
108				return self::_textBox($fieldname, $data, 'text', true); break;
109			case 'password':
110				return self::_textBox($fieldname, $data, 'password'); break;
111			case 'hidden':
112				return self::_textBox($fieldname, $data, 'hidden'); break;
113			case 'yesno':
114				return self::_yesnoBox($data); break;
115			case 'select':
116				return self::_selectBox($fieldname, $data); break;
117			case 'label':
118				return self::_labelField($data); break;
119			case 'textarea':
120				return self::_textArea($fieldname, $data); break;
121			case 'checkbox':
122				return self::_checkbox($fieldname, $data); break;
123			case 'file':
124				return self::_file($fieldname, $data); break;
125			case 'int':
126				return self::_int($fieldname, $data); break;
127		}
128	}
129
130	private static function _getMandatoryFlag($data = array())
131	{
132		if(isset($data['mandatory']))
133		{
134			return '&nbsp;<span class="red">*</span>';
135		}
136		elseif(isset($data['mandatory_ex']))
137		{
138			return '&nbsp;<span class="red">**</span>';
139		}
140		return '';
141	}
142
143	private static function _textBox($fieldname = '', $data = array(), $type = 'text', $unlimited = false)
144	{
145		$return = '';
146		$extras = '';
147		if(isset($data['maxlength'])) {
148			$extras .= ' maxlength="'.$data['maxlength'].'"';
149		}
150		if(isset($data['size'])) {
151			$extras .= ' size="'.$data['size'].'"';
152		}
153		if(isset($data['autocomplete'])) {
154			$extras .= ' autocomplete="'.$data['autocomplete'].'"';
155		}
156
157		// add support to save reloaded forms
158		if (isset($data['value'])) {
159			$value = $data['value'];
160		} elseif (isset($_SESSION['requestData'][$fieldname])) {
161			$value = $_SESSION['requestData'][$fieldname];
162		} else {
163			$value = '';
164		}
165
166		$ulfield = ($unlimited == true ? '&nbsp;'.$data['ul_field'] : '');
167		if(isset($data['display']) && $data['display'] != '')
168		{
169			$ulfield = '<strong>'.$data['display'].'</strong>';
170		}
171
172		eval("\$return = \"" . getTemplate("misc/form/input_text", "1") . "\";");
173		return $return;
174	}
175
176	private static function _textArea($fieldname = '', $data = array())
177	{
178		$return = '';
179		$extras = '';
180		if(isset($data['cols'])) {
181			$extras .= ' cols="'.$data['cols'].'"';
182		}
183		if(isset($data['rows'])) {
184			$extras .= ' rows="'.$data['rows'].'"';
185		}
186
187		// add support to save reloaded forms
188		if (isset($data['value'])) {
189			$value = $data['value'];
190		} elseif (isset($_SESSION['requestData'][$fieldname])) {
191			$value = $_SESSION['requestData'][$fieldname];
192		} else {
193			$value = '';
194		}
195		trim($value);
196
197		eval("\$return = \"" . getTemplate("misc/form/input_textarea", "1") . "\";");
198		return $return;
199	}
200
201	private static function _yesnoBox($data = array())
202	{
203		return $data['yesno_var'];
204	}
205
206	private static function _labelField($data = array())
207	{
208		return $data['value'];
209	}
210
211	private static function _selectBox($fieldname = '', $data = array())
212	{
213		// add support to save reloaded forms
214		if (isset($data['select_var'])) {
215			$select_var = $data['select_var'];
216		} elseif (isset($_SESSION['requestData'][$fieldname])) {
217			$select_var = $_SESSION['requestData'][$fieldname];
218		} else {
219			$select_var = '';
220		}
221
222		return '<select
223				id="'.$fieldname.'"
224						name="'.$fieldname.'"
225								'.(isset($data['class']) ? ' class="'.$data['class'] .'" ' : '').'
226										>'
227										.$select_var.
228										'</select>';
229	}
230
231	/**
232	 * Function to generate checkboxes.
233	 *
234	 * <code>
235	 * $data = array(
236	 *                       'label' => $lng['customer']['email_imap'],
237	 *                       'type' => 'checkbox',
238	 *                       'values' => array(
239	 *                                         array(  'label' => 'active',
240	 *                                                 'value' => '1'
241	 *                                               )
242	 *                                           ),
243	 *                       'value' => array('1'),
244	 *                       'mandatory' => true
245	 *          )
246	 * </code>
247	 *
248	 * @param string $fieldname contains the fieldname
249	 * @param array $data contains the data array
250	 */
251	public static function _checkbox($fieldname = '', $data = array()) {
252		// $data['value'] contains checked items
253
254		$checked = array();
255		if (isset($data['value'])) {
256			$checked = $data['value'];
257		}
258
259		if (isset($_SESSION['requestData'])) {
260			if(isset($_SESSION['requestData'][$fieldname])) {
261				$checked = array($_SESSION['requestData'][$fieldname]);
262			}
263		}
264
265		// default value is none, so the checkbox isn't an array
266		$isArray = '';
267
268		if (count($data['values']) > 1 || (isset($data['is_array']) && $data['is_array'] == 1)) {
269			$isArray = '[]';
270		}
271
272		// will contain the output
273		$output = "";
274		foreach($data['values'] as $val) {
275			$key = $val['label'];
276			// is this box checked?
277			$isChecked = '';
278			if (is_array($checked) && count($checked) > 0) {
279				foreach($checked as $tmp) {
280					if ($tmp == $val['value']) {
281						$isChecked = ' checked="checked" ';
282						break;
283					}
284				}
285			}
286			$output .= '<label><input type="checkbox" name="'.$fieldname.$isArray.'" value="'.$val['value'].'" '.$isChecked.'/>'.$key.'</label>';
287		}
288
289		return $output;
290	}
291
292	private static function _file($fieldname = '', $data = array())
293	{
294		$return = '';
295		$extras = '';
296		if(isset($data['maxlength'])) {
297			$extras .= ' maxlength="'.$data['maxlength'].'"';
298		}
299
300		// add support to save reloaded forms
301		if (isset($data['value'])) {
302			$value = $data['value'];
303		} elseif (isset($_SESSION['requestData'][$fieldname])) {
304			$value = $_SESSION['requestData'][$fieldname];
305		} else {
306			$value = '';
307		}
308
309		if(isset($data['display']) && $data['display'] != '')
310		{
311			$ulfield = '<strong>'.$data['display'].'</strong>';
312		}
313
314		eval("\$return = \"" . getTemplate("misc/form/input_file", "1") . "\";");
315		return $return;
316	}
317
318	private static function _int($fieldname = '', $data = array())
319	{
320		$return = '';
321		$extras = '';
322		if(isset($data['int_min'])) {
323			$extras .= ' min="'.$data['int_min'].'"';
324		}
325		if(isset($data['int_max'])) {
326			$extras .= ' max="'.$data['int_max'].'"';
327		}
328
329		// add support to save reloaded forms
330		if (isset($data['value'])) {
331			$value = $data['value'];
332		} elseif (isset($_SESSION['requestData'][$fieldname])) {
333			$value = $_SESSION['requestData'][$fieldname];
334		} else {
335			$value = '';
336		}
337
338		$type = 'number';
339		$ulfield = '';
340		eval("\$return = \"" . getTemplate("misc/form/input_text", "1") . "\";");
341		return $return;
342	}
343}
344