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

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

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

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

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

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

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

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

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

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

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

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

}
?>