<?php 
class AthletesController extends AppController{
	var $name="Athletes";
	var $uses=array('AthleteMainCategory','Athlete','AthleteSubCategory');
	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('name','date','status','id');
		$this->set('main_categories', $this->paginate('AthleteMainCategory'));
		$this->render('viewAllMainCategories');
		
	}
function viewMainCategoryDetails($id=null){
	$data=$this->AthleteMainCategory->find('all',array('conditions'=>array('AthleteMainCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

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

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

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

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

}



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

function viewAthletes(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->set('Athletes', $this->paginate('Athlete'));
		$this->render('viewAllAthletes');
		
	}
function viewAthleteDetails($id=null){
	$data=$this->Athlete->find('all',array('conditions'=>array('Athlete.id'=>$id)));	
	$this->set(compact('data'));	
	$main_cats=$this->AthleteMainCategory->find('list', array('fields'=>'name'));
	$sub_cats=$this->AthleteSubCategory->find('list', array('fields'=>'name'));
	$this->set(compact('main_cats','sub_cats'));
	
}

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

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

function viewEnabledAthletes(){
		$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('Athletes', $this->paginate('Athlete'));
		$this->render('viewAthletes');
		
	}
	
function viewDisabledAthletes(){
		$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('Athletes', $this->paginate('Athlete'));
		$this->render('viewAthletes');
		
	}
	
	
function enableAthletes($id=null){
	$this->Athlete->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('Athletes', $this->paginate('Athlete'));
	$this->redirect('/Athletes/viewDisabledAthletes');
}

function enableAthlete($id=null){
	$this->Athlete->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('Athletes', $this->paginate('Athlete'));
	$this->redirect('/Athletes/viewAthletes');
}

function disableAthlete($id=null){
	$this->Athlete->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('Athletes', $this->paginate('Athlete'));
	$this->redirect('/Athletes/viewAthletes');
}

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

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

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

}
?>