PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Customer/Model/Attribute/Data/Date.php

https://bitbucket.org/andrewjleavitt/magestudy
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
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Customer
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Customer Attribute Date Data Model
  28. *
  29. * @category Mage
  30. * @package Mage_Customer
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Customer_Model_Attribute_Data_Date extends Mage_Customer_Model_Attribute_Data_Abstract
  34. {
  35. /**
  36. * Extract data from request and return value
  37. *
  38. * @param Zend_Controller_Request_Http $request
  39. * @return array|string
  40. */
  41. public function extractValue(Zend_Controller_Request_Http $request)
  42. {
  43. $value = $this->_getRequestValue($request);
  44. return $this->_applyInputFilter($value);
  45. }
  46. /**
  47. * Validate data
  48. * Return true or array of errors
  49. *
  50. * @param array|string $value
  51. * @return boolean|array
  52. */
  53. public function validateValue($value)
  54. {
  55. $errors = array();
  56. $attribute = $this->getAttribute();
  57. $label = Mage::helper('customer')->__($attribute->getStoreLabel());
  58. if ($value === false) {
  59. // try to load original value and validate it
  60. $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
  61. }
  62. if ($attribute->getIsRequired() && empty($value)) {
  63. $errors[] = Mage::helper('customer')->__('"%s" is a required value.', $label);
  64. }
  65. if (!$errors && !$attribute->getIsRequired() && empty($value)) {
  66. return true;
  67. }
  68. $result = $this->_validateInputRule($value);
  69. if ($result !== true) {
  70. $errors = array_merge($errors, $result);
  71. }
  72. //range validation
  73. $validateRules = $attribute->getValidateRules();
  74. if ((!empty($validateRules['date_range_min']) && (strtotime($value) < $validateRules['date_range_min']))
  75. || (!empty($validateRules['date_range_max']) && (strtotime($value) > $validateRules['date_range_max']))) {
  76. if (!empty($validateRules['date_range_min']) && !empty($validateRules['date_range_max'])) {
  77. $errors[] = Mage::helper('customer')->__('Please enter a valid date between %s and %s at %s.',
  78. date('d/m/Y', $validateRules['date_range_min']),
  79. date('d/m/Y', $validateRules['date_range_max']),
  80. $label
  81. );
  82. } elseif (!empty($validateRules['date_range_min'])) {
  83. $errors[] = Mage::helper('customer')->__('Please enter a valid date equal to or greater than %s at %s.',
  84. date('d/m/Y', $validateRules['date_range_min']),
  85. $label
  86. );
  87. } elseif (!empty($validateRules['date_range_max'])) {
  88. $errors[] = Mage::helper('customer')->__('Please enter a valid date less than or equal to %s at %s.',
  89. date('d/m/Y', $validateRules['date_range_max']),
  90. $label
  91. );
  92. }
  93. }
  94. if (count($errors) == 0) {
  95. return true;
  96. }
  97. return $errors;
  98. }
  99. /**
  100. * Export attribute value to entity model
  101. *
  102. * @param array|string $value
  103. * @return Mage_Customer_Model_Attribute_Data_Text
  104. */
  105. public function compactValue($value)
  106. {
  107. if ($value !== false) {
  108. if (empty($value)) {
  109. $value = null;
  110. }
  111. $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
  112. }
  113. return $this;
  114. }
  115. /**
  116. * Restore attribute value from SESSION to entity model
  117. *
  118. * @param array|string $value
  119. * @return Mage_Customer_Model_Attribute_Data_Abstract
  120. */
  121. public function restoreValue($value)
  122. {
  123. return $this->compactValue($value);
  124. }
  125. /**
  126. * Return formated attribute value from entity model
  127. *
  128. * @return string|array
  129. */
  130. public function outputValue($format = Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
  131. {
  132. $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  133. if ($value) {
  134. switch ($format) {
  135. case Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_TEXT:
  136. case Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_HTML:
  137. case Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_PDF:
  138. $this->_dateFilterFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
  139. break;
  140. }
  141. $value = $this->_applyOutputFilter($value);
  142. }
  143. $this->_dateFilterFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
  144. return $value;
  145. }
  146. }