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 Multiply line 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_Multiline extends Mage_Eav_Model_Attribute_Data_Text
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: if (!is_array($value)) {
47: $value = false;
48: } else {
49: $value = array_map(array($this, '_applyInputFilter'), $value);
50: }
51: return $value;
52: }
53:
54: /**
55: * Validate data
56: * Return true or array of errors
57: *
58: * @param array|string $value
59: * @return boolean|array
60: */
61: public function validateValue($value)
62: {
63: $errors = array();
64: $attribute = $this->getAttribute();
65:
66: if ($value === false) {
67: // try to load original value and validate it
68: $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
69: if (!is_array($value)) {
70: $value = explode("\n", $value);
71: }
72: }
73:
74: if (!is_array($value)) {
75: $value = array($value);
76: }
77: for ($i = 0; $i < $attribute->getMultilineCount(); $i ++) {
78: if (!isset($value[$i])) {
79: $value[$i] = null;
80: }
81: // validate first line
82: if ($i == 0) {
83: $result = parent::validateValue($value[$i]);
84: if ($result !== true) {
85: $errors = $result;
86: }
87: } else {
88: if (!empty($value[$i])) {
89: $result = parent::validateValue($value[$i]);
90: if ($result !== true) {
91: $errors = array_merge($errors, $result);
92: }
93: }
94: }
95: }
96:
97: if (count($errors) == 0) {
98: return true;
99: }
100: return $errors;
101: }
102:
103: /**
104: * Export attribute value to entity model
105: *
106: * @param Mage_Core_Model_Abstract $entity
107: * @param array|string $value
108: * @return Mage_Eav_Model_Attribute_Data_Multiline
109: */
110: public function compactValue($value)
111: {
112: if (is_array($value)) {
113: $value = trim(implode("\n", $value));
114: }
115: return parent::compactValue($value);
116: }
117:
118: /**
119: * Restore attribute value from SESSION to entity model
120: *
121: * @param array|string $value
122: * @return Mage_Eav_Model_Attribute_Data_Multiline
123: */
124: public function restoreValue($value)
125: {
126: return $this->compactValue($value);
127: }
128:
129: /**
130: * Return formated attribute value from entity model
131: *
132: * @return string|array
133: */
134: public function outputValue($format = Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_TEXT)
135: {
136: $values = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
137: if (!is_array($values)) {
138: $values = explode("\n", $values);
139: }
140: $values = array_map(array($this, '_applyOutputFilter'), $values);
141: switch ($format) {
142: case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_ARRAY:
143: $output = $values;
144: break;
145: case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_HTML:
146: $output = implode("<br />", $values);
147: break;
148: case Mage_Eav_Model_Attribute_Data::OUTPUT_FORMAT_ONELINE:
149: $output = implode(" ", $values);
150: break;
151: default:
152: $output = implode("\n", $values);
153: break;
154: }
155: return $output;
156: }
157: }
158: