123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- namespace Ppb\Form\Element;
- use Cube\Form\Element,
- Cube\Controller\Front,
- Cube\Validate\NotEmpty,
- Cube\Locale\Format as LocaleFormat,
- Ppb\Model\Shipping as ShippingModel;
- class Dimensions extends Element
- {
-
- protected $_element = 'dimensions';
-
- protected $_request;
-
- protected $_fieldLabels = array();
-
- public function __construct($name)
- {
- parent::__construct('text', $name);
- $this->_request = Front::getInstance()->getRequest();
- }
-
- public function setFieldLabels($fieldLabels)
- {
- $this->_fieldLabels = $fieldLabels;
- return $this;
- }
-
- public function getFieldLabels()
- {
- return $this->_fieldLabels;
- }
-
- public function getFieldLabel($key)
- {
- if (isset($this->_fieldLabels[$key])) {
- return $this->_fieldLabels[$key];
- }
- return $this->_label;
- }
-
- public function getValue($key = null)
- {
- $value = parent::getValue();
- if ($key !== null) {
- if (array_key_exists($key, (array)$value)) {
- return $value[$key];
- }
- else {
- return null;
- }
- }
- return $value;
- }
-
- public function renderAttributes($type = null)
- {
- $attributes = null;
- foreach ($this->_attributes as $key => $value) {
- $attributes .= $key . '="' . ((is_array($value)) ? $value[$type] : $value) . '" ';
- }
- return $attributes;
- }
-
- public function isValid()
- {
- $valid = true;
- if (!$this->_request->isPost()) {
- return true;
- }
- if ($this->_required === true) {
- $this->addValidator(
- new NotEmpty());
- }
- $lengthLabel = $this->getFieldLabel(ShippingModel::DIMENSION_LENGTH);
- $widthLabel = $this->getFieldLabel(ShippingModel::DIMENSION_WIDTH);
- $heightLabel = $this->getFieldLabel(ShippingModel::DIMENSION_HEIGHT);
- $lengthValue = $this->getValue(ShippingModel::DIMENSION_LENGTH);
- $widthValue = $this->getValue(ShippingModel::DIMENSION_WIDTH);
- $heightValue = $this->getValue(ShippingModel::DIMENSION_HEIGHT);
-
- $label = $this->getLabel();
- $data = $this->_data;
- foreach ($this->getValidators() as $validator) {
-
- $this->setLabel($lengthLabel);
- $this->setData($lengthValue);
- $valid = ($this->_checkValidator($validator) === true) ? $valid : false;
-
- $this->setLabel($widthLabel);
- $this->setData($widthValue);
- $valid = ($this->_checkValidator($validator) === true) ? $valid : false;
-
- $this->setLabel($heightLabel);
- $this->setData($heightValue);
- $valid = ($this->_checkValidator($validator) === true) ? $valid : false;
- }
-
- $this->setLabel($label);
- $this->setData($data);
- return (bool)$valid;
- }
-
- public function render()
- {
- $localizedLength = LocaleFormat::getInstance()->numericToLocalized(
- $this->getValue(ShippingModel::DIMENSION_LENGTH), true);
- $localizedWidth = LocaleFormat::getInstance()->numericToLocalized(
- $this->getValue(ShippingModel::DIMENSION_WIDTH), true);
- $localizedHeight = LocaleFormat::getInstance()->numericToLocalized(
- $this->getValue(ShippingModel::DIMENSION_HEIGHT), true);
- return $this->getPrefix() . ' '
- . '<input type="' . $this->_type . '" '
- . 'name="' . $this->_name . '[' . ShippingModel::DIMENSION_LENGTH . ']" '
- . $this->renderAttributes(ShippingModel::DIMENSION_LENGTH)
- . 'value="' . $localizedLength . '" '
- . $this->_endTag . ' '
- . ' x '
- . '<input type="' . $this->_type . '" '
- . 'name="' . $this->_name . '[' . ShippingModel::DIMENSION_WIDTH . ']" '
- . $this->renderAttributes(ShippingModel::DIMENSION_WIDTH)
- . 'value="' . $localizedWidth . '" '
- . $this->_endTag . ' '
- . ' x '
- . '<input type="' . $this->_type . '" '
- . 'name="' . $this->_name . '[' . ShippingModel::DIMENSION_HEIGHT . ']" '
- . $this->renderAttributes(ShippingModel::DIMENSION_HEIGHT)
- . 'value="' . $localizedHeight . '" '
- . $this->_endTag . ' '
- . $this->getSuffix();
- }
- }
|