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

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

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

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

function disableMainCategory($id=null){
	$this->ProjectMainCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('ProjectMainCategory'));
	$this->redirect('/Projects/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('ProjectCategory'));
		$this->render('viewAllCategories');
		
	}
function viewCategoryDetails($id=null){
	$data=$this->ProjectCategory->find('all',array('conditions'=>array('ProjectCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$main_cats=$this->ProjectMainCategory->find('list', array('fields'=>'title'));
	$this->set(compact('main_cats'));
}

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

	function editCategories($id=null){
		$main_cats=$this->ProjectMainCategory->find('list', array('fields'=>'title'));
			$this->set(compact('main_cats'));
		
	if(empty($this->data)){
		$current_image=$this->ProjectCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['ProjectCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->ProjectCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->ProjectCategory->find('all',array('conditions'=>array('id'=>$data['ProjectCategory']['id'])));		
		$current_image=$current_image[0]['ProjectCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['ProjectCategory'])){
		
				if($data['ProjectCategory']['remove']==0){
				$data['ProjectCategory']['image']=$current_image;}
				
				elseif($data['ProjectCategory']['remove']!=0){
				$data['ProjectCategory']['image']='';}
		}
			if(($this->data['ProjectCategory']['image']['size']!=0) &&($this->data['ProjectCategory']['image']['size']<=2103361)) 
		  	{			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['ProjectCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['ProjectCategory']['image'] = $image;
			}
		   else if($this->data['ProjectCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/Projects/editCategories/'.$this->data['ProjectCategory']['id']);
			}									
			if($this->ProjectCategory->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/Projects/viewCategoryDetails/'.$this->data['ProjectCategory']['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('ProjectCategory'));
		$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('ProjectCategory'));
		$this->render('viewCategories');
		
	}
	
	
function enableCategories($id=null){
	$this->ProjectCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('ProjectCategory'));
	$this->redirect('/Projects/viewDisabledCategories');
}

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

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

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

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

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

function deleteMainCategoryDetails($id){
	$this->ProjectMainCategory->delete($id);
	$this->redirect($this->referer());
}

}
?>