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

function add(){
		$cat = $this->DownloadCat->find('list', array('fields'=>array('cat_id','title'),'conditions'=>array('status'=>'1')));
		$this->set(compact('cat'));
		ini_set('upload_max_filesize', '10M');
		if(!empty($this->data)){
		$data=$this->data;
		
		//$data['Download']['year'] = $data['Download']['year']['year'];
		//$data['Download']['release_year'] = $data['Download']['release_year']['year'];
	//	print_r($data);
		$pic = $this->Download->find('all',array('order'=>array('Download.id DESC')));
		if(empty($pic)){$pic = 0;}
		$pic_id1 = $pic[0]['Download']['image'];
		$pic_id2 = explode(".", $pic_id1);
		$pic_id3 = $pic_id2[0];
		$pic_id = $pic_id3 + 1;
		//echo $pic_id; exit;
		if($data['Download']['image']['size']!=0) {
			
		$file = new File($data['Download']['image']['tmp_name']);
		$ext = $data['Download']['image']['name'];
		$point = strrpos($ext,".");
		
		$ext = substr($ext,$point+1,(strlen($ext)-$point));
		$ext=strtolower($ext);
	if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'pdf' && $ext != 'doc' && $ext != 'docx' && $ext != 'odt') {
		
		$data['Download']['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.'files/Download/'.$filename;
		$file = new File(WWW_ROOT.'files/Download/'.$filename,true);
		$file->write($data1);
		//echo "whoop";
		$filename=$filename;
		
		$data['Download']['image']=$filename;
		}
	}
	else{
		
		$data['Download']['image']='';		
	}			
	
			if($this->Download->validates()){
							
			if($this->Download->save($data)){
			//print_r($data);
				
				if(isset($file)){
				$file->close();
				}
				
				$this->Session->setFlash('Added Successfully to disabled Downloads.');

			$this->redirect('/downloads/disabled');
			}else{
				
			if(isset($file)){
					$file->close(); 
					$file->delete();
				}
				
				
				
					
		}
		}
		
		
		
		
		
		}
	}
	

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

function editDownload($id=null){

	$cat = $this->DownloadCat->find('list', array('fields'=>array('cat_id','title'),'conditions'=>array('status'=>'1')));
	$this->set(compact('cat'));
	ini_set('upload_max_filesize', '10M');	
	if(empty($this->data)){
		$current_image = $this->Download->find('all',array('conditions'=>array('id'=>$id)));
		
		$current_image = $current_image[0]['Download']['image'];
		
		$this->set(compact('current_image'));
		$data = $this->Download->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}
	
	elseif(!empty($this->data)){
		$data = $this->data;
		$pic = $this->Download->find('all',array('conditions'=>array('id'=>$data['Download']['id']),'order'=>array('Download.id DESC')));
		$pic_name = $pic[0]['Download']['image'];

		if($data['Download']['image']['name'] == ''){
		$current_image = $this->Download->find('all',array('conditions'=>array('id'=>$data['Download']['id'])));
		//$current_image = $current_image[0]['Download']['image'];
		$this->set(compact('current_image'));
		$data['Download']['image'] = $current_image[0]['Download']['image'];
		} 
		elseif($data['Download']['image']['size']!=0){
		$file = new File($data['Download']['image']['tmp_name']);
		$ext = $data['Download']['image']['name'];
		$point = strrpos($ext,".");
		
		$ext = substr($ext,$point+1,(strlen($ext)-$point));
		$ext = strtolower($ext);
		if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'pdf' && $ext != 'doc' && $ext != 'docx' && $ext != 'odt') {
			
			$data['Download']['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.'files/Download/'.$filename;
			$file = new File(WWW_ROOT.'files/Download/'.$filename,true);
			$file->write($data1);
			
			//$filename = $filename;
			
			$data['Download']['image'] = $filename;
			}
		}
		/*else{
		
		if(array_key_exists('remove',$data['Download'])){
		if($data['Download']['remove']==0){
		$data['Download']['image']=$current_image;
		
		}
		elseif($data['Download']['remove']!=0){
			$data['Download']['image']='';
		}
		}
		else{
			$data['Download']['image']='';
			
		}			
	}*/			
	
			if($this->Download->validates()){
							
			if($this->Download->save($data)){
			//print_r($data);
				
				if(isset($file)){
				$file->close();
				}
				
			$this->Session->setFlash('Successfully Edited');	
			$this->redirect('/Downloads/enabled');
			}else{
				
			if(isset($file)){
					$file->close(); 
					$file->delete();
					}
				
				
				
					
				}
		 	}
			
		}
	}
	
	
	//download category
	function add_cat(){
		if(!empty($this->data)){
			if($this->DownloadCat->save($this->data)){
				$this->Session->setFlash('Successfully Saved');
				$this->redirect('/Downloads/view_disabled');
			}else{
				$this->Session->setFlash('Failed');
				$this->redirect($this->referer());
			}
			
		}
		
		
	}
	
	function edit_cat($id = null){
		$down = $this->DownloadCat->find('first', array('conditions'=>array('cat_id'=>$id)));
		$this->set(compact('down'));
		$this->DownloadCat->id = $id;
		if(empty($this->data)){
			$this->data = $this->DownloadCat->read();
		}else{
			if($this->DownloadCat->save($this->data)){
				$this->Session->setFlash('Successfully Edited');
				$this->redirect('/Downloads/view_enabled');
			}else{
				$this->Session->setFlash('Failed');
				$this->redirect($this->referer());
			}
		}
		
	}
	
	function enable_cat($id = null){
	$this->DownloadCat->updateAll(array('status'=>'1'),array('cat_id'=>$id));
	$this->Session->setFlash('Enabled Successfully.');
	$this->redirect($this->referer());	
		
	}
	
	function disable_cat($id = null){
	$this->DownloadCat->updateAll(array('status'=>'0'),array('cat_id'=>$id));
	$this->Session->setFlash('Disabled Successfully.');
    $this->redirect($this->referer());	
		
	}
	
	function view_enabled(){
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date_added' => 'desc');
   	$this->paginate['fields'] = array('title','date_added','status','cat_id');
	$this->paginate['conditions']= array('status'=>'1');
	$this->set('status','1');
   	$this->set('main_categories', $this->paginate('DownloadCat'));	
		
	}
	
	function view_disabled(){
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date_added' => 'desc');
   	$this->paginate['fields'] = array('title','date_added','status','cat_id');
	$this->paginate['conditions']= array('status'=>'0');
	$this->set('status','0');
   	$this->set('main_categories', $this->paginate('DownloadCat'));	
		
	}
	//end download category
	
	
	function enable($id=null){
	$this->Download->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->Session->setFlash('Enabled Successfully.');
	$this->redirect($this->referer());
}

function disable($id=null){
	$this->Download->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->Session->setFlash('Disabled Successfully.');
    $this->redirect($this->referer());
}

function enabled(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id','cat_id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('main_categories', $this->paginate('Download'));
		$cat = $this->DownloadCat->find('list', array('conditions'=>array('status'=>'1')));
		$this->set(compact('cat'));
		
	}
	
function disabled(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id','cat_id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('main_categories', $this->paginate('Download'));
		$cat = $this->DownloadCat->find('list', array('conditions'=>array('status'=>'1')));
		$this->set(compact('cat'));
		
	}
	
	function delete_Download($id=null){
	$this->Download->delete($id);
	$this->redirect($this->referer());
}

}
?>