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

function listpartner(){
		$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('Partner'));
	}
	

function addpartner(){
	
		if(!empty($this->data)){
		$data=$this->data;
		$pic = $this->Partner->find('all',array('order'=>array('Partner.id DESC')));
		if(empty($pic)){$pic = 0;}
		$pic_id1 = $pic[0]['Partner']['image'];
		$pic_id2 = explode(".", $pic_id1);
		$pic_id3 = $pic_id2[0];
		$pic_id = $pic_id3 + 1;
		//echo $pic_id; exit;
		if($data['Partner']['image']['size']!=0) {
			
		$file = new File($data['Partner']['image']['tmp_name']);
		$ext = $data['Partner']['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['Partner']['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/Partner/'.$filename;
		$file = new File(WWW_ROOT.'img/Partner/'.$filename,true);
		$file->write($data1);
		//echo "whoop";
		$filename=$filename;
		
		$data['Partner']['image']=$filename;
		}
	}
	else{
		
		$data['Partner']['image']='';		
	}			
	
			if($this->Partner->validates()){
							
			if($this->Partner->save($data)){
			//print_r($data);
				
				if(isset($file)){
				$file->close();
				}
				
				
			$this->redirect('/Partners/addresponse');
			}else{
				
			if(isset($file)){
					$file->close(); 
					$file->delete();
				}
				
				
				
					
		}
		}
		
		
		
		
		
		}
	}
	
	function addresponse() {

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

function editpartner($id=null){
		
		
	/*if(empty($this->data)){
		$data=$this->partner->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
			if($this->partner->validates()){
							
			if($this->partner->save($data)){
			//print_r($data);
				
			$this->redirect('/Partners/partnereditresponse');
		 	}
			
		}
	}*/
	
	if(empty($this->data)){
		$current_image=$this->Partner->find('all',array('conditions'=>array('id'=>$id)));
		
		$current_image=$current_image[0]['Partner']['image'];
		
		$this->set(compact('current_image'));
		$data=$this->Partner->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$pic = $this->Partner->find('all',array('conditions'=>array('id'=>$data['Partner']['id']),'order'=>array('Partner.id DESC')));
		$pic_name = $pic[0]['Partner']['image'];
		
		$current_image=$this->Partner->find('all',array('conditions'=>array('id'=>$data['Partner']['id'])));
		
		$current_image=$current_image[0]['Partner']['image'];
		$this->set(compact('current_image'));
		
		
		if($data['Partner']['image']['size']!=0){
		$file = new File($data['Partner']['image']['tmp_name']);
		$ext = $data['Partner']['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['Partner']['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/Partner/'.$filename;
		$file = new File(WWW_ROOT.'img/Partner/'.$filename,true);
		$file->write($data1);
		
		$filename=$filename;
		
		$data['Partner']['image']=$filename;
		}
	}
	else{
		
		if(array_key_exists('remove',$data['Partner'])){
		if($data['Partner']['remove']==0){
		$data['Partner']['image']=$current_image;
		
		}
		elseif($data['Partner']['remove']!=0){
			$data['Partner']['image']='';
		}
		}
		else{
			$data['Partner']['image']='';
			
		}			
	}			
	
			if($this->Partner->validates()){
							
			if($this->Partner->save($data)){
			//print_r($data);
				
				if(isset($file)){
				$file->close();
				}
				
				
			$this->redirect('/Partners/partnereditresponse');
			}else{
				
			if(isset($file)){
					$file->close(); 
					$file->delete();
					}
				
				
				
					
				}
		 	}
			
		}
	}
	
	function partnereditresponse(){
		
		
	}
	
	function enablepartner($id=null){
	$this->Partner->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('Partner'));
   	$this->render('listpartner');
}

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

function viewenablepartners(){
		$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('Partner'));
		$this->render('listpartner');
		
	}
	
function viewdisablepartners(){
		$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('Partner'));
		$this->render('listpartner');
		
	}

}
?>