<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Main extends Controller {

	public function action_act($category=0) 
	{
		if(!Auth::instance()->logged_in()){
			Request::instance()->redirect("");
		}
		
		if($category==-1) Session::instance()->set("bolumunden", 1);
		if($category==-2) Session::instance()->set("bolumunden", 2);
		if($category==-3) Session::instance()->set("bolumunden", 3);
		if((isset($_POST["university"]) && $_POST["university"]) || (isset($_POST["search"]) && $_POST["search"] ) ||
				(Session::instance()->get("bolumunden")==1) || (Session::instance()->get("bolumunden")==2) || (Session::instance()->get("bolumunden")==3))
		{
			if(isset($_POST["search"]) && $_POST["search"] ){
			    $sale = Search::instance()->find($_POST["search"]);
			    $id_array=array();
			    for($i=0;$i<count($sale);$i++)
				$id_array[$i]=$sale[$i]->searchid;  			    
			    $sales1 = ORM::factory("sale")->where('id', 'IN', $id_array)
								    ->where('sold','=',0)
								    ->order_by('id', 'DESC')								    
								    ->find_all()->as_array();
			    
			}
			if(isset($_POST["university"]) && $_POST["university"] )
			{
				Session::instance()->set("bolumunden", 0);
				Session::instance()->set("univId", $_POST["university"]);
				Session::instance()->set("depId", $_POST["department"]);
				if($category==0){
					$sales1 = ORM::factory("sale")//->where('category_id','=',$category)
									->where('university_id','=',$_POST["university"])
									->where('department_id','=',$_POST["department"])
									->where('sold','=',0)
									->order_by('id', 'DESC')
									->find_all()->as_array();
				}
				else{	
					$sales1 = ORM::factory("sale")	->where('category_id','=',$category)
									->where('university_id','=',$_POST["university"])
									->where('department_id','=',$_POST["department"])
									->where('sold','=',0)
									->order_by('id', 'DESC')
									->find_all()->as_array();
				}
					
			}
			if(Session::instance()->get("bolumunden")==1) //bolumunden
			{
				Session::instance()->set("univId", Auth::instance()->get_user()->university_id);
				Session::instance()->set("depId", Auth::instance()->get_user()->department_id);
				if(($category==0)||($category==-1)){
					if($category==0){
						$sales1 = ORM::factory("sale")//->where('category_id','=',$category)
										->where('university_id','=',Session::instance()->get("univId"))
										->where('department_id','=',Session::instance()->get("depId"))
										->where('sold','=',0)
										->order_by('id', 'DESC')
										->find_all()->as_array();
					}
					if($category==-1){
						$sales1 = ORM::factory("sale")//->where('category_id','=',$category)
										->where('university_id','=',Session::instance()->get("univId"))
										->where('department_id','=',Session::instance()->get("depId"))
										->where('sold','=',0)
										->order_by('id', 'DESC')
										->find_all()->as_array();
					}
				}
				else{	
					$sales1 = ORM::factory("sale")	->where('category_id','=',$category)
									->where('university_id','=',Session::instance()->get("univId"))
									->where('department_id','=',Session::instance()->get("depId"))
									->where('sold','=',0)
									->order_by('id', 'DESC')
									->find_all()->as_array();
				}
					
			}
			if(Session::instance()->get("bolumunden")==2) //universiteden
			{
				Session::instance()->set("univId", Auth::instance()->get_user()->university_id);
				if(($category==0)||($category==-2)){
					if($category==0){
						$sales1 = ORM::factory("sale")	->where('university_id','=',Session::instance()->get("univId"))
										->where('sold','=',0)
										->order_by('id', 'DESC')
										->find_all()->as_array();
							}
					if($category==-2){
						$sales1 = ORM::factory("sale")	->where('university_id','=',Session::instance()->get("univId"))
										->where('sold','=',0)
										->order_by('id', 'DESC')
										->find_all()->as_array();
					}
				}
				else{	
					$sales1 = ORM::factory("sale")	->where('category_id','=',$category)
									->where('university_id','=',Session::instance()->get("univId"))
									->where('sold','=',0)
									->order_by('id', 'DESC')
									->find_all()->as_array();
				}
					
			}
			if(Session::instance()->get("bolumunden")==3) //her yer
			{
				Session::instance()->set("univId", Auth::instance()->get_user()->university_id);
				if(($category==0)||($category==-3)){
					if($category==0){
						$sales1 = ORM::factory("sale")	->where('sold','=',0)
										->order_by('id', 'DESC')
										->find_all()->as_array();
							}
					if($category==-3){
						$sales1 = ORM::factory("sale")	->where('sold','=',0)
										->order_by('id', 'DESC')
										->find_all()->as_array();
					}
				}
				else{	
					$sales1 = ORM::factory("sale")	->where('category_id','=',$category)
									->where('sold','=',0)
									->order_by('id', 'DESC')
									->find_all()->as_array();
				}
					
			}
		}
		else
		{
			if($category==0){
				$sales1 = ORM::factory("sale")//->where('category_id','=',$category)
								->where('sold','=',0)
								->order_by('id', 'DESC')
								->find_all()->as_array();
			}

			else{	
				$sales1 = ORM::factory("sale")	->where('category_id','=',$category)
								->where('sold','=',0)
								->order_by('id', 'DESC')
								->find_all()->as_array();
			}			
		}	
		
		$categories = ORM::factory("category")->find_all();
		$categories_arr = array(0=>"hepsi");
		foreach($categories as $category)
		{
		    $categories_arr [$category->id] = $category->catname;
		}
		
		$university_model 	= Model::factory("university");
		$universities_arr 	= $university_model->getUniversities();
		
		$department_model 	= Model::factory("department");
		$departments_arr 	= $department_model->getDepartments();
		
		$message_model 	= Model::factory('message');
		$message_model->getNumberOfNewMessages();
		

		$data = array(  "categories_arr" => $categories_arr ,
				"universities_arr" =>$universities_arr,
				"departments_arr"=>$departments_arr);
   
		$this->request->response = View::factory('mainview', $data)->bind('pagination', $pagination)
		->bind('sales', $sales);
		$total      =count($sales1); 
/*		if ($total == 0)
		{
		Kohana::$log->add(Kohana::INFO, 'No users found');
		$this->template->content = View::factory('admin/users/none');
		return;
		}
*/		
		$pagination = Pagination::factory(array(
		'current_page'   => array('source'=>'query_string', 'key'=>'page'),
		'total_items' => $total,
		'items_per_page' => 20,
		));
		$sales  = array_slice($sales1,$pagination->offset ,$pagination->items_per_page);

	}

}
?>
