<?php 
class ClientsController extends AppController{
	var $name="Clients";
	var $uses=array('ClientMainCategory','ClientCategory','ClientSubCategory');
	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('ClientMainCategory'));
		$this->render('viewAllMainCategories');
		
	}
function viewMainCategoryDetails($id=null){
	$data=$this->ClientMainCategory->find('all',array('conditions'=>array('ClientMainCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

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

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

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

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

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

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

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

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

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

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

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

}
?>