<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class XMLWork
{
  
  public $attrList;

  
  public function addDataModul2Node($pDataModul, $pXMLNode, $pRootName = '')
  {
    if (is_array($pDataModul)) {
      foreach ($pDataModul as $pDataModulItem) {
        $this->addDataModul2Node($pDataModulItem, $pXMLNode);
      }

      return;
    }

    
    $className = get_class($pDataModul);

    if (empty($this->rootName) or ($this->rootName == $className)) {
      $pRootName = $className;
      $rootNode = new XMLNode($pRootName, '');
    } else {
      $rootNode = new XMLNode($pRootName, '', 'class="' . $className . '"');
    }


    $this->attrList = get_object_vars($pDataModul);
    $this->onBeforeAddDataModul2Node($pDataModul, $rootNode); 


    foreach ($this->attrList as $attrName => $attrValue) {
      if ((is_string($attrValue) && $attrValue != '') || (is_int($attrValue))) {     
        $rootNode->addChild(new XMLNode($attrName, $attrValue));
      } elseif (is_a($attrValue, 'DataModul')) {
        $this->addDataModul2Node($attrValue, $rootNode, $attrName);
      } elseif (is_array($attrValue)) {
        $node = new XMLNode($attrName);
        $this->addDataModul2Node($attrValue, $node);
        $rootNode->addChild($node);
      }
    }
    $this->onAfterAddDataModul2Node($pDataModul, $rootNode); 
    $pXMLNode->addChild($rootNode);
  }

  
  public function onBeforeAddDataModul2Node($pDataModul, $XMLNode)
  {
  }

  
  public function onAfterAddDataModul2Node($pDataModul, $XMLNode)
  {
  }
}