<?
class frmTreemenu extends MForm
{
        public $query;

		function __construct() 
		{
			parent::__construct('MTreeMenu Sample');
			if($this->page->isPostBack())
			{
				$this->eventHandler();
			}
            $this->addField(new ViewSource(__FILE__));
		}

		function createFields()
		{
            $array = array(
               array(0,'BaseControl','','root'),
               array(1,'Control1','',0),
               array(2,'Control2','',0),
               array(3,'Control3','',0),
               array(4,'Control4','',0),
               array(5,'Control5','',0),
               array(6,'Control11','',1),
               array(7,'Control21','',2),
               array(8,'Control22','',2),
               array(9,'Control23','',2),
               array(10,'Control41','',4),
               array(11,'Control111','',6),
               array(12,'Control112','',6),
               array(13,'Control113','',6),
               array(14,'Control12','',1),
               array(15,'Control121','',14),
               array(16,'Control122','',14),
               array(17,'Control123','',14)
            );

            $aluno = $this->manager->getBusiness('example','aluno');
            $criteria =  $aluno->getCriteria();
            $criteria->addColumnAttribute('substr(nome,1,1)','first');
            $criteria->addColumnAttribute('nome');
            $criteria->addOrderAttribute('nome');
            $this->query = $criteria->retrieveAsQuery();

            $fields = array(
    	       new MSeparator('<b>Javascript Event - click on leaf node</b>'),
               new MTreeMenu('tree1'),
               array(
    	           new MTextField('leaf0','','Selected'),
    	           new MTextField('leaf0id','','Id'),
               ),
	           new MSeparator(''),
	           new MSeparator('<b>Miolo Event - submit on click on leaf node</b>'),
               new MTreeMenu('tree2'),
               new MDiv('divitemid',''),
	           new MSeparator(''),
	           new MSeparator('<b>Tree from Query</b>'),
               new MTreeMenu('tree3',2),
               new MDiv('itemaluno',''),
            );
            $this->setFields($fields);
            $this->tree1->setItemsFromArray($array);
            $this->tree1->setSelectEvent("miolo.getElementById(\"leaf0\").value = item.description; miolo.getElementById(\"leaf0id\").value = item.id;");
            $this->tree2->setItemsFromArray($array);
            $this->tree2->setEventHandler('OnTree2Click');
            $this->tree3->setItemsFromResult($this->query->result,'Alunos','0','1');
            $this->tree3->setEventHandler();
       }

       public function onTree2Click($itemId)
       {
            $field = new MText('itemid',"<b>Tree: $name  -  Item Id: $itemId</b>");
            $this->setResponse($field,'divitemid');
       }

       public function tree3_click($itemId)
       {
            $field = new MText('itemalunoid',"<b>Item Id: $itemId</b>");
            $this->setResponse($field,'itemaluno');
       }

}
?>