<?php
 class FormStructureVO implements MotoDomObjectInterface { public $formItems = array(); public $formControls = array(); public function __construct(DOMNode $parent = null) { if (!empty($parent)) { $this->loadDomElement($parent); } } public function loadDomElement(DOMNode $parent) { $nodes = MotoXML::findByXPath('.//formItems/item', $parent); if ($nodes != null) { foreach($nodes as $node) { if (!$node->hasAttribute('type')) $vo = 'FormTextItemVO'; else $vo = 'Form' . ucfirst($node->getAttribute('type')). 'ItemVO'; if (!class_exists($vo)) { throw new Exception($vo . " doesn't exists"); $vo = 'FormTextItemVO'; } if (class_exists($vo)) { $this->formItems[] = new $vo($node); } else { throw new Exception($vo . " doesn't exists"); } } } $this->formControls = FormControlItemVO::findAll(MotoXML::findOneByXPath('.//formControls', $parent)); return $this; } private function compare($a, $b) { if ($a->mailOrder == $b->mailOrder) return 0; return ($a->mailOrder < $b->mailOrder) ? -1 : 1; } public function saveDomElement(DOMNode $parent) { $formItemsNode = new DOMElement('formItems'); $parent->replaceChild($formItemsNode, MotoXML::findOneByXPath('.//formItems', $parent)); usort($this->formItems, array($this, "compare")); foreach ($this->formItems as $formItemVO) { $formItemVO->saveDomElement($formItemsNode->appendChild(new DOMElement('item'))); } $formControlsNode = new DOMElement('formControls'); $parent->replaceChild($formControlsNode, MotoXML::findOneByXPath('.//formControls', $parent)); foreach ($this->formControls as $formControlItemVO) { $formControlItemVO->saveDomElement($formControlsNode->appendChild(new DOMElement('item'))); } return $parent; } } ?>