<?php
class HomesController extends AppController {

	var $name = 'Homes';
	var $components = array('Session','SlideHandler','ImageHandler');
	var $uses=array('Home','Slide');
	var $helpers = array('Form', 'Html', 'Text','Javascript', 'Time', 'Number', 'Session', 'Paginator', 'Ajax', 'Cache');

	
	function index() {
		$this->set('menuTab', 'Welcome Messages');

	}
	
	function view($id) {
		$this->Home->id = $id;
		$this->set('item', $this->Home->read());
		$this->set('menuTab', 'Welcome Messages');

	}

	function add() {
		$this->set('menuTab', 'Welcome Messages');
		if (!empty($this->data)) {
		
		if($this->data['Home']['image']){
		$image_path = $this->ImageHandler->upload_image_and_thumbnail($this->data['Home']['image']);
		$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
		$this->data['Home']['image'] = $image;
		}else{
		$this->data['Home']['image'] = '';
		}
		
			if ($this->Home->save($this->data)) {
				$this->Session->setFlash('Saved Successfully.');
				$this->redirect(array('action' => 'unpublished'));
			}
		}
	}
	
		
	
	function delete($id) {
		$this->set('menuTab', 'Welcome Messages');
	if ($this->Home->delete($id)) {
		$this->Session->setFlash('Deleted Succesfully.');
		$this->redirect($this->referer());
								  }
	}
	
	function edit($id = null) {
	$this->set('menuTab', 'Welcome Messages');
	$this->Home->id = $id;
	if (empty($this->data)) {
		$this->data = $this->Home->read();
	} else {
	
	
	if($this->data['Home']['image']){
		$image_path = $this->ImageHandler->upload_image_and_thumbnail($this->data['Home']['image']);
		$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
		$this->data['Home']['image'] = $image;
		}else{
		$img=$this->Home->find('all',array('conditions'=>array('id'=>$this->data['Home']['id']),'fields'=>array('image')));
		$this->data['Home']['image'] = $img[0]['Home']['image'];
		}
	
		if ($this->Home->save($this->data)) {
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect(array('action' => 'unpublished'));
			}
		}
	}
	
	function published() {
		$this->set('menuTab', 'Welcome Messages');
		$this->paginate['limit'] = 50;
		$this->paginate['conditions'] = array('status' => 1);
		$this->paginate['order'] = array('id' => 'desc');
		$this->set('listitems', $this->paginate('Home'));	
	}
	
	function unpublished() {
		$this->set('menuTab', 'Welcome Messages');
		$this->paginate['limit'] = 50;
		$this->paginate['conditions'] = array('status' => 0);
		$this->paginate['order'] = array('id' => 'desc');
		$this->set('listitems', $this->paginate('Home'));	
	}
	
	function publish($id){
		$this->set('menuTab', 'Welcome Messages');
		if($id){
		  $this->Home->id = $id;
		  $this->Home->saveField('status', 1);
		  $this->redirect($this->referer());
		    }
		else {
		$this->Session->setFlash('Invalid Operation.');
		$this->redirect($this->referer());	
			}
		
				
	}
	
	function unpublish($id){
		$this->set('menuTab', 'Welcome Messages');
		if($id){
		  $this->Home->id = $id;
		  $this->Home->saveField('status', 0);
		  $this->redirect($this->referer());
		    }
		else {
		$this->Session->setFlash('Invalid Operation.');
		$this->redirect($this->referer());	
			}
		
				
	}
	

		function enabledslides() {
		$this->paginate['limit'] = 50;
		$this->paginate['conditions'] = array('status' => 1);
		$this->paginate['order'] = array('position' => 'ASC');
		$this->set('listitems', $this->paginate('Slide'));	
	}
	
		function disabledslides() {
		$this->paginate['limit'] = 50;
		$this->paginate['conditions'] = array('status' => 0);
		$this->paginate['order'] = array('id' => 'desc');
		$this->set('listitems', $this->paginate('Slide'));	
	}
	/*// Add slide
	function addslide(){
		if(!empty($this->data)){
		$data=$this->data;
		if($data['Slide']['image']['size']!=0) {			
		$file = new File($data['Slide']['image']['tmp_name']);
		$ext = $data['Slide']['image']['name'];
		$point = strrpos($ext,".");
		$ext = substr($ext,$point+1,(strlen($ext)-$point));
		$ext=strtolower($ext);
	if ($ext != 'jpg' && $ext != 'png' && $ext != 'gif' && $ext != 'jpeg') {		
		$data['Slide']['image']='';		
		} else {
		ini_set('date.timezone', 'Europe/London');   
        $now = date('Y-m-d-His');
        $name = "Slide";
		$filename = $name.'-'.$now.'.'.$ext;
		$data1 = $file->read();
		$file->close();
		
		$path=WWW_ROOT.'img/Flash/'.$filename;
		$file = new File(WWW_ROOT.'img/Flash/'.$filename,true);
		$file->write($data1);
	
		$filename='Flash/'.$filename;
		
		$data['Slide']['image']=$filename;
		}
	}
	else{		
		$data['Slide']['image']='';		
	}				
			if($this->Slide->validates()){			
						if($this->Slide->save($data)){
						$this->Session->setFlash('Slide uploaded Successfully.');				
						if(isset($file)){
						$file->close();
						}				
			$this->redirect('/Homes/disabledslides');
			}else{				
			if(isset($file)){
					$file->close(); 
					$file->delete();
				}
			}
		  }		
		}
	}
	
	// End Add slide*/
	
	function addslide(){	
			
		if((isset($this->data['Slide']['image']['size']))&&($this->data['Slide']['image']['size']!=0) &&($this->data['Slide']['image']['size']>2103361)){
		 		$this->data['Slide']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addslide'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->SlideHandler->upload_image_and_thumbnail($data['Slide']['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'];			
						
			$data['Slide']['image'] = $image;
			$data['Slide']['image_thumbnail'] = $image_thumbnail;
			if ($this->Slide->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect(array('action' => 'addslide'));
		  }
		else if($this->data['Slide']['image']['size']>2103361)		
				{
				 $this->data['Slide']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addslide'));
				}		
		      }
	    }

		function disableslide($id){
		$this->set('menuTab', 'Welcome Messages');
		if($id){
		  $this->Slide->id = $id;
		  $this->Slide->saveField('status', 0);
		  $this->redirect($this->referer());
		    }
		else {
		$this->Session->setFlash('Invalid Operation.');
		$this->redirect($this->referer());	
			}
		
				
	}
	
	
		function enableslide($id){
		$this->set('menuTab', 'Welcome Messages');
		if($id){
		  $this->Slide->id = $id;
		  $this->Slide->saveField('status', 1);
		  $this->redirect($this->referer());
		    }
		else {
		$this->Session->setFlash('Invalid Operation.');
		$this->redirect($this->referer());	
			}				
	}
	
		function deleteslide($id) {
		if ($this->Slide->delete($id)) {
		$this->Session->setFlash('Deleted Succesfully.');
		$this->redirect($this->referer());
								  }
	}
	
	function editslide($id = null) {
	$this->Slide->id = $id;
	if (empty($this->data)) {
		$this->data = $this->Slide->read();
	} else {
	
			if($this->data['Slide']['image']['size'] != 0){
				$image_path = $this->SlideHandler->upload_image_and_thumbnail($this->data['Slide']['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'];			
						
			$this->data['Slide']['image'] = $image;
			$this->data['Slide']['image_thumbnail'] = $image_thumbnail;
			}
			else{
		$img=$this->Slide->find('all',array('conditions'=>array('id'=>$this->data['Slide']['id']),'fields'=>array('image')));
		$this->data['Slide']['image'] = $img[0]['Slide']['image'];
		}
		//print_r($this->data);exit;
		if ($this->Slide->save($this->data)) {
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect(array('action' => 'enabledslides'));
			}
		}
	}
}
?>
