<?php 
class ControllerCategory extends Controller {  
	function index() { 
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$image    =& $this->locator->get('image');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$tax      =& $this->locator->get('tax');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
			
		$language->load('controller/category.php');
	
		$category_info = $database->getRow("select distinct name from category c left join category_description cd on (c.category_id = cd.category_id) where c.category_id = '" . (int)((!$request->get('path')) ? '0' : (int)end(explode('_', $request->get('path')))) . "' and cd.language_id = '" . (int)$language->getId() . "'");
	
		if ($category_info) {
	  		$template->set('title', $category_info['name']);

      		$view = $this->locator->create('template');

      		$breadcrumb = array();

      		$breadcrumb[] = array(
        		'href'      => $url->href('home'),
        		'text'      => $language->get('text_home'),
        		'separator' => FALSE
      		);

      		foreach (explode('_', $request->get('path')) as $category_id) {
        		$result = $database->getRow("select cd.name, c.path from category c left join category_description cd on c.category_id = cd.category_id where c.category_id = '" . (int)$category_id . "' and cd.language_id = '" . (int)$language->getId() . "'");

        		$breadcrumb[] = array(
          			'href'      => $url->href('category', FALSE, array('path' => $result['path'])),
          			'text'      => $result['name'],
          			'separator' => $language->get('text_separator')
        		);
      		} 

			if (($database->countRows("select * from product_to_category p2c left join product p on p2c.product_id = p.product_id where p.status = '1' and p2c.category_id = '" . (int)end(explode('_', $request->get('path'))) . "'")) || ($database->countRows("select * from category where parent_id = '" . (int)end(explode('_', $request->get('path'))) . "'"))) {
        		$view->set('breadcrumbs', $breadcrumb);

        		$category_data = array();
        		
				$results = $database->getRows("select * 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 = '" . (int)(!$request->get('path') ? '0' : (int)end(explode('_', $request->get('path')))) . "' and cd.language_id = '" . (int)$language->getId() . "'");
				
        		foreach ($results as $result) {
          			$category_data[] = array(
            			'name'  => $result['name'],
            			'href'  => $url->href('category', FALSE, array('path' => ($request->get('path')) ? $request->get('path') . '_' . $result['category_id'] : $result['category_id'])),
            			'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height'))
          			);
        		}

        		$view->set('categories', $category_data);
				
				$product_data = array();
        		
				$results = $database->getRows($database->splitQuery("select * from product p left join product_description pd on (p.product_id = pd.product_id) left join product_to_category p2c on (p.product_id = p2c.product_id) left join image i on (p.image_id = i.image_id) where status = '1' and language_id = '" . (int)$language->getId() . "' and p2c.category_id = '" . (int)end(explode('_', $request->get('path'))) . "' and p.date_available < now() and p.status = '1' order by p.sort_order, pd.name", ($request->has('path') ? $session->get('category.' . $request->get('path') . '.page') : $session->get('category.page')), $config->get('config_max_rows')));

        		foreach ($results as $result) {
          			$query = array(
            			'path'       => $request->get('path'),
            			'product_id' => $result['product_id']
          			);

          			$product_data[] = array(
            			'name'  => $result['name'],
            			'href'  => $url->href('product', FALSE, $query),
            			'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
            			'price' => $currency->format($tax->calculate($result['price'], $result['tax_class_id'], $config->get('config_tax')))
          			);
        		}

        		$view->set('products', $product_data);

        		$view->set('heading_title', $category_info['name']);

        		$view->set('text_product', $language->get('text_product'));
        		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));

	      		$view->set('entry_page', $language->get('entry_page'));
      			
				$view->set('action', $url->href('category', 'page', array('path' => $request->get('path'))));
				
				$view->set('page', ($request->has('path') ? $session->get('category.' . $request->get('path') . '.page') : $session->get('category.page')));
							
				$page_data = array();

      			for ($i = 1; $i <= $database->getPages(); $i++) {
        			$page_data[] = array(
          				'text'  => $language->get('text_pages', $i, $database->getPages()),
          				'value' => $i
        			);
      			}

      			$view->set('pages', $page_data);
			
				$template->set('content', $view->fetch('content/category.tpl'));
			
				$template->set($module->fetch());

    			$response->set($template->fetch('layout.tpl')); 				
      		} else {
        		$template->set('title', $category_info['name']);

				$view = $this->locator->create('template');

        		$view->set('heading_title', $category_info['name']);

        		$view->set('text_error', $language->get('text_empty'));

        		$view->set('button_continue', $language->get('button_continue'));

        		$view->set('breadcrumbs', $breadcrumb);

        		$view->set('continue', $url->href('home'));
		
				$template->set('content', $view->fetch('content/error.tpl'));
			
				$template->set($module->fetch());

    			$response->set($template->fetch('layout.tpl')); 				
      		}
    	} else {
	  		$template->set('title', $language->get('text_error'));	
 
      		$view = $this->locator->create('template');

      		$view->set('heading_title', $language->get('text_error'));

      		$view->set('text_error', $language->get('text_error'));

      		$view->set('button_continue', $language->get('button_continue'));

      		$view->set('continue', $url->href('home'));
	  
	  		$template->set('content', $view->fetch('content/error.tpl'));
    	
			$template->set($module->fetch());

    		$response->set($template->fetch('layout.tpl')); 	
		}
  	}
	
	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
		
		if ($request->has('page', 'post')) {
			$session->set(($request->has('path') ? 'category.' . $request->get('path') . '.page' : 'category.page'), $request->get('page', 'post'));
		}

		$response->redirect($url->ssl('category', FALSE, array('path' => $request->get('path'))));
	}
}
?>
