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_Eav
23: * @copyright Copyright (c) 2012 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:
28: /**
29: * EAV Entity Attribute Select Data Model
30: *
31: * @category Mage
32: * @package Mage_Eav
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Eav_Model_Attribute_Data_Select extends Mage_Eav_Model_Attribute_Data_Abstract
36: {
37: /**
38: * Extract data from request and return value
39: *
40: * @param Zend_Controller_Request_Http $request
41: * @return array|string
42: */
43: public function extractValue(Zend_Controller_Request_Http $request)
44: {
45: return $this->_getRequestValue($request);
46: }
47:
48: /**
49: * Validate data
50: * Return true or array of errors
51: *
52: * @param array|string $value
53: * @return boolean|array
54: */
55: public function validateValue($value)
56: {
57: $errors = array();
58: $attribute = $this->getAttribute();
59: $label = Mage::helper('eav')->__($attribute->getStoreLabel());
60:
61: if ($value === false) {
62: // try to load original value and validate it
63: $value = $this->getEntity()->getData($attribute->getAttributeCode());
64: }
65:
66: if ($attribute->getIsRequired() && empty($value) && $value != '0') {
67: $errors[] = Mage::helper('eav')->__('"%s" is a required value.', $label);
68: }
69:
70: if (!$errors && !$attribute->getIsRequired() && empty($value)) {
71: return true;
72: }
73:
74: if (count($errors) == 0) {
75: return true;
76: }
77:
78: return $errors;
79: }
80:
81: /**
82: * Export attribute value to entity model
83: *
84: * @param array|string $value
85: * @return Mage_Eav_Model_Attribute_Data_Select
86: */
87: public function compactValue($value)
88: {
89: if ($value !== false) {
90: $this->getEntity()->setData($this->getAttribute()->getAttributeCode(), $value);
91: }
92: return $this;
93: }
94:
95: /**
96: * Restore attribute value from SESSION to entity model
97: *
98: * @param array|string $value
99: * @return Mage_Eav_Model_Attribute_Data_Select
100: */
101: public function restoreValue($value)
102: {
103: return $this->compactValue($value);
104: }
105:
106: /**
107: * Return a text for option value
108: *
109: * @param int $value
110: * @return string
111: */
112: protected function _getOptionText($value)
113: {
114: return $this->getAttribute()->getSource()->getOptionText($value);
115: }
116:
117: /**
118: * Return formated attribute value from entity model
119: *
120: * @return string|array
121: */
122: public function outputValue($format = Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
123: {
124: $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
125: switch ($format) {
126: case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_JSON:
127: $output = $value;
128: break;
129: default:
130: if ($value != '') {
131: $output = $this->_getOptionText($value);
132: } else {
133: $output = '';
134: }
135: break;
136: }
137:
138: return $output;
139: }
140: }
141: