<?php 
class PhotosController extends AppController{
	var $name="Photos";
	var $uses=array('PhotoMainCategory','Photo','PhotoSubCategory');
	var $helpers = array('Form', 'Html', 'Text','Javascript', 'Time', 'Number','Paginator','Cache','Ajax', 'Session');
	var $components=array('RequestHandler','PhotoImageHandler','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('PhotoMainCategory'));
		$this->render('viewAllMainCategories');
		
	}
function viewMainCategoryDetails($id=null){
	$data=$this->PhotoMainCategory->find('all',array('conditions'=>array('PhotoMainCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

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

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

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

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

}



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

function viewPhotos(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->set('products', $this->paginate('Photo'));
		$this->render('viewAllPhotos');
		
	}
function viewPhotoDetails($id=null){
	$data=$this->Photo->find('all',array('conditions'=>array('Photo.id'=>$id)));	
	$this->set(compact('data'));	
	$main_cats=$this->PhotoMainCategory->find('list', array('fields'=>'name'));
	$sub_cats=$this->PhotoSubCategory->find('list', array('fields'=>'name'));
	$this->set(compact('main_cats','sub_cats'));
	
}

	function addPhoto(){	
		if(empty($this->data)){
		$main_cats=$this->PhotoMainCategory->find('list', array('fields'=>'name'));
		$sub_cats=$this->PhotoSubCategory->find('list', array('fields'=>'name'));
		$this->set(compact('main_cats','sub_cats'));
		}
		if((isset($this->data['Photo']['image']))&&($this->data['Photo']['image']['size']!=0) &&($this->data['Photo']['image']['size']>2103361)){
		 		$this->data['Photo']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addPhoto'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->PhotoImageHandler->upload_image_and_thumbnail($data['Photo']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['Photo']['image'] = $image;
			if ($this->Photo->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/Photos/viewPhotos');
		  }
		else if($this->data['Photo']['image']['size']>2103361)		
				{
				 $this->data['Photo']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addPhoto'));
				}		
		      }
	    }
///////////////////////////////////////////

	function editPhotos($id=null){
		$main_cats=$this->PhotoMainCategory->find('list', array('fields'=>'name'));
		$sub_cats=$this->PhotoSubCategory->find('list', array('fields'=>'name'));
		$this->set(compact('main_cats','sub_cats'));
		
	if(empty($this->data)){
		$current_image=$this->Photo->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['Photo']['image'];		
		$this->set(compact('current_image'));
		$data=$this->Photo->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->Photo->find('all',array('conditions'=>array('id'=>$data['Photo']['id'])));		
		$current_image=$current_image[0]['Photo']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['Photo'])){
		
				if($data['Photo']['remove']==0){
				$data['Photo']['image']=$current_image;}
				
				elseif($data['Photo']['remove']!=0){
				$data['Photo']['image']='';}
		}
			if(($this->data['Photo']['image']['size']!=0) &&($this->data['Photo']['image']['size']<=2103361)) 
		  	{			 
		  	$data=$this->data;  
			$image_path = $this->PhotoImageHandler->upload_image_and_thumbnail($data['Photo']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['Photo']['image'] = $image;
			}
		   else if($this->data['Photo']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/Photos/editPhotos/'.$this->data['Photo']['id']);
			}									
			if($this->Photo->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/Photos/viewPhotoDetails/'.$this->data['Photo']['id']);
			}
	}
		}

function viewEnabledPhotos(){
		$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('products', $this->paginate('Photo'));
		$this->render('viewPhotos');
		
	}
	
function viewDisabledPhotos(){
		$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('products', $this->paginate('Photo'));
		$this->render('viewPhotos');
		
	}
	
	
function enablePhotos($id=null){
	$this->Photo->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Photo'));
	$this->redirect('/Photos/viewDisabledPhotos');
}

function enablePhoto($id=null){
	$this->Photo->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Photo'));
	$this->redirect('/Photos/viewPhotos');
}

function disablePhoto($id=null){
	$this->Photo->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Photo'));
	$this->redirect('/Photos/viewPhotos');
}

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

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

function disableSubCategory($id=null){
	$this->PhotoSubCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('PhotoSubCategory'));
	$this->redirect('/Photos/viewSubCategories');	
	}
	
function enableSubCategory($id=null){
	$this->PhotoSubCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('PhotoSubCategory'));
	$this->redirect('/Photos/viewSubCategories');
	}
	
function deleteMainCategory($id){
	$this->PhotoMainCategory->delete($id);
	$this->redirect($this->referer());
	}
	
function deletePhoto($id){
	$this->Photo->delete($id);
	$this->redirect($this->referer());
}
	
	

}
?>