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: * Entity/Attribute/Model - attribute frontend abstract
30: *
31: * @category Mage
32: * @package Mage_Eav
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: abstract class Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
36: implements Mage_Eav_Model_Entity_Attribute_Frontend_Interface
37: {
38:
39: /**
40: * Reference to the attribute instance
41: *
42: * @var Mage_Eav_Model_Entity_Attribute_Abstract
43: */
44: protected $_attribute;
45:
46: /**
47: * Set attribute instance
48: *
49: * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
50: * @return Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
51: */
52: public function setAttribute($attribute)
53: {
54: $this->_attribute = $attribute;
55: return $this;
56: }
57:
58: /**
59: * Get attribute instance
60: *
61: * @return Mage_Eav_Model_Entity_Attribute_Abstract
62: */
63: public function getAttribute()
64: {
65: return $this->_attribute;
66: }
67:
68: /**
69: * Get attribute type for user interface form
70: *
71: * @return string
72: */
73: public function getInputType()
74: {
75: return $this->getAttribute()->getFrontendInput();
76: }
77:
78: /**
79: * Retreive lable
80: *
81: * @return string
82: */
83: public function getLabel()
84: {
85: $label = $this->getAttribute()->getFrontendLabel();
86: if (($label === null) || $label == '') {
87: $label = $this->getAttribute()->getAttributeCode();
88: }
89:
90: return $label;
91: }
92:
93: /**
94: * Retreive attribute value
95: *
96: * @param $object
97: * @return mixed
98: */
99: public function getValue(Varien_Object $object)
100: {
101: $value = $object->getData($this->getAttribute()->getAttributeCode());
102: if (in_array($this->getConfigField('input'), array('select','boolean'))) {
103: $valueOption = $this->getOption($value);
104: if (!$valueOption) {
105: $opt = Mage::getModel('eav/entity_attribute_source_boolean');
106: $options = $opt->getAllOptions();
107: if ($options) {
108: foreach ($options as $option) {
109: if ($option['value'] == $value) {
110: $valueOption = $option['label'];
111: }
112: }
113: }
114: }
115: $value = $valueOption;
116: } elseif ($this->getConfigField('input') == 'multiselect') {
117: $value = $this->getOption($value);
118: if (is_array($value)) {
119: $value = implode(', ', $value);
120: }
121: }
122:
123: return $value;
124: }
125:
126: /**
127: * Checks if attribute is visible on frontend
128: *
129: * @return boolean
130: */
131: public function isVisible()
132: {
133: return $this->getConfigField('frontend_visible');
134: }
135:
136: /**
137: * Retrieve frontend class
138: *
139: * @return string
140: */
141: public function getClass()
142: {
143: $out = array();
144: $out[] = $this->getAttribute()->getFrontendClass();
145: if ($this->getAttribute()->getIsRequired()) {
146: $out[] = 'required-entry';
147: }
148:
149: $inputRuleClass = $this->_getInputValidateClass();
150: if ($inputRuleClass) {
151: $out[] = $inputRuleClass;
152: }
153: if (!empty($out)) {
154: $out = implode(' ', $out);
155: } else {
156: $out = '';
157: }
158: return $out;
159: }
160:
161: /**
162: * Return validate class by attribute input validation rule
163: *
164: * @return string|false
165: */
166: protected function _getInputValidateClass()
167: {
168: $class = false;
169: $validateRules = $this->getAttribute()->getValidateRules();
170: if (!empty($validateRules['input_validation'])) {
171: switch ($validateRules['input_validation']) {
172: case 'alphanumeric':
173: $class = 'validate-alphanum';
174: break;
175: case 'numeric':
176: $class = 'validate-digits';
177: break;
178: case 'alpha':
179: $class = 'validate-alpha';
180: break;
181: case 'email':
182: $class = 'validate-email';
183: break;
184: case 'url':
185: $class = 'validate-url';
186: break;
187: default:
188: $class = false;
189: break;
190: }
191: }
192: return $class;
193: }
194:
195: /**
196: * Reireive config field
197: *
198: * @param string $fieldName
199: * @return mixed
200: */
201: public function getConfigField($fieldName)
202: {
203: return $this->getAttribute()->getData('frontend_' . $fieldName);
204: }
205:
206: /**
207: * Get select options in case it's select box and options source is defined
208: *
209: * @return array
210: */
211: public function getSelectOptions()
212: {
213: return $this->getAttribute()->getSource()->getAllOptions();
214: }
215:
216: /**
217: * Retreive option by option id
218: *
219: * @param int $optionId
220: * @return mixed|boolean
221: */
222: public function getOption($optionId)
223: {
224: $source = $this->getAttribute()->getSource();
225: if ($source) {
226: return $source->getOptionText($optionId);
227: }
228: return false;
229: }
230:
231: /**
232: * Retrieve Input Renderer Class
233: *
234: * @return string
235: */
236: public function getInputRendererClass() {
237: $className = $this->getAttribute()->getData('frontend_input_renderer');
238: if ($className) {
239: return Mage::getConfig()->getBlockClassName($className);
240: }
241: return null;
242: }
243: }
244: