<?php

/**
 * Goweb Sitemanager 5 Framework
 *
 * Classe para gerar menu do tipo JSCookMenu
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Js
 * @subpackage Tree
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */

/**
 * Goweb_Js_Tree_Node
 */
//Goweb::loadClass('Goweb_Js_JSCookMenu');

/**
 * Goweb_Tree
 */
Goweb::loadClass('Goweb_Tree');

/**
 * Classe para gerar menu do tipo JSCookMenu
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_Js
 * @subpackage Tree
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_Js_JSCookMenu {

    /**
     * Nome da variável da tree
     * 
     * @var  string
     */
    protected $_name = '';
    protected $_tree = null;
    protected $_html = '';

    
    public function __construct($name) {
        $this->_name = $name;
        $this->_tree = new Goweb_Tree('id', 'parentId');
    }

    public function buildFromTree(Goweb_Tree $tree) {
        $this->_html= $this->_build($tree->getTree());
        if (strlen($this->_html)) {
            $this->_html = substr($this->_html, 2);
            $this->_html = str_replace("''", "null", $this->_html);
            $this->_html = str_replace("'notLink'", "''", $this->_html);
            $this->_html = '<script type="text/javascript"><!-- var ' . $this->_name . ' = [' . $this->_html . ']; --></script>';            
        }
        return $this->_html;
    }
    
    public function addNode($id ,$parentId, $title, $description = '', $url = "#", $target = '_self', $icon = '') {
        $this->_tree->addElement(array('id'          => $id,
                                       'parentId'    => $parentId,
                                       'title'       => $title,
                                       'description' => $description,
                                       'url'         => $url,
                                       'target'      => $target,
                                       'icon'        => $icon));
    }

    public function build($parentId = 0) {
        $this->_tree->build($parentId);
        $this->_html = $this->_build($this->_tree->getTree());
        if (strlen($this->_html)) {
            $this->_html = substr($this->_html, 2);
            $this->_html = str_replace("''", "null", $this->_html);
             $this->_html = str_replace("'notLink'", "''", $this->_html);
            $this->_html = 'var ' . $this->_name . ' = [' . $this->_html . '];';
        }
        return $this->_html;
    }

    public function getHtml() {
        return $this->_html;
    }
    
    protected function _build($tree, $html = '') {
        $html = '';
        if ($tree) {
            foreach ($tree as $node) {
                $html .= ", ['";
                $html .= $node->icon . "','";
                
                // correcao do problema com as plicas
                $titulo = str_replace("\\", "", $node->title);
                $titulo = str_replace("\\'", "'", $titulo);
                $titulo = str_replace("\'", "'", $titulo);
                $titulo = str_replace("'", "\'", $titulo);
                
                $html .= htmlentities($titulo, ENT_NOQUOTES, CHARSET) . "','";                
                $html .= $node->url . "','";
                $html .= $node->target . "','";
                $html .= htmlentities($node->description) . "'";
                if (count($node->childs) > 0) {
                    $html .= $this->_build($node->childs);                    
                }
                $html .= "]";
            }

        }
        return $html;        
    }
}
