<?php 
class ControllerCategoryMap extends Controller { 

	function index() {
		$customer =& $this->locator->get('customer');      
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$currency =& $this->locator->get('currency');
		$database =& $this->locator->get('database');
		$view = $this->locator->create('template');
		$view->set('heading_title', 'Category Map');
		$template->set('title', 'Category Map');
		$category_data = array();
		$total_cateogry = 0;
		$root_cateogry  = 0;					
		$results = $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 = '".$root_cateogry."' and status = 1 and language_id = '" . (int)$language->getId() . "' order by c.sort_order");	
		foreach ($results as $k=>$result) 
		{
			$results_sub = $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 = '".$result["category_id"]."' and c.status = 1 and cd.language_id = '" . (int)$language->getId() . "' order by c.sort_order");
			$category_sub_data = array();			
			$total_cateogry = $database->countRows("select category_id from category where parent_id = ".$result["category_id"]);
			foreach ($results_sub as $result_sub) 
			{
				$category_sub_data[] = array(
					'name'  => $result_sub['name'],
					'href' => $url->href('category', false, array('path' => $result['category_id'] . '_' . $result_sub['category_id'])) ,
					'child_sub_id'=>$result_sub['category_id'],
				);
				
			}				
			$category_data[] = array(
				'name' => $result['name'],
				'href' => $url->href('category', false, array('path' => $result['category_id'])),
				'child'=> $category_sub_data,
				'total' => $total_cateogry,
			);		
		}
		$view->set('categories', $category_data);
		$template->set('content', $view->fetch('content/category_map.tpl'));
		$template->set($module->fetchPublic());
		$response->set($template->fetch('layout.tpl'));	
	}
}
?>