<?php
class Tree_model extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';
	var $encryption_key = "i:Dvv%^uJKK9)_K<";

    function Tree_model()
    {
        // Call the Model constructor
        parent::Model();
    }
	
	function checkId($id) {
		$query = $this->db->get_where('tree', array('ID_tree' => $id));
		if($query && $query->num_rows()>0){
			return TRUE;
		} else return FALSE;
	}
	
	function getRootId($userId){
		$query = $this->db->get_where('tree',array('tree_user_id' => $userId, 'tree_path' => 'root'));
		if($query->num_rows()>0){
			$row = $query->row();
			return $row->ID_tree;
		} else return NULL;
	}
    
	function getNode($nodeId){
		$sql="SELECT * FROM tree WHERE ID_tree={$nodeId}";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0){
			$row = $query->row();
			return $row;
		} else return NULL;
	}
	
	function getNodeData($nodeDataId){
		$sql = "SELECT * FROM nodes WHERE ID_nodes={$nodeDataId} ";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0){
			$row = $query->row();
			return $row;
		} else return NULL;
	}
	
	function getNodeChildsCount($path, $userId){
		$sql="SELECT * FROM tree WHERE tree_path REGEXP '^".$path.".((\.)([0-9]+))?$' and tree_user_id={$userId}";
		$result = $this->db->query($sql);
		if($result->num_rows()>0)
		{
			return $result->num_rows();
		}
		else return 0;
	}
	
	function getNodeFirtLevelChilds($path, $userId) {
		$sql="SELECT * FROM tree WHERE tree_path REGEXP '^".$path.".((\.)([0-9]+))?$' and tree_user_id={$userId} order by tree_node_order";
		$result= $this->db->query($sql);
		return $result;
	}
	
	function addChild($data, $order, $path, $userId){
		
		$this->db->query('LOCK TABLES nodes WRITE');
		
		$this->db->set('nodes_added', 'sysdate()', FALSE);
		$this->db->insert('nodes', $data);
		$insertNodeId= $this->db->insert_id();
		
		$this->db->query('UNLOCK TABLES');
		
		
		$this->db->query('LOCK TABLES tree WRITE');
		
		$this->db->insert('tree',array(
			'tree_user_id' => $userId,
			'tree_node_order' => $order,
			'tree_node_id' => $insertNodeId
			));
		
		$insertTreeId= $this->db->insert_id();
		
		$this->db->where('ID_tree', $insertTreeId);
		$this->db->update('tree', array('tree_path' => $path.'.'.$insertTreeId));
	
		
		$this->db->query('UNLOCK TABLES');
		return $insertTreeId;
		
	}
	
	function getMaxOrder($path, $userId) {
		$sql="SELECT MAX(tree_node_order) FROM tree WHERE tree_path REGEXP '^".$path.".(\.(.+))?$' and tree_user_id={$userId}";
		$result= $this->db->query($sql);
		if($result->num_rows()>0){
			$row = $result->row_array();
			return $row['MAX(tree_node_order)'];
		} else return 1;
	}
	
	function updateNode($data, $id){
		//$result = $this->db->get_where('tree', array('ID_tree' => $id));
		$node = $this->getNode($id);
		if($node){
			$this->db->where('ID_nodes', $node->tree_node_id);
			$this->db->set('nodes_updated', 'sysdate()', FALSE);
			$this->db->update('nodes', $data);
		}	
	}
	
	function deleteNode($path, $userId) {
		
		$this->load->model('Admin_model');
		$Admin_model = new Admin_model();
		$sql="SELECT * FROM tree WHERE tree_path REGEXP '^".$path.".((\.)([0-9]+))?$' and tree_user_id={$userId}";
		$result = $this->db->query($sql);
		if($result && $result->num_rows()>0){
			foreach($result->result() as $row){
				$query = $this->db->get_where('modules', array('modules_tree_id' => $row->ID_tree));
				if($query && $query->num_rows()>0){
					foreach($query->result() as $q){
						$Admin_model->removemodule($q->ID_modules, $q->modules_type);
					}
				}
				
				$this->deleteNode($row->tree_path, $userId);
				$this->db->where('ID_nodes', $row->tree_node_id);
				$this->db->delete('nodes');
				
				$this->db->where('ID_tree', $row->ID_tree);
				$this->db->delete('tree');
			}
		}
		$sql="SELECT * FROM tree WHERE tree_path='".$path."' and tree_user_id={$userId}";
		$result = $this->db->query($sql);
		if($result && $result->num_rows()>0){
			foreach($result->result() as $row){
				$query = $this->db->get_where('modules', array('modules_tree_id' => $row->ID_tree));
				if($query && $query->num_rows()>0){
					foreach($query->result() as $q){
						$Admin_model->removemodule($q->ID_modules, $q->modules_type);
					}
				}
				$this->db->where('ID_nodes', $row->tree_node_id);
				$this->db->delete('nodes');
				
				$this->db->where('ID_tree', $row->ID_tree);
				$this->db->delete('tree');
			}
		}
	}
	
	function moveup($id, $path, $order, $userId){
		$order_1 = $order - 1;
		
		$sql="SELECT * FROM tree WHERE tree_path REGEXP '^".$path.".((\.)([0-9]+))?$' and tree_user_id={$userId} and tree_node_order=".$order_1." ";
		$result = $this->db->query($sql);
		if($result && $result->num_rows()>0){
		
			$row = $result->row();
			$this->db->where('ID_tree', $id);			
			$this->db->update('tree', array('tree_node_order'=>$order_1));
			$this->db->where('ID_tree', $row->ID_tree);			
			$this->db->update('tree', array('tree_node_order'=>$order));
		}
	}
	
	function movedown($id, $path, $order, $userId){
		$order_1 = $order + 1;
		
		$sql="SELECT * FROM tree WHERE tree_path REGEXP '^".$path.".((\.)([0-9]+))?$' and tree_user_id={$userId} and tree_node_order=".$order_1." ";
		$result = $this->db->query($sql);
		if($result && $result->num_rows()>0){
			$row = $result->row();
			$this->db->where('ID_tree', $id);			
			$this->db->update('tree', array('tree_node_order'=>$order_1));
			$this->db->where('ID_tree', $row->ID_tree);			
			$this->db->update('tree', array('tree_node_order'=>$order));
		}
	}

}
?>