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

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

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

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

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

}



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

function viewNews(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('long_title','date','status','id');
		$this->set('products', $this->paginate('New'));
		$this->render('viewNews');
		
	}
function viewNewDetails($id=null){
	$data=$this->New->find('all',array('conditions'=>array('New.id'=>$id)));	
	$this->set(compact('data'));	
	$main_cats=$this->NewMainCategory->find('list', array('fields'=>'name'));
	$sub_cats=$this->NewSubCategory->find('list', array('fields'=>'name'));
	$this->set(compact('main_cats','sub_cats'));
	
}

	function addNew(){	
		if(empty($this->data)){
		$main_cats=$this->NewMainCategory->find('list', array('fields'=>'name'));
		$sub_cats=$this->NewSubCategory->find('list', array('fields'=>'name'));
		$this->set(compact('main_cats','sub_cats'));
		}
		if((isset($this->data['New']['image']))&&($this->data['New']['image']['size']!=0) &&($this->data['New']['image']['size']>2103361)){
		 		$this->data['New']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addNew'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['New']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			/*// add thumbnail image.....
			$image_thumbnail = $image_path['dir'].$image_path['img_folder'].$image_path['img_file']."_thumbnail.". $image_path['type'];			
			echo $image_thumbnail;exit;
			// end thumbnail image.....
			*/
			$data['New']['image'] = $image;
			if ($this->New->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/News/viewDisabledNews');
		  }
		else if($this->data['New']['image']['size']>2103361)		
				{
				 $this->data['New']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addNew'));
				}		
		      }
	    }
///////////////////////////////////////////

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

function viewEnabledNews(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('long_title','status','date','id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('products', $this->paginate('New'));
		$this->render('viewNews');
		
	}
	
function viewDisabledNews(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('long_title','date','status','id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('products', $this->paginate('New'));
		$this->render('viewNews');
		
	}
	
	
function enableNews($id=null){
	$this->New->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('New'));
	$this->redirect('/News/viewDisabledNews');
}

function enableNew($id=null){
	$this->New->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('New'));
	$this->redirect('/News/viewNews');
}

function disableNew($id=null){
	$this->New->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('New'));
	$this->redirect('/News/viewNews');
}

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

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

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

}
?>