/PFBC/Element.php

https://gitlab.com/oytunistrator/php-bootstrap-form · PHP · 179 lines · 122 code · 26 blank · 31 comment · 22 complexity · 828c238b8c1f76aabe8443811c1c9af4 MD5 · raw file

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