<?php 
class ProductsController extends AppController{
	var $name="Products";
	var $uses=array('ProductMainCategory','Product','ProductSubCategory');
	var $helpers = array('Form', 'Html', 'Text','Javascript', 'Time', 'Number','Paginator','Cache','Ajax', 'Session');
	var $components=array('RequestHandler','ImageHandler','Session');
	
function index() {
		
	}
	
	
	
//MAIN CATEGORIES SECTION STARTS HERE	
function viewMainCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->set('main_categories', $this->paginate('ProductMainCategory'));
		$this->render('viewAllMainCategories');
		
	}
function viewMainCategoryDetails($id=null){
	$data=$this->ProductMainCategory->find('all',array('conditions'=>array('ProductMainCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

		
		function addMainCategory() {		
		if((isset($this->data['ProductMainCategory']['image']))&&($this->data['ProductMainCategory']['image']['size']!=0) &&($this->data['ProductMainCategory']['image'				        ]['size']>2103361)){
		 		$this->data['ProductMainCategory']['image']='';
				$this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addMainCategory'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['ProductMainCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['ProductMainCategory']['image'] = $image;
			if ($this->ProductMainCategory->save($data)) {
				$this->Session->setFlash('Saved Successfully.');
				$this->redirect('/Products/viewMainCategories');
		  }
		else if($this->data['ProductMainCategory']['image']['size']>2103361)		
				{
				 $this->data['ProductMainCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addMainCategory'));
				}		
		      }
	    }
	
	
function editMainCategories($id=null){		
	if(empty($this->data)){
		$current_image=$this->ProductMainCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['ProductMainCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->ProductMainCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->ProductMainCategory->find('all',array('conditions'=>array('id'=>$data['ProductMainCategory']['id'])));		
		$current_image=$current_image[0]['ProductMainCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['ProductMainCategory'])){
		
				if($data['ProductMainCategory']['remove']==0){
				$data['ProductMainCategory']['image']=$current_image;}
				
				elseif($data['ProductMainCategory']['remove']!=0){
				$data['ProductMainCategory']['image']='';}
		}
			if(($this->data['ProductMainCategory']['image']['size']!=0) &&($this->data['ProductMainCategory']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['ProductMainCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['ProductMainCategory']['image'] = $image;
			}
		   else if($this->data['ProductMainCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');		
			$this->redirect('/Products/editMainCategories/'.$this->data['ProductMainCategory']['id']);
		}
		
			
										
			if($this->ProductMainCategory->save($data)){	
			$this->Session->setFlash('Updated Successfully.');				
			$this->redirect('/Products/viewMainCategoryDetails/'.$this->data['ProductMainCategory']['id']);
			}
	}
	}
	


function viewEnabledMainCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','status','date','id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('main_categories', $this->paginate('ProductMainCategory'));
		$this->render('viewMainCategories');
		
	}
	
function viewDisabledMainCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('main_categories', $this->paginate('ProductMainCategory'));
		$this->render('viewMainCategories');
		
	}
	
		
function enableMainCategories($id=null){
	$this->ProductMainCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('ProductMainCategory'));
   	//$this->render('viewMainCategories');
	$this->redirect('/Products/viewDisabledMainCategories');
}

function enableMainCategory($id=null){
	$this->ProductMainCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('ProductMainCategory'));
	$this->redirect('/Products/viewMainCategories');
}

function disableMainCategories($id=null){
	$this->ProductMainCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('ProductMainCategory'));
	$this->redirect('/Products/viewEnabledMainCategories');
}

function disableMainCategory($id=null){
	$this->ProductMainCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('main_categories', $this->paginate('ProductMainCategory'));
	$this->redirect('/Products/viewMainCategories');

}



//MAIN CATEGORY SECTION ENDS HERE
//CATEGORIES SECTION STARTS HERE

function viewProducts(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->set('products', $this->paginate('Product'));
		$this->render('viewAllProducts');
		
	}
function viewProductDetails($id=null){
	$data=$this->Product->find('all',array('conditions'=>array('Product.id'=>$id)));	
	$this->set(compact('data'));	
	$main_cats=$this->ProductMainCategory->find('list', array('fields'=>'name'));
	$sub_cats=$this->ProductSubCategory->find('list', array('fields'=>'name'));
	$this->set(compact('main_cats','sub_cats'));
	
}

	function addProduct(){	
		if(empty($this->data)){
		$main_cats=$this->ProductMainCategory->find('list', array('conditions'=>array('status'=>1),'fields'=>'name'));
		$sub_cats=$this->ProductSubCategory->find('list', array('conditions'=>array('status'=>1),'fields'=>'name'));
		$this->set(compact('main_cats','sub_cats'));
		}
		if((isset($this->data['Product']['image']))&&($this->data['Product']['image']['size']!=0) &&($this->data['Product']['image']['size']>2103361)){
		 		$this->data['Product']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addProduct'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['Product']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['Product']['image'] = $image;
			if ($this->Product->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/Products/viewProducts');
		  }
		else if($this->data['Product']['image']['size']>2103361)		
				{
				 $this->data['Product']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addProduct'));
				}		
		      }
	    }
///////////////////////////////////////////

	function editProducts($id=null){
		$main_cats=$this->ProductMainCategory->find('list', array('conditions'=>array('status'=>1),'fields'=>'name'));
		$sub_cats=$this->ProductSubCategory->find('list', array('conditions'=>array('status'=>1),'fields'=>'name'));
		$this->set(compact('main_cats','sub_cats'));
		
	if(empty($this->data)){
		$current_image=$this->Product->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['Product']['image'];		
		$this->set(compact('current_image'));
		$data=$this->Product->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->Product->find('all',array('conditions'=>array('id'=>$data['Product']['id'])));		
		$current_image=$current_image[0]['Product']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['Product'])){
		
				if($data['Product']['remove']==0){
				$data['Product']['image']=$current_image;}
				
				elseif($data['Product']['remove']!=0){
				$data['Product']['image']='';}
		}
			if(($this->data['Product']['image']['size']!=0) &&($this->data['Product']['image']['size']<=2103361)) 
		  	{			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['Product']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['Product']['image'] = $image;
			}
		   else if($this->data['Product']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/Products/editProducts/'.$this->data['Product']['id']);
			}									
			if($this->Product->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/Products/viewProductDetails/'.$this->data['Product']['id']);
			}
	}
		}

function viewEnabledProducts(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','status','date','id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('products', $this->paginate('Product'));
		$this->render('viewProducts');
		
	}
	
function viewDisabledProducts(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('products', $this->paginate('Product'));
		$this->render('viewProducts');
		
	}
	
	
function enableProducts($id=null){
	$this->Product->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Product'));
	$this->redirect('/Products/viewDisabledProducts');
}

function enableProduct($id=null){
	$this->Product->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Product'));
	$this->redirect('/Products/viewProducts');
}

function disableProduct($id=null){
	$this->Product->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Product'));
	$this->redirect('/Products/viewProducts');
}

function disableProducts($id=null){
	$this->Product->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('products', $this->paginate('Product'));
	$this->redirect('/Products/viewEnabledProducts');
}
	
	
//CATEGORIES SECTION ENDS HERE
//SUB CATEGORIES SECTION STARTS HERE	
	
function viewSubCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->set('sub_categories', $this->paginate('ProductSubCategory'));
		$this->render('viewAllSubCategories');		
	}
	
function viewSubCategoryDetails($id=null){
	$data=$this->ProductSubCategory->find('all',array('conditions'=>array('ProductSubCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$cats=$this->ProductMainCategory->find('list', array('fields'=>'name'));
	$this->set(compact('cats'));
}
 ////////////
 function addSubCategory(){	
	$cats=$this->ProductMainCategory->find('list', array('fields'=>'name'));
	$this->set(compact('cats'));	
		if((isset($this->data['ProductSubCategory']['image']))&&($this->data['ProductSubCategory']['image']['size']!=0) &&($this->data['ProductSubCategory']['image'				        ]['size']>2103361)){
		 		$this->data['ProductSubCategory']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addSubCategory'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['ProductSubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['ProductSubCategory']['image'] = $image;
			if ($this->ProductSubCategory->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/Products/viewSubCategories');
		  }
		else if($this->data['ProductSubCategory']['image']['size']>2103361)		
				{
				 $this->data['ProductSubCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addSubCategory'));
				}		
		      }
	    }
 ///////////
///*****************************///
function editSubCategories($id=null){
		$cats=$this->ProductMainCategory->find('list', array('fields'=>'name'));
			$this->set(compact('cats'));
		
	if(empty($this->data)){
		$current_image=$this->ProductSubCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['ProductSubCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->ProductSubCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->ProductSubCategory->find('all',array('conditions'=>array('id'=>$data['ProductSubCategory']['id'])));		
		$current_image=$current_image[0]['ProductSubCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['ProductSubCategory'])){
		
				if($data['ProductSubCategory']['remove']==0){
				$data['ProductSubCategory']['image']=$current_image;}
				
				elseif($data['ProductSubCategory']['remove']!=0){
				$data['ProductSubCategory']['image']='';}
		}
			if(($this->data['ProductSubCategory']['image']['size']!=0) &&($this->data['ProductSubCategory']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['ProductSubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['ProductSubCategory']['image'] = $image;
			}
		   else if($this->data['ProductSubCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/Products/editSubCategories/'.$this->data['ProductSubCategory']['id']);
		}
		
			
										
			if($this->ProductSubCategory->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/Products/viewSubCategoryDetails/'.$this->data['ProductSubCategory']['id']);
			}
	}
		}
		
///*****************************///
	
		


function viewEnabledSubCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('sub_categories', $this->paginate('ProductSubCategory'));
		$this->render('viewSubCategories');
		
	}
	
function viewDisabledSubCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('name','date','status','id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('sub_categories', $this->paginate('ProductSubCategory'));
		$this->render('viewSubCategories');
		
	}
	
	
function enableSubCategories($id=null){
	$this->ProductSubCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('sub_categories', $this->paginate('ProductSubCategory'));
	$this->redirect('/Products/viewDisabledSubCategories');
}

function disableSubCategories($id=null){
	$this->ProductSubCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('sub_categories', $this->paginate('ProductSubCategory'));
	$this->redirect('/Products/viewEnabledSubCategories');
}

function disableSubCategory($id=null){
	$this->ProductSubCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('ProductSubCategory'));
	$this->redirect('/Products/viewSubCategories');	
	}
	
function enableSubCategory($id=null){
	$this->ProductSubCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('ProductSubCategory'));
	$this->redirect('/Products/viewSubCategories');
	}

}
?>