-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.form.php
executable file
·116 lines (101 loc) · 3.92 KB
/
class.form.php
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
<?php
/**
* HTML FORM design pattern - Generates all HTML FORM elements
*
* <p>This class contains the methods for generating forms snd related elements so we do not
* have to poison our code with HTML
*
* @author Peter Lorimer <[email protected]>
* @license Interleaf
* @copyright Interleaf Technology Ltd
*
* Date: 22 July 2011
*/
class HTML_Form {
/**
* Class to implement an HTML FORM design pattern
*
* @package HTML_Form
*/
public static $MONTHS_LONG = array('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December');
public function startForm($action = '#', $method = 'post', $id = NULL, $attr_ar = array() ) {
$str = "<form action=\"$action\" method=\"$method\"";
if ( isset($id) ) {
$str .= " id=\"$id\"";
}
$str .= $attr_ar? $this->addAttributes( $attr_ar ) . '>': '>';
return $str;
}
private function addAttributes( $attr_ar ) {
$str = '';
// check minimized attributes
$min_atts = array('checked', 'disabled', 'readonly', 'multiple');
foreach( $attr_ar as $key=>$val ) {
if ( in_array($key, $min_atts) ) {
if ( !empty($val) ) {
$str .= " $key=\"$key\"";
}
} else {
$str .= " $key=\"$val\"";
}
}
return $str;
}
public function addInput($type, $name, $value, $attr_ar = array() ) {
$str = "<input type=\"$type\" name=\"$name\" value=\"$value\"";
if ($attr_ar) {
$str .= $this->addAttributes( $attr_ar );
}
$str .= ' />';
return $str;
}
public function addTextarea($name, $rows = 4, $cols = 30, $value = '', $attr_ar = array() ) {
$str = "<textarea name=\"$name\" rows=\"$rows\" cols=\"$cols\"";
if ($attr_ar) {
$str .= $this->addAttributes( $attr_ar );
}
$str .= ">$value</textarea>";
return $str;
}
// for attribute refers to id of associated form element
public function addLabelFor($forID, $text, $attr_ar = array() ) {
$str = "<label for=\"$forID\"";
if ($attr_ar) {
$str .= $this->addAttributes( $attr_ar );
}
$str .= ">$text</label>";
return $str;
}
// from parallel arrays for option values and text
public function addSelectListArrays($name, $val_list, $txt_list, $selected_value = NULL, $header = NULL, $attr_ar = array() ) {
$option_list = array_combine( $val_list, $txt_list );
$str = $this->addSelectList($name, $option_list, true, $selected_value, $header, $attr_ar );
return $str;
}
// option values and text come from one array (can be assoc)
// $bVal false if text serves as value (no value attr)
public function addSelectList($name, $option_list, $bVal = true, $selected_value = NULL, $header = NULL, $attr_ar = array() ) {
$str = "<select name=\"$name\"";
if ($attr_ar) {
$str .= $this->addAttributes( $attr_ar );
}
$str .= ">\n";
if ( isset($header) ) {
$str .= " <option value=\"\">$header</option>\n";
}
foreach ( $option_list as $val => $text ) {
$str .= $bVal? " <option value=\"$val\"": " <option";
if ( isset($selected_value) && ( $selected_value === $val || $selected_value === $text) ) {
$str .= ' selected="selected"';
}
$str .= ">$text</option>\n";
}
$str .= "</select>";
return $str;
}
public function endForm() {
return "</form>";
}
}
?>