This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Element.php
216 lines (180 loc) · 6.58 KB
/
Element.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
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
209
210
211
212
213
214
215
216
<?php
/**
* @package Arastta Form Component
* @copyright Copyright (C) 2015 Arastta Association. All rights reserved. (arastta.org)
* @copyright Copyright (C) 2009-2013 Luke Korth
* @license GNU General Public License version 3; see LICENSE.txt
*/
namespace Arastta\Component\Form;
abstract class Element extends Base
{
protected $errors = array();
protected $attributes = array();
protected $form;
protected $label;
protected $shortDesc;
protected $longDesc;
protected $validation = array();
public function __construct($label, $name, array $properties = null)
{
$configuration = array(
"label" => $label,
"name" => $name
);
/*Merge any properties provided with an associative array containing the label
and name properties.*/
if (is_array($properties)) {
$configuration = array_merge($configuration, $properties);
}
$this->configure($configuration);
}
/*When an element is serialized and stored in the session, this method prevents any non-essential
information from being included.*/
public function __sleep()
{
return array("attributes", "label", "validation");
}
/*If an element requires external stylesheets, this method is used to return an
array of entries that will be applied before the form is rendered.*/
public function getCSSFiles()
{
}
public function getErrors()
{
return $this->errors;
}
/*If an element requires external javascript file, this method is used to return an
array of entries that will be applied after the form is rendered.*/
public function getJSFiles()
{
}
public function getLabel()
{
return $this->label;
}
public function getLongDesc()
{
return $this->longDesc;
}
/*This method provides a shortcut for checking if an element is required.*/
public function isRequired()
{
if (!empty($this->validation)) {
foreach ($this->validation as $validation) {
if ($validation instanceof Validation\Required) {
return true;
}
}
}
return false;
}
public function getShortDesc()
{
return $this->shortDesc;
}
/*The isValid method ensures that the provided value satisfies each of the
element's validation rules.*/
public function isValid($value)
{
$valid = true;
if (!empty($this->validation)) {
if (!empty($this->label)) {
$element = $this->label;
} elseif (!empty($this->attributes["placeholder"])) {
$element = $this->attributes["placeholder"];
} else {
$element = $this->attributes["name"];
}
if (substr($element, -1) == ":") {
$element = substr($element, 0, -1);
}
foreach ($this->validation as $validation) {
if (!$validation->isValid($value)) {
/*In the error message, %element% will be replaced by the element's label (or
name if label is not provided).*/
$this->errors[] = str_replace("%element%", $element, $validation->getMessage());
$valid = false;
}
}
}
return $valid;
}
/*If an element requires jQuery, this method is used to include a section of javascript
that will be applied within the jQuery(document).ready(function() {}); section after the
form has been rendered.*/
public function jQueryDocumentReady()
{
}
/*Elements that have the jQueryOptions property included (Date, Sort, Checksort, and Color)
can make use of this method to render out the element's appropriate jQuery options.*/
public function jQueryOptions()
{
if (!empty($this->jQueryOptions)) {
$options = "";
foreach ($this->jQueryOptions as $option => $value) {
if (!empty($options)) {
$options .= ", ";
}
$options .= $option . ': ';
/*When javascript needs to be applied as a jQuery option's value, no quotes are needed.*/
if (is_string($value) && substr($value, 0, 3) == "js:") {
$options .= substr($value, 3);
} else {
$options .= var_export($value, true);
}
}
echo "{ ", $options, " }";
}
}
/*Many of the included elements make use of the <input> tag for display. These include the Hidden, Textbox,
Password, Date, Color, Button, Email, and File element classes. The project's other element classes will
override this method with their own implementation.*/
public function getInput()
{
return '<input' . $this->getAttributes() . '/>';
}
/*If an element requires inline stylesheet definitions, this method is used send them to the browser before
the form is rendered.*/
public function renderCSS()
{
}
/*If an element requires javascript to be loaded, this method is used send them to the browser after
the form is rendered.*/
public function renderJS()
{
}
public function setForm(Form $form)
{
$this->form = $form;
}
public function setLabel($label)
{
$this->label = $label;
}
/*This method provides a shortcut for applying the Required validation class to an element.*/
public function setRequired($required)
{
if (!empty($required)) {
$this->validation[] = new Validation\Required;
}
$this->attributes["required"] = "";
}
/*This method applies one or more validation rules to an element. If can accept a single concrete
validation class or an array of entries.*/
public function setValidation($validation)
{
/*If a single validation class is provided, an array is created in order to reuse the same logic.*/
if (!is_array($validation)) {
$validation = array($validation);
}
foreach ($validation as $object) {
/*Ensures $object contains a existing concrete validation class.*/
if ($object instanceof Validation) {
$this->validation[] = $object;
if ($object instanceof Validation\Required) {
$this->attributes["required"] = "";
}
}
}
}
}