PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Eav/Model/Attribute/Data/Select.php

https://gitlab.com/blingbang2016/shop
PHP | 140 lines | 59 code | 13 blank | 68 comment | 15 complexity | f1bcb979195337a74e5cfdb282551cd3 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Eav
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * EAV Entity Attribute Select Data Model
  28. *
  29. * @category Mage
  30. * @package Mage_Eav
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Eav_Model_Attribute_Data_Select extends Mage_Eav_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. return $this->_getRequestValue($request);
  44. }
  45. /**
  46. * Validate data
  47. * Return true or array of errors
  48. *
  49. * @param array|string $value
  50. * @return boolean|array
  51. */
  52. public function validateValue($value)
  53. {
  54. $errors = array();
  55. $attribute = $this->getAttribute();
  56. $label = Mage::helper('eav')->__($attribute->getStoreLabel());
  57. if ($value === false) {
  58. // try to load original value and validate it
  59. $value = $this->getEntity()->getData($attribute->getAttributeCode());
  60. }
  61. if ($attribute->getIsRequired() && empty($value) && $value != '0') {
  62. $errors[] = Mage::helper('eav')->__('"%s" is a required value.', $label);
  63. }
  64. if (!$errors && !$attribute->getIsRequired() && empty($value)) {
  65. return true;
  66. }
  67. if (count($errors) == 0) {
  68. return true;
  69. }
  70. return $errors;
  71. }
  72. /**
  73. * Export attribute value to entity model
  74. *
  75. * @param array|string $value
  76. * @return Mage_Eav_Model_Attribute_Data_Select
  77. */
  78. public function compactValue($value)
  79. {
  80. if ($value !== false) {
  81. $this->getEntity()->setData($this->getAttribute()->getAttributeCode(), $value);
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Restore attribute value from SESSION to entity model
  87. *
  88. * @param array|string $value
  89. * @return Mage_Eav_Model_Attribute_Data_Select
  90. */
  91. public function restoreValue($value)
  92. {
  93. return $this->compactValue($value);
  94. }
  95. /**
  96. * Return a text for option value
  97. *
  98. * @param int $value
  99. * @return string
  100. */
  101. protected function _getOptionText($value)
  102. {
  103. return $this->getAttribute()->getSource()->getOptionText($value);
  104. }
  105. /**
  106. * Return formated attribute value from entity model
  107. *
  108. * @return string|array
  109. */
  110. public function outputValue($format = Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
  111. {
  112. $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  113. switch ($format) {
  114. case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_JSON:
  115. $output = $value;
  116. break;
  117. default:
  118. if ($value != '') {
  119. $output = $this->_getOptionText($value);
  120. } else {
  121. $output = '';
  122. }
  123. break;
  124. }
  125. return $output;
  126. }
  127. }