Dimensions.php 6.0 KB

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