<?php
/**
 * Goweb_Xml_Maker
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Xml
 * @copyright  Copyright (c) 2009 Goweb (http://www.goweb.pt)
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
*/
class Goweb_Xml_Maker {

    /*
    * XML Array
    * @var array
    * @access private
    */
    private $_XMLArray;

    /*
    * array is OK
    * @var bool
    * @access private
    */
    private $_arrayOK;

    /*
    * XML file name
    * @var string
    * @access private
    */
    private $_XMLFile;

    /*
    * file is present
    * @var bool
    * @access private
    */
    private $_fileOK;

    /*
    * DOM document instance
    * @var DOMDocument
    * @access private
    */
    private $_doc;

    /**
    * Constructor
    * @access public
    */
    public function __construct() {}

    /**
    * setteur setXMLFile
    * @access public
    * @param string $XMLFile
    * @return bool
    */
    public function setXMLFile($XMLFile){
        if (file_exists($XMLFile)){
            $this->_XMLFile = $XMLFile;
            $this->_fileOK = true;
        } else {
            $this->_fileOK = false;
        }
        return $this->_fileOK;
    }

    /**
    * saveXml
    * @access public
    * @param string $XMLFile
    * @param string $rootName
    * @param string $encoding
    * @return bool
    */
    public function saveXml($XMLFile, $rootName = "", $encoding = "utf-8") {
        global $debug;
        $this->_doc = new DOMDocument("1.0", $encoding);
        $arr = array();
        if (count($this->_XMLArray) > 1){
            if ($rootName != ""){
                $root = $this->_doc->createElement($rootName);
            } else {
                $root = $this->_doc->createElement("root");
                $rootName = "root";
            }
            $arr = $this->_XMLArray;
        } else {
            $key = key($this->_XMLArray);
            $val = $this->_XMLArray[$key];

            if (!is_int($key)){
                $root = $this->_doc->createElement($key);
                $rootName = $key;
            } else {
                if ($rootName != ""){
                    $root = $this->_doc->createElement($rootName);
                } else {
                    $root = $this->_doc->createElement("root");
                    $rootName = "root";
                }
            }
            $arr = $this->_XMLArray[$key];
        }

        $root = $this->_doc->appendChild($root);

        $this->addArray($arr, $root, $rootName);

        if ($this->_doc->save($XMLFile) == 0){
            return false;
        } else {
            chmod($XMLFile, 0777);
            return true;
        }
        
    }

    /**
    * displayXml
    * @access public
    * @param string $XMLFile
    * @param string $rootName
    * @param string $encoding
    * @return string
    */
    public function displayXml($XMLFile, $rootName = "", $encoding = "utf-8") {
        global $debug;
        $this->_doc = new DOMDocument("1.0", $encoding);
        $arr = array();
        if (count($this->_XMLArray) > 1){
            if ($rootName != ""){
                $root = $this->_doc->createElement($rootName);
            } else {
                $root = $this->_doc->createElement("root");
                $rootName = "root";
            }
            $arr = $this->_XMLArray;
        } else {
            $key = key($this->_XMLArray);
            $val = $this->_XMLArray[$key];

            if (!is_int($key)){
                $root = $this->_doc->createElement($key);
                $rootName = $key;
            } else {
                if ($rootName != ""){
                    $root = $this->_doc->createElement($rootName);
                } else {
                    $root = $this->_doc->createElement("root");
                    $rootName = "root";
                }
            }
            $arr = $this->_XMLArray[$key];
        }

        $root = $this->_doc->appendChild($root);

        $this->addArray($arr, $root, $rootName);

        return $this->_doc->saveXML();
    }

    /**
    * addArray recursive function 
    * @access public
    * @param array $arr
    * @param DomNode &$n
    * @param string $name
    */
    public function addArray($arr, &$n, $name=""){
        foreach ($arr as $key => $val){
            if (is_int($key)){
                if (strlen($name)>1){
                    $newKey = substr($name, 0, strlen($name)-1);
                } else {
                    $newKey = "item";
                }
            } else {
                $newKey = $key;
            }

            $node = $this->_doc->createElement($newKey);
            if (is_array($val)) {
                $this->addArray($arr[$key], $node, $key);
            } else {
                if ($val instanceof Goweb_Db_Model_Row) {
                    $valAux = $val->__toString();
                    $val = $valAux;
                }
                if (CHARSET == 'UTF-8') $val = utf8_decode($val);
                $nodeText = $this->_doc->createTextNode($val);
                $node->appendChild($nodeText);
            }
            $n->appendChild($node);
        }
    }

    /**
    * setteur setArray
    * @access public
    * @param array $XMLArray
    * @return bool
    */
    public function setArray($XMLArray){
        if ((is_array($XMLArray)) && (count($XMLArray) != 0)){
            $this->_XMLArray = $XMLArray;
            $this->_arrayOK = true;
        } else {
            $this->_arrayOK = false;
        }
        return $this->_arrayOK;
    }

}
