<?php 
class EventsController extends AppController{
	var $name="Events";
	var $uses=array('EventMainCategory','Event','EventSubCategory');
	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('name','date','status','id');
		$this->set('main_categories', $this->paginate('EventMainCategory'));
		$this->render('viewAllMainCategories');
		
	}
function viewMainCategoryDetails($id=null){
	$data=$this->EventMainCategory->find('all',array('conditions'=>array('EventMainCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

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


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

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

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

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

}



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

function viewEvents(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->set('products', $this->paginate('Event'));
		$this->render('viewAllEvents');
		
	}
function viewEventDetails($id=null){
	$data=$this->Event->find('all',array('conditions'=>array('Event.id'=>$id)));	
	$this->set(compact('data'));	
	$main_cats=$this->EventMainCategory->find('list', array('fields'=>'name'));
	$sub_cats=$this->EventSubCategory->find('list', array('fields'=>'name'));
	$this->set(compact('main_cats','sub_cats'));
	
}

	function addEvent(){	
		if(empty($this->data)){
		$main_cats=$this->EventMainCategory->find('list', array('fields'=>'name'));
		$sub_cats=$this->EventSubCategory->find('list', array('fields'=>'name'));
		$this->set(compact('main_cats','sub_cats'));
		}
		if((isset($this->data['Event']['image']))&&($this->data['Event']['image']['size']!=0) &&($this->data['Event']['image']['size']>2103361)){
		 		$this->data['Event']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addEvent'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['Event']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['Event']['image'] = $image;
			$data['Event']['news_day'] = $data['Event']['news_date']['day'];
			$data['Event']['news_month'] = $data['Event']['news_date']['month'];
			$data['Event']['news_year'] = $data['Event']['news_date']['year'];
			$data['Event']['news_hour'] = $data['Event']['news_date']['hour'];
			$data['Event']['news_min'] = $data['Event']['news_date']['min'];
			if ($this->Event->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/Events/viewEvents');
		  }
		else if($this->data['Event']['image']['size']>2103361)		
				{
				 $this->data['Event']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addEvent'));
				}		
		      }
	    }
///////////////////////////////////////////

	function editEvents($id=null){
		$main_cats=$this->EventMainCategory->find('list', array('fields'=>'name'));
		$sub_cats=$this->EventSubCategory->find('list', array('fields'=>'name'));
		$this->set(compact('main_cats','sub_cats'));
		
	if(empty($this->data)){
		//$current_image=$this->Event->find('all',array('conditions'=>array('id'=>$id)));		
		//$current_image=$current_image[0]['Event']['image'];
		//$data['Event']['news_min'] = $data['Event']['news_date']['min'];		
		//$this->set(compact('current_image'));
		$data=$this->Event->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
		$this->data['Event']['news_date']['day'] = $this->data['Event']['news_day'];
		$this->data['Event']['news_date']['month'] = $this->data['Event']['news_month'];
		$this->data['Event']['news_date']['year'] = $this->data['Event']['news_year'];
		$this->data['Event']['news_date']['hour'] = $this->data['Event']['news_hour'];
		$this->data['Event']['news_date']['min'] = $this->data['Event']['news_min'];
		
		//print_r($this->data);exit;
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		//$current_image=$this->Event->find('all',array('conditions'=>array('id'=>$data['Event']['id'])));		
		//$current_image=$current_image[0]['Event']['image'];
		//$this->set(compact('current_image'));
			
		/*if(array_key_exists('remove',$data['Event'])){
		
				if($data['Event']['remove']==0){
				$data['Event']['image']=$current_image;}
				
				elseif($data['Event']['remove']!=0){
				$data['Event']['image']='';}
		}
			if(($this->data['Event']['image']['size']!=0) &&($this->data['Event']['image']['size']<=2103361)) 
		  	{			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['Event']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['Event']['image'] = $image;
			}
		   else if($this->data['Event']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/Events/editEvents/'.$this->data['Event']['id']);
			}*/
			$data['Event']['news_day'] = $data['Event']['news_date']['day'];
			$data['Event']['news_month'] = $data['Event']['news_date']['month'];
			$data['Event']['news_year'] = $data['Event']['news_date']['year'];
			$data['Event']['news_hour'] = $data['Event']['news_date']['hour'];
			$data['Event']['news_min'] = $data['Event']['news_date']['min'];									
			if($this->Event->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/Events/viewEventDetails/'.$this->data['Event']['id']);
			}
	}
		}

function viewEnabledEvents(){
		$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('products', $this->paginate('Event'));
		$this->render('viewEvents');
		
	}
	
function viewDisabledEvents(){
		$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('products', $this->paginate('Event'));
		$this->render('viewEvents');
		
	}
	
	
function enableEvents($id=null){
	$this->Event->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Event'));
	$this->redirect('/Events/viewDisabledEvents');
}

function enableEvent($id=null){
	$this->Event->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Event'));
	$this->redirect('/Events/viewEvents');
}

function disableEvent($id=null){
	$this->Event->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Event'));
	$this->redirect('/Events/viewEvents');
}

function disableEvents($id=null){
	$this->Event->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Event'));
	$this->redirect('/Events/viewEnabledEvents');
}
	
	
//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('name','date','status','id');
		$this->set('sub_categories', $this->paginate('EventSubCategory'));
		$this->render('viewAllSubCategories');		
	}
	
function viewSubCategoryDetails($id=null){
	$data=$this->EventSubCategory->find('all',array('conditions'=>array('EventSubCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$cats=$this->EventMainCategory->find('list', array('fields'=>'name'));
	$this->set(compact('cats'));
}
 ////////////
 function addSubCategory(){	
	$cats=$this->EventMainCategory->find('list', array('fields'=>'name'));
	$this->set(compact('cats'));	
		if((isset($this->data['EventSubCategory']['image']))&&($this->data['EventSubCategory']['image']['size']!=0) &&($this->data['EventSubCategory']['image'				        ]['size']>2103361)){
		 		$this->data['EventSubCategory']['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['EventSubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['EventSubCategory']['image'] = $image;
			if ($this->EventSubCategory->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/Events/viewSubCategories');
		  }
		else if($this->data['EventSubCategory']['image']['size']>2103361)		
				{
				 $this->data['EventSubCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addSubCategory'));
				}		
		      }
	    }
 ///////////
///*****************************///
function editSubCategories($id=null){
		$cats=$this->EventMainCategory->find('list', array('fields'=>'name'));
			$this->set(compact('cats'));
		
	if(empty($this->data)){
		$current_image=$this->EventSubCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['EventSubCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->EventSubCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->EventSubCategory->find('all',array('conditions'=>array('id'=>$data['EventSubCategory']['id'])));		
		$current_image=$current_image[0]['EventSubCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['EventSubCategory'])){
		
				if($data['EventSubCategory']['remove']==0){
				$data['EventSubCategory']['image']=$current_image;}
				
				elseif($data['EventSubCategory']['remove']!=0){
				$data['EventSubCategory']['image']='';}
		}
			if(($this->data['EventSubCategory']['image']['size']!=0) &&($this->data['EventSubCategory']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['EventSubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['EventSubCategory']['image'] = $image;
			}
		   else if($this->data['EventSubCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/Events/editSubCategories/'.$this->data['EventSubCategory']['id']);
		}
		
			
										
			if($this->EventSubCategory->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/Events/viewSubCategoryDetails/'.$this->data['EventSubCategory']['id']);
			}
	}
		}
		
///*****************************///
	
		


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

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

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

}
?>