FullName.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 52/EQe8SrLY0O/jmnVx3A9lneEs3jluN4AKRXhJkJyc=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.0
  11. */
  12. /**
  13. * full name (first name - last name) form element
  14. */
  15. namespace Ppb\Form\Element;
  16. use Cube\Form\Element,
  17. Cube\Controller\Front,
  18. Cube\Validate\NotEmpty;
  19. class FullName extends Element
  20. {
  21. const KEY_FIRST = 'first';
  22. const KEY_LAST = 'last';
  23. /**
  24. *
  25. * type of element - override the variable from the parent class
  26. *
  27. * @var string
  28. */
  29. protected $_element = 'fullName';
  30. /**
  31. *
  32. * request object
  33. *
  34. * @var \Cube\Controller\Request\AbstractRequest
  35. */
  36. protected $_request;
  37. // /**
  38. // *
  39. // * the names for each of the two fields
  40. // *
  41. // * @var array
  42. // */
  43. // protected $_fieldNames = array();
  44. /**
  45. *
  46. * the labels for each of the two fields
  47. *
  48. * @var array
  49. */
  50. protected $_fieldLabels = array();
  51. /**
  52. *
  53. * class constructor
  54. *
  55. * @param string $name
  56. */
  57. public function __construct($name)
  58. {
  59. parent::__construct('text', $name);
  60. $this->_request = Front::getInstance()->getRequest();
  61. }
  62. /**
  63. *
  64. * set field labels
  65. *
  66. * @param array $fieldLabels
  67. * @return $this
  68. */
  69. public function setFieldLabels($fieldLabels)
  70. {
  71. $this->_fieldLabels = $fieldLabels;
  72. return $this;
  73. }
  74. /**
  75. *
  76. * get field labels
  77. *
  78. * @return array
  79. */
  80. public function getFieldLabels()
  81. {
  82. return $this->_fieldLabels;
  83. }
  84. /**
  85. *
  86. * get individual field label
  87. *
  88. * @param $key
  89. * @return string
  90. */
  91. public function getFieldLabel($key)
  92. {
  93. if (isset($this->_fieldLabels[$key])) {
  94. return $this->_fieldLabels[$key];
  95. }
  96. return $this->_label;
  97. }
  98. /**
  99. *
  100. * return the value(s) of the element, either the element's data or default value(s)
  101. *
  102. * @param string $key
  103. * @return mixed
  104. */
  105. public function getValue($key = null)
  106. {
  107. $value = parent::getValue();
  108. if ($key !== null) {
  109. if (array_key_exists($key, (array) $value)) {
  110. return $value[$key];
  111. }
  112. else {
  113. return null;
  114. }
  115. }
  116. return $value;
  117. }
  118. /**
  119. *
  120. * render element attributes
  121. *
  122. * @param string $type
  123. * @return \Cube\Form\Element
  124. */
  125. public function renderAttributes($type = null)
  126. {
  127. $attributes = null;
  128. foreach ($this->_attributes as $key => $value) {
  129. $attributes .= $key . '="' . ((is_array($value)) ? $value[$type] : $value) . '" ';
  130. }
  131. return $attributes;
  132. }
  133. /**
  134. *
  135. * check if the composite element is valid
  136. *
  137. * @return bool
  138. */
  139. public function isValid()
  140. {
  141. $valid = true;
  142. if (!$this->_request->isPost()) {
  143. return true;
  144. }
  145. if ($this->_required === true) {
  146. $this->addValidator(
  147. new NotEmpty());
  148. }
  149. $firstNameLabel = $this->getFieldLabel(self::KEY_FIRST);
  150. $lastNameLabel = $this->getFieldLabel(self::KEY_LAST);
  151. $firstNameValue = $this->getValue(self::KEY_FIRST);
  152. $lastNameValue = $this->getValue(self::KEY_LAST);
  153. // get original values
  154. $label = $this->getLabel();
  155. $data = $this->_data;
  156. foreach ($this->getValidators() as $validator) {
  157. // check first name
  158. $this->setLabel($firstNameLabel);
  159. $this->setData($firstNameValue);
  160. $valid = ($this->_checkValidator($validator) === true) ? $valid : false;
  161. // check last name
  162. $this->setLabel($lastNameLabel);
  163. $this->setData($lastNameValue);
  164. $valid = ($this->_checkValidator($validator) === true) ? $valid : false;
  165. }
  166. // restore values
  167. $this->setLabel($label);
  168. $this->setData($data);
  169. return (bool)$valid;
  170. }
  171. /**
  172. *
  173. * render composite element
  174. *
  175. * @return string
  176. */
  177. public function render()
  178. {
  179. return $this->getPrefix() . ' '
  180. . '<input type="' . $this->_type . '" '
  181. . 'name="' . $this->_name . '[' . self::KEY_FIRST . ']" '
  182. . $this->renderAttributes(self::KEY_FIRST)
  183. . 'value="' . $this->getValue(self::KEY_FIRST) . '" '
  184. . $this->_endTag . ' '
  185. . $this->getSuffix()
  186. . ' '
  187. . $this->getPrefix() . ' '
  188. . '<input type="' . $this->_type . '" '
  189. . 'name="' . $this->_name . '[' . self::KEY_LAST . ']" '
  190. . $this->renderAttributes(self::KEY_LAST)
  191. . 'value="' . $this->getValue(self::KEY_LAST) . '" '
  192. . $this->_endTag . ' '
  193. . $this->getSuffix();
  194. }
  195. }