1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_Model_Simplexml_Form_Abstract extends Varien_Object
35: {
36: 37: 38: 39: 40:
41: protected $_id;
42:
43: 44: 45: 46: 47:
48: protected $_elements;
49:
50: 51: 52: 53: 54:
55: protected $_types = array();
56:
57: 58: 59: 60: 61:
62: protected $_xml;
63:
64: 65: 66: 67: 68:
69: protected $_mainNode = 'form';
70:
71: 72: 73: 74: 75:
76: protected $_nameRequired = true;
77:
78: 79: 80: 81: 82:
83: protected $_customAttributes = array();
84:
85: 86: 87: 88: 89:
90: public function __construct($attributes = array())
91: {
92: parent::__construct($attributes);
93: $this->_prepareXmlObject();
94: }
95:
96: 97: 98: 99: 100:
101: protected function _prepareXmlObject()
102: {
103: $this->setXmlObject(
104: Mage::getModel('xmlconnect/simplexml_element', '<' . $this->_mainNode . '></' . $this->_mainNode . '>')
105: );
106: return $this;
107: }
108:
109: 110: 111: 112: 113:
114: public function getXmlObject()
115: {
116: return $this->_xml;
117: }
118:
119: 120: 121: 122: 123: 124:
125: public function setXmlObject(Mage_XmlConnect_Model_Simplexml_Element $xml)
126: {
127: $this->_xml = $xml;
128: return $this;
129: }
130:
131: 132: 133: 134: 135:
136: public function getId()
137: {
138: return $this->_id;
139: }
140:
141: 142: 143: 144: 145: 146:
147: public function setId($id)
148: {
149: $this->_id = $id;
150: $this->setData('xml_id', $id);
151: return $this;
152: }
153:
154: 155: 156: 157: 158:
159: public function getXmlId()
160: {
161: return $this->getXmlIdPrefix() . $this->getData('xml_id') . $this->getXmlIdSuffix();
162: }
163:
164: 165: 166: 167: 168: 169: 170:
171: public function addType($type, $className)
172: {
173: $this->_types[$type] = $className;
174: return $this;
175: }
176:
177: 178: 179: 180: 181:
182: public function getElements()
183: {
184: if (empty($this->_elements)) {
185: $this->_elements = Mage::getModel('xmlconnect/simplexml_form_element_collection', $this);
186: }
187: return $this->_elements;
188: }
189:
190: 191: 192: 193: 194: 195: 196:
197: public function addElement(Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element, $after = false)
198: {
199: $element->setForm($this);
200: $this->getElements()->add($element, $after);
201: return $this;
202: }
203:
204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216:
217: public function addField($elementId, $type, $config, $after = false)
218: {
219: if (isset($this->_types[$type])) {
220: $className = $this->_types[$type];
221: } else {
222: $className = 'Mage_XmlConnect_Model_Simplexml_Form_Element_' . uc_words($type);
223: }
224:
225: $element = Mage::getModel($className, $config);
226: $element->setId($elementId);
227: $this->addElement($element, $after);
228: return $element;
229: }
230:
231: 232: 233: 234: 235: 236:
237: public function removeField($elementId)
238: {
239: $this->getElements()->remove($elementId);
240: return $this;
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250:
251: public function addFieldset($elementId, $config = array(), $after = false)
252: {
253: $element = Mage::getModel('xmlconnect/simplexml_form_element_fieldset', $config);
254: $element->setId($elementId);
255: $this->addElement($element, $after);
256: return $element;
257: }
258:
259: 260: 261: 262: 263: 264: 265:
266: public function addValidator($config = array(), $after = false)
267: {
268: $element = Mage::getModel('xmlconnect/simplexml_form_element_validator', $config);
269: $element->setId($this->getXmlId());
270: $this->addElement($element, $after);
271: return $element;
272: }
273:
274: 275: 276: 277: 278: 279:
280: public function __toArray(array $arrAttributes = array())
281: {
282: $res = array();
283: $res['config'] = $this->getData();
284: $res['formElements']= array();
285: foreach ($this->getElements() as $element) {
286: $res['formElements'][] = $element->toArray();
287: }
288: return $res;
289: }
290:
291: 292: 293: 294: 295:
296: public function getXmlAttributes()
297: {
298: return array('enctype');
299: }
300:
301: 302: 303: 304: 305:
306: public function getRequiredXmlAttributes()
307: {
308: return array('action' => null, 'method' => 'post');
309: }
310:
311: 312: 313: 314: 315:
316: public function getAfterElementXml()
317: {
318: return $this->getData('after_element_xml');
319: }
320:
321: 322: 323: 324: 325: 326:
327: public function getXmlObjAttributes($attributes = array())
328: {
329: $data = array();
330: if (empty($attributes)) {
331: $attributes = array_keys($this->_data);
332: }
333:
334: foreach ($this->_data as $key => $value) {
335: if (in_array($key, $attributes)) {
336: $data[$key] = $value;
337: }
338: }
339: ksort($data);
340: return $data;
341: }
342:
343: 344: 345: 346: 347:
348: public function getAttributes()
349: {
350: $attributes = array_merge($this->getXmlAttributes(), $this->getCustomAttributes());
351: return $this->getXmlObjAttributes($attributes);
352: }
353:
354: 355: 356: 357: 358: 359:
360: public function addAfterXmlElementToObj(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
361: {
362: if ($this->_checkXmlInstance($this->getAfterElementXml())) {
363: $xmlObj->appendChild($this->getAfterElementXml());
364: } elseif (is_array($this->getAfterElementXml())) {
365: foreach ($this->getAfterElementXml() as $afterElement) {
366: if (!$this->_checkXmlInstance($afterElement)) {
367: continue;
368: }
369: $xmlObj->appendChild($afterElement);
370: }
371: }
372: return $this;
373: }
374:
375: 376: 377: 378: 379: 380: 381:
382: protected function _addRequiredAttributes(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
383: {
384: $this->_addId($xmlObj);
385: $this->_addName($xmlObj);
386:
387: foreach ($this->getRequiredXmlAttributes() as $attribute => $defValue) {
388: $data = $this->getData($this->_underscore($attribute));
389: if ($data) {
390: $xmlObj->addAttribute($attribute, $xmlObj->xmlAttribute($data));
391: } elseif(null !== $defValue){
392: $xmlObj->addAttribute($attribute, $xmlObj->xmlAttribute($defValue));
393: } else {
394: Mage::throwException(Mage::helper('xmlconnect')->__('%s attribute is required.', $attribute));
395: }
396: }
397: return $this;
398: }
399:
400: 401: 402: 403: 404: 405:
406: protected function _addValidator(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
407: {
408: return $this;
409: }
410:
411: 412: 413: 414: 415: 416: 417:
418: protected function _addId(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
419: {
420: if ($this->getXmlId()) {
421: $xmlObj->addAttribute('id', $xmlObj->xmlAttribute($this->getXmlId()));
422: } else {
423: Mage::throwException(
424: Mage::helper('xmlconnect')->__('"id" attribute is required for a "%s" field.', $this->getType())
425: );
426: }
427: return $this;
428: }
429:
430: 431: 432: 433: 434: 435:
436: protected function _addName(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
437: {
438: if ($this->getName()) {
439: $name = $this->getName();
440: } elseif($this->getNameRequired()) {
441: $name = $this->getXmlId();
442: }
443:
444: if (isset($name)) {
445: $xmlObj->addAttribute('name', $xmlObj->xmlAttribute($name));
446: }
447:
448: return $this;
449: }
450:
451: 452: 453: 454: 455: 456:
457: protected function _checkXmlInstance($object)
458: {
459: return $object instanceof Mage_XmlConnect_Model_Simplexml_Element;
460: }
461:
462: 463: 464: 465: 466:
467: public function getNameRequired()
468: {
469: return $this->_nameRequired;
470: }
471:
472: 473: 474: 475: 476: 477:
478: public function setNameRequired($nameRequired)
479: {
480: $this->_nameRequired = $nameRequired;
481: return $this;
482: }
483:
484: 485: 486: 487: 488:
489: public function getCustomAttributes()
490: {
491: return $this->_customAttributes;
492: }
493:
494: 495: 496: 497: 498: 499:
500: public function setCustomAttributes(array $customAttributes)
501: {
502: $this->_customAttributes = $customAttributes;
503: return $this;
504: }
505:
506: 507: 508: 509: 510: 511: 512:
513: public function checkAttribute($attribute, $value = null)
514: {
515: if (null === $value) {
516: $value = $this->getData($attribute);
517: }
518:
519: if (null !== $value) {
520: return array($attribute => $value);
521: }
522: return array();
523: }
524: }
525: