123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- <?php
- /**
- * The root element class. All other form elements must inherit from this class.
- *
- * @author Ironpilot
- * @copyright Copywrite (c) 2011, STAPLE CODE
- *
- * This file is part of the STAPLE Framework.
- *
- * The STAPLE Framework is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your option)
- * any later version.
- *
- * The STAPLE Framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
- */
- abstract class Staple_Form_Element
- {
- /**
- * Name of the field on the form.
- * @var string
- */
- protected $name;
- /**
- * Form field's label.
- * @var string
- */
- protected $label;
- /**
- * The field's current value.
- * @var string
- */
- protected $value;
- /**
- * An array of HTML classes for the form field.
- * @var array
- */
- protected $classes = array();
- /**
- * Form field ID, uses the form name if the ID is not specified.
- * @var string
- */
- protected $id;
- /**
- * Holds instructions for completing the form field.
- * @var string
- */
- protected $instructions;
- /**
- * An array that holds extra HTML attributes.
- * @var array
- */
- protected $attrib = array();
- /**
- * Sets the field to be required.
- * @var boolean
- */
- protected $required = false;
- /**
- * Holds the true|false readOnly value.
- * @var boolean
- */
- protected $readOnly = false;
-
- /**
- * Hold the true|false disabled value.
- * @var boolean
- */
- protected $disabled = false;
- /**
- * An array of filters that will be applied to any form values.
- * @var array
- */
- protected $filters = array();
- /**
- * An array that holds the validator objects assigned to the form field.
- * @var array
- */
- protected $validators = array();
- /**
- * An array that holds the validation error messages.
- * @var array
- */
- protected $errors = array();
-
- /**
- * Form field constructor. Requires a name, has an optional label, id and attribute array.
- *
- * @param string $name
- * @param string $label
- * @param string $id
- * @param array $attrib
- */
- public function __construct($name, $label = NULL, $id = NULL, array $attrib = array())
- {
- $this->setName($name); //Name for the form field
- $this->setLabel($label); //Label that the user sees
- if(isset($id)) //ID for the form field
- {
- $this->setId($id);
- }
- else
- {
- $this->setId($name);
- }
- foreach($attrib as $key=>$value) //Additional HTML Attributes
- {
- $this->addAttrib($key, $value);
- }
- }
- /**
- * Class overload either calls a getter for a protected value or gets the value from the attribute array.
- *
- * @param string $name
- * @return null|string
- */
- public function __get($name)
- {
- $method = 'get'.$name;
- if(method_exists(get_class($this), $method))
- {
- return $this->$method();
- }
- else
- {
- if(array_key_exists($name,$this->attrib))
- {
- return $this->attrib[$name];
- }
- else
- {
- return NULL;
- }
- }
- }
-
- /**
- * Class overload either calls a setter for a protected value or sets that value within the attributes array.
- *
- * @param string $name
- * @param string $value
- */
- public function __set($name, $value)
- {
- $method = 'set'.$name;
- if(method_exists(get_class($this), $method))
- {
- $this->$method($value);
- }
- else
- {
- $this->attrib[$name] = $value;
- }
- }
-
- /**
- * Calls the field->build() function.
- */
- public function __toString()
- {
- try{
- return $this->build();
- }
- catch (Exception $e)
- {
- return '<p>Field Error...</p>';
- }
- }
-
- /**
- * Clone validators and filters so that they don't overlap;
- */
- public function __clone()
- {
- $vals = array();
- foreach($this->validators as $key=>$val)
- {
- $vals[$key] = clone $val;
- }
- $this->validators = $vals;
-
- $filts = array();
- foreach($this->filters as $key=>$fil)
- {
- $filts[$key] = clone $fil;
- }
- $this->filters = $filts;
- }
- /**
- * Returns a string with all html entities replaced.
- * @param string $text
- * @return string
- */
- protected function escape($text)
- {
- return htmlentities($text);
- }
-
- /**
- * A factory function to create form fields.
- *
- * @param string $name
- * @param string $label
- * @param string $id
- * @param array $attrib
- * @return Staple_Form_Element
- */
- public static function Create($name, $label = NULL, $id = NULL, array $attrib = array())
- {
- return new static($name, $label, $id, $attrib);
- }
-
- /*
- * -------------------------------------VALIDATION FUNCTIONS-------------------------------------
- */
- /**
- * Adds a field filter to the form field.
- * @param Staple_Form_Filter $filter
- * @return $this
- */
- public function addFilter(Staple_Form_Filter $filter)
- {
- $this->filters[$filter->getName()] = $filter;
- return $this;
- }
- /**
- * Adds a field validator to the form field.
- * @param Staple_Form_Validator $validator
- * @return $this
- */
- public function addValidator(Staple_Form_Validator $validator)
- {
- $this->validators[] = $validator;
- return $this;
- }
-
- /**
- * Removes all validators from a form field.
- */
- public function clearValidators()
- {
- $this->validators = array();
- return $this;
- }
- /**
- * Sets the field to be required to be completed to be valid. The optional parameter also allows you
- * to set required to false using this function.
- * @param bool $bool
- * @return Staple_Form_Element
- */
- public function setRequired($bool = true)
- {
- $this->required = (bool)$bool;
- if($bool === true)
- {
- $this->addClass('form_required');
- }
- return $this;
- }
-
- /**
- * Sets the field to not be required.
- * @return Staple_Form_Element
- */
- public function setNotRequired()
- {
- $this->required = false;
- return $this;
- }
-
- /**
- * Returns a boolean value of whether the field is required.
- * @return bool
- */
- public function isRequired()
- {
- return (bool)$this->required;
- }
-
- /**
- * Adds a custom error errors array for a specific field.
- * @param array $err
- */
- public function addError($err = NULL)
- {
- if(isset($err))
- {
- $this->errors[] = $err;
- }
- else
- {
- $this->errors[] = static::DEFAULT_ERROR;
- }
- }
- /**
- * Returns an array of the errors that occurred during form validation.
- * @return array
- */
- public function getErrors()
- {
- return $this->errors;
- }
-
- /**
- * Checks the form field against any validators to confirm valid data was entered into the form.
- * If no validators are associated with the field, it is assumed that no validation is required
- * and the field will always return valid. If the field is required then a value must be filled
- * into the field for it to be valid, even with no validators.
- *
- * @return boolean
- * @throws Exception
- */
- public function isValid()
- {
- foreach($this->validators as $val)
- {
- if($val instanceof Staple_Form_Validator)
- {
- $val->clearErrors();
- if(!$val->check($this->Value))
- {
- $this->errors[$val->getName()] = $val->getErrors();
- }
- }
- else
- {
- throw new Exception('Validation Error', Staple_Error::VALIDATION_ERROR);
- }
- }
-
- //@todo check for field content if not validators present.
-
- if(count($this->errors) == 0)
- {
- if($this->isRequired() && $this->getValue() !== NULL && $this->getValue() !== '')
- {
- return true;
- }
- else
- {
- if(!$this->isRequired())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- else
- {
- return false;
- }
- }
-
- /**
- * This function queries all of the validators for javascript to verify their data.
- * @return string
- */
- public function clientJQuery()
- {
- $script = '';
- foreach ($this->validators as $val)
- {
- if($val instanceof Staple_Form_Validator)
- {
- $script .= $val->clientJQuery(get_class($this), $this);
- }
- else
- {
- throw new Exception('Form Error', Staple_Error::FORM_ERROR);
- }
- }
- return $script;
- }
- /**
- * This function queries all of the validators for javascript to verify their data.
- * @throws Exception
- * @return string
- */
- public function clientJS()
- {
- $script = '';
- foreach ($this->validators as $val)
- {
- if($val instanceof Staple_Form_Validator)
- {
- $script .= $val->clientJS(get_class($this), $this->id);
- }
- else
- {
- throw new Exception('Form Error', Staple_Error::FORM_ERROR);
- }
- }
- return $script;
- }
-
- /*
- * -------------------------------------SET/GET FUNCTIONS-------------------------------------
- */
-
- /**
- * Sets the name.
- * @param string $insert
- * @return Staple_Form_Element
- */
- public function setName($insert)
- {
- $this->name = $insert;
- return $this;
- }
-
- /**
- * Gets the name.
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * Sets the field label.
- * @param string $insert
- * @param bool $noescape
- * @return $this
- */
- public function setLabel($insert, $noescape = FALSE)
- {
- if($noescape === true)
- {
- $this->label = $insert;
- }
- else
- {
- $this->label = $this->escape($insert);
- }
- return $this;
- }
-
- /**
- * Gets the field label.
- */
- public function getLabel()
- {
- return $this->label;
- }
- /**
- * Sets the field value, if field is not read only.
- * @param string $insert
- * @throws Exception
- * @return Staple_Form_Element
- */
- public function setValue($insert)
- {
- if(!$this->isReadOnly())
- {
- //Process Filters
- foreach($this->filters as $name=>$filter)
- {
- if($filter instanceof Staple_Form_Filter)
- {
- $insert = $filter->filter($insert);
- }
- else
- {
- throw new Exception('Filter Error', Staple_Error::FORM_ERROR);
- }
- }
-
- $this->value = $insert;
- }
- return $this;
- }
-
- /**
- * Gets the form fields value.
- */
- public function getValue()
- {
- return $this->value;
- }
- /**
- * @return string $id
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * @param string $id
- * @return $this
- */
- public function setId($id)
- {
- $this->id = $id;
- return $this;
- }
- /**
- * @return string $instructions
- */
- public function getInstructions()
- {
- return $this->instructions;
- }
- /**
- * @param string $instructions
- * @return $this
- */
- public function setInstructions($instructions)
- {
- $this->instructions = $instructions;
- return $this;
- }
-
- /**
- * Sets $readOnly to true
- * @return Staple_Form_Element
- */
- public function setReadOnly()
- {
- $this->readOnly = true;
- return $this;
- }
-
- /**
- * Returns the readOnly value.
- * @return boolean
- */
- public function isReadOnly()
- {
- return $this->readOnly;
- }
- /**
- * @return bool $disabled
- */
- public function isDisabled()
- {
- return $this->disabled;
- }
- /**
- * @param boolean $disabled
- * @return $this
- */
- public function setDisabled($disabled = true)
- {
- $this->disabled = (bool)$disabled;
- return $this;
- }
- /**
- *
- * Adds an attribute to the attributes array. $attrib is the key or attribute name, and $value is its value.
- * @param string $attrib
- * @param string $value
- * @return $this
- */
- public function addAttrib($attrib, $value)
- {
- $this->attrib[$attrib] = $value;
- return $this;
- }
-
- /**
- * Add a CSS class to the Element
- * @param string $class
- * @return Staple_Form_Element
- */
- public function addClass($class)
- {
- if(in_array($class, $this->classes) === false)
- {
- $this->classes[] = $class;
- }
- return $this;
- }
-
- /**
- * Remove a class from the Element
- * @param string $class
- * @return Staple_Form_Element
- */
- public function removeClass($class)
- {
- if(($key = array_search($class, $this->classes)) !== false)
- {
- unset($this->classes[$key]);
- }
- return $this;
- }
-
- /**
- * Returns the classes array as a string
- * @return string
- */
- public function getClassString()
- {
- if(count($this->classes) >= 1)
- {
- $ctemp = ' class="';
- foreach($this->classes as $class)
- {
- $ctemp .= $this->escape($class).' ';
- }
- $ctemp = substr($ctemp, 0, strlen($ctemp)-1).'"';
- return $ctemp;
- }
- else
- {
- return '';
- }
- }
-
- /**
- * Returns the classes array.
- * @return array[string]
- */
- public function getCSSClasses()
- {
- return $this->classes;
- }
-
- /**
- * Returns all the attributes formatted as and HTML string.
- * @return string
- */
- public function getAttribString()
- {
- $attribs = '';
- if($this->isDisabled())
- {
- $attribs .= ' disabled';
- }
- if($this->isReadOnly())
- {
- $attribs .= ' readOnly';
- }
- $attribs .= $this->getClassString();
- foreach($this->attrib as $key=>$value)
- {
- $attribs .= ' '.$this->escape($key).'="'.$this->escape($value).'"';
- }
- return $attribs;
- }
-
- /*
- * -------------------------------------FORM FUNCTIONS-------------------------------------
- */
-
- /**
- * Build the field instructions
- */
- public function instructions()
- {
- if(strlen($this->instructions) > 0)
- {
- return ' <p class="field_instructions">'.$this->escape($this->instructions).'</p>';
- }
- else
- {
- return '';
- }
- }
-
- /**
- * Build the field label
- */
- abstract public function label();
-
- /**
- * Build the field itself
- */
- abstract public function field();
-
- /**
- * Build the field using a layout, or with the default build.
- */
- abstract public function build();
-
- /*----------------------------------------Helpers----------------------------------------*/
- /**
- *
- * If an array is supplied, a link is created to a controller/action. If a string is
- * supplied, a file link is specified.
- * @param string | array $link
- * @param array $get
- * @return string
- */
- public function link($link,array $get = array())
- {
- return Staple_Main::get()->link($link,$get);
- }
- }
|