/
FormBuilder.class.php
146 lines (94 loc) · 2.83 KB
/
FormBuilder.class.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
<?php
class FormBuilder extends ContentBuilder
{
protected static $debugging = false;
protected $require_processing_for_render = true;
protected $should_build_fields = true;
protected $data;
protected $processed = false;
protected $fields_built = false;
protected $fields_content;
protected $validates;
protected $validation_attempted = false;
protected $redirects;
public $messages;
public function __construct($template=false, $redirects=false)
{
if($template) $this->template = $template;
if($redirects) $this->redirects = $redirects;
}// constrcutor
public function process($data=false)
{
//echo 'DEBUG processing form with data<pre>';
//print_r($data);
//echo '</pre>';
if($data && is_array($data)) $this->data = $data;
else if(!isset($this->data) || !is_array($this->data)) $this->data = array();
// If validation not attempted, validate
if(!$this->validation_attempted)
{
if(isset($_REQUEST) && count($_REQUEST)) $this->data = array_merge($this->data, $_REQUEST);
//echo 'DEBUG data<pre>';
//print_r($this->data);
//echo '</pre>';
}
$this->processed = true;
}// process
public function hasProcessed()
{
return $this->processed;
}// hasProcessed
protected function redirect($redirects=false)
{
if(!$redirects)
{
if(isset($this->redirects) && count($this->redirects)) $redirects = $this->redirects;
else return false;
}
if($this->validation_attempted)
{
if($this->validates && isset($redirects['validates']))
{
header('Location: '.$redirects['validates']);
exit();
}
}
}// redirect
public function getData()
{
if((!isset($this->data) || !$this->data || !count($this->data)) && !$this->processed) $this->process();
return $this->data;
}// getData
public function setData($data)
{
$this->data = $data;
}// setData
public function addData($additional_data)
{
$this->data = array_merge($this->data, $additional_data);
}// addData
protected function buildFields($fields=false)
{
if(!$fields)
{
if(isset($this->fields)) $fields = $this->fields;
else return false;
}
$this->fields_built = true;
}// buildFields
public function setShouldBuildFields($should_build_fields)
{
$this->should_build_fields = $should_build_fields;
}// setShouldBuildFields
public function getContent($template=false, $additional_replacements=false)
{
if(!$template) $template = $this->template;
if($this->require_processing_for_render && !$this->processed) $this->process();
if($this->should_build_fields && !$this->fields_built) $this->buildFields();
$replacements = $this->getData();
if(is_array($additional_replacements)) $replacements = array_merge($replacements, $additional_replacements);
$content = parent::getContent($template, $replacements);
return $content;
}// getContent
}// class FormBuilder
?>