aboutsummaryrefslogtreecommitdiff
path: root/h-source/Library/Html/Form.php
blob: 0714d3cce8bb85d993aaaa6cc5c7d4c4c76e982d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php

// EasyGiant is a PHP framework for creating and managing dynamic content
//
// Copyright (C) 2009 - 2011  Antonio Gallo
// See COPYRIGHT.txt and LICENSE.txt.
//
// This file is part of EasyGiant
//
// EasyGiant is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EasyGiant is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EasyGiant.  If not, see <http://www.gnu.org/licenses/>.

if (!defined('EG')) die('Direct access not allowed!');

//create the HTML of the inputs of a form
class Html_Form {
	
	//return the HTML of a select
	//$name: name of the select
	//$value: the selected value of the select (set $value equal to null if you don't want to select an option)
	//$options: options of the select. This param can be a comma-separated list of options or an associative array ('name'=>'value')
	//$className: the class name of the select
	//$idName: name of the id
	static public function select($name, $value, $options, $className = null, $idName = null, $reverse = null, $attributes = null)
	{
		$strClass = isset($className) ? "class='".$className."'" : null;
		$idStr = isset($idName) ? "id='".$idName."'" : null;
		
		$returnString = null;
		$returnString .= "<select $attributes ".$idStr." $strClass name='".$name."'>\n";
		if (is_string($options)) {
			$tempArray = explode(',',$options);
			foreach ($tempArray as $item)
			{
				if (strstr($item,'optgroupOpen:'))
				{
					$temp = explode(':',$item);
					$optionsArray[$temp[1]] = "optgroupOpen";
				}
				else
				{
					$optionsArray[$item] = $item;
				}
			}
		}
		else
		{
			$optionsArray = $options;
		}

		$flag = 0;
		foreach ($optionsArray as $optionName => $optionValue) {

			$a = $optionName;
			$b = $optionValue;
			
			if (strcmp($reverse,'yes') === 0)
			{
				$b = $optionName;
				$a = $optionValue;
			}
			
			if (strcmp($b,'optgroupOpen') === 0)
			{
				if ($flag === 1) $returnString .= "</optgroup>\n";
				$returnString .= "<optgroup label='" . $a . "'>\n";
				$flag = 1;
			}
			else
			{
				$str= (strcmp($value,$b) === 0) ? "selected='$b'" : null;
				$returnString .= "<option value='".$b."' $str>$a</option>\n";
			}
		}
		if ($flag === 1) $returnString .= "</optgroup>\n";
		$returnString .= "</select>\n";
		return $returnString;
	}

	//return the HTML of an <input type='text' ...>
	//$name: the name of the input
	//$value: the value of the input
	//$className: the class name of the input
	//$idName: name of the id
	static public function input($name, $value, $className = null, $idName = null, $attributes = null)
	{
		$strClass = isset($className) ? "class='".$className."'" : null;
		$idStr = isset($idName) ? "id='".$idName."'" : null;
		
		$returnString ="<input $attributes ".$idStr." $strClass type='text' name='" .$name. "' value = '$value' />\n";
		return $returnString;
	}

	//return the HTML of an <input type='file' ...>
	//$name: the name of the input
	//$className: the class name of the input
	//$idName: name of the id
	static public function fileUpload($name, $value, $className = null, $idName = null, $attributes = null)
	{
		$strClass = isset($className) ? "class='".$className."'" : null;
		$idStr = isset($idName) ? "id='".$idName."'" : null;

		$returnString ="<input $attributes ".$idStr." $strClass type='file' name='" .$name. "' />\n";
		return $returnString;
	}
	
	//return the HTML of a checkBox
	//$name: name of the checkBox (string)
	//$value: the value of the checkBox (string or number)
	//$option: option of the checkBox (string or number)
	//$className: the class name of the checkBox (string)
	//$idName: name of the id
	static public function checkbox($name, $value, $option, $className = null, $idName = null, $attributes = null)
	{
		$strClass = isset($className) ? "class='".$className."'" : null;
		$idStr = isset($idName) ? "id='".$idName."'" : null;
		
		$str = (strcmp($value,$option) === 0) ? "checked = 'checked'" : null;
		return "<input $attributes ".$idStr." $strClass type='checkbox' name='".$name."' value='".$option."' $str />\n";
	}
	
	//return the HTML of a hidden entry
	//$name: name of the hidden entry (string)
	//$value: the value of the hidden entry (string or number)
	static public function hidden($name, $value)
	{
		return "<input type='hidden' name='" .$name. "' value = '$value'>\n";
	}

	//return the HTML of a password entry
	//$name: name of the password entry (string)
	//$value: the value of the password entry (string or number)
	//$idName: name of the id
	static public function password($name, $value, $className = null, $idName = null, $attributes = null)
	{
		$strClass = isset($className) ? "class='".$className."'" : null;
		$idStr = isset($idName) ? "id='".$idName."'" : null;
		
		return "<input $attributes ".$idStr." $strClass type='password' name='" .$name. "' value='$value' />\n";
	}

	//return the HTML of a textarea
	//$name: name of the textarea (string)
	//$value: the value of the textarea (string or number)
	//$idName: name of the id
	static public function textarea($name, $value, $className = null, $idName = null, $attributes = null)
	{
		$strClass = isset($className) ? "class='".$className."'" : null;
		$idStr = isset($idName) ? "id='".$idName."'" : null;
		
		return "<textarea $attributes ".$idStr." $strClass name='" .$name. "'>$value</textarea>\n";
	}
	
	//return the HTML of a radio button
	//$name: name of the radio button
	//$value: the selected value of the radio button (set $value equal to null if you don't want to select an option)
	//$options: options of the radio button. This param can be a comma-separated list of options or an associative array ('name'=>'value')
	//$className: the class name of the radio button
	//$position: position of the strings of the radio with respect to the "circles". It can be before or after
	//$idName: name of the id
	static public function radio($name, $value, $options, $className = null, $position = 'after', $idName = null, $attributes = null)
	{
		$strClass = isset($className) ? "class='".$className."'" : null;
		$idStr = isset($idName) ? "id='".$idName."'" : null;
		
		$returnString = null;
		
		if (is_string($options)) {
			$tempArray = explode(',',$options);
			foreach ($tempArray as $item)
			{
				$optionsArray[$item] = $item;
			}
		} else {
			$optionsArray = $options;
		}
		
		foreach ($optionsArray as $optionName => $optionValue) {
			
			if ($position === 'before')
			{
				$before = $optionName;
				$after = null;
			}
			else
			{
				$before = null;
				$after = $optionName;
			}
			
			$str= (strcmp($value,$optionValue) === 0) ? "checked='checked'" : null;
			$returnString .= "$before<input $attributes ".$idStr." $strClass type='radio' name='".$name."' value='".$optionValue."' $str />$after\n";
		}
		
		return $returnString;
	}
	
}