<?php

class node{
	public $id;
	public $path;
	public $depth;
	public $order;
	public $nodeDataId;
	public $nodeData = array(
		'name'=>'',
		'name_url'=>'',
		'title'=>'',
		'description'=>'',
		'text'=>'',
		'published'=>'',
		'sitemap'=>'',
		'link'=>'',
		'template'=>'',
		'menu'=>'',
		'photo'=>'',
		'mini'=>'',
		'link_val'=>'',
		'menu2'=>'',
		'added'=>''
	);
	public $model;
	public $userId;
	
	private $newId;
	private $lang_nr;
	
	function node($model, $nodeId=false) {
		
		$this->model = $model;
		$this->lang_nr = $this->model->lang_nr;
		
		if($nodeId) {
			
			if($this->model->checkId($nodeId) == FALSE) return NULL;
			$this->getNodeData($nodeId);
			$this->depth= $this->getNodeDepth();
			
		}
	}
	
	public function getUserId(){
		return $this->userId;
	}
	
	private function getNodeData($nodeId){
		$this->id=$nodeId;
		$row = $this->model->getNode($nodeId);
		if($row) {
			$this->path= $row->tree_path;
			$this->order= $row->tree_node_order;
			$this->nodeDataId=$row->tree_node_id;
			$this->userId = $row->tree_user_id;
		}
		$row2 = $this->model->getNodeData($this->nodeDataId);
		if($row2){
			$this->nodeData['name']= $row2->nodes_name;
			$this->nodeData['name_url']= $row2->nodes_name_url;
			$this->nodeData['title']= $row2->nodes_title;
			$this->nodeData['description']= $row2->nodes_description;
			$this->nodeData['text']= $row2->nodes_text;
			$this->nodeData['published']= $row2->nodes_published;
			$this->nodeData['sitemap']= $row2->nodes_sitemap;
			$this->nodeData['template']= $row2->nodes_template;
			$this->nodeData['link']= $row2->nodes_link;
			$this->nodeData['menu']= $row2->nodes_menu; 
			$this->nodeData['mini']= $row2->nodes_mini;
			$this->nodeData['photo']= $row2->nodes_photo;
			$this->nodeData['link_val']= $row2->nodes_link_val;
			$this->nodeData['menu2']= $row2->nodes_menu2;
			$this->nodeData['added']= $row2->nodes_added;
		}
	}
	
	public function getNodeDepth()
	{
		$count = substr_count($this->path, '.');
		return $count;
	}
	
	public function getNodeChildsCount() {
		if (!$this->id) return 0;
		return $this->model->getNodeChildsCount($this->path, $this->userId);
	}
	
	public function getNodeFirtLevelChilds() {
		$result = $this->model->getNodeFirtLevelChilds($this->path, $this->userId);
		if($result->num_rows()>0) {
			foreach($result->result() as $row) {
				$nodeChild = new node($this->model, $row->ID_tree);
				$level[]=$nodeChild;
			}
			return $level;
		}
		else return 0;
	}
	
	public function getNodeFirtLevelChilds2() {
		$result = $this->model->getNodeFirtLevelChilds($this->path, $this->userId);
		if($result->num_rows()>0) {
			foreach($result->result() as $row) {
				$nodeChild = new node($this->model, $row->ID_tree);
				if($nodeChild->nodeData['published'] == 1)// $level[]=$nodeChild;
					$level[] = array(
						'name'=>$nodeChild->nodeData['name'],
						'name_url'=>$nodeChild->nodeData['name_url'],
						'title'=>$nodeChild->nodeData['title'],
						'description'=>$nodeChild->nodeData['description'],
						'text'=>$nodeChild->nodeData['text'],
						'published'=>$nodeChild->nodeData['published'],
						'sitemap'=>$nodeChild->nodeData['sitemap'],
						'link'=>$nodeChild->nodeData['link'],
						'template'=>$nodeChild->nodeData['template'],
						'menu'=>$nodeChild->nodeData['menu'],
						'photo'=>$nodeChild->nodeData['photo'],
						'mini'=>$nodeChild->nodeData['mini'],
						'link_val'=>$nodeChild->nodeData['link_val'],
						'menu2'=>$nodeChild->nodeData['menu2'],
						'added'=>$nodeChild->nodeData['added']
					);
			}
			return $level;
		}
		else return 0;
	}
	
	public function addChild($node) {
		$node->order = $this->getMaxOrder()+1;
		
		
		$data = array(
			'nodes_name' => $node->name,
			'nodes_name_url' => $node->name_url,
			'nodes_title' => $node->title,
			'nodes_description' => $node->description,
			'nodes_text' => $node->text,
			'nodes_published' => $node->published,
			'nodes_sitemap' => $node->sitemap,
			'nodes_template' => $node->template,
			'nodes_link' => $node->link,
			'nodes_menu' => $node->menu,
			'nodes_mini' => $node->mini,
			'nodes_link_val' => $node->link_val,
			'nodes_menu2' => $node->menu2
		//,			'nodes_lang' => $this->lang_nr
			//,		'nodes_photo' => $node->photo
		);
		
		
		$this->newId = $this->model->addChild($data, $node->order, $this->path, $this->userId, $node->photo, $node->x, $node->y);
	}
	
	public function getNewId(){
		return $this->newId;
	}
	
	private function getMaxOrder() {
		return $this->model->getMaxOrder($this->path, $this->userId);
	}
	
	public function updateNode($node) {
		$treeId = $node->id;
		$data = array(
			'nodes_name' => $node->name,
			'nodes_name_url' => $node->name_url,
			'nodes_title' => $node->title,
			'nodes_description' => $node->description,
			//'nodes_text' => $node->text,
			'nodes_published' => $node->published,
			'nodes_sitemap' => $node->sitemap,
			'nodes_template' => $node->template,
			'nodes_link' => $node->link,
			'nodes_menu' => $node->menu,
			//'nodes_mini' => $node->mini,
			'nodes_link_val' => $node->link_val,
			'nodes_menu2' => $node->menu2
			//,		'nodes_photo' => $node->photo
		);
		$this->model->updateNode($data, $treeId, $node->photo, $node->x, $node->y, $node->delphoto);
	}
	
	public function updateNodeText($node){
		$treeId = $node->id;
		$data = array(
			'nodes_text' => $node->text
		);
		$this->model->updateNodeText($data, $treeId);
	}
	
	public function deleteNode() {
		$this->model->deletenode($this->path, $this->userId);	
		$this->model->refreshTreeOrder($this->getParentPath(), $this->userId);	
	}
	
	public function getParentPath()
	{
		$pathExploded = explode('.',$this->path);
		
		$counter = count($pathExploded);
		$parentPath = 'root';
		for($i=1;$i<$counter-1;$i++){
			$parentPath .= '.'.$pathExploded[$i];
		}
		
		return $parentPath;
	}
	
	public function moveup(){
		$this->model->refreshTreeOrder($this->getParentPath(), $this->userId);
		
		$this->order = $this->model->getNodeOrderForId($this->id);
		$this->model->moveup($this->id, $this->getParentPath(), $this->order, $this->userId);
	}
	
	public function movedown(){
		$this->model->refreshTreeOrder($this->getParentPath(), $this->userId);

		$this->order = $this->model->getNodeOrderForId($this->id);
		$this->model->movedown($this->id, $this->getParentPath(), $this->order, $this->userId);
	}
	
	public function getParentsNodes()
	{
		$parentsObj=array();
		$parentsIds = explode('.',$this->path);
		$counter = count($parentsIds);
		for($i=1;$i<$counter-1;$i++)
		{
			$node = new node($this->model, $parentsIds[$i]);
			array_push($parentsObj, $node);
		}
		array_push($parentsObj, $this);
		return $parentsObj;
	}
	
}


