<?php  
class ModuleCategory extends Controller 
{
	function fetch() 
	{	
//ini_set("display_errors", "On");
error_reporting(0);

		$config   =& $this->locator->get('config');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$url      =& $this->locator->get('url');
		$user     =& $this->locator->get('user');
		$customer =& $this->locator->get('customer');		
		$currency =& $this->locator->get('currency');
		$tax      =& $this->locator->get('tax');
		$image    =& $this->locator->get('image');
		$template = $this->locator->create('template');		
		
		$language->load('extension/module/category.php');		
			
    	$template->set('heading_title', $language->get('heading_title'));
    		
    		$category_1_data = array();
			
			$category_1_rows = $database->getRows("select c.category_id, cd.name from category c left join category_description cd on (c.category_id = cd.category_id) where parent_id = 0 and status = 1 order by c.sort_order");				
			
			foreach ($category_1_rows as $k=>$category_1_row) 
			{ 
				$sub_total = 0;	
					       		
				$category_1_id = intval($category_1_row['category_id']);
				$category_2_rows = $database->getRows("select c.category_id,cd.name from category c left join category_description cd on (c.category_id = cd.category_id) left join image i on (c.image_id = i.image_id) where c.parent_id = $category_1_id and c.status = 1 order by c.sort_order");
			    $sub_total	+= count($category_2_rows);
			    
				$category_2_data = array();			
				
				foreach ($category_2_rows as $k=>$category_2_row) 
				{
					$category_3_rows = $database->getRows("select c.category_id,cd.name from category c left join category_description cd on (c.category_id = cd.category_id) left join image i on (c.image_id = i.image_id) where c.parent_id = '".$category_2_row["category_id"]."' and c.status = 1 order by c.sort_order");
					$sub_total	+= count($category_3_rows);
					
					$category_3_data =  array();
					foreach ($category_3_rows as $category_3_row){
						$category_3_data[] = array(
						    'id'=>$category_3_row['category_id'],
							'name'  => $category_3_row['name'],							
							'href' => $url->href('category', false, array('path' => $category_1_row['category_id'] . '_' . $category_2_row['category_id']. '_' . $category_3_row['category_id'])) 
						);
					}
					
					$category_2_data[] = array(
						'id'=>$category_2_row['category_id'],		
						'name'  => $category_2_row['name'],
						'href' => $url->href('category', false, array('path' => $category_1_row['category_id'] . '_' . $category_2_row['category_id'])) ,
						'child_3'=>$category_3_data
					);
				}			
				
				$category_2_columns = array (); $category_column_1 = array(); $category_column_2 = array(); $category_column_3 = array();
				$pos=0;
				$column_average_total = ceil ($sub_total/3); $column_1_total=0;
				foreach ($category_2_data as $k2=>$category_2){					
					if($column_1_total < $column_average_total){
						$column_1_total++; $pos = $k2;									
			          	 foreach ($category_2['child_3'] as $k3=>$category_3){
			          	 	$column_1_total++;
			          	 }
			          	 $category_column_1[] = $category_2;
					}
				}
				$column_average_total = ceil(($sub_total-$column_1_total)/2); $column_2_total=0;
				foreach ($category_2_data as $k2=>$category_2){
					if( ($k2 > $pos)&&($column_2_total < $column_average_total) ){
						$column_2_total++; $pos=$k2;								
			          	 foreach ($category_2['child_3'] as $k3=>$category_3){
			          	 	$column_2_total++;
			          	 }
			          	 $category_column_2[] = $category_2;
					}
				}
               	foreach ($category_2_data as $k2=>$category_2){
               		if($k2 > $pos){
               			$category_column_3[] = $category_2;
               		}
               	}
					
				$category_2_columns = array(
				    'column_1'   => $category_column_1,
					'column_2' => $category_column_2,
					'column_3' => $category_column_3
				);
				
				$category_1_data[] = array(
				    'id'   => $category_1_id,
					'name' => stripslashes($category_1_row['name']),
					'href' => $url->href('category', false, array('path' => $category_1_id)),
					'columns' => $category_2_columns
				);			
			}

    		$template->set('categories', $category_1_data);	    					
		
    		$get_url=explode('&',$_SERVER['REQUEST_URI']);    	
    		if(strpos($get_url[0],'controller')!==false){     		
                return array();
  			 }
    
			return $template->fetch('module/category.tpl');		
  	}
  	
  	function getCategoryTree($category_id=0) 
	{
		$database =& $this->locator->get('database');
		$category_data = array();		
		$sub_category_rows = $database->getRows("select c.category_id, cd.name from category c left join category_description cd on (c.category_id = cd.category_id) where parent_id = $category_id and status = 1 order by c.sort_order");	
						
		foreach ($sub_category_rows as $sub_category) 
		{
			$category_data[] = array(
				    'id'   => $sub_category["category_id"],
					'name' => stripslashes($sub_category['name']),
					'href' => $url->href('category', false, array('path' => $sub_category['category_id'])),
					'sub_total' => count($sub_category_rows),
					'child'=> getCategoryTree($sub_category["category_id"])			
				);
		}
		
		return $category_data;
	}
}
?>
