<?php


class Lib_Category
{

	protected $parentCategory = array();

	public function parentList($id, $ctype = 0) 
	{	
		$Category = new Category();
		$select = $Category->select();
		$select->where('category_id = ?', $id);	
		$select->where('category_type = ?', $ctype);	
		$row = $Category->fetchRow($select);
		
		$i = count($this->parentCategory);
		$this->parentCategory[$i] = $row;
		
		if( $row['category_parent_id'] == 0 ){
			return $this->parentCategory;
		}else{
			return $this->parentList($row['category_parent_id'], $ctype);
		}
	} 

	public function parentListD($id, $ctype = 0) 
	{	
		$Category = new Category();
		$dataChild = $Category->fetchAll('category_type = '.$ctype.' AND category_parent_id = '.$id);
		
		foreach( $dataChild as $key => $val ){
			$i = count($this->parentCategory);
			$this->parentCategory[$i] = $val;
			$this->parentListD($val['category_id'], $ctype);		
		}
		
		return $this->parentCategory;
	} 

	public function parentListO($id, $ctype = 0) 
	{

		$dataParentCategory = $this->parentList($id, $ctype);
		$data = array();
		
		for( $i = count($dataParentCategory)-1; $i>=0; $i-- ){
			$data[] = $dataParentCategory[$i];
		}
	
		return $data;
	
	} 

	public function categoryList($id, $ctype = 0) 
	{	
		$Category = new Category();
		$data = $Category->fetchAll('category_type = '.$ctype.' AND category_parent_id = '.$id);
		return $data;
	}

	public function productSearch(){
		$Product = new Product();
		$data = $Product->fetchAll(null,'product_name ASC');
		return $data;		
	}
	
}

