-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformkons.php
166 lines (138 loc) · 3.9 KB
/
formkons.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
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
<?php
include_once 'khelpers.php';
include_once 'kelements.php';
class Formkons {
var $method;
var $attributes = array();
var $elements;
var $errors = false;
var $global_wrapper = false;
private $submitted = false;
function __construct($config=false) {
if(is_array($config)) {
$this->attributes = array_merge($this->attributes,$config);
}
if(empty($config['method'])) $this->method = "get";
else $this->method = "post";
$this->submitted = $this->is_submitted($this->method);
}
/**
*
* @param type $element
* @param type $id
* @param type $attr either an array of attributes or input type
* @return type
*/
public function add_element($element, $id, $attr=false)
{
$element = strtolower(trim($element));
if($element == "input") {
if(is_array($attr)) {
if(empty($attr['type'])) $attr['type'] = "text";
}
else $attr = array( "type" => empty($attr)?"text":$attr);
if($attr['type'] == "checkbox" || $attr['type'] == "radio") $element = "checkbox";//checkboxes and radios need a separate class
if($attr['type'] == "file") {
$element = "upload";
$this->add_attribute("enctype", "multipart/form-data");
}
}
$classname = "K_".$element;
$this->elements[$id] = new $classname($id,$attr);
if($this->global_wrapper !== false)
$this->elements[$id]->set_wrapper($this->global_wrapper['tag'], $this->global_wrapper['attr']);
return $this->elements[$id];
}
/**
* Add a single attribute to the opening form tag
* @param type $name
* @param type $value
*/
public function add_attribute($name, $value) {
$this->attributes[$name] = $value;
}
/**
* Global upload config. ??
* @param type $config
*/
public function configure_upload($config) {
}
/**
* Checks if form is submitted and populates elements with values.
* @return boolean
*/
public function submitted_and_valid() {
if($this->submitted) {
foreach($this->elements as $id => $element) {
$element->set_value($element->get_value($this->method));
if($element->has_errors()) $this->errors = true;
}
return !$this->errors;
}
else return false;
}
/**
* Returns the array of all form values. Non declared values (values of elements
* not set by the formkons) are ignored.
* @return type
*/
public function values_array() {
$result = array();
foreach($this->elements as $id => $element) {
$result[$id] = $element->value;
}
return $result;
}
/**
* Convenience method for outputing the form with a one liner.
* @return string
*/
public function html() {
$out = $this->form_open();
foreach($this->elements as $id => $element)
{
$out .= $element->html(true);
}
$out .= "</form>";
return $out;
}
/**
* Create the opening FORM tag with all attributes.
* You are required to use this function if you are outputing the form in a
* view (not using the above html() method)
*/
public function form_open() {
$out = "<form";
$out .= write_attributes($this->attributes);
$out .= ">";
return $out;
}
/**
* This is just to be consistent in your view files.
* @return string
*/
public function form_close() {
return "</form>";
}
/**
* You can set a global wrapper tag for all elements. It's a convenience. Use
* with care!
* @param type $tag
* @param type $attributes
*/
public function set_global_wrapper($tag,$attributes=array()) {
$this->global_wrapper = array('tag'=> $tag,'attr' => $attributes);
}
/**
* This function is private and used by the constructor that needs(?) to know
* if the form is submitted in advance. Do not mix with public function submitted_and_valid()
* that actually runs all the validation and value abstraction processes.
* @param type $method
* @return boolean
*/
private function is_submitted($method) {
if($method=="post" && (!empty($_POST))) return true;
if($method=="get" && (!empty($_GET))) return true;
return false;
}
}