<?php 
class SubCategoriesController extends AppController {
	
	var $name="SubCategories";
	var $uses=array('Product','Category','SubCategory');
	var $helpers = array('Form', 'Html', 'Text','Javascript', 'Time', 'Number','Paginator','Cache','Ajax', 'Session');
	var $components=array('RequestHandler');
	
	
	function index() {
		$this->redirect('view');
	}
	
	function add(){
		$categories=$this->Category->find('list',array('fields'=>array('category_name')));
		$this->set(compact('categories'));
		if(!empty($this->data)){
		$data=$this->data;
		$data['SubCategory']['status']=1;
		if ($data['SubCategory']['sub_cat_image_link']['size']!=0) {
			
		$file = new File($data['SubCategory']['sub_cat_image_link']['tmp_name']);
		$ext = $data['SubCategory']['sub_cat_image_link']['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['SubCategory']['sub_cat_image_link']='';
		
		
		} else {
		ini_set('date.timezone', 'Europe/London');   
        $now = date('Y-m-d-His');
        $name=str_replace(" ","-",$data['SubCategory']['sub_cat_name']);
		$filename = $name.'-'.$now.'.'.$ext;
		$data1 = $file->read();
		$file->close();
		$path=WWW_ROOT.'img/sub_category_images/'.$filename;
		$file = new File(WWW_ROOT.'img/sub_category_images/'.$filename,true);
		$file->write($data1);
		$filename='sub_category_images/'.$filename;
		
		$data['SubCategory']['sub_cat_image_link']=$filename;
		}
	}
	else{
		
		$data['SubCategory']['sub_cat_image_link']='';		
	}			
			if($this->SubCategory->validates()){
							
			if($this->SubCategory->save($data)){
				if(isset($file)){
			$file->close();}
			$this->redirect('/SubCategories/addResponse');
			}else{
				
			if(isset($file)){
					$file->close(); 
					$file->delete();
				}
				
				
				
			//print_r($this->data);			
		}
		}
			
			
			
		}		
		
	}
	function view(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('SubCategory.date_added' => 'desc');
		$categories=$this->Category->find('list',array('fields'=>array('Category.category_name')));
		$this->set('categories',$categories);
		$this->set('subcategories', $this->paginate('SubCategory'));
	}
function viewSubCategoryDetails($id=null){
	
	$subcategories=$this->SubCategory->find('all',array('conditions'=>array('SubCategory.id'=>$id)));
	$categories=$this->Category->find('list',array('fields'=>array('Category.category_name')));
	$this->set(compact('categories','subcategories'));
	//$this->render('viewSubCategoryDetails');
	
}
function viewEnabled(){
	$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('SubCategory.date_added' => 'desc');
   		$this->paginate['conditions']= array('SubCategory.status'=>'1');
		$categories=$this->Category->find('list',array('fields'=>array('Category.category_name')));
		$this->set('categories',$categories);
		$this->set('subcategories', $this->paginate('SubCategory'));
		$this->set('status','1');
		$this->render('view');
		
		
		
	}
function viewDisabled(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('SubCategory.date_added' => 'desc');
   		$this->paginate['conditions']= array('SubCategory.status'=>'0');
		$categories=$this->Category->find('list',array('fields'=>array('Category.category_name')));
		$this->set('categories',$categories);
		$this->set('subcategories', $this->paginate('SubCategory'));
		$this->set('status','0');
		$this->render('view');
	}
function enable($id=null){
	$this->SubCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('SubCategory.date_added' => 'desc');
		$categories=$this->Category->find('list',array('fields'=>array('Category.category_name')));
		$this->set('categories',$categories);
		$this->set('subcategories', $this->paginate('SubCategory'));
	
	$this->redirect('/SubCategories/view');
}

function disable($id=null){
	$this->SubCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('SubCategory.date_added' => 'desc');
		$categories=$this->Category->find('list',array('fields'=>array('Category.category_name')));
		$this->set('categories',$categories);
		$this->set('subcategories', $this->paginate('SubCategory'));
	
	$this->redirect('/SubCategories/view');
	
}
	function edit($id=null){
	$subcategories=$this->SubCategory->find('all',array('conditions'=>array('id'=>$id)));
	$categories=$this->Category->find('list',array('fields'=>array('Category.category_name')));
	$this->set('categories',$categories);
	$this->set('subcategories',$subcategories);
	
	if(empty($this->data)){
		$this->data=$subcategories[0];
		
				
	}
	elseif(!empty($this->data)){
		$subcategories=$this->SubCategory->find('all',array('conditions'=>array('id'=>$this->data['SubCategory']['id'])));
		$old_image_link=$subcategories[0]['SubCategory']['sub_cat_image_link'];
		$this->set('subcategories',$subcategories);
		
		$data=$this->data;
		$data['SubCategory']['status']=1;
		if ($data['SubCategory']['sub_cat_image_link']['size']!=0) {
			
		$file = new File($data['SubCategory']['sub_cat_image_link']['tmp_name']);
		$ext = $data['SubCategory']['sub_cat_image_link']['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['SubCategory']['sub_cat_image_link']='';
		
		
		} else {
		ini_set('date.timezone', 'Europe/London');   
        $now = date('Y-m-d-His');
        $name=str_replace(" ","-",$data['SubCategory']['category_name']);
		$filename = $name.'-'.$now.'.'.$ext;
		$data1 = $file->read();
		$file->close();
		$path=WWW_ROOT.'img/sub_category_images/'.$filename;
		$file = new File(WWW_ROOT.'img/sub_category_images/'.$filename,true);
		$file->write($data1);
		$filename='sub_category_images/'.$filename;
		
		$data['SubCategory']['sub_cat_image_link']=$filename;
		}
	}
	else{
		
		$data['SubCategory']['sub_cat_image_link']=$subcategories[0]['SubCategory']['sub_cat_image_link'];		
	}			
			if($this->SubCategory->validates()){
							
			if($this->SubCategory->save($data)){
				if(isset($file)){
				$file->close();
				}
				if($data['SubCategory']['sub_cat_image_link']!=$old_image_link){
				$file=new File(WWW_ROOT.'img/'.$old_image_link,true);
				$file->close();
					$file->delete();
				}
			$this->redirect('/SubCategories/editResponse');
			}else{
				
			if(isset($file)){
					$file->close(); 
					$file->delete();
				}
				
				
				
			//print_r($this->data);			
		}
		}
		
		
	}

		
		
	}
	
	
	function addResponse(){}
function editResponse(){}
	
	
}
	
	