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 Text 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_Text 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: $value = $this->_getRequestValue($request);
46: return $this->_applyInputFilter($value);
47: }
48:
49: /**
50: * Validate data
51: * Return true or array of errors
52: *
53: * @param array|string $value
54: * @return boolean|array
55: */
56: public function validateValue($value)
57: {
58: $errors = array();
59: $attribute = $this->getAttribute();
60: $label = Mage::helper('eav')->__($attribute->getStoreLabel());
61:
62: if ($value === false) {
63: // try to load original value and validate it
64: $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
65: }
66:
67: if ($attribute->getIsRequired() && empty($value)) {
68: $errors[] = Mage::helper('eav')->__('"%s" is a required value.', $label);
69: }
70:
71: if (!$errors && !$attribute->getIsRequired() && empty($value)) {
72: return true;
73: }
74:
75: // validate length
76: $length = Mage::helper('core/string')->strlen(trim($value));
77:
78: $validateRules = $attribute->getValidateRules();
79: if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
80: $v = $validateRules['min_text_length'];
81: $errors[] = Mage::helper('eav')->__('"%s" length must be equal or greater than %s characters.', $label, $v);
82: }
83: if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
84: $v = $validateRules['max_text_length'];
85: $errors[] = Mage::helper('eav')->__('"%s" length must be equal or less than %s characters.', $label, $v);
86: }
87:
88: $result = $this->_validateInputRule($value);
89: if ($result !== true) {
90: $errors = array_merge($errors, $result);
91: }
92: if (count($errors) == 0) {
93: return true;
94: }
95:
96: return $errors;
97: }
98:
99: /**
100: * Export attribute value to entity model
101: *
102: * @param array|string $value
103: * @return Mage_Eav_Model_Attribute_Data_Text
104: */
105: public function compactValue($value)
106: {
107: if ($value !== false) {
108: $this->getEntity()->setDataUsingMethod($this->getAttribute()->getAttributeCode(), $value);
109: }
110: return $this;
111: }
112:
113: /**
114: * Restore attribute value from SESSION to entity model
115: *
116: * @param array|string $value
117: * @return Mage_Eav_Model_Attribute_Data_Text
118: */
119: public function restoreValue($value)
120: {
121: return $this->compactValue($value);
122: }
123:
124: /**
125: * Return formated attribute value from entity model
126: *
127: * @param string $format
128: * @return string|array
129: */
130: public function outputValue($format = Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
131: {
132: $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
133: $value = $this->_applyOutputFilter($value);
134:
135: return $value;
136: }
137: }
138: