<?php 
class CommunitiesController extends AppController{
	var $name="Communities";
	var $uses=array('CommunityMainCategory','CommunityCategory','CommunitySubCategory');
	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('CommunityMainCategory'));
		$this->render('viewMainCategories');
		
	}
function viewMainCategoryDetails($id=null){
	$data=$this->CommunityMainCategory->find('all',array('conditions'=>array('CommunityMainCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

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

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

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

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

}


function addMainCategoryResponse(){
	
	
	
}










//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('CommunityCategory'));
		$this->render('viewCategories');
		
	}
function viewCategoryDetails($id=null){
	$data=$this->CommunityCategory->find('all',array('conditions'=>array('CommunityCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$main_cats=$this->CommunityMainCategory->find('list', array('fields'=>'title'));
	$this->set(compact('main_cats'));
}

///////////////////////////////////////////
	function addCategory(){	
	$main_cats=$this->CommunityMainCategory->find('list', array('fields'=>'title'));
	$this->set(compact('main_cats'));	
		if((isset($this->data['CommunityCategory']['image']))&&($this->data['CommunityCategory']['image']['size']!=0) &&($this->data['CommunityCategory']['image'				        ]['size']>2103361)){
		 		$this->data['CommunityCategory']['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['CommunityCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['CommunityCategory']['image'] = $image;
			if ($this->CommunityCategory->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/communities/viewDisabledCategories');
		  }
		else if($this->data['CommunityCategory']['image']['size']>2103361)		
				{
				 $this->data['CommunityCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addCategory'));
				}		
		      }
	    }
///////////////////////////////////////////

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

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('CommunityCategory'));
		$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('CommunityCategory'));
		$this->render('viewCategories');
		
	}
	
	
function enableCategories($id=null){
	$this->CommunityCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('CommunityCategory'));
	$this->redirect('/communities/viewDisabledCategories');
}

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

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


function addCategoryResponse(){
	
	
	
}
	
	
	
//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('CommunitySubCategory'));
		$this->render('viewAllSubCategories');		
	}
	
function viewSubCategoryDetails($id=null){
	$data=$this->CommunitySubCategory->find('all',array('conditions'=>array('CommunitySubCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$cats=$this->CommunityCategory->find('list', array('fields'=>'title'));
	$this->set(compact('cats'));
}
 ////////////
 function addSubCategory(){	
	$cats=$this->CommunityCategory->find('list', array('fields'=>'title'));
	$this->set(compact('cats'));	
		if((isset($this->data['CommunitySubCategory']['image']))&&($this->data['CommunitySubCategory']['image']['size']!=0) &&($this->data['CommunitySubCategory']['image'				        ]['size']>2103361)){
		 		$this->data['CommunitySubCategory']['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['CommunitySubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['CommunitySubCategory']['image'] = $image;
			if ($this->CommunitySubCategory->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/communities/viewSubCategories');
		  }
		else if($this->data['CommunitySubCategory']['image']['size']>2103361)		
				{
				 $this->data['CommunitySubCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addSubCategory'));
				}		
		      }
	    }
 ///////////
///*****************************///
function editSubCategories($id=null){
		$cats=$this->CommunityCategory->find('list', array('fields'=>'title'));
			$this->set(compact('cats'));
		
	if(empty($this->data)){
		$current_image=$this->CommunitySubCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['CommunitySubCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->CommunitySubCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->CommunitySubCategory->find('all',array('conditions'=>array('id'=>$data['CommunitySubCategory']['id'])));		
		$current_image=$current_image[0]['CommunitySubCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['CommunitySubCategory'])){
		
				if($data['CommunitySubCategory']['remove']==0){
				$data['CommunitySubCategory']['image']=$current_image;}
				
				elseif($data['CommunitySubCategory']['remove']!=0){
				$data['CommunitySubCategory']['image']='';}
		}
			if(($this->data['CommunitySubCategory']['image']['size']!=0) &&($this->data['CommunitySubCategory']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['CommunitySubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['CommunitySubCategory']['image'] = $image;
			}
		   else if($this->data['CommunitySubCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/communities/editSubCategories/'.$this->data['CommunitySubCategory']['id']);
		}
		
			
										
			if($this->CommunitySubCategory->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/communities/viewSubCategoryDetails/'.$this->data['CommunitySubCategory']['id']);
			}
	}
		}
		
///*****************************///
	
		
	function subCategoryEditResponse(){
		
		
	}

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('CommunitySubCategory'));
		$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('CommunitySubCategory'));
		$this->render('viewSubCategories');
		
	}
	
	
function enableSubCategories($id=null){
	$this->CommunitySubCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('sub_categories', $this->paginate('CommunitySubCategory'));
	$this->redirect('/communities/viewDisabledSubCategories');
}

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

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


function addSubCategoryResponse(){	
    }
	
function delete($id){
	$this->CommunityMainCategory->delete($id);
	$this->redirect($this->referer());
}

function deleteCategoryDetails($id) {
	$this->CommunityCategory->delete($id);
	$this->redirect($this->referer());
	}
}
?>