<?php 
class MarketsController extends AppController{
	var $name="Markets";
	var $uses=array('MarketMainCategory','MarketCategory','MarketSubCategory');
	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('title','date','status','id');
		$this->set('main_categories', $this->paginate('MarketMainCategory'));
		$this->render('viewMainCategories');
		
	}
function viewMainCategoryDetails($id=null){
	$data=$this->MarketMainCategory->find('all',array('conditions'=>array('MarketMainCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	
}

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

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

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

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

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

}


function addMainCategoryResponse(){
	
	
	
}










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

function viewCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->set('categories', $this->paginate('MarketCategory'));
		$this->render('viewCategories');
		
	}
function viewCategoryDetails($id=null){
	$data=$this->MarketCategory->find('all',array('conditions'=>array('MarketCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$main_cats=$this->MarketMainCategory->find('list', array('fields'=>'title'));
	$this->set(compact('main_cats'));
}

///////////////////////////////////////////
	function addCategory(){	
	$main_cats=$this->MarketMainCategory->find('list', array('fields'=>'title'));
	$this->set(compact('main_cats'));	
		if((isset($this->data['MarketCategory']['image']))&&($this->data['MarketCategory']['image']['size']!=0) &&($this->data['MarketCategory']['image'				        ]['size']>2103361)){
		 		$this->data['MarketCategory']['image']='';
				$this->Session->setFlash('Maximum file size of 2MB exceeded.');
				$this->redirect(array('action' => 'addCategory'));
		}		
		if (!empty($this->data)) {				
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['MarketCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['MarketCategory']['image'] = $image;
			if ($this->MarketCategory->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/markets/viewDisabledCategories');
		  }
		else if($this->data['MarketCategory']['image']['size']>2103361)		
				{
				 $this->data['MarketCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addCategory'));
				}		
		      }
	    }
///////////////////////////////////////////

	function editCategories($id=null){
		$main_cats=$this->MarketMainCategory->find('list', array('fields'=>'title'));
			$this->set(compact('main_cats'));
		
	if(empty($this->data)){
		$current_image=$this->MarketCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['MarketCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->MarketCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->MarketCategory->find('all',array('conditions'=>array('id'=>$data['MarketCategory']['id'])));		
		$current_image=$current_image[0]['MarketCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['MarketCategory'])){
		
				if($data['MarketCategory']['remove']==0){
				$data['MarketCategory']['image']=$current_image;}
				
				elseif($data['MarketCategory']['remove']!=0){
				$data['MarketCategory']['image']='';}
		}
			if(($this->data['MarketCategory']['image']['size']!=0) &&($this->data['MarketCategory']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['MarketCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['MarketCategory']['image'] = $image;
			}
		   else if($this->data['MarketCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/markets/editCategories/'.$this->data['MarketCategory']['id']);
		}
		
			
										
			if($this->MarketCategory->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/markets/viewCategoryDetails/'.$this->data['MarketCategory']['id']);
			}
	}
		}
		
		
		
		
		
		
	
	
	
	function CategoryEditResponse(){
		
		
	}

function viewEnabledCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','status','date','id');
		$this->paginate['conditions']= array('status'=>'1');
		$this->set('status','1');
   		$this->set('categories', $this->paginate('MarketCategory'));
		$this->render('viewCategories');
		
	}
	
function viewDisabledCategories(){
		$this->paginate['limit'] = 50;
   		$this->paginate['order'] = array('date' => 'desc');
   		$this->paginate['fields'] = array('title','date','status','id');
		$this->paginate['conditions']= array('status'=>'0');
		$this->set('status','0');
   		$this->set('categories', $this->paginate('MarketCategory'));
		$this->render('viewCategories');
		
	}
	
	
function enableCategories($id=null){
	$this->MarketCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('MarketCategory'));
	$this->redirect('/markets/viewDisabledCategories');
}

function enableCategory($id=null){
	$this->MarketCategory->updateAll(array('status'=>'1'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('MarketCategory'));
   	//$this->render('viewCategories');
	$this->redirect('/markets/viewCategories');
}

function disableCategory($id=null){
	$this->MarketCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('MarketCategory'));
   	//$this->render('viewCategories');
	$this->redirect('/markets/viewCategories');
}
function disableCategories($id=null){
	$this->MarketCategory->updateAll(array('status'=>'0'),array('id'=>$id));
	$this->paginate['limit'] = 50;
   	$this->paginate['order'] = array('date' => 'desc');
	$this->set('categories', $this->paginate('MarketCategory'));
   	//$this->render('viewCategories');
	$this->redirect('/markets/viewEnabledCategories');
}


function addCategoryResponse(){
	
	
	
}
	
	
	
//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('title','date','status','id');
		$this->set('sub_categories', $this->paginate('MarketSubCategory'));
		$this->render('viewAllSubCategories');		
	}
	
function viewSubCategoryDetails($id=null){
	$data=$this->MarketSubCategory->find('all',array('conditions'=>array('MarketSubCategory.id'=>$id)));
	
	$this->set(compact('data'));	
	$cats=$this->MarketCategory->find('list', array('fields'=>'title'));
	$this->set(compact('cats'));
}
 ////////////
 function addSubCategory(){	
	$cats=$this->MarketCategory->find('list', array('fields'=>'title'));
	$main_cats=$this->MarketMainCategory->find('list', array('fields'=>'title'));
	$this->set(compact('main_cats','cats'));	
		if((isset($this->data['MarketSubCategory']['image']))&&($this->data['MarketSubCategory']['image']['size']!=0) &&($this->data['MarketSubCategory']['image'				        ]['size']>2103361)){
		 		$this->data['MarketSubCategory']['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['MarketSubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['MarketSubCategory']['image'] = $image;
			if ($this->MarketSubCategory->save($data)) {
			$this->Session->setFlash('Saved Successfully');
				$this->redirect('/markets/viewSubCategories');
		  }
		else if($this->data['MarketSubCategory']['image']['size']>2103361)		
				{
				 $this->data['MarketSubCategory']['image']='';
				 $this->Session->setFlash('Maximum file size exceeded.');
				$this->redirect(array('action' => 'addSubCategory'));
				}		
		      }
	    }
 ///////////
///*****************************///
function editSubCategories($id=null){
		$cats=$this->MarketCategory->find('list', array('fields'=>'title'));
		$main_cats=$this->MarketMainCategory->find('list', array('fields'=>'title'));
			$this->set(compact('cats','main_cats'));
		
	if(empty($this->data)){
		$current_image=$this->MarketSubCategory->find('all',array('conditions'=>array('id'=>$id)));		
		$current_image=$current_image[0]['MarketSubCategory']['image'];		
		$this->set(compact('current_image'));
		$data=$this->MarketSubCategory->find('all',array('conditions'=>array('id'=>$id)));
		$this->data=$data[0];
	}	
	elseif(!empty($this->data)){
		$data=$this->data;
		$current_image=$this->MarketSubCategory->find('all',array('conditions'=>array('id'=>$data['MarketSubCategory']['id'])));		
		$current_image=$current_image[0]['MarketSubCategory']['image'];
		$this->set(compact('current_image'));
			
		if(array_key_exists('remove',$data['MarketSubCategory'])){
		
				if($data['MarketSubCategory']['remove']==0){
				$data['MarketSubCategory']['image']=$current_image;}
				
				elseif($data['MarketSubCategory']['remove']!=0){
				$data['MarketSubCategory']['image']='';}
		}
			if(($this->data['MarketSubCategory']['image']['size']!=0) &&($this->data['MarketSubCategory']['image']['size']<=2103361)) 
		  {			 
		  	$data=$this->data;  
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($data['MarketSubCategory']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$data['MarketSubCategory']['image'] = $image;
			}
		   else if($this->data['MarketSubCategory']['image']['size']>2103361) {
		    $this->Session->setFlash('Maximum file size exceeded.');
			$this->redirect('/markets/editSubCategories/'.$this->data['MarketSubCategory']['id']);
		}
		
			
										
			if($this->MarketSubCategory->save($data)){				
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect('/markets/viewSubCategoryDetails/'.$this->data['MarketSubCategory']['id']);
			}
	}
		}
		
function updateCityList(){
		$maincatid=$this->data['MarketSubCategory']['main_cat_id'];
		$options = $this->MarketCategory->find('list',array('fields' => array('id','title'), 'order'=>'title', 'conditions'=>array('main_cat_id'=>$maincatid)));
		$this->set('citylist', $options);
		$this->render('update_city_list', 'ajax');		
	}
		
///*****************************///
	
		
	function subCategoryEditResponse(){
		
		
	}

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

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

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


function addSubCategoryResponse(){	
    }
	
function delete($id){
	$this->MarketMainCategory->delete($id);
	$this->redirect($this->referer());
}

function deleteCategoryDetails($id) {
	$this->MarketCategory->delete($id);
	$this->redirect($this->referer());
	}
}
?>