<?php 
class BannersController extends AppController{
var $name = 'Banners';
var $uses = array('Banner');
var $helpers = array('Form', 'Html', 'Text','Javascript', 'Time', 'Number','Paginator','Cache','Ajax', 'Session');
var $components=array('RequestHandler');

function listbanner(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','status','image','date','id');
		$this->set('main_categories', $this->paginate('Banner'));
	}
	

function addbanner(){
	
		if(!empty($this->data)){
		$data=$this->data;
		$pic = $this->Banner->find('all',array('order'=>array('Banner.id DESC')));
		if(empty($pic)){$pic = 0;}
		$pic_id1 = $pic[0]['Banner']['image'];
		$pic_id2 = explode(".", $pic_id1);
		$pic_id3 = $pic_id2[0];
		$pic_id = $pic_id3 + 1;
		//echo $pic_id; exit;
		if($data['Banner']['image']['size']!=0) {
			
		$file = new File($data['Banner']['image']['tmp_name']);
		$ext = $data['Banner']['image']['name'];
		$point = strrpos($ext,".");
		
		$ext = substr($ext,$point+1,(strlen($ext)-$point));
		$ext=strtolower($ext);
	if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png') {
		
		$data['Banner']['image']='';
		
		
		} else {
		ini_set('date.timezone', 'Europe/London');   
        $now = date('Y-m-d-His');
        $name= $pic_id;
		$filename = $name.'.'.$ext;
		$data1 = $file->read();
		$file->close();
		
		$path=WWW_ROOT.'img/banner/'.$filename;
		$file = new File(WWW_ROOT.'img/banner/'.$filename,true);
		$file->write($data1);
		//echo "whoop";
		$filename=$filename;
		
		$data['Banner']['image']=$filename;
		}
	}
	else{
		
		$data['Banner']['image']='';		
	}			
	
			if($this->Banner->validates()){
							
			if($this->Banner->save($data)){
			//print_r($data);
				
				if(isset($file)){
				$file->close();
				}
				
				
			$this->redirect('/Banners/addresponse');
			}else{
				
			if(isset($file)){
					$file->close(); 
					$file->delete();
				}
				
				
				
					
		}
		}
		
		
		
		
		
		}
	}
	
	function addresponse() {

	}
	
	function viewbannerdetails($id=null){
	$data=$this->Banner->find('all',array('conditions'=>array('Banner.id'=>$id)));
	//print_r($data);exit;
	$this->set(compact('data'));	
	
}

function editbanner($id=null){
		
		
	/*if(empty($this->data)){
		$data=$this->Banner->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
			if($this->Banner->validates()){
							
			if($this->Banner->save($data)){
			//print_r($data);
				
			$this->redirect('/Banners/bannereditresponse');
		 	}
			
		}
	}*/
	
	if(empty($this->data)){
		$current_image=$this->Banner->find('all',array('conditions'=>array('id'=>$id)));
		
		$current_image=$current_image[0]['Banner']['image'];
		
		$this->set(compact('current_image'));
		$data=$this->Banner->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$pic = $this->Banner->find('all',array('conditions'=>array('id'=>$data['Banner']['id']),'order'=>array('Banner.id DESC')));
		$pic_name = $pic[0]['Banner']['image'];
		
		$current_image=$this->Banner->find('all',array('conditions'=>array('id'=>$data['Banner']['id'])));
		
		$current_image=$current_image[0]['Banner']['image'];
		$this->set(compact('current_image'));
		
		
		if($data['Banner']['image']['size']!=0){
		$file = new File($data['Banner']['image']['tmp_name']);
		$ext = $data['Banner']['image']['name'];
		$point = strrpos($ext,".");
		
		$ext = substr($ext,$point+1,(strlen($ext)-$point));
		$ext=strtolower($ext);
	if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png') {
		
		$data['Banner']['image']='';
		
		
		} else {
		ini_set('date.timezone', 'Europe/London');   
        $now = date('Y-m-d-His');
        $name=$pic_name;
		$filename = $name;
		$data1 = $file->read();
		$file->close();
		
		$path=WWW_ROOT.'img/banner/'.$filename;
		$file = new File(WWW_ROOT.'img/banner/'.$filename,true);
		$file->write($data1);
		
		$filename=$filename;
		
		$data['Banner']['image']=$filename;
		}
	}
	else{
		
		if(array_key_exists('remove',$data['Banner'])){
		if($data['Banner']['remove']==0){
		$data['Banner']['image']=$current_image;
		
		}
		elseif($data['Banner']['remove']!=0){
			$data['Banner']['image']='';
		}
		}
		else{
			$data['Banner']['image']='';
			
		}			
	}			
	
			if($this->Banner->validates()){
							
			if($this->Banner->save($data)){
			//print_r($data);
				
				if(isset($file)){
				$file->close();
				}
				
				
			$this->redirect('/Banners/bannereditresponse');
			}else{
				
			if(isset($file)){
					$file->close(); 
					$file->delete();
					}
				
				
				
					
				}
		 	}
			
		}
	}
	
	function bannereditresponse(){
		
		
	}
	
	function enablebanner($id=null){
	$this->Banner->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('Banner'));
   	$this->render('listbanner');
}

function disablebanner($id=null){
	$this->Banner->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('Banner'));
   	$this->render('listbanner');
}

function viewenablebanners(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id','image');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('main_categories', $this->paginate('Banner'));
		$this->render('listbanner');
		
	}
	
function viewdisablebanners(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id','image');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('main_categories', $this->paginate('Banner'));
		$this->render('listbanner');
		
	}

}
?>