<?php

/**
 * Goweb_Form_Element_TafelTree
 *
 * @category    Goweb
 * @package     Goweb_Form
 * @subpackage  Element
 * @version     5.0.19
 * @author      I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright   2010 (c) Goweb (http://www.goweb.pt)
 */

/**
 * Goweb_Form_Element
 */
require_once 'Zend/Form/Element.php';

/**
 * Classe para representar um elemento do tipo TafelTree
 *
 * @version    	5.0.19
 * @category    Goweb
 * @package     Goweb_Form
 * @subpackage  Element
 * @author      I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright   2010 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_Form_Element_TafelTree extends Zend_Form_Element {

    /**
     * Parâmetros de configuração da Tree
     *
     * @var array
     */
    protected $_configTree = array();

    /**
     * Constroi o input em XHTML.
     *
     * @return string
     */
    public function render() {
        if (!($this->_configTree['tree'] instanceof Goweb_Js_TafelTree)) {
            throw new Goweb_Exception('O parâmetro "tree" tem de ser um objecto to tipo Goweb_Js_TafelTree');
        }
        Goweb::loadCss('/admin/media/js/tafelTree/css/tree.css');
        Goweb::loadJavascript('/admin/media/js/prototype.js');
        Goweb::loadJavascript('/admin/media/js/scriptaculous/scriptaculous.js');
        Goweb::loadJavascript('/admin/media/js/tafelTree/Tree-optimized.js');
        return $this->_configTree['tree']->display();
    }

    /**
     * Define o objecto Tree a ser usado
     *
     * @param Goweb_Js_TafelTree $tree
     */
    public function setTree($tree){
        $this->_configTree['tree'] = $tree;
    }

    /**
     * Atribui um valor ao elemento.
     *
     * @param  string  $value  O valor do elemento
     */
    public function setValue($value) {
        $this->_value = $value;
    }

    /**
     * Magic function de compatibilidade
     *
     * @param string $name
     * @param mixed $value
     * @return mixed
     */
    public function __set($name, $value){
        if (strcmp($name, 'tree') == 0) $this->setTree($value);
        if (strcmp($name, 'expand') == 0) $this->_configTree['expand'] = $value;
        return parent::__set($name, $value);
    }
}