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_XmlConnect
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: * Xmlconnect form multiline element
29: *
30: * @category Mage
31: * @package Mage_XmlConnect
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_XmlConnect_Model_Simplexml_Form_Element_Multiline
35: extends Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract
36: {
37: /**
38: * Format for Xml elements id attribute
39: *
40: * @var string
41: */
42: protected $_fieldIdFormat = '%1$s';
43:
44: /**
45: * Format for Xml elements name attribute
46: *
47: * @var string
48: */
49: protected $_fieldNameFormat = '%1$s';
50:
51: /**
52: * Init multiline element
53: *
54: * @param array $attributes
55: */
56: public function __construct($attributes = array())
57: {
58: if (!isset($attributes['line_count'])) {
59: Mage::throwException(
60: Mage::helper('xmlconnect')->__('"line_count" attribute is required for "multiline" element.')
61: );
62: }
63: parent::__construct($attributes);
64: $this->setType('multiline');
65: }
66:
67: /**
68: * Required element attribute array
69: *
70: * @return array
71: */
72: public function getRequiredXmlAttributes()
73: {
74: return array(
75: 'label' => null,
76: 'type' => null
77: );
78: }
79:
80: /**
81: * Return Xml id for element
82: *
83: * @param null|string $index
84: * @return string
85: */
86: public function getXmlId($index = null)
87: {
88: $format = $this->_fieldIdFormat;
89: if (!is_null($index)) {
90: $format .= '_%2$s';
91: }
92: return sprintf($format, $this->getData('attribute_code'), $index);
93: }
94:
95: /**
96: * Return Xml id for element
97: *
98: * @param null|string $index
99: * @return string
100: */
101: public function getFieldName($index = null)
102: {
103: $format = $this->_fieldNameFormat;
104: if (!is_null($index)) {
105: $format .= '[%2$s]';
106: }
107: return sprintf($format, $this->getData('attribute_code'), $index);
108: }
109:
110: /**
111: * Add value to element
112: *
113: * @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
114: * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract
115: */
116: protected function _addValue(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
117: {
118: $values = $this->getEscapedValue();
119: if (!empty($values)) {
120: $valuesXmlObj = $xmlObj->addCustomChild('values');
121: for ($i = 0; $i < $this->getData('line_count'); $i++) {
122: $value = !empty($values[$i]) ? array('value' => $values[$i]) : array();
123:
124: $valuesXmlObj->addCustomChild('item', null, array(
125: 'id' => $this->getXmlId($i),
126: 'name' => $this->getFieldName($i)
127: ) + $value);
128: }
129: }
130: return $this;
131: }
132: }
133: