_priceObj = new App_ECommerce_ShippingPrice_Admin_Obj();
return parent::init();
}
protected function _initElements()
{
parent::_initElements();
$firstPrice = $this->_priceObj->getFirstShippingPriceItem();
if (($this->_controllerAction == 'insert' && !$firstPrice)
|| ($this->_controllerAction == 'update' && $firstPrice['id'] == $this->_getData('id'))
) {
$this->addElement(
'static',
'startPriceTitle',
array(
'label' => 'Start Price',
'value' => '$' . self::MIN_PRICE . ''
)
);
$this->addElement('hidden', 'startPrice', array('value' => self::MIN_PRICE));
} else {
$this->addElement(
'numeric',
'startPrice',
array(
'label' => 'Start Price',
'validators' => array(array('Float', false)),
'required' => true,
'negative' => false,
)
);
/** @var $numeric Qs_Form_Decorator_Numeric */
$numeric = $this->getElement('startPrice')->getDecorator('Numeric');
$numeric->setAdditionalHtmlBeforeElement('$');
}
$this->addElement(
'numeric',
'endPrice',
array(
'label' => 'End Price',
'validators' => array(array('Float', false)),
'required' => true,
'negative' => false
)
);
/** @var $numeric Qs_Form_Decorator_Numeric */
$numeric = $this->getElement('endPrice')->getDecorator('Numeric');
$numeric->setAdditionalHtmlBeforeElement('$');
$this->addElement(
'numeric',
'price',
array(
'label' => 'Shipping Price',
'required' => true,
'negative' => false
)
);
/** @var $numeric Qs_Form_Decorator_Numeric */
$numeric = $this->getElement('price')->getDecorator('Numeric');
$numeric->setAdditionalHtmlBeforeElement('$');
$this->addElement(
'numeric',
'orderPercent',
array(
'label' => '% of the order cost to add to the shipping price',
'required' => true,
'negative' => false,
)
);
/** @var $numeric Qs_Form_Decorator_Numeric */
$numeric = $this->getElement('orderPercent')->getDecorator('Numeric');
$numeric->setAdditionalHtmlBeforeElement('%');
$this->addFormRule(array($this, 'validateForm'));
return $this;
}
/**
* convert currency string to float string
* @param $currencyStr
* @return mixed
*/
public static function currencyToFloat($currencyStr)
{
return preg_replace('/[^\d\.]/', '', $currencyStr);
}
public function validateForm(array $data)
{
$errors = array();
$previousPrice = $this->_priceObj->getNeighborPrice($this->_getData('id'));
if ($data['startPrice'] != '') {
if ($this->currencyToFloat($data['startPrice']) < static::MIN_PRICE) {
$errors['startPrice'] = str_replace('%value%', static::MIN_PRICE, $this->_wrongPrice);
} else if ($previousPrice
&& $this->currencyToFloat($data['startPrice']) < $this->currencyToFloat($previousPrice['startPrice'])
+ App_ECommerce_ShippingPrice_Admin_Obj::PRICE_MIN_DELAY
) {
$errors['startPrice'] = str_replace(
'%value%',
$previousPrice['startPrice'],
$this->_notRecommendedStartPrice
);
}
}
if ($data['endPrice'] != '') {
if ($this->currencyToFloat($data['endPrice']) < static::MIN_PRICE) {
$errors['endPrice'] = str_replace('%value%', static::MIN_PRICE, $this->_wrongPrice);
} else if ($this->currencyToFloat($data['startPrice']) > $this->currencyToFloat($data['endPrice'])) {
$errors['endPrice'] = $this->_endPriceGrater;
}
}
return (empty($errors)) ? true : $errors;
}
}