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

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

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

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

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

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

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

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

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

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

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

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

}
?>