| 1 |
<?php |
| 2 |
abstract class Element extends Base { |
| 3 |
private $errors = array(); |
| 4 |
|
| 5 |
protected $attributes; |
| 6 |
protected $form; |
| 7 |
protected $label; |
| 8 |
protected $description; |
| 9 |
protected $validation = array(); |
| 10 |
protected $preHTML; |
| 11 |
protected $postHTML; |
| 12 |
protected $width; |
| 13 |
|
| 14 |
public function __construct($label, $name, array $properties = null) { |
| 15 |
$configuration = array( |
| 16 |
"label" => $label, |
| 17 |
"name" => $name |
| 18 |
); |
| 19 |
|
| 20 |
/*Merge any properties provided with an associative array containing the label |
| 21 |
and name properties.*/ |
| 22 |
if(is_array($properties)) |
| 23 |
$configuration = array_merge($configuration, $properties); |
| 24 |
|
| 25 |
$this->configure($configuration); |
| 26 |
} |
| 27 |
|
| 28 |
/*When an element is serialized and stored in the session, this method prevents any non-essential |
| 29 |
information from being included.*/ |
| 30 |
public function __sleep() { |
| 31 |
return array("attributes", "label", "validation"); |
| 32 |
} |
| 33 |
|
| 34 |
/*If an element requires external stylesheets, this method is used to return an |
| 35 |
array of entries that will be applied before the form is rendered.*/ |
| 36 |
public function getCSSFiles() {} |
| 37 |
|
| 38 |
public function getDescription() { |
| 39 |
return $this->description; |
| 40 |
} |
| 41 |
|
| 42 |
public function getErrors() { |
| 43 |
return $this->errors; |
| 44 |
} |
| 45 |
|
| 46 |
public function getID() { |
| 47 |
if(!empty($this->attributes["id"])) |
| 48 |
return $this->attributes["id"]; |
| 49 |
else |
| 50 |
return ""; |
| 51 |
} |
| 52 |
|
| 53 |
/*If an element requires external javascript file, this method is used to return an |
| 54 |
array of entries that will be applied after the form is rendered.*/ |
| 55 |
public function getJSFiles() {} |
| 56 |
|
| 57 |
public function getLabel() { |
| 58 |
return $this->label; |
| 59 |
} |
| 60 |
|
| 61 |
public function getName() { |
| 62 |
if(!empty($this->attributes["name"])) |
| 63 |
return $this->attributes["name"]; |
| 64 |
else |
| 65 |
return ""; |
| 66 |
} |
| 67 |
|
| 68 |
public function getPostHTML() { |
| 69 |
return $this->postHTML; |
| 70 |
} |
| 71 |
|
| 72 |
public function getPreHTML() { |
| 73 |
return $this->preHTML; |
| 74 |
} |
| 75 |
|
| 76 |
public function getWidth() { |
| 77 |
return $this->width; |
| 78 |
} |
| 79 |
|
| 80 |
/*This method provides a shortcut for checking if an element is required.*/ |
| 81 |
public function isRequired() { |
| 82 |
if(!empty($this->validation)) { |
| 83 |
foreach($this->validation as $validation) { |
| 84 |
if($validation instanceof Validation_Required) |
| 85 |
return true; |
| 86 |
} |
| 87 |
} |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
/*The isValid method ensures that the provided value satisfies each of the |
| 92 |
element's validation rules.*/ |
| 93 |
public function isValid($value) { |
| 94 |
$valid = true; |
| 95 |
if(!empty($this->validation)) { |
| 96 |
if(!empty($this->label)) { |
| 97 |
$element = $this->label; |
| 98 |
if(substr($element, -1) == ":") |
| 99 |
$element = substr($element, 0, -1); |
| 100 |
} |
| 101 |
else |
| 102 |
$element = $this->attributes["name"]; |
| 103 |
|
| 104 |
foreach($this->validation as $validation) { |
| 105 |
if(!$validation->isValid($value)) { |
| 106 |
/*In the error message, %element% will be replaced by the element's label (or |
| 107 |
name if label is not provided).*/ |
| 108 |
$this->errors[] = str_replace("%element%", $element, $validation->getMessage()); |
| 109 |
$valid = false; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
return $valid; |
| 114 |
} |
| 115 |
|
| 116 |
/*If an element requires jQuery, this method is used to include a section of javascript |
| 117 |
that will be applied within the jQuery(document).ready(function() {}); section after the |
| 118 |
form has been rendered.*/ |
| 119 |
public function jQueryDocumentReady() {} |
| 120 |
|
| 121 |
/*Elements that have the jQueryOptions property included (Date, Sort, Checksort, and Color) |
| 122 |
can make use of this method to render out the element's appropriate jQuery options.*/ |
| 123 |
public function jQueryOptions() { |
| 124 |
if(!empty($this->jQueryOptions)) { |
| 125 |
$options = ""; |
| 126 |
foreach($this->jQueryOptions as $option => $value) { |
| 127 |
if(!empty($options)) |
| 128 |
$options .= ", "; |
| 129 |
$options .= $option . ': '; |
| 130 |
/*When javascript needs to be applied as a jQuery option's value, no quotes are needed.*/ |
| 131 |
if(is_string($value) && substr($value, 0, 3) == "js:") |
| 132 |
$options .= substr($value, 3); |
| 133 |
else |
| 134 |
$options .= var_export($value, true); |
| 135 |
} |
| 136 |
echo "{ ", $options, " }"; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/*Many of the included elements make use of the <input> tag for display. These include the Hidden, Textbox, |
| 141 |
Password, Date, Color, Button, Email, and File element classes. The project's other element classes will |
| 142 |
override this method with their own implementation.*/ |
| 143 |
public function render() { |
| 144 |
echo '<input', $this->getAttributes(), '/>'; |
| 145 |
} |
| 146 |
|
| 147 |
/*If an element requires inline stylesheet definitions, this method is used send them to the browser before |
| 148 |
the form is rendered.*/ |
| 149 |
public function renderCSS() {} |
| 150 |
|
| 151 |
/*If an element requires javascript to be loaded, this method is used send them to the browser after |
| 152 |
the form is rendered.*/ |
| 153 |
public function renderJS() {} |
| 154 |
|
| 155 |
public function setClass($class) { |
| 156 |
if(!empty($this->attributes["class"])) |
| 157 |
$this->attributes["class"] .= " " . $class; |
| 158 |
else |
| 159 |
$this->attributes["class"] = $class; |
| 160 |
} |
| 161 |
|
| 162 |
public function setForm(Form $form) { |
| 163 |
$this->form = $form; |
| 164 |
} |
| 165 |
|
| 166 |
public function setID($id) { |
| 167 |
$this->attributes["id"] = $id; |
| 168 |
} |
| 169 |
|
| 170 |
public function setValue($value) { |
| 171 |
$this->attributes["value"] = $value; |
| 172 |
} |
| 173 |
|
| 174 |
public function setWidth($width) { |
| 175 |
if(substr($width, -2) == "px") |
| 176 |
$width = substr($width, 0, -2); |
| 177 |
elseif(substr($width, -1) == "%") |
| 178 |
$width = substr($width, 0, -1); |
| 179 |
$this->width = $width; |
| 180 |
} |
| 181 |
|
| 182 |
/*This method provides a shortcut for applying the Required validation class to an element.*/ |
| 183 |
public function setRequired($required) { |
| 184 |
if(!empty($required)) |
| 185 |
$this->validation[] = new Validation_Required; |
| 186 |
} |
| 187 |
|
| 188 |
/*This method applies one or more validation rules to an element. If can accept a single concrete |
| 189 |
validation class or an array of entries.*/ |
| 190 |
public function setValidation($validation) { |
| 191 |
/*If a single validation class is provided, an array is created in order to reuse the same logic.*/ |
| 192 |
if(!is_array($validation)) |
| 193 |
$validation = array($validation); |
| 194 |
foreach($validation as $object) { |
| 195 |
/*Ensures $object contains a existing concrete validation class.*/ |
| 196 |
if($object instanceof Validation) |
| 197 |
$this->validation[] = $object; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|