/app/code/core/Mage/Customer/Model/Attribute/Data/Date.php
PHP | 162 lines | 83 code | 16 blank | 63 comment | 19 complexity | 33f82827fc49fd74100f4740f12f42f9 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
- <?php
- /**
- * Magento
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Open Software License (OSL 3.0)
- * that is bundled with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://opensource.org/licenses/osl-3.0.php
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@magentocommerce.com so we can send you a copy immediately.
- *
- * DISCLAIMER
- *
- * Do not edit or add to this file if you wish to upgrade Magento to newer
- * versions in the future. If you wish to customize Magento for your
- * needs please refer to http://www.magentocommerce.com for more information.
- *
- * @category Mage
- * @package Mage_Customer
- * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- */
- /**
- * Customer Attribute Date Data Model
- *
- * @category Mage
- * @package Mage_Customer
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class Mage_Customer_Model_Attribute_Data_Date extends Mage_Customer_Model_Attribute_Data_Abstract
- {
- /**
- * Extract data from request and return value
- *
- * @param Zend_Controller_Request_Http $request
- * @return array|string
- */
- public function extractValue(Zend_Controller_Request_Http $request)
- {
- $value = $this->_getRequestValue($request);
- return $this->_applyInputFilter($value);
- }
- /**
- * Validate data
- * Return true or array of errors
- *
- * @param array|string $value
- * @return boolean|array
- */
- public function validateValue($value)
- {
- $errors = array();
- $attribute = $this->getAttribute();
- $label = Mage::helper('customer')->__($attribute->getStoreLabel());
- if ($value === false) {
- // try to load original value and validate it
- $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
- }
- if ($attribute->getIsRequired() && empty($value)) {
- $errors[] = Mage::helper('customer')->__('"%s" is a required value.', $label);
- }
- if (!$errors && !$attribute->getIsRequired() && empty($value)) {
- return true;
- }
- $result = $this->_validateInputRule($value);
- if ($result !== true) {
- $errors = array_merge($errors, $result);
- }
- //range validation
- $validateRules = $attribute->getValidateRules();
- if ((!empty($validateRules['date_range_min']) && (strtotime($value) < $validateRules['date_range_min']))
- || (!empty($validateRules['date_range_max']) && (strtotime($value) > $validateRules['date_range_max']))) {
- if (!empty($validateRules['date_range_min']) && !empty($validateRules['date_range_max'])) {
- $errors[] = Mage::helper('customer')->__('Please enter a valid date between %s and %s at %s.',
- date('d/m/Y', $validateRules['date_range_min']),
- date('d/m/Y', $validateRules['date_range_max']),
- $label
- );
- } elseif (!empty($validateRules['date_range_min'])) {
- $errors[] = Mage::helper('customer')->__('Please enter a valid date equal to or greater than %s at %s.',
- date('d/m/Y', $validateRules['date_range_min']),
- $label
- );
- } elseif (!empty($validateRules['date_range_max'])) {
- $errors[] = Mage::helper('customer')->__('Please enter a valid date less than or equal to %s at %s.',
- date('d/m/Y', $validateRules['date_range_max']),
- $label
- );
- }
- }
- if (count($errors) == 0) {
- return true;
- }
- return $errors;
- }
- /**
- * Export attribute value to entity model
- *
- * @param array|string $value
- * @return Mage_Customer_Model_Attribute_Data_Text
- */
- public function compactValue($value)
- {
- if ($value !== false) {
- if (empty($value)) {
- $value = null;
- }
- $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
- }
- return $this;
- }
- /**
- * Restore attribute value from SESSION to entity model
- *
- * @param array|string $value
- * @return Mage_Customer_Model_Attribute_Data_Abstract
- */
- public function restoreValue($value)
- {
- return $this->compactValue($value);
- }
- /**
- * Return formated attribute value from entity model
- *
- * @return string|array
- */
- public function outputValue($format = Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
- {
- $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
- if ($value) {
- switch ($format) {
- case Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_TEXT:
- case Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_HTML:
- case Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_PDF:
- $this->_dateFilterFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
- break;
- }
- $value = $this->_applyOutputFilter($value);
- }
- $this->_dateFilterFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
- return $value;
- }
- }