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

class Controller_Login extends Controller {

    public function action_index($category=0)
    {

    	if(Auth::instance()->get_user()){
    	    
	    Session::instance()->set("univId", Auth::instance()->get_user()->university_id);
	    Session::instance()->set("depId", Auth::instance()->get_user()->department_id);
	    Session::instance()->set("bolumunden", 0);
    	    Request::instance()->redirect("main/act");
	}
    	else {
	    if((isset($_POST["universities"]) && $_POST["universities"]) || Session::instance()->get("universityselect")==-1  )
		if((isset($_POST["universities"]) && $_POST["universities"]))
		Session::instance()->set("universityselect", $_POST["universities"]);
		else Session::instance()->set("universityselect", -2);
		else{
		    if(Session::instance()->get("universityselect")==-2 )
		    Session::instance()->set("universityselect", -1);
		}
		if($category==-1){
		    
		    Session::instance()->set("universityselect", -1);
		    $category=0;
		}
		
			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;
			    if(count($id_array)!=0){
				$sales1 = ORM::factory("sale")->where('id', 'IN', $id_array)
									->where('sold','=',0)
									->order_by('id', 'DESC')								    
									->find_all()->as_array();
								    
			    }
			    else $sales1 = array();
			    
			}
			elseif(Session::instance()->get("universityselect")!= -1 && Session::instance()->get("universityselect")!= -2 && Session::instance()->get("universityselect")!=NULL)  
			{
			    if($category==0){
				    $sales1 = ORM::factory("sale")->where('university_id','=',Session::instance()->get("universityselect"))
								    ->where('sold','=',0)
								    ->order_by('id', 'DESC')
								    ->find_all()->as_array();
			    }
    
			    else{	
				    $sales1 = ORM::factory("sale")
								    ->where('university_id','=',Session::instance()->get("universityselect"))
								    ->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();
			    }
			}
		$university_model 	= Model::factory("university");
		$universities_arr 	= $university_model->getUniversities();
		array_unshift($universities_arr, 'Universite Seciniz');
		$data = array("universities"=>$universities_arr);
		$this->request->response = View::factory('signin',$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);
	}
    }

}