<?php

/***
 * 
 * @file
 *
 * @brief 
 *  
 * */

defined( 'RMAGIC' ) or die( 'Restricted access' );

class CTreeViewDTComponent extends CDTComponent
{
	function __construct($name, $options=null)
	{
		parent::__construct($name, $options);
	}
	
	function CTreeViewDTComponent($name, $options=null)
	{
		$this->__construct($name, $options);
	}
	
	protected function show(&$options=array()) 
	{
		parent::show($options);
		
		$id = $this->_id;
		$tablename = $this->_modname;
		$table_id = $tablename.rand();
		
		
		$sortName = isset($_COOKIE['sortName'])?$_COOKIE['sortName']:'';
		$sortOrder = isset($_COOKIE['sortOrder'])?$_COOKIE['sortOrder']:'';
		
		$m = $this->getModel();
		
		//²ιΡ―
		$parentdb = array();
		$m->getParents($id, $parentdb);
		//keyword
		$this->getParams($params);
		if (!$params)
			$params['pid']=$id;
		if ($sortName)
			$params['__orderby'] = array($sortName=>$sortOrder);
		
		$this->initParamsForShow($params, $options);
		
		$rows = $m->selectForView($params, $options);
		
		$modinfo = $m->getModelInfo();
		$fdb = $params['fdb'];
		$pkey = $params['pkey'];
		
		$_base = $options['_base'];
		foreach($rows as $key=>&$v)  {
			$name = $v['name'];
			$id = $v['id'];
			if ($m->hasChildren($id))
				$v['_name'] = "<a href='$_base?id=$id'>$name</a>";
		}
		
		
		//array_reverse($parentdb);
		//nav
		$nav = '';
		foreach ($parentdb as $k2 => $v2) {
			$nav = "<i class='fa fa-angle-right'> </i> <a href='$_base?id=$v2[id]'> $v2[name] </a>".$nav;
		}
		
		$this->assign('sortName', $sortName);
		$this->assign('sortOrder', $sortOrder);			
		$this->assign('nav', $nav);			
		$this->assign('table_id', $table_id);			
		$this->assign('tablename', $tablename);
		$mi18n = get_i18n('mod_'.$tablename);
		$this->assign('mi18n', $mi18n);		
		$this->assign('fdb', $fdb);
		$this->assign('pkey', $fdb[$pkey]);
		$this->assign('tabledata', $rows);		
		
		$this->setTpl('treeview');
		
		return $rows;	
	}
}