<?php

class CategoryController extends Zend_Controller_Action {

	public function init() {		
		$this->view->menuSel = 2;
	}	
	
	
	public function indexAction() {			
		$v = $this->_request->getParams();
		
		if( !isset($v['id']) ){
			$idCategory = 0;
		}else{
			$idCategory = $v['id'];
		}		
		
		$this->view->idCategory = $idCategory;
		
		$ctype = 0;
		$Category = new Category();		
		$data = $Category->fetchAll('category_type = '.$ctype.' AND category_parent_id = '.$idCategory , 'category_name ASC');
		if( count($data) > 0 ){
			$this->view->dataTable = $data;
		}else{
			$this->_helper->redirector('product','category',null,array('id' => $idCategory));			
		} 
		
		if( $idCategory == 0 ){
		
			$mt['title'] = 'Dla głównej';
			$mt['description'] = '';
			$mt['keywords'] = '';
			$this->view->mt = $mt;
		
		}else{
		
			$select = $Category->select();		
			$select->where('category_id = ?', $idCategory);	
			$row = $Category->fetchRow($select);
		
			$mt['title'] = $row['category_mt_title'];
			$mt['description'] = $row['category_mt_description'];
			$mt['keywords'] = $row['category_mt_keywords'];
			$this->view->mt = $mt;
		}
	}
	
	
	public function setAction() {			
		$v = $this->_request->getParams();
		
		if( !isset($v['id']) ){
			$idCategory = 0;
		}else{
			$idCategory = $v['id'];
		}		
		$this->view->menuSel = 3;
		
		$this->view->idCategory = $idCategory;
		
		$ctype = 1;
		$Category = new Category();		
		$data = $Category->fetchAll('category_type = '.$ctype.' AND category_parent_id = '.$idCategory , 'category_name ASC');
		if( count($data) > 0 ){
			$this->view->dataTable = $data;
		}else{
			$this->_helper->redirector('product2','category',null,array('id' => $idCategory));			
		} 
	}
	
	
	public function productAction() {			
		$v = $this->_request->getParams();
		
		if( !isset($v['id']) ){
			$idCategory = 0;
		}else{
			$idCategory = $v['id'];
		}		
		
		$this->view->idCategory = $idCategory;
		
		$Product = new Product();		
		$data = $Product->fetchAll('category_id = '.$idCategory , 'product_name ASC');
		$this->view->dataTable = $data;
		
		if( $idCategory == 0 ){
		
		
		}else{
		
			$Category = new Category();
			$select = $Category->select();		
			$select->where('category_id = ?', $idCategory);	
			$row = $Category->fetchRow($select);
		
			$mt['title'] = $row['category_mt_title'];
			$mt['description'] = $row['category_mt_description'];
			$mt['keywords'] = $row['category_mt_keywords'];
			$this->view->mt = $mt;
		}
	}
	
	
	public function product2Action() {			
		$v = $this->_request->getParams();
		
		if( !isset($v['id']) ){
			$idCategory = 0;
		}else{
			$idCategory = $v['id'];
		}		
		$this->view->menuSel = 3;
		
		$this->view->idCategory = $idCategory;
		
		$Product = new Product();		
		$data = $Product->fetchAll('category_id1 = '.$idCategory , 'product_name ASC');
		$this->view->dataTable = $data;
		
		if( $idCategory == 0 ){
		
		
		}else{
		
			$Category = new Category();
			$select = $Category->select();		
			$select->where('category_id = ?', $idCategory);	
			$row = $Category->fetchRow($select);
		
			$mt['title'] = $row['category_mt_title'];
			$mt['description'] = $row['category_mt_description'];
			$mt['keywords'] = $row['category_mt_keywords'];
			$this->view->mt = $mt;
		}
	}

}
