<?php 
class ProgramsController extends AppController{
	var $name="Programs";
	var $uses=array('Program');
	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 viewPrograms(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','id');
		$this->set('main_categories', $this->paginate('Program'));
		$this->render('viewAllPrograms');
		
	}
function viewProgramsDetails($id=null){
	$data=$this->Program->find('all',array('conditions'=>array('Program.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

		
		function addProgram() {		
		if((isset($this->data['Program']['image']))&&($this->data['Program']['image']['size']!=0) &&($this->data['Program']['image']['size']>2103361)){
		 		$this->data['Program']['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['Program']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['Program']['image'] = $image;
			if ($this->Program->save($data)) {
				$this->Session->setFlash('Saved Successfully.');
				$this->redirect('/Programs/viewPrograms');
		  }
		else if($this->data['Program']['image']['size']>2103361)		
				{
				 $this->data['Program']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addProgram'));
				}		
		      }
	    }
	
	
function editPrograms($id=null){		
	if(empty($this->data)){
		$current_image=$this->Program->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['Program']['image'];		
		$this->set(compact('current_image'));
		$data=$this->Program->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->Program->find('all',array('conditions'=>array('id'=>$data['Program']['id'])));		
		$current_image=$current_image[0]['Program']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['Program'])){
		
				if($data['Program']['remove']==0){
				$data['Program']['image']=$current_image;}
				
				elseif($data['Program']['remove']!=0){
				$data['Program']['image']='';}
		}
			if(($this->data['Program']['image']['size']!=0) &&($this->data['Program']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['Program']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['Program']['image'] = $image;
			}
		   else if($this->data['Program']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');		
			$this->redirect('/Programs/editPrograms/'.$this->data['Program']['id']);
		}
		
			
										
			if($this->Program->save($data)){	
			$this->Session->setFlash('Updated Successfully.');				
			$this->redirect('/Programs/viewProgramsDetails/'.$this->data['Program']['id']);
			}
	}
	}
	
	
	function ProgramsEditResponse(){
		
		
	}

function viewEnabledPrograms(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','id');
   		$this->set('main_categories', $this->paginate('Program'));
		$this->render('viewPrograms');
		
	}
	
function viewDisabledPrograms(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','id');
   		$this->set('main_categories', $this->paginate('Program'));
		$this->render('viewPrograms');
		
	}
	
		
function enableProgram($id=null){
	$this->Program->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('Program'));
   	//$this->render('viewPrograms');
	$this->redirect('/Programs/viewDisabledPrograms');
}

function enablePrograms($id=null){
	$this->Program->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('Program'));
	$this->redirect('/Programs/viewPrograms');
}

function disableProgram($id=null){
	$this->Program->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('Program'));
	$this->redirect('/Programs/viewEnabledPrograms');
}

function disablePrograms($id=null){
	$this->Program->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('Program'));
	$this->redirect('/Programs/viewPrograms');

}



//MAIN CATEGORY SECTION ENDS HERE


}
?>