<?php 
class FacilitsController extends AppController{
	var $name="Facilits";
	var $uses=array('FacilitMainCategory','FacilitCategory','FacilitSubCategory');
	var $helpers = array('Form', 'Html', 'Text','Javascript', 'Time', 'Number','Paginator','Cache','Ajax', 'Session');
	var $components=array('RequestHandler','ImageHandler','Session');
	
function index() {
		
	}
	
	
	
//MAIN CATEGORIES SECTION STARTS HERE	
function viewMainCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->set('main_categories', $this->paginate('FacilitMainCategory'));
		$this->render('viewAllMainCategories');
		
	}
function viewMainCategoryDetails($id=null){
	$data=$this->FacilitMainCategory->find('all',array('conditions'=>array('FacilitMainCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

		
		function addMainCategory() {		
		if((isset($this->data['FacilitMainCategory']['image']))&&($this->data['FacilitMainCategory']['image']['size']!=0) &&($this->data['FacilitMainCategory']['image'				        ]['size']>2103361)){
		 		$this->data['FacilitMainCategory']['image']='';
				$this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addMainCategory'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['FacilitMainCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['FacilitMainCategory']['image'] = $image;
			if ($this->FacilitMainCategory->save($data)) {
				$this->Session->setFlash('Saved Successfully.');
				$this->redirect('/Facilits/viewMainCategories');
		  }
		else if($this->data['FacilitMainCategory']['image']['size']>2103361)		
				{
				 $this->data['FacilitMainCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addMainCategory'));
				}		
		      }
	    }
	
	
function editMainCategories($id=null){		
	if(empty($this->data)){
		$current_image=$this->FacilitMainCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['FacilitMainCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->FacilitMainCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->FacilitMainCategory->find('all',array('conditions'=>array('id'=>$data['FacilitMainCategory']['id'])));		
		$current_image=$current_image[0]['FacilitMainCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['FacilitMainCategory'])){
		
				if($data['FacilitMainCategory']['remove']==0){
				$data['FacilitMainCategory']['image']=$current_image;}
				
				elseif($data['FacilitMainCategory']['remove']!=0){
				$data['FacilitMainCategory']['image']='';}
		}
			if(($this->data['FacilitMainCategory']['image']['size']!=0) &&($this->data['FacilitMainCategory']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['FacilitMainCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['FacilitMainCategory']['image'] = $image;
			}
		   else if($this->data['FacilitMainCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');		
			$this->redirect('/Facilits/editMainCategories/'.$this->data['FacilitMainCategory']['id']);
		}
		
			
										
			if($this->FacilitMainCategory->save($data)){	
			$this->Session->setFlash('Updated Successfully.');				
			$this->redirect('/Facilits/viewMainCategoryDetails/'.$this->data['FacilitMainCategory']['id']);
			}
	}
	}
	
	
	function mainCategoryEditResponse(){
		
		
	}

function viewEnabledMainCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','status','date','id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('main_categories', $this->paginate('FacilitMainCategory'));
		$this->render('viewMainCategories');
		
	}
	
function viewDisabledMainCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('main_categories', $this->paginate('FacilitMainCategory'));
		$this->render('viewMainCategories');
		
	}
	
		
function enableMainCategories($id=null){
	$this->FacilitMainCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('FacilitMainCategory'));
   	//$this->render('viewMainCategories');
	$this->redirect('/Facilits/viewDisabledMainCategories');
}

function enableMainCategory($id=null){
	$this->FacilitMainCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('FacilitMainCategory'));
	$this->redirect('/Facilits/viewMainCategories');
}

function disableMainCategories($id=null){
	$this->FacilitMainCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('FacilitMainCategory'));
	$this->redirect('/Facilits/viewEnabledMainCategories');
}

function disableMainCategory($id=null){
	$this->FacilitMainCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('FacilitMainCategory'));
	$this->redirect('/Facilits/viewMainCategories');

}



//MAIN CATEGORY SECTION ENDS HERE
//CATEGORIES SECTION STARTS HERE

function viewCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->set('categories', $this->paginate('FacilitCategory'));
		$this->render('viewAllCategories');
		
	}
function viewCategoryDetails($id=null){
	$data=$this->FacilitCategory->find('all',array('conditions'=>array('FacilitCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$main_cats=$this->FacilitMainCategory->find('list', array('fields'=>'title'));
	$this->set(compact('main_cats'));
}

///////////////////////////////////////////
	function addCategory(){	
	$main_cats=$this->FacilitMainCategory->find('list', array('fields'=>'title'));
	$this->set(compact('main_cats'));	
		if((isset($this->data['FacilitCategory']['image']))&&($this->data['FacilitCategory']['image']['size']!=0) &&($this->data['FacilitCategory']['image'				        ]['size']>2103361)){
		 		$this->data['FacilitCategory']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addCategory'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['FacilitCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['FacilitCategory']['image'] = $image;
			if ($this->FacilitCategory->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/Facilits/viewCategories');
		  }
		else if($this->data['FacilitCategory']['image']['size']>2103361)		
				{
				 $this->data['FacilitCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addCategory'));
				}		
		      }
	    }
///////////////////////////////////////////

	function editCategories($id=null){
		$main_cats=$this->FacilitMainCategory->find('list', array('fields'=>'title'));
			$this->set(compact('main_cats'));
		
	if(empty($this->data)){
		$current_image=$this->FacilitCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['FacilitCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->FacilitCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->FacilitCategory->find('all',array('conditions'=>array('id'=>$data['FacilitCategory']['id'])));		
		$current_image=$current_image[0]['FacilitCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['FacilitCategory'])){
		
				if($data['FacilitCategory']['remove']==0){
				$data['FacilitCategory']['image']=$current_image;}
				
				elseif($data['FacilitCategory']['remove']!=0){
				$data['FacilitCategory']['image']='';}
		}
			if(($this->data['FacilitCategory']['image']['size']!=0) &&($this->data['FacilitCategory']['image']['size']<=2103361)) 
		  	{			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['FacilitCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['FacilitCategory']['image'] = $image;
			}
		   else if($this->data['FacilitCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/Facilits/editCategories/'.$this->data['FacilitCategory']['id']);
			}									
			if($this->FacilitCategory->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/Facilits/viewCategoryDetails/'.$this->data['FacilitCategory']['id']);
			}
	}
		}

function viewEnabledCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','status','date','id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('categories', $this->paginate('FacilitCategory'));
		$this->render('viewCategories');
		
	}
	
function viewDisabledCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('categories', $this->paginate('FacilitCategory'));
		$this->render('viewCategories');
		
	}
	
	
function enableCategories($id=null){
	$this->FacilitCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('FacilitCategory'));
	$this->redirect('/Facilits/viewDisabledCategories');
}

function enableCategory($id=null){
	$this->FacilitCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('FacilitCategory'));
	$this->redirect('/Facilits/viewCategories');
}

function disableCategory($id=null){
	$this->FacilitCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('FacilitCategory'));
	$this->redirect('/Facilits/viewCategories');
}

function disableCategories($id=null){
	$this->FacilitCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('FacilitCategory'));
	$this->redirect('/Facilits/viewEnabledCategories');
}
	
	
//CATEGORIES SECTION ENDS HERE
//SUB CATEGORIES SECTION STARTS HERE	
	
function viewSubCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->set('sub_categories', $this->paginate('FacilitSubCategory'));
		$this->render('viewAllSubCategories');		
	}
	
function viewSubCategoryDetails($id=null){
	$data=$this->FacilitSubCategory->find('all',array('conditions'=>array('FacilitSubCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$cats=$this->FacilitCategory->find('list', array('fields'=>'title'));
	$this->set(compact('cats'));
}
 ////////////
 function addSubCategory(){	
	$cats=$this->FacilitCategory->find('list', array('fields'=>'title'));
	$this->set(compact('cats'));	
		if((isset($this->data['FacilitSubCategory']['image']))&&($this->data['FacilitSubCategory']['image']['size']!=0) &&($this->data['FacilitSubCategory']['image'				        ]['size']>2103361)){
		 		$this->data['FacilitSubCategory']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addSubCategory'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['FacilitSubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['FacilitSubCategory']['image'] = $image;
			if ($this->FacilitSubCategory->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/Facilits/viewSubCategories');
		  }
		else if($this->data['FacilitSubCategory']['image']['size']>2103361)		
				{
				 $this->data['FacilitSubCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addSubCategory'));
				}		
		      }
	    }
 ///////////
///*****************************///
function editSubCategories($id=null){
		$cats=$this->FacilitCategory->find('list', array('fields'=>'title'));
			$this->set(compact('cats'));
		
	if(empty($this->data)){
		$current_image=$this->FacilitSubCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['FacilitSubCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->FacilitSubCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->FacilitSubCategory->find('all',array('conditions'=>array('id'=>$data['FacilitSubCategory']['id'])));		
		$current_image=$current_image[0]['FacilitSubCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['FacilitSubCategory'])){
		
				if($data['FacilitSubCategory']['remove']==0){
				$data['FacilitSubCategory']['image']=$current_image;}
				
				elseif($data['FacilitSubCategory']['remove']!=0){
				$data['FacilitSubCategory']['image']='';}
		}
			if(($this->data['FacilitSubCategory']['image']['size']!=0) &&($this->data['FacilitSubCategory']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['FacilitSubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['FacilitSubCategory']['image'] = $image;
			}
		   else if($this->data['FacilitSubCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/Facilits/editSubCategories/'.$this->data['FacilitSubCategory']['id']);
		}
		
			
										
			if($this->FacilitSubCategory->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/Facilits/viewSubCategoryDetails/'.$this->data['FacilitSubCategory']['id']);
			}
	}
		}
		
///*****************************///
	
		


function viewEnabledSubCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('sub_categories', $this->paginate('FacilitSubCategory'));
		$this->render('viewSubCategories');
		
	}
	
function viewDisabledSubCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('sub_categories', $this->paginate('FacilitSubCategory'));
		$this->render('viewSubCategories');
		
	}
	
	
function enableSubCategories($id=null){
	$this->FacilitSubCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('sub_categories', $this->paginate('FacilitSubCategory'));
	$this->redirect('/Facilits/viewDisabledSubCategories');
}

function disableSubCategories($id=null){
	$this->FacilitSubCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('sub_categories', $this->paginate('FacilitSubCategory'));
	$this->redirect('/Facilits/viewEnabledSubCategories');
}

function disableSubCategory($id=null){
	$this->FacilitSubCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('FacilitSubCategory'));
	$this->redirect('/Facilits/viewSubCategories');	
	}
	
function enableSubCategory($id=null){
	$this->FacilitSubCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('FacilitSubCategory'));
	$this->redirect('/Facilits/viewSubCategories');
	}

}
?>