Skip to content
This repository was archived by the owner on Jan 4, 2020. It is now read-only.

Commit 5338f9c

Browse files
author
ajporterfield@gmail.com
committed
Added 3.x-php5.3 to trunk and 3.x-php5 as new branch
git-svn-id: http://php-form-builder-class.googlecode.com/svn/trunk@570 18c9a15c-d092-11de-bb02-e302c5db49d5
1 parent 25616b6 commit 5338f9c

File tree

676 files changed

+64360
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

676 files changed

+64360
-0
lines changed

PFBC/Base.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
namespace PFBC;
3+
4+
abstract class Base {
5+
public function configure(array $properties = null) {
6+
if(!empty($properties)) {
7+
$class = get_class($this);
8+
9+
/*The property_reference lookup array is created so that properties can be set
10+
case-insensitively.*/
11+
$available = array_keys(get_class_vars($class));
12+
$property_reference = array();
13+
foreach($available as $property)
14+
$property_reference[strtolower($property)] = $property;
15+
16+
/*The method reference lookup array is created so that "set" methods can be called
17+
case-insensitively.*/
18+
$available = get_class_methods($class);
19+
$method_reference = array();
20+
foreach($available as $method)
21+
$method_reference[strtolower($method)] = $method;
22+
23+
foreach($properties as $property => $value) {
24+
$property = strtolower($property);
25+
/*The attributes property cannot be set directly.*/
26+
if($property != "attributes") {
27+
/*If the appropriate class has a "set" method for the property provided, then
28+
it is called instead or setting the property directly.*/
29+
if(isset($method_reference["set" . $property]))
30+
$this->$method_reference["set" . $property]($value);
31+
elseif(isset($property_reference[$property]))
32+
$this->$property_reference[$property] = $value;
33+
/*Entries that don't match an available class property are stored in the attributes
34+
property if applicable. Typically, these entries will be element attributes such as
35+
class, value, onkeyup, etc.*/
36+
elseif(isset($property_reference["attributes"]))
37+
$this->attributes[$property] = $value;
38+
}
39+
}
40+
}
41+
return $this;
42+
}
43+
44+
/*This method can be used to view a class' state.*/
45+
public function debug() {
46+
echo "<pre>", print_r($this, true), "</pre>";
47+
}
48+
49+
/*This method prevents double/single quotes in html attributes from breaking the markup.*/
50+
protected function filter($str) {
51+
return str_replace('"', '&quot;', $str);
52+
}
53+
54+
/*This method is used by the Form class and all Element classes to return a string of html
55+
attributes. There is an ignore parameter that allows special attributes from being included.*/
56+
public function getAttributes($ignore = "") {
57+
$str = "";
58+
if(!empty($this->attributes)) {
59+
if(!is_array($ignore))
60+
$ignore = array($ignore);
61+
$attributes = array_diff(array_keys($this->attributes), $ignore);
62+
foreach($attributes as $attribute) {
63+
$str .= ' ' . $attribute;
64+
if($this->attributes[$attribute] !== "")
65+
$str .= '="' . $this->filter($this->attributes[$attribute]) . '"';
66+
}
67+
}
68+
return $str;
69+
}
70+
}
71+
?>

PFBC/Element.php

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

PFBC/Element/Button.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace PFBC\Element;
3+
4+
class Button extends \PFBC\Element {
5+
protected $attributes = array("type" => "submit", "value" => "Submit");
6+
protected $icon;
7+
8+
public function __construct($label = "Submit", $type = "", array $properties = null) {
9+
if(!is_array($properties))
10+
$properties = array();
11+
12+
if(!empty($type))
13+
$properties["type"] = $type;
14+
15+
$class = "btn";
16+
if(empty($type) || $type == "submit")
17+
$class .= " btn-primary";
18+
$properties["class"] = $class;
19+
20+
if(empty($properties["value"]))
21+
$properties["value"] = $label;
22+
23+
parent::__construct("", "", $properties);
24+
}
25+
}

PFBC/Element/CKEditor.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace PFBC\Element;
3+
4+
class CKEditor extends Textarea {
5+
protected $basic;
6+
7+
function renderJS() {
8+
echo 'CKEDITOR.replace("', $this->attributes["id"], '"';
9+
if(!empty($this->basic))
10+
echo ', { toolbar: "Basic" }';
11+
echo ');';
12+
13+
$ajax = $this->form->getAjax();
14+
$id = $this->form->getID();
15+
if(!empty($ajax)) {
16+
echo <<<JS
17+
jQuery("#$id").bind("submit", function() {
18+
CKEDITOR.instances["{$this->attributes["id"]}"].updateElement();
19+
});
20+
JS;
21+
}
22+
}
23+
24+
function getJSFiles() {
25+
return array(
26+
$this->form->getResourcesPath() . "/ckeditor/ckeditor.js"
27+
);
28+
}
29+
}

PFBC/Element/Captcha.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace PFBC\Element;
3+
4+
class Captcha extends \PFBC\Element {
5+
protected $privateKey = "6LcazwoAAAAAAD-auqUl-4txAK3Ky5jc5N3OXN0_";
6+
protected $publicKey = "6LcazwoAAAAAADamFkwqj5KN1Gla7l4fpMMbdZfi";
7+
8+
public function __construct($label = "", array $properties = null) {
9+
parent::__construct($label, "recaptcha_response_field", $properties);
10+
}
11+
12+
public function render() {
13+
$this->validation[] = new \PFBC\Validation\Captcha($this->privateKey);
14+
require_once(__DIR__ . "/../Resources/recaptchalib.php");
15+
echo recaptcha_get_html($this->publicKey);
16+
}
17+
}

PFBC/Element/Checkbox.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace PFBC\Element;
3+
4+
class Checkbox extends \PFBC\OptionElement {
5+
protected $attributes = array("type" => "checkbox");
6+
protected $inline;
7+
8+
public function render() {
9+
if(isset($this->attributes["value"])) {
10+
if(!is_array($this->attributes["value"]))
11+
$this->attributes["value"] = array($this->attributes["value"]);
12+
}
13+
else
14+
$this->attributes["value"] = array();
15+
16+
if(substr($this->attributes["name"], -2) != "[]")
17+
$this->attributes["name"] .= "[]";
18+
19+
$labelClass = $this->attributes["type"];
20+
if(!empty($this->inline))
21+
$labelClass .= " inline";
22+
23+
$count = 0;
24+
foreach($this->options as $value => $text) {
25+
$value = $this->getOptionValue($value);
26+
if(!empty($this->inline) && $count > 0)
27+
echo ' ';
28+
echo '<label class="', $labelClass, '"><input id="', $this->attributes["id"], '-', $count, '"', $this->getAttributes(array("id", "value", "checked", "required")), ' value="', $this->filter($value), '"';
29+
if(in_array($value, $this->attributes["value"]))
30+
echo ' checked="checked"';
31+
echo '/>', $text, '</label>';
32+
++$count;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)