PageRenderTime 718ms CodeModel.GetById 193ms app.highlight 393ms RepoModel.GetById 101ms app.codeStats 0ms

/zan/helpers/forms.php

https://github.com/triartdesign/ZanPHP
PHP | 431 lines | 428 code | 0 blank | 3 comment | 1 complexity | edf670a7b5ccac2316ac4b99c7ab23a6 MD5 | raw file
  1<?php
  2/**
  3 * Access from index.php:
  4 */
  5if(!defined("_access")) {
  6	die("Error: You don't have permission to access here...");
  7}
  8
  9/**
 10 * Forms Helper
 11 *
 12 * 
 13 *
 14 * @package		ZanPHP
 15 * @subpackage	Core
 16 * @category	Helpers 
 17 * @author		MilkZoft
 18 * @link		http://www.milkzoft.com
 19 */
 20
 21/**
 22 * formCheckbox
 23 * 
 24 * Sets a specific <input /> type Checkbox tag and its attributes
 25 * 
 26 * @param string  $text     = NULL
 27 * @param string  $position = "Right"
 28 * @param string  $name
 29 * @param string  $value
 30 * @param string  $ID       = NULL
 31 * @param boolean $checked  = FALSE
 32 * @param string  $events   = NULL
 33 * @param boolean $disabled = FALSE
 34 * @return string value
 35 */		
 36function formCheckbox($attributes = FALSE) {
 37	if(isset($attributes) and is_array($attributes)) {
 38		$attrs = NULL;
 39		
 40		foreach($attributes as $attribute => $value) {
 41			if($attribute !== "position" and $attribute !== "text" and $attribute !== "type" and $attribute !== "checked") {
 42				$attrs .= ' '. strtolower($attribute) .'="'. encode($value) .'"';
 43			} else {
 44				$$attribute = encode($value);
 45			}
 46		}
 47
 48		if(isset($checked) and $checked) {
 49			$check = ' checked="checked"';
 50		} else {
 51			$check = NULL;
 52		}
 53		
 54		if(isset($position) and $position === "left" and isset($text)) {
 55			return $text .' <input'. $attrs .' type="checkbox"'. $check .' />';
 56		} elseif(isset($position) and $position === "right" and isset($text)) {
 57			return '<input'. $attrs .' type="checkbox"'. $check .' /> '. $text;
 58		} elseif(isset($text)) {
 59			return $text .' <input'. $attrs .' type="checkbox"'. $check .' />';
 60		} else {
 61			return '<input'. $attrs .' type="checkbox"'. $check .' />';
 62		}
 63	} else {
 64		return NULL;
 65	}
 66}
 67
 68/**
 69 * formClose
 70 * 
 71 * Closes a Basic Form structure
 72 * 
 73 * @returns string $HTML
 74 */	
 75function formClose() {
 76	$HTML  = "\t" . "</fieldset>" . "\n";
 77	$HTML .= "</form>";		
 78	
 79	return $HTML;
 80}
 81
 82function formField($a = NULL, $text, $raquo = TRUE) {
 83	$raquo = ($raquo === TRUE) ? "&raquo; " : "";
 84	
 85	if(!is_null($a)) {
 86		$HTML  = '<p class="field">' . "\n";
 87		$HTML .= "\t" . '<a '. $a .'>'. $raquo . $text .'</a>' . "\n";
 88		$HTML .= '</p>' . "\n";
 89	} else {
 90		$HTML  = '<p class="field">' . "\n";
 91		$HTML .= "\t" . $raquo . $text . "\n"; 
 92		$HTML .= '</p>' . "\n";
 93	}
 94	
 95	return $HTML;
 96}	
 97	
 98/**
 99 * formInput
100 * 
101 * Sets an <input /> tag with a custom attributes.
102 * 
103 * @param mixed   $p      = "Yes"
104 * @param string  $text   = NULL
105 * @param string  $name   = NULL
106 * @param string  $value  = NULL
107 * @param string  $class  = "input"
108 * @param string  $type   = "text"
109 * @param string  $ID     = NULL
110 * @param string  $events = NULL
111 * @param string  $src    = NULL
112 * @param boolean $raquo  = NULL
113 * @returns string $HTML
114 */		 
115function formInput($attributes = FALSE) {
116	if(isset($attributes) and is_array($attributes)) {
117		$attrs = NULL;
118		
119		foreach($attributes as $attribute => $value) {
120			if($attribute === "required") {
121				$attrs .= ' required ';
122			} elseif($attribute === "events") {
123				$attrs .= ' '. $value .' ';
124			} elseif($attribute !== "type" and $attribute !== "p" and $attribute !== "field") {
125				$attrs .= ' '. strtolower($attribute) .'="'. $value .'"';
126			} else {
127				$$attribute = $value;
128			}
129		}
130		
131		if(isset($type)) {
132			if($type === "text") {
133				$HTML = '<input'. $attrs .' type="text" /> ' . "\n";
134			} elseif($type === "password") {
135				$HTML = '<input'. $attrs .' type="password" /> ' . "\n";
136			} elseif($type === "submit") {
137				$HTML = '<input'. $attrs .' type="submit" /> ' . "\n";
138			} elseif($type === "button") {
139				$HTML = '<input'. $attrs .' type="button" /> ' . "\n";
140			} elseif($type === "checkbox") {
141				$HTML = '<input'. $attrs .' type="checkbox" /> ' . "\n";
142			} elseif($type === "radio") {
143				$HTML = '<input'. $attrs .' type="radio" /> ' . "\n";
144			} elseif($type === "file") {
145				$HTML = '<input'. $attrs .' type="file" /> ' . "\n";
146			} elseif($type === "hidden") {
147				$HTML = '<input'. $attrs .' type="hidden" /> ' . "\n";
148			} elseif($type === "image") {
149				$HTML = '<input'. $attrs .' type="image" /> ' . "\n";
150			} elseif($type === "reset") {
151				$HTML = '<input'. $attrs .' type="reset" /> ' . "\n";
152			} else {
153				$HTML = '<input'. $attrs .' type="text" /> ' . "\n";
154			}
155		} else {
156			$HTML = '<input'. $attrs .' type="text" /> ' . "\n";
157		}
158
159		if(isset($p) and $p and isset($field)) {
160			$HTML = '	<p>
161							<span class="field">&raquo; '. $field .'</span><br />
162							'. $HTML .'
163						</p>';
164		} elseif(isset($p) and $p) {
165			$HTML = '	<p>
166							'. $HTML .'
167						</p>';
168		} elseif(isset($field)) {
169			$HTML = '<span class="field">&raquo; '. $field .'</span><br />'. $HTML .'';
170		}
171
172		return $HTML;
173	} elseif($attributes) {
174		return '<input name="'. $attributes .'" type="text" />' . "\n";
175	} else {
176		return NULL;	
177	}
178}
179
180/**
181 * formLabel
182 * 
183 * Sets a <label> tag.
184 * 
185 * @param string  $for
186 * @param string  $value
187 * @param boolean $br = TRUE
188 * @returns string $HTML
189 */
190function formLabel($for, $text, $br = TRUE) {
191	$HTML = "<label for=\"$for\">$text: </label>";
192	
193	if($br == TRUE) {
194		$HTML .= "<br />" . "\n";
195	}
196	
197	return $HTML;
198}
199
200/**
201 * formOpen
202 * 
203 * Sets and Opens a basic form structure
204 *
205 * @param string $ID      = NULL
206 * @param string $text    = NULL
207 * @param string $action
208 * @param string $class   = "forms"
209 * @param string $method  = "post"
210 * @param string $enctype = "multipart/form-data"
211 * @returns string $HTML
212 */	
213function formOpen($action = NULL, $class = "forms", $ID = NULL, $legend = NULL, $method = "post", $enctype = "multipart/form-data") {	
214	$ID     = (isset($ID))     ? ' id="'. $ID .'"' 			  			 : NULL;
215	$legend = (isset($legend)) ? "<legend>$legend</legend>" . "\n" : NULL;
216	$action = (strstr($action, "http://")) ? $action : get("webBase") . "/" . $action;
217	
218	$HTML  = '<form'. $ID .' action="'. $action .'" method="'. $method .'" class="'. $class .'" enctype="'. $enctype .'">' . "\n\t";
219	$HTML .= '<fieldset>' . "\n\t\t";
220	$HTML .= $legend . "\n";			
221
222	return $HTML;
223}
224
225/**
226 * formRadio
227 * 
228 * Sets a <input /> type Radio tag and its attributes 
229 *
230 * @param string  $text     = NULL
231 * @param string  $position = "Right"
232 * @param string  $name
233 * @param string  $value
234 * @param string  $ID       = NULL
235 * @param boolean $checked  = FALSE
236 * @param string  $events   = NULL
237 * @param boolean $disable  = FALSE 
238 * @returns string value
239 */	
240function formRadio($attributes, $options = FALSE) {
241	if(isset($attributes) and is_array($attributes)) {
242		$attrs = NULL;
243		
244		foreach($attributes as $attr => $value) {
245			if($attr !== "position" and $attr !== "text" and $attr !== "type" and $attr !== "p" and $attr !== "field" and $attr !== "checked") {
246				$attrs .= ' '. strtolower($attr) .'="'. $value .'"';
247			} else {
248				$$attr = $value;
249			}
250		}
251		
252		if(is_array($options)) {
253			$HTML = NULL;
254
255			foreach($options as $option) {
256				if(is_array($option)) { 
257					foreach($option as $attribute) {
258						if($attribute["default"]) {
259							$check = ' checked="checked"';
260						} else {
261							$check = NULL;	
262						}
263
264						$HTML .= ' <input '. $attrs .' value="'. $attribute["name"] .'" type="radio"'. $check .' />'. $attribute["value"];
265					}					
266				}	
267			}
268		} else {
269			if(isset($checked) and $checked) {
270				$check = ' checked="checked"';
271			} else {
272				$check = NULL;
273			}
274
275			if(isset($position) and $position === "left" and isset($text)) {
276				$HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
277			} elseif(isset($position) and $position === "right" and isset($text)) {
278				$HTML = '<input'. $attrs .' type="radio"'. $check .' /> '. $text;
279			} elseif(isset($text)) {
280				$HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
281			} else {
282				$HTML = '<input'. $attrs .' type="radio"'. $check .' />';
283			}	
284		}
285		
286		if(isset($p) and isset($field)) {
287			$HTML = '	<p>
288							<span class="field">&raquo; '. $field .'</span><br />
289							'. $HTML .'
290						</p>';
291
292		}
293
294		return $HTML;
295	} else {
296		return NULL;
297	}
298}	
299
300/**
301 * formSelect
302 * 
303 * Sets a <select> tag and its attributes
304 *
305 * @param boolean $p        = TRUE
306 * @param string  $text
307 * @param string  $name
308 * @param mixed   $options
309 * @param string  $class    = "Select"
310 * @param string  $selected = NULL
311 * @param string  $ID       = NULL
312 * @param string  $size     = "1"
313 * @param boolean $raquo    = TRUE 
314 * @returns string $HTML
315 */	
316function formSelect($attributes = FALSE, $options = FALSE, $select = FALSE) {
317	if(isset($attributes) and is_array($attributes)) {
318		$attrs = NULL;
319		
320		foreach($attributes as $attribute => $value) {
321			if($attribute !== "p" and $attribute !== "field") {
322				$attrs .= ' '. strtolower($attribute) .'="'. $value .'"';
323			} else {
324				$$attribute = $value;
325			}
326		}
327		
328		$HTML = "\t" . '<select'. $attrs .' size="1">'. "\n";
329		
330		if(is_array($options)) {
331			foreach($options as $option) {
332				if($select) {
333					$HTML .= $select;
334					
335					$select = FALSE;
336				}
337				
338				if(is_string($option)) {
339					$HTML .= "\t\t" . '<option>'. $option .'</option>' . "\n";	
340				} else {
341					$selected = (isset($option["selected"]) and $option["selected"]) ? ' selected="selected"' : NULL;
342					$value    = (isset($option["value"]))  ? $option["value"]  : NULL;
343					$text 	  = (isset($option["option"])) ? $option["option"] : NULL;
344					
345					$HTML .= "\t\t" . '<option value="' . $value . '"' . $selected . '>' . $text . '</option>' . "\n";		
346				}			
347			}
348		}
349		
350		$HTML .= "\t" . '</select>' . "\n";
351		
352		unset($options);
353
354		if(isset($p) and isset($field)) {
355			$HTML = '	<p>
356							<span class="field">&raquo; '. $field .'</span><br />
357							'. $HTML .'
358						</p>';
359
360		}
361		
362		return $HTML;
363	} else {
364		return NULL;
365	}
366}	
367
368/**
369 * formTextarea
370 * 
371 * Sets a <textarea> tag and its attributes
372 *
373 * @param boolean $p        = TRUE
374 * @param string  $text
375 * @param string  $name
376 * @param string  $value    = NULL
377 * @param string  $class    = "textarea"
378 * @param string  $selected = NULL
379 * @param string  $ID       = NULL
380 * @param int     $rows     = 25
381 * @param int     $cols     = 90 
382 * @param boolean $raquo    = TRUE
383 * @returns string $HTML
384 */	
385function formTextarea($attributes = FALSE) {
386	if(isset($attributes) and is_array($attributes)) {
387		$attrs = NULL;
388		
389		foreach($attributes as $attribute => $val) {
390			if($attribute !== "type" and $attribute !== "value" and $attribute !== "p" and $attribute !== "field") {
391				$attrs .= ' '. strtolower($attribute) .'="'. $val .'"';
392			} else {
393				$$attribute = $val;
394			}
395		}
396		
397		$value = isset($value) ? $value : NULL;
398		
399		$HTML = '<textarea'. $attrs .'>'. $value .'</textarea>';
400
401		if(isset($p) and isset($field)) {
402			$HTML = '	<p>
403							<span class="field">&raquo; '. $field .'</span><br />
404							'. $HTML .'
405						</p>';
406
407		}
408
409		return $HTML;
410	} else {
411		return NULL;
412	}								
413}
414
415function formSave($action = NULL) {
416	if($action === "save") {
417		$href = path(segment(0, isLang()) ."/cpanel/add/");
418	} else {
419		$href = path(segment(0, isLang()) ."/cpanel/edit/". segment(3, isLang()));
420	} 
421	
422	$onclick = 'onclick="document.getElementById(\'form-add\').target=\'\'; document.getElementById(\'form-add\').action=\''. $href .'\'"';
423	
424	$HTML = '	
425		<p class="save-cancel">
426			<input id="'. $action .'" name="'. $action .'" value="'. __(ucfirst($action)) .'" '. $onclick .' type="submit" class="btn btn-success">
427			<input id="cancel" name="cancel" value="'. __("Cancel") .'" type="submit" class="btn btn-danger" />
428		</p>';
429	
430	return $HTML;
431}