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 class
29: *
30: * @category Mage
31: * @package Mage_XmlConnect
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_XmlConnect_Model_Simplexml_Form extends Mage_XmlConnect_Model_Simplexml_Form_Abstract
35: {
36: /**
37: * All form elements collection
38: *
39: * @var Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
40: */
41: protected $_allElements;
42:
43: /**
44: * form elements index
45: *
46: * @var array
47: */
48: protected $_elementsIndex;
49:
50: /**#@+
51: * Custom form components renderer
52: *
53: * @var object
54: */
55: static protected $_defaultElementRenderer;
56: static protected $_defaultFieldsetRenderer;
57: static protected $_defaultValidatorRenderer;
58: static protected $_defaultValidatorRuleRenderer;
59: static protected $_defaultFieldsetElementRenderer;
60: /**#@-*/
61:
62: /**
63: * Init simple xml form
64: *
65: * @param array $attributes
66: */
67: public function __construct($attributes = array())
68: {
69: parent::__construct($attributes);
70: $this->setType('form');
71: $this->_allElements = Mage::getModel('xmlconnect/simplexml_form_element_collection', $this);
72: }
73:
74: /**
75: * Set element renderer
76: *
77: * @static $_defaultElementRenderer
78: * @param Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
79: * @return null
80: */
81: public static function setElementRenderer(
82: Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
83: ) {
84: self::$_defaultElementRenderer = $renderer;
85: }
86:
87: /**
88: * Set validator renderer
89: *
90: * @static $_defaultValidatorRenderer
91: * @param Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
92: * @return null
93: */
94: public static function setValidatorRenderer(
95: Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
96: ) {
97: self::$_defaultValidatorRenderer = $renderer;
98: }
99:
100: /**
101: * Set validator rule renderer
102: *
103: * @static $_defaultValidatorRuleRenderer
104: * @param Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
105: * @return null
106: */
107: public static function setValidatorRuleRenderer(
108: Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
109: ) {
110: self::$_defaultValidatorRuleRenderer = $renderer;
111: }
112:
113: /**
114: * Set fieldset renderer
115: *
116: * @static $_defaultFieldsetRenderer
117: * @param Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
118: * @return null
119: */
120: public static function setFieldsetRenderer(
121: Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
122: ) {
123: self::$_defaultFieldsetRenderer = $renderer;
124: }
125:
126: /**
127: * Set fieldset element renderer
128: *
129: * @static $_defaultFieldsetElementRenderer
130: * @param Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
131: * @return null
132: */
133: public static function setFieldsetElementRenderer(
134: Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer
135: ) {
136: self::$_defaultFieldsetElementRenderer = $renderer;
137: }
138:
139: /**
140: * Get element renderer
141: *
142: * @static $_defaultElementRenderer
143: * @return object
144: */
145: public static function getElementRenderer()
146: {
147: return self::$_defaultElementRenderer;
148: }
149:
150: /**
151: * Get validator renderer
152: *
153: * @static $_defaultValidatorRenderer
154: * @return object
155: */
156: public static function getValidatorRenderer()
157: {
158: return self::$_defaultValidatorRenderer;
159: }
160:
161: /**
162: * Get validator rule renderer
163: *
164: * @static $_defaultValidatorRuleRenderer
165: * @return object
166: */
167: public static function getValidatorRuleRenderer()
168: {
169: return self::$_defaultValidatorRuleRenderer;
170: }
171:
172: /**
173: * Get fieldset renderer
174: *
175: * @static $_defaultFieldsetRenderer
176: * @return object
177: */
178: public static function getFieldsetRenderer()
179: {
180: return self::$_defaultFieldsetRenderer;
181: }
182:
183: /**
184: * Get fieldset element renderer
185: *
186: * @static $_defaultFieldsetElementRenderer
187: * @return object
188: */
189: public static function getFieldsetElementRenderer()
190: {
191: return self::$_defaultFieldsetElementRenderer;
192: }
193:
194: /**
195: * Add form element
196: *
197: * @param Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element
198: * @param bool $after
199: * @return Mage_XmlConnect_Model_Simplexml_Form
200: */
201: public function addElement(Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element, $after = false)
202: {
203: $this->checkElementId($element->getId());
204: parent::addElement($element, $after);
205: $this->addElementToCollection($element);
206: return $this;
207: }
208:
209: /**
210: * Check existing element
211: *
212: * @param string $elementId
213: * @return bool
214: */
215: protected function _elementIdExists($elementId)
216: {
217: return isset($this->_elementsIndex[$elementId]);
218: }
219:
220: /**
221: * Add form element to collection
222: *
223: * @param $element
224: * @return Mage_XmlConnect_Model_Simplexml_Form
225: */
226: public function addElementToCollection($element)
227: {
228: $this->_elementsIndex[$element->getId()] = $element;
229: $this->_allElements->add($element);
230: return $this;
231: }
232:
233: /**
234: * Insure existing element
235: *
236: * @throws Exception
237: * @param $elementId
238: * @return bool
239: */
240: public function checkElementId($elementId)
241: {
242: if ($this->_elementIdExists($elementId)) {
243: throw new Exception(Mage::helper('xmlconnect')->__('Element with id %s already exists', $elementId));
244: }
245: return true;
246: }
247:
248: /**
249: * Get form object
250: *
251: * @return Mage_XmlConnect_Model_Simplexml_Form
252: */
253: public function getForm()
254: {
255: return $this;
256: }
257:
258: /**
259: * Get element object
260: *
261: * @param $elementId
262: * @return null|Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract
263: */
264: public function getElement($elementId)
265: {
266: if ($this->_elementIdExists($elementId)) {
267: return $this->_elementsIndex[$elementId];
268: }
269: return null;
270: }
271:
272: /**
273: * Set values to the form elements
274: *
275: * @param array $values
276: * @return Mage_XmlConnect_Model_Simplexml_Form
277: */
278: public function setValues($values)
279: {
280: foreach ($this->_allElements as $element) {
281: if (isset($values[$element->getId()])) {
282: $element->setValue($values[$element->getId()]);
283: } else {
284: $element->setValue(null);
285: }
286: }
287: return $this;
288: }
289:
290: /**
291: * Add values to the form elements
292: *
293: * @param array $values
294: * @return Mage_XmlConnect_Model_Simplexml_Form
295: */
296: public function addValues($values)
297: {
298: if (!is_array($values)) {
299: return $this;
300: }
301:
302: foreach ($values as $elementId => $value) {
303: $element = $this->getElement($elementId);
304: if ($element) {
305: $element->setValue($value);
306: }
307: }
308: return $this;
309: }
310:
311: /**
312: * Remove field from collection
313: *
314: * @param string $elementId
315: * @return Mage_XmlConnect_Model_Simplexml_Form
316: */
317: public function removeField($elementId)
318: {
319: if ($this->_elementIdExists($elementId)) {
320: unset($this->_elementsIndex[$elementId]);
321: }
322: return $this;
323: }
324:
325: /**
326: * Set field id prefix
327: *
328: * @param string $prefix
329: * @return Mage_XmlConnect_Model_Simplexml_Form
330: */
331: public function setFieldContainerIdPrefix($prefix)
332: {
333: $this->setData('field_container_id_prefix', $prefix);
334: return $this;
335: }
336:
337: /**
338: * Get field container id prefix
339: *
340: * @return mixed
341: */
342: public function getFieldContainerIdPrefix()
343: {
344: return $this->getData('field_container_id_prefix');
345: }
346:
347: /**
348: * Retrieve form xml object or an array of Simplexml elements
349: *
350: * @return array|Mage_XmlConnect_Model_Simplexml_Element
351: */
352: public function toXmlObject()
353: {
354: $xmlObj = $this->getXmlObject();
355: if ($useContainer = $this->getUseContainer()) {
356: $this->_addRequiredAttributes($xmlObj);
357: foreach ($this->getAttributes() as $key => $val) {
358: $xmlObj->addAttribute($key, $xmlObj->xmlAttribute($val));
359: }
360: }
361:
362: foreach ($this->getElements() as $element) {
363: $xmlObj->appendChild($element->toXmlObject());
364: }
365:
366: if (!$useContainer) {
367: $result = array();
368: foreach ($xmlObj->children() as $child) {
369: $result[] = $child;
370: }
371: }
372: return isset($result) ? $result : $xmlObj;
373: }
374:
375: /**
376: * Get from xml as string
377: *
378: * @throws Mage_Core_Exception
379: * @return string
380: */
381: public function getXml()
382: {
383: if ($this->getUseContainer()) {
384: return $this->toXmlObject()->asNiceXml();
385: }
386: Mage::throwException(Mage::helper('xmlconnect')->__('Container is not defined.'));
387: }
388: }
389: