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 simple xml form fieldset
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_Fieldset
35: extends Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract
36: {
37: /**
38: * Sort child elements by specified data key
39: *
40: * @var string
41: */
42: protected $_sortChildrenByKey = '';
43:
44: /**
45: * Children sort direction
46: *
47: * @var int
48: */
49: protected $_sortChildrenDirection = SORT_ASC;
50:
51: /**
52: * Main element node
53: *
54: * @var string
55: */
56: protected $_mainNode = 'fieldset';
57:
58: /**
59: * Is name attribute required
60: *
61: * @var bool
62: */
63: protected $_nameRequired = false;
64:
65: /**
66: * Init fieldset object
67: *
68: * @param array $attributes
69: */
70: public function __construct($attributes = array())
71: {
72: parent::__construct($attributes);
73: $this->_renderer = Mage_XmlConnect_Model_Simplexml_Form::getFieldsetRenderer();
74: $this->setType('fieldset');
75: }
76:
77: /**
78: * Get fieldset element object
79: *
80: * @return Mage_XmlConnect_Model_Simplexml_Element
81: */
82: public function getElementXml()
83: {
84: $xmlObj = $this->getXmlObject();
85: $this->_addRequiredAttributes($xmlObj);
86: foreach ($this->getAttributes() as $key => $val) {
87: $xmlObj->addAttribute($key, $xmlObj->xmlAttribute($val));
88: }
89: foreach ($this->getChildrenXml(false) as $element) {
90: $xmlObj->appendChild($element);
91: }
92: foreach ($this->getChildrenXml(true) as $fieldset) {
93: $xmlObj->appendChild($fieldset);
94: }
95: $this->addAfterXmlElementToObj($xmlObj);
96: return $xmlObj;
97: }
98:
99: /**
100: * Default element attribute array
101: *
102: * @return array
103: */
104: public function getXmlAttributes()
105: {
106: return array('title', 'disabled');
107: }
108:
109: /**
110: * Required element attribute array
111: *
112: * @return array
113: */
114: public function getRequiredXmlAttributes()
115: {
116: return array();
117: }
118:
119: /**
120: * Get children array of elements
121: *
122: * @param bool $isFieldset
123: * @return array
124: */
125: public function getChildrenXml($isFieldset = false)
126: {
127: $result = array();
128: foreach ($this->getSortedElements() as $element) {
129: if ($this->_checkFieldset($element, $isFieldset)) {
130: $result[] = $element->toXmlObject();
131: }
132: }
133: return $result;
134: }
135:
136: /**
137: * Check weather is element a fieldset
138: *
139: * @param Mage_XmlConnect_Model_Simplexml_Form_Abstract $element
140: * @param bool $equal
141: * @return bool
142: */
143: protected function _checkFieldset($element, $equal = true) {
144: if ($equal) {
145: return $element->getType() == 'fieldset';
146: } else {
147: return $element->getType() != 'fieldset';
148: }
149: }
150:
151: /**
152: * Add field element to fieldset
153: *
154: * @param string $elementId
155: * @param string $type
156: * @param array $config
157: * @param boolean $after
158: * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract
159: */
160: public function addField($elementId, $type, $config, $after = false)
161: {
162: $element = parent::addField($elementId, $type, $config, $after);
163: if ($renderer = Mage_XmlConnect_Model_Simplexml_Form::getFieldsetElementRenderer()) {
164: $element->setRenderer($renderer);
165: }
166: return $element;
167: }
168:
169: /**
170: * Commence sorting elements by values by specified data key
171: *
172: * @param string $key
173: * @param int $direction
174: * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset
175: */
176: public function setSortElementsByAttribute($key, $direction = SORT_ASC)
177: {
178: $this->_sortChildrenByKey = $key;
179: $this->_sortDirection = $direction;
180: return $this;
181: }
182:
183: /**
184: * Get sorted elements as array
185: *
186: * @return array
187: */
188: public function getSortedElements()
189: {
190: $elements = array();
191: // sort children by value by specified key
192: if ($this->_sortChildrenByKey) {
193: $sortKey = $this->_sortChildrenByKey;
194: $uniqueIncrement = 0; // in case if there are elements with same values
195: /** @var Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element */
196: foreach ($this->getElements() as $element) {
197: $key = '_' . $uniqueIncrement;
198: if ($element->hasData($sortKey)) {
199: $key = $element->getDataUsingMethod($sortKey) . $key;
200: }
201: $elements[$key] = $element;
202: $uniqueIncrement++;
203: }
204:
205: if ($this->_sortDirection == SORT_ASC) {
206: ksort($elements, $this->_sortChildrenDirection);
207: } else {
208: krsort($elements, $this->_sortChildrenDirection);
209: }
210:
211: $elements = array_values($elements);
212: } else {
213: foreach ($this->getElements() as $element) {
214: $elements[] = $element;
215: }
216: }
217: return $elements;
218: }
219: }
220: