<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Product_admin extends CI_Controller {


	private $img			= 	'' 		;
	private $img2			= 	'' 		;
	private $img3			= 	'' 		;
	private $img4			= 	'' 		;
	private $img5			= 	'' 		;
	private $img6			= 	'' 		;
	private $img7			= 	'' 		;
	private $img8			= 	'' 		;
	private $tmb_img		= 	'' 		;
	private $upload_arr		=	array() ; 

	public function __construct()
	{
		parent::__construct();

		$this->load->library('loginfunctions');

		//logged out
		if($this->loginfunctions->checkloggedin()===FALSE)
		{
			redirect('admin/') ;
		}
		else
		{
			$this->load->model('Db_model') ;
	
			$this->load->library(array('form_validation'));
	     }
		 
		 
		 if($this->session->userdata('shop_user'))
		{
			$this->user = $this->encrypt->decode($this->session->userdata('shop_user')); 
			$this->logged = 1;
		}
		 


	}

	public function index()
	{
		$this->view();
	}


	private function _page( $data=array()  )
	{
		$this->load->view('admin/product_header',$data);
		$this->load->view('admin/menu');
		//$this->load->view('admin/top_links',$data);
		$this->load->view('admin/product_form',$data);
		$this->load->view('admin/footer');
	}


	public function create()
	{
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;

		$category_result= array();
		$subcategory_result = array() ;
		$unit_result= array();
		$brand_result= array();
		$dist_result= array();
		$post_result= array();
		
		



		if($this->Db_model->get_all_by_order('shop_category','title','asc') !== FALSE )
		$category_result = $this->Db_model->get_where_by_order('shop_category',array(),'title','asc') ;

		if($this->Db_model->get_all_by_order('shop_unit','order','asc') !== FALSE )
		$unit_result = $this->Db_model->get_all_by_order('shop_unit','order','asc') ;
		
		
		if($this->Db_model->get_all_by_order('shop_brand','title','asc') !== FALSE )
		$brand_result = $this->Db_model->get_all_by_order('shop_brand','title','asc') ;
		
		
		//if( $this->Db_model->get_where_by_order('delivery',array('type'=>'delivery'),'aid','desc') !== FALSE )
		
		
		if($this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') !== FALSE )
		$dist_result = $this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') ;
		
	    if($this->Db_model->get_where_by_order('pincodes',array('state_id'=>'18'),'id','asc') !== FALSE )
		$post_result = $this->Db_model->get_where_by_order('pincodes',array('state_id'=>'18'),'id','asc') ;
		
    	if($this->Db_model->get_all_by_order('city','title','asc') !== FALSE )
		$city_result = $this->Db_model->get_all_by_order('city','title','asc') ;
		


		//check if submit button clicked
		if($this->input->post('submit'))
		{
			$this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
			$this->form_validation->set_rules('category_id', 'Category', 'trim|required|numeric');
			$this->form_validation->set_rules('subcategory_id', 'subcategory', 'trim|required|numeric');
			
			//$this->form_validation->set_rules('dist_result', 'District', 'trim|required|numeric');
			$this->form_validation->set_rules('city_mainid', 'City', 'trim|required|numeric');
			//$this->form_validation->set_rules('post_id', 'Post Office', 'trim|required|numeric');
			
			//$this->form_validation->set_rules('unit_id', 'Unit', 'trim|required|numeric');
			$this->form_validation->set_rules('title', 'Property Name', 'trim|required');
			$this->form_validation->set_rules('price', 'Price', 'trim|required|required');
			//$this->form_validation->set_rules('uprice', 'uprice', 'trim|required|numeric');
			//$this->form_validation->set_rules('stock', 'stock', 'trim|required|numeric');
			//$this->form_validation->set_rules('stock2', 'Stock', 'trim|required|numeric');
			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');
  	        $this->form_validation->set_rules('img2', 'Image', 'callback_image_upload2');
            $this->form_validation->set_rules('img3', 'Image', 'callback_image_upload3');
			$this->form_validation->set_rules('img4', 'Image', 'callback_image_upload4');
			$this->form_validation->set_rules('img5', 'Image', 'callback_image_upload5');
			$this->form_validation->set_rules('img6', 'Image', 'callback_image_upload6');
			$this->form_validation->set_rules('img7', 'Image', 'callback_image_upload7');
			$this->form_validation->set_rules('img8', 'Image', 'callback_image_upload8');

			//if validation errors
			if($this->form_validation->run() === FALSE)
			{
	
	
			if($this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$this->input->post('category_id')),'title','asc') !== FALSE )
			{
			$subcategory_result = $this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$this->input->post('category_id')),'title','asc') ;
			}
			
			
				$data = array(
							'page_name' =>  'Property Details' ,
							'heading' => 'Property Details' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Product_admin/view') ,
							'category_result' => $category_result,
							'subcategory_result' => $subcategory_result,
							'brand_result' => $brand_result,
							'dist_result' => $dist_result,
							'post_result' => $post_result,
							'unit_result' => $unit_result,
							'city_result' => $city_result,
							'aid'=>'',
							'featured'=>$this->input->post('featured'),
							//'entrance'=>$this->input->post('entrance'),
							//'entrance_det'=>$this->input->post('entrance_det'),
							//'school'=>$this->input->post('school'),
							//'school_det'=>$this->input->post('school_det'),
							'order'=>$this->input->post('order'),
							'category_id'=>$this->input->post('category_id'),
							'subcategory_id'=>$this->input->post('subcategory_id'),
							'brand_id'=>$this->input->post('brand_id'),
							
							'dist_id'=>$this->input->post('dist_id'),
							'post_id'=>$this->input->post('post_id'),
							'city_mainid'=>$this->input->post('city_mainid'),
							'ready'=>$this->input->post('ready'),
							'c_year'=>$this->input->post('c_year'),
							
							'land_unit'=>$this->input->post('land_unit'),
							'land'=>$this->input->post('land'),
							
							
							
							'unit_id'=>$this->input->post('unit_id'),
							'title' => $this->input->post('title'),
							'text'=>$this->input->post('text'),
							//'earliest'=>$this->input->post('earliest'),
							
							
							
							
							'code'=>$this->input->post('code'),
							'entry'=>$this->input->post('entry'),
							'bed'=>$this->input->post('bed'),
							'bath'=>$this->input->post('bath'),
							'cperson'=>$this->input->post('cperson'),
							'cnumber'=>$this->input->post('cnumber'),
							'cemail'=>$this->input->post('cemail'),
							'pname'=>$this->input->post('pname'),
							'pstreet1'=>$this->input->post('pstreet1'),
							'pstreet2'=>$this->input->post('pstreet2'),
							'state_id'=>$this->input->post('state_id'),
							'dist_id'=>$this->input->post('dist_id'),
							//'city_id'=>$this->input->post('city_id'),
							'city_mainid'=>$this->input->post('city_mainid'),
							
							
							
							
							//'specification'=>$this->input->post('specification'),
							//'activity_control'=>$this->input->post('activity_control'),
							'price'=>$this->input->post('price'),
							'uprice'=>$this->input->post('uprice'),
							'stock2'=>$this->input->post('stock2'),
							//'stock'=>$this->input->post('stock'),
							//'offer' => $this->input->post('offer'),
							'offer_det' => $this->input->post('offer_det'),
							'titl'=>$this->input->post('titl'),
                            'keyword'=>$this->input->post('keyword'),
	                        'desc'=>$this->input->post('desc'),
							
							
							//'image_result'=>array(),
							'img' => $this->img,
							'img2' => $this->img2,
							'img3' => $this->img3,
							'img4' => $this->img4,
							'img5' => $this->img5,
							'img6' => $this->img6,
							'img7' => $this->img7,
							'img8' => $this->img8,
							
							
							
							'gift' => 1,
							'tmb_img' => $this->tmb_img,
							'str' => ''
							);

				$this->_page($data);

			}

			else
			{
				
				
$ip_address = $_SERVER['REMOTE_ADDR'];
$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone );
$date=$date->format( 'Y-m-d H:i:s' );
$post_id='';
$dpost='';
				
$subcategory_id = $this->input->post('subcategory_id'); 			
$post_id = $this->input->post('post_id'); 	 
$city_mainid = $this->input->post('city_mainid');
$dist_id = $this->input->post('dist_id');	

$sql2 ="SELECT * FROM districts where id=$dist_id ";
$query2 = $this->db->query($sql2);
if ($query2->num_rows() > 0) {
foreach($query2->result() as $row2) {
$dname=$row2->name;
}
}			

$sql3 ="SELECT * FROM city where id=$city_mainid ";
$query3 = $this->db->query($sql3);
if ($query3->num_rows() > 0) {
foreach($query3->result() as $row3) {
$dcity=$row3->title;
}
}


if($post_id!='') {
$sql4 ="SELECT * FROM pincodes where id=$post_id  ";
$query4 = $this->db->query($sql4);
if ($query4->num_rows() > 0) {
foreach($query4->result() as $row4) {
$dpost=$row4->officename;
}
}
}

$locations = $dname.",".$dcity.",".$dpost;
			
				

				$data = array(
             			    'featured'=>$this->input->post('featured',TRUE),
							//'entrance'=>$this->input->post('entrance',TRUE),
							//'entrance_det'=>$this->input->post('entrance_det',TRUE),
							//'school'=>$this->input->post('school',TRUE),
							//'school_det'=>$this->input->post('school_det',TRUE),
							'order'=>$this->input->post('order',TRUE),
							'category_id'=>$this->input->post('category_id',TRUE),
							'subcategory_id'=>$this->input->post('subcategory_id',TRUE),
							'brand_id'=>$this->input->post('brand_id',TRUE),
							'unit_id'=>$this->input->post('unit_id',TRUE),
							'title' => $this->input->post('title',TRUE),
							'text'=>$this->input->post('text',TRUE),
							//'earliest'=>$this->input->post('earliest',TRUE),
							
							
							
							'post_id'=>$this->input->post('post_id'),
							
							
							'code'=>$this->input->post('code',TRUE),
							'entry'=>$this->input->post('entry',TRUE),
							'bed'=>$this->input->post('bed',TRUE),
							'bath'=>$this->input->post('bath',TRUE),
							'cperson'=>$this->input->post('cperson',TRUE),
							'cnumber'=>$this->input->post('cnumber',TRUE),
							'cemail'=>$this->input->post('cemail',TRUE),
							'pname'=>$this->input->post('pname',TRUE),
							'pstreet1'=>$this->input->post('pstreet1',TRUE),
							'pstreet2'=>$this->input->post('pstreet2',TRUE),
							'state_id'=>$this->input->post('state_id',TRUE),
							'dist_id'=>$this->input->post('dist_id',TRUE),
							//'city_id'=>$this->input->post('city_id',TRUE),
							'city_mainid'=>$this->input->post('city_mainid',TRUE),
							'ready'=>$this->input->post('ready',TRUE),
							'c_year'=>$this->input->post('c_year',TRUE),
							
							'land'=>$this->input->post('land',TRUE),
							'land_unit'=>$this->input->post('land_unit',TRUE),
							
							
							'reg_date'=>$date,
							'reg_user'=>$this->_userid(), 
							'reg_user_type'=>'1',
							'locations'=>$locations,
							'La_modi_date'=>$date,
				  	
							
							
							//'specification'=>$this->input->post('specification',TRUE),
							//'activity_control'=>$this->input->post('activity_control',TRUE),
							'price'=>$this->input->post('price',TRUE),
							//'uprice'=>$this->input->post('uprice',TRUE),
							
							'stock2'=>$this->input->post('stock2',TRUE),
							//'stock'=>$this->input->post('stock',TRUE),
							//'offer' => $this->input->post('offer',TRUE),
							'offer_det' => $this->input->post('offer_det',TRUE),
							'titl'=>$this->input->post('titl',TRUE),
                            'keyword'=>$this->input->post('keyword',TRUE),
                            'desc'=>$this->input->post('desc',TRUE),
							
							
							
							'img' => $this->img,
							'img2' => $this->img2,
							'img3' => $this->img3,
							'img4' => $this->img4,
							'img5' => $this->img5,
							'img6' => $this->img6,
							'img7' => $this->img7,
							'img8' => $this->img8,
							'tmb_img' => $this->tmb_img,
							'active'=>'Y',
							'gift' => 1,
							 );


				//insert data
				$insert_id = $this->Db_model->insert('shop_product',$data) ;
				
			
					
				$reg_id= "KL".$city_mainid."P".$post_id."R".$insert_id;
				//$en_reg_id= md5($reg_id);
				
	// Unic Code
    $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';  
	$length_of_string=10;
    $en_reg_id=substr(str_shuffle($str_result),0, $length_of_string); 

				
				
				
			
				$this->Db_model->update('shop_product',array('reg_id'=>$reg_id,'en_reg_id'=>$en_reg_id),array('aid'=>$insert_id)) ;	
				
				

				redirect('Product_admin/view');

			}

		}//if($this->input->post('submit'))

		else
		{

			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/

			$data = array(
							'page_name' =>  'Property Details' ,
							'heading' => 'Property Details' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Product_admin/view') ,
							'category_result' => $category_result,
							'subcategory_result' => $subcategory_result,
							'unit_result' => $unit_result,
							'brand_result' => $brand_result,
							'dist_result' => $dist_result,
							'city_result' => $city_result,
							'post_result' => $post_result,
							'aid'=>'',
							'featured'=>0,
							//'entrance'=>0,
							//'entrance_det'=>'',
							//'school'=>0,
							//'school_det'=>'',
							'order'=>'',
							'category_id'=>'',
							'subcategory_id'=>'',
							'unit_id'=>'',
							'title' => '',
							'text'=>'',
							'earliest'=>'',
							
							
							'code' => '',
							'bed' => '',
							'bath' => '',
							'cperson'=>'',
							'cnumber'=>'',
							'cemail'=>'',
							'pname'=>'',
							'pstreet1'=>'',
							'pstreet2' => '',
							'state_id' => '',
							'dist_id'=>'',
							'city_id'=>'',
							'city_mainid'=>'',
							'c_year'=>'',
							'ready'=>'1',
							
							'land'=>'',
							'land_unit'=>'',

							'entry' => '',
							'offer' => '',
							'offer_det' => '',
							'titl'=>'',
							'keyword'=>'',
							'desc'=>'',
							'img2'=>'',
							'img3'=>'',
							'img4'=>'',
							'img5'=>'',
							'img6'=>'',
							'img7'=>'',
							'img8'=>'',
							
							//'specification'=>'',
							//'activity_control'=>'',
							'price'=>'',
							'uprice'=>'',
							//'image_result'=>array(),
							'img'=>'',
							'tmb_img'=>'',
							'gift' => 1,
							'str' => ''
							);

			$this->_page($data);
		}

	}

	/**
	*
	*   Product modification
	*
	*/
	
	public function edit($id='')
	{

		$featured = '' ;
		//$entrance = 0 ;
		//$entrance_det = '' ;
		//$school= 0 ;
		//$school_det= '' ;
		$order = '' ;
		$unit_id = '' ;
		$brand_id = '' ;

		$category_id = '' ;
		$subcategory_id = '' ;
		$title = '';
		$earliest = '';
		$code='';
		$text = '';
		//$specification = '';
		//$activity_control = '';
		$price = '';
		$uprice = '';
		
		$offer = '';
		$offer_det = '';
		$stock = '';
		$stock2 = '';
		$titl = '';
		$keyword = '';
		$desc = '';
		
		
		
		$bed = '';
		$entry = '';
		$bath = '';
		$cperson = '';
		$cnumber = '';
		$cemail = '';
		$pname = '';
		$pstreet1 = '';
		$pstreet2 = '';
		$dist_id = '';
		$city_mainid = '';
		$post_id = '';
		$ready = '';
		$c_year = '';
		
		$land = '';
		$land_unit = '';
		
		$land = '';
		$land_unit = '';
		
		
		$reg_id = '';
		$reg_date = '';
		
		$ready = '';
		$c_year = '';
		
		$La_modi_User_id = '';
		$La_modi_date = '';
		
		
		$dist_result = array() ;
		$city_result = array() ;
		$post_result = array() ;
		
		
		$category_result = array() ;
		$subcategory_result = array() ;
		$unit_result = array() ;
		$brand_result = array() ;
		$image_result = array();

		$id = $this->uri->segment(3,'');//select 3rd field(ie, id) from uri , if not set, default to ''


    	//retrieve details from database matching to the id
		$details = $this->Db_model->get_by_id('shop_product','aid',$id);


		if($details!==FALSE)
		{
			$featured = $details->featured ;
			//$entrance = $details->entrance ;
			//$entrance_det = $details->entrance_det ;
			//$school = $details->school ;
			//$school_det = $details->school_det ;
			$order = $details->order ;
			$unit_id = $details->unit_id ;
			$brand_id = $details->brand_id ;
			$category_id = $details->category_id ;
			$subcategory_id = $details->subcategory_id ;
			$earliest = $details->earliest ;
			$title = $details->title ;
			$text = $details->text ;
			//$specification = $details->specification ;
			//$activity_control = $details->activity_control ;
			$price = $details->price ;
			$uprice = $details->uprice ;
			$code=$details->code;
			
			$stock = $details->stock ;
			$stock2 = $details->stock2 ;
			$offer = $details->offer ;
			$offer_det = $details->offer_det;
			$titl = $details->titl ;
			$keyword = $details->keyword ;
			$desc = $details->desc ;
			$this->img2 = $details->img2 ;
			$this->img3 = $details->img3 ;
			$this->img4 = $details->img4 ;
			$this->img5 = $details->img5 ;
			$this->img6 = $details->img6 ;
			$this->img7 = $details->img7;
			$this->img8 = $details->img8;
			
			
			
			
			$bed = $details->bed;
			$entry = $details->entry;
			$bath = $details->bath;
			$cperson = $details->cperson;
			$cnumber = $details->cnumber;
			$cemail = $details->cemail;
			$pname = $details->pname;
			$pstreet1 = $details->pstreet1;
			$pstreet2 = $details->pstreet2;
			$dist_id = $details->dist_id;
			$city_mainid = $details->city_mainid;
			$post_id = $details->post_id;
			$reg_id = $details->reg_id;
			$reg_date = $details->reg_date;
			$ready = $details->ready;
			$c_year = $details->c_year;
			
			$land = $details->land;
			$land_unit = $details->land_unit;
			
			
			
			
			
			$this->img = $details->img ;
			$this->tmb_img = $details->tmb_img ;


			if($this->Db_model->get_all_by_order('shop_category','title','asc') !== FALSE )
			$category_result = $this->Db_model->get_where_by_order('shop_category',array(),'title','asc') ;

			if($this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$category_id),'title','asc') !== FALSE )
			$subcategory_result = $this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$category_id),'title','asc') ;

			if($this->Db_model->get_all_by_order('shop_unit','title','asc') !== FALSE )
			$unit_result = $this->Db_model->get_all_by_order('shop_unit','title','asc') ;
			
			
			if($this->Db_model->get_all_by_order('shop_brand','title','asc') !== FALSE )
			$brand_result = $this->Db_model->get_all_by_order('shop_brand','title','asc') ;
			
			
	    if($this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') !== FALSE )
		$dist_result = $this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') ;
		
	    if($this->Db_model->get_where_by_order('pincodes',array('dis_id'=>$dist_id),'id','asc') !== FALSE )
		$post_result = $this->Db_model->get_where_by_order('pincodes',array('dis_id'=>$dist_id),'id','asc') ;
		
 /*   	if($this->Db_model->get_all_by_order('city','title','asc') !== FALSE )
		$city_result = $this->Db_model->get_all_by_order('city','title','asc') ;*/
		
        if($this->Db_model->get_where_by_order('city',array('1'=>'1'),'id','asc') !== FALSE )
		$city_result = $this->Db_model->get_where_by_order('city',array('dist_id'=>$dist_id),'id','asc') ;

		}


		$data = array(
					'page_name' => 'Property' ,
					'heading' => 'Property - Edit' ,
					'top_link_name' => 'View' ,
					'top_link' => site_url('Product_admin/view') ,
					'category_result' => $category_result,
					'subcategory_result' => $subcategory_result,
					'unit_result' => $unit_result,
					'brand_result' => $brand_result,
					
					'dist_result' => $dist_result,
					'post_result' => $post_result,
					'city_result' => $city_result,
					
					'aid'=>$id,
					'featured'=>$featured,
					//'entrance'=>$entrance,
					//'entrance_det'=>$entrance_det,
					//'school'=>$school,
					//'school_det'=>$school_det,
					'order'=>$order,
					'category_id'=>$category_id,
					'subcategory_id'=>$subcategory_id,
					'unit_id'=>$unit_id,
					'brand_id'=>$brand_id,
					'title' => $title,
					'earliest' => $earliest,
					'text'=>$text,
					
					
					
					'code'=>$code,
					'entry'=>$entry,
					'bed'=>$bed,
					'bath'=>$bath,
					'cperson'=>$cperson,
					'cnumber'=>$cnumber,
					'cemail'=>$cemail,
					'pname'=>$pname,
					'pstreet1'=>$pstreet1,
					'pstreet2'=>$pstreet2,
					'dist_id'=>$dist_id,
					'city_mainid'=>$city_mainid,
					'post_id'=>$post_id,
					'reg_id'=>$reg_id,
					'reg_date'=>$reg_date,
					'ready'=>$ready,
					'c_year'=>$c_year,
					
					'land'=>$land,
					'land_unit'=>$land_unit,
				
					
					
					'stock'=>$stock,
					'stock2'=>$stock2,
					'offer' => $offer,
					'offer_det' => $offer_det,
					'titl'=>$titl,
					'keyword'=>$keyword,
					'desc'=>$desc,
					'img2'=>$this->img2,
					'img3'=>$this->img3,
					'img4'=>$this->img4,
					'img5'=>$this->img5,
					'img6'=>$this->img6,
					'img7'=>$this->img7,
					'img8'=>$this->img8,
			
					//'specification'=>$specification,
					//'activity_control'=>$activity_control,
					'price'=>$price,
					'uprice'=>$uprice,
					'code'=>$code,
					//'image_result'=>$image_result,
					'img'=>$this->img,
					'tmb_img'=>$this->tmb_img,
					'str' => ''
					);
		

		//check if submit button clicked
		if($this->input->post('submit'))
		{

		//	$this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
//			$this->form_validation->set_rules('unit_id', 'Unit', 'trim|required|numeric');
//			$this->form_validation->set_rules('category_id', 'Category', 'trim|required|numeric');
//			$this->form_validation->set_rules('subcategory_id', 'Subcategory', 'trim|required|numeric');
//			$this->form_validation->set_rules('title', 'Product Name', 'trim|required');
//			$this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
//			//$this->form_validation->set_rules('uprice', 'uprice', 'trim|required|numeric');
//			$this->form_validation->set_rules('stock', 'Stock', 'trim|required|numeric');
//			$this->form_validation->set_rules('stock2', 'Stock', 'trim|required|numeric');


	       $this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
			$this->form_validation->set_rules('category_id', 'Category', 'trim|required|numeric');
			$this->form_validation->set_rules('subcategory_id', 'subcategory', 'trim|required|numeric');
			$this->form_validation->set_rules('city_mainid', 'City', 'trim|required|numeric');
			//$this->form_validation->set_rules('post_id', 'Post Office', 'trim|required|numeric');
			//$this->form_validation->set_rules('unit_id', 'Unit', 'trim|required|numeric');
		    $this->form_validation->set_rules('title', 'Property Name', 'trim|required');
			$this->form_validation->set_rules('price', 'Price', 'trim|required|required');
			
			
			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');
			$this->form_validation->set_rules('img2', 'Image', 'callback_image_upload2');
			$this->form_validation->set_rules('img3', 'Image', 'callback_image_upload3');
			$this->form_validation->set_rules('img4', 'Image', 'callback_image_upload4');
			$this->form_validation->set_rules('img5', 'Image', 'callback_image_upload5');
			$this->form_validation->set_rules('img6', 'Image', 'callback_image_upload6');
			$this->form_validation->set_rules('img7', 'Image', 'callback_image_upload7');
			$this->form_validation->set_rules('img8', 'Image', 'callback_image_upload8');

			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{


			if($this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$this->input->post('category_id')),'title','asc') !== FALSE )
			{
			$subcategory_result = $this->Db_model->get_where_by_order('shop_subcategory',array('category_id'=>$this->input->post('category_id')),'title','asc') ;
			}
			else
			{
				$subcategory_result = array() ;
			}

				$data = array(
				
							'page_name' =>  'Property Details' ,
							'heading' => 'Property Details' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Product_admin/view') ,
							'category_result' => $category_result,
							'subcategory_result' => $subcategory_result,
							'brand_result' => $brand_result,
							'dist_result' => $dist_result,
							'post_result' => $post_result,
							'unit_result' => $unit_result,
							'city_result' => $city_result,
							'aid'=>'',
							'featured'=>$this->input->post('featured'),
	
							'order'=>$this->input->post('order'),
							'category_id'=>$this->input->post('category_id'),
							'subcategory_id'=>$this->input->post('subcategory_id'),
							'brand_id'=>$this->input->post('brand_id'),
							
							'dist_id'=>$this->input->post('dist_id'),
							'post_id'=>$this->input->post('post_id'),
							'city_mainid'=>$this->input->post('city_mainid'),
                             'unit_id'=>$this->input->post('unit_id'),
							'title' => $this->input->post('title'),
							'text'=>$this->input->post('text'),
							//'earliest'=>$this->input->post('earliest'),
							
							
							
							
							'code'=>$this->input->post('code'),
							'entry'=>$this->input->post('entry'),
							'bed'=>$this->input->post('bed'),
							'bath'=>$this->input->post('bath'),
							'cperson'=>$this->input->post('cperson'),
							'cnumber'=>$this->input->post('cnumber'),
							'cemail'=>$this->input->post('cemail'),
							'pname'=>$this->input->post('pname'),
							'pstreet1'=>$this->input->post('pstreet1'),
							'pstreet2'=>$this->input->post('pstreet2'),
							'state_id'=>$this->input->post('state_id'),
							'dist_id'=>$this->input->post('dist_id'),
							//'city_id'=>$this->input->post('city_id'),
							'city_mainid'=>$this->input->post('city_mainid'),
							'ready'=>$this->input->post('ready'),
							
							'ready'=>$this->input->post('ready'),
							
							'land'=>$this->input->post('land'),
							'land_unit'=>$this->input->post('land_unit'),
							
							
							
							//'specification'=>$this->input->post('specification'),
							//'activity_control'=>$this->input->post('activity_control'),
							'price'=>$this->input->post('price'),
							'uprice'=>$this->input->post('uprice'),
							'stock2'=>$this->input->post('stock2'),
							//'stock'=>$this->input->post('stock'),
							//'offer' => $this->input->post('offer'),
							'offer_det' => $this->input->post('offer_det'),
							'titl'=>$this->input->post('titl'),
                            'keyword'=>$this->input->post('keyword'),
	                        'desc'=>$this->input->post('desc'),
							
							
							//'image_result'=>array(),
							'img' => $this->img,
							'img2' => $this->img2,
							'img3' => $this->img3,
							'img4' => $this->img4,
							'img5' => $this->img5,
							'img6' => $this->img6,
							'img7' => $this->img7,
							'img8' => $this->img8,
							
							'gift' => 1,
							'tmb_img' => $this->tmb_img,
							'str' => ''
							);


				$this->_page($data);

			}


			else
			{
	


$post_id='';
$dpost='';

$subcategory_id = $this->input->post('subcategory_id'); 			
$post_id = $this->input->post('post_id'); 	 
$city_mainid = $this->input->post('city_mainid');
$dist_id = $this->input->post('dist_id');	

$sql2 ="SELECT * FROM districts where id=$dist_id ";
$query2 = $this->db->query($sql2);
if ($query2->num_rows() > 0) {
foreach($query2->result() as $row2) {
$dname=$row2->name;
}
}			

$sql3 ="SELECT * FROM city where id=$city_mainid ";
$query3 = $this->db->query($sql3);
if ($query3->num_rows() > 0) {
foreach($query3->result() as $row3) {
$dcity=$row3->title;
}
}


if($post_id!='') {
$sql4 ="SELECT * FROM pincodes where id=$post_id  ";
$query4 = $this->db->query($sql4);
if ($query4->num_rows() > 0) {
foreach($query4->result() as $row4) {
$dpost=$row4->officename;
}
}
}

$locations = $dname.",".$dcity.",".$dpost;


$ip_address = $_SERVER['REMOTE_ADDR'];
$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone );
$date=$date->format( 'Y-m-d H:i:s' );

				$data = array(
             			    'featured'=>$this->input->post('featured',TRUE),
							//'entrance'=>$this->input->post('entrance',TRUE),
							//'entrance_det'=>$this->input->post('entrance_det',TRUE),
							//'school'=>$this->input->post('school',TRUE),
							//'school_det'=>$this->input->post('school_det',TRUE),
							'order'=>$this->input->post('order',TRUE),
							'category_id'=>$this->input->post('category_id',TRUE),
							'subcategory_id'=>$this->input->post('subcategory_id',TRUE),
							'brand_id'=>$this->input->post('brand_id',TRUE),
							'unit_id'=>$this->input->post('unit_id',TRUE),
							'title' => $this->input->post('title',TRUE),
							'text'=>$this->input->post('text',TRUE),
							//'earliest'=>$this->input->post('earliest',TRUE),
							
							
							
							'post_id'=>$this->input->post('post_id'),
							
							
							'code'=>$this->input->post('code',TRUE),
							'entry'=>$this->input->post('entry',TRUE),
							'bed'=>$this->input->post('bed',TRUE),
							'bath'=>$this->input->post('bath',TRUE),
							'cperson'=>$this->input->post('cperson',TRUE),
							'cnumber'=>$this->input->post('cnumber',TRUE),
							'cemail'=>$this->input->post('cemail',TRUE),
							'pname'=>$this->input->post('pname',TRUE),
							'pstreet1'=>$this->input->post('pstreet1',TRUE),
							'pstreet2'=>$this->input->post('pstreet2',TRUE),
							'state_id'=>$this->input->post('state_id',TRUE),
							'dist_id'=>$this->input->post('dist_id',TRUE),
							//'city_id'=>$this->input->post('city_id',TRUE),
							'city_mainid'=>$this->input->post('city_mainid',TRUE),
							'ready'=>$this->input->post('ready',TRUE),
							'c_year'=>$this->input->post('c_year',TRUE),
							
							
							'land'=>$this->input->post('land',TRUE),
							'land_unit'=>$this->input->post('land_unit',TRUE),
							
				 
							'locations'=>$locations,
							'La_modi_date'=>$date,
							
							//'specification'=>$this->input->post('specification',TRUE),
							//'activity_control'=>$this->input->post('activity_control',TRUE),
							'price'=>$this->input->post('price',TRUE),
							//'uprice'=>$this->input->post('uprice',TRUE),
							
							'stock2'=>$this->input->post('stock2',TRUE),
							//'stock'=>$this->input->post('stock',TRUE),
							//'offer' => $this->input->post('offer',TRUE),
							'offer_det' => $this->input->post('offer_det',TRUE),
							'titl'=>$this->input->post('titl',TRUE),
                            'keyword'=>$this->input->post('keyword',TRUE),
                            'desc'=>$this->input->post('desc',TRUE),
							
							
							
							'img' => $this->img,
							'img2' => $this->img2,
							'img3' => $this->img3,
							'img4' => $this->img4,
							'img5' => $this->img5,
							'img6' => $this->img6,
							'img7' => $this->img7,
							'img8' => $this->img8,
						
							'tmb_img' => $this->tmb_img,
							'active'=>'Y',
							'gift' => 1,
							 );

				//update data
				$this->Db_model->update('shop_product',$data,array('aid'=>$id)) ;

				redirect('Product_admin/view');

			}

		}//if($this->input->post('submit'))

		else
		{
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/

			$this->_page($data);
		}



	}


	public function view()
	{

		$result_array = array() ;
		$image_result = array() ;
		$this->load->helpers('basic_helper');
		
		
	
		
		//$data['category'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		

/*		if( $this->Db_model->get_all_by_order('shop_product','aid','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_where_by_order('shop_product',array(),'aid','desc') ;
		}
		*/
		
		
		
		if($this->input->post('catsubmit'))
		{
		$data = array(
		$statuso=$this->input->post('category_id'));
		//$result_array = $this->Db_model->get_all_by_order('shop_shipdetails','order_date','desc') ;
		$result_array = $this->Db_model->get_where_by_order('shop_product',array('category_id'=>$statuso),'aid','desc') ;
		}
		
		elseif($this->input->post('subcatsubmit'))
		{
		$data = array(
		$statuso=$this->input->post('category_id'));
		//$result_array = $this->Db_model->get_all_by_order('shop_shipdetails','order_date','desc') ;
		$result_array = $this->Db_model->get_where_by_order('shop_product',array('subcategory_id'=>$statuso),'aid','desc') ;
		}
		
		
		else
		{
		$result_array = $this->Db_model->get_where_by_order('shop_product',array(),'aid','desc') ;
		}
		
	
		


		/***
		*
		*	set uniq_id field & store it in session to make security while deletion
		*
		*/
		
		
		
		

		$uniq_id = uniqid();

		$this->load->library('session');

		$this->session->set_userdata('uniq_id',$uniq_id);

		$data=  array(
					  'page_name' => 'Property Management' ,
					  'heading' => 'Property Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('Product_admin/create') ,
					  'result_array' => $result_array,
					  'uniq_id' => $uniq_id
					 );


		$this->load->view('admin/home_header',$data);
		$this->load->view('admin/menu');
		//$this->load->view('admin/top_links',$data);
		$this->load->view('admin/product_view',$data);
		$this->load->view('admin/footer');
		
		
		
		

	}


	public function delete($aid='',$del=0,$uniq_id='')
	{

		$this->load->library('session');

		$uniq_id_uri = $this->uri->segment(5,'') ;

		//delete iff session matches with uniq_id in uri
		if($this->session->userdata('uniq_id') == $uniq_id_uri)
		{

			if ($this->uri->segment(4,'')==1)
			{
				//delete record on setting 3rd segment, ie: id of row
				$id = $this->uri->segment(3,'');

				$details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;

				if($details !== FALSE )
				{

					if(  $details[0]['img'] != '' )
					{
						unlink('assets/images/'.$details[0]['img']) ;
					}
					
					if(  $details[0]['tmb_img'] != '' )
					{
						unlink('assets/images/'.$details[0]['tmb_img']) ;
					}

				}

				$this->Db_model->delete('shop_product',array('aid'=>$aid));

				//redirect page to hide the id
				redirect('Product_admin/view') ;

			}

		}
		else
		{
			redirect('Product_admin/view') ;
		}

	}


	public function image_delete($id='',$del=0)
	{



		if ($this->uri->segment(4,'')==1)

		{
          $this->id = $this->uri->segment(3,'');
          $details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;

    if($details !== FALSE )

			{if(  $details[0]['img'] != '' )
               {
           unlink('assets/images/'.$details[0]['img']) ;
              }

             if(  $details[0]['tmb_img'] != '' )

				{

					unlink('assets/images/'.$details[0]['tmb_img']) ;

				}



			}



			$this->db->update('shop_product',array('img'=>'','tmb_img'=>''),array('aid'=>$id)) ;



			//redirect page to hide the id

			redirect('Product_admin/edit/'.$id);





		}


if ($this->uri->segment(4,'')==2)

		{
          $this->id = $this->uri->segment(3,'');
          $details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;

    if($details !== FALSE )

			{if(  $details[0]['img2'] != '' )
               {
           unlink('assets/images/'.$details[0]['img2']) ;
              }

             if(  $details[0]['tmb_img2'] != '' )

				{

					unlink('assets/images/'.$details[0]['tmb_img2']) ;

				}



			}



			$this->db->update('shop_product',array('img2'=>''),array('aid'=>$id)) ;



			//redirect page to hide the id

			redirect('Product_admin/edit/'.$id);





		}




if ($this->uri->segment(4,'')==3)

		{
          $this->id = $this->uri->segment(3,'');
          $details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;

    if($details !== FALSE )

			{if(  $details[0]['img3'] != '' )
               {
           unlink('assets/images/'.$details[0]['img3']) ;
              }

             if(  $details[0]['tmb_img3'] != '' )

				{

					unlink('assets/images/'.$details[0]['tmb_img3']) ;

				}



			}



			$this->db->update('shop_product',array('img3'=>''),array('aid'=>$id)) ;



			//redirect page to hide the id

			redirect('Product_admin/edit/'.$id);





		}
		
		
		
		
		
	
if ($this->uri->segment(4,'')==4)
{
$this->id = $this->uri->segment(4,'');
$details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;
if($details !== FALSE )
{if(  $details[0]['img4'] != '' )
{
unlink('assets/images/'.$details[0]['img4']) ;
}
if(  $details[0]['tmb_img4'] != '' )
{unlink('assets/images/'.$details[0]['tmb_img4']) ;}
}
$this->db->update('shop_product',array('img4'=>''),array('aid'=>$id)) ;
//redirect page to hide the id
redirect('Product_admin/edit/'.$id);
}




if ($this->uri->segment(4,'')==5)
{
$this->id = $this->uri->segment(5,'');
$details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;
if($details !== FALSE )
{if(  $details[0]['img5'] != '' )
{
unlink('assets/images/'.$details[0]['img5']) ;
}
if(  $details[0]['tmb_img5'] != '' )
{unlink('assets/images/'.$details[0]['tmb_img5']) ;}
}
$this->db->update('shop_product',array('img5'=>''),array('aid'=>$id)) ;
//redirect page to hide the id
redirect('Product_admin/edit/'.$id);
}




if ($this->uri->segment(4,'')==6)
{
$this->id = $this->uri->segment(6,'');
$details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;
if($details !== FALSE )
{if(  $details[0]['img6'] != '' )
{
unlink('assets/images/'.$details[0]['img6']) ;
}
if(  $details[0]['tmb_img6'] != '' )
{unlink('assets/images/'.$details[0]['tmb_img6']) ;}
}
$this->db->update('shop_product',array('img6'=>''),array('aid'=>$id)) ;
//redirect page to hide the id
redirect('Product_admin/edit/'.$id);
}





if ($this->uri->segment(4,'')==7)
{
$this->id = $this->uri->segment(7,'');
$details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;
if($details !== FALSE )
{if(  $details[0]['img7'] != '' )
{
unlink('assets/images/'.$details[0]['img7']) ;
}
if(  $details[0]['tmb_img7'] != '' )
{unlink('assets/images/'.$details[0]['tmb_img7']) ;}
}
$this->db->update('shop_product',array('img7'=>''),array('aid'=>$id)) ;
//redirect page to hide the id
redirect('Product_admin/edit/'.$id);
}




if ($this->uri->segment(4,'')==8)
{
$this->id = $this->uri->segment(8,'');
$details = $this->Db_model->get_where('shop_product',array('aid'=>$id)) ;
if($details !== FALSE )
{if(  $details[0]['img8'] != '' )
{
unlink('assets/images/'.$details[0]['img8']) ;
}
if(  $details[0]['tmb_img8'] != '' )
{unlink('assets/images/'.$details[0]['tmb_img8']) ;}
}
$this->db->update('shop_product',array('img8'=>''),array('aid'=>$id)) ;
//redirect page to hide the id
redirect('Product_admin/edit/'.$id);
}
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		




	}

	public function image_upload()
	{

		if( isset($_FILES['img']['name']) && !empty($_FILES['img']['name']) )
		{

			$this->load->library('single_file_upload') ;

			/**
			*
			*	my do_upload method : do_upload(field_name,max_size,max_width,max_height,thumb_create)
			*	return value is an array
			*	if $arr['upload']==1 uploaded , $arr['filename'] = name of file
			*	if $arr['upload']==0 not uploaded , $arr['errors'] = upload errors
			*
			*/

			$this->upload_arr = $this->single_file_upload->do_upload_main('img','5000','5000','5000','1') ;

			if($this->upload_arr['upload']==1)
			{ 
			
	
				$this->img = $this->upload_arr['filename'] ;
				$this->tmb_img = $this->upload_arr['thumb'] ;
				return true;
			}

			else
			{
				$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
        		return false;
			}

		}
		else
		{
			return true;
		}
		
	}
	
	
	
	
	
	public function change_del_status()
	{ 
		
		if(isset($_POST['status']) && $_POST['status']!='' && isset($_POST['oid']) && $_POST['oid']!='')
		{
		echo	$status = $_POST['status'];
			
		echo	$order_id = $_POST['oid'];
			
			//change status			
			$this->Db_model->update('shop_product',array('stock'=>$status),array('aid'=>$order_id));
			
			
			

			
			
			//redirect to order_admin page
			redirect('Product_admin');
		}
		
	}
	
	
	
	
	public function image_upload2()

	{

       if( isset($_FILES['img2']['name']) && !empty($_FILES['img2']['name']) )

		{
            $this->load->library('single_file_upload') ;
            $this->upload_arr = $this->single_file_upload->do_upload('img2','5000','5000','5000','1') ;

            if($this->upload_arr['upload']==1)
            {
                 
				 $this->img2 = $this->upload_arr['filename'] ;
                 //$this->tmb_img2 = $this->upload_arr['thumb'] ;
                 return true;
             }
           
		   
		   else
             {
             $this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
             return false;
             }
             }

		else
            {return true;}

		

	}
	
	
public function image_upload3()
{
 if( isset($_FILES['img3']['name']) && !empty($_FILES['img3']['name']) )
{
$this->load->library('single_file_upload') ;
$this->upload_arr = $this->single_file_upload->do_upload('img3','5000','5000','5000','1') ;
if($this->upload_arr['upload']==1)
{
$this->img3 = $this->upload_arr['filename'] ;
//$this->tmb_img2 = $this->upload_arr['thumb'] ;
return true;
}
else
{
$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
return false;
}
}
else
{return true;}
}










public function image_upload4()
{
 if( isset($_FILES['img4']['name']) && !empty($_FILES['img4']['name']) )
{
$this->load->library('single_file_upload') ;
$this->upload_arr = $this->single_file_upload->do_upload('img4','5000','5000','5000','1') ;
if($this->upload_arr['upload']==1)
{
$this->img4 = $this->upload_arr['filename'] ;
//$this->tmb_img2 = $this->upload_arr['thumb'] ;
return true;
}
else
{
$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
return false;
}
}
else
{return true;}
}


public function image_upload5()
{
 if( isset($_FILES['img5']['name']) && !empty($_FILES['img5']['name']) )
{
$this->load->library('single_file_upload') ;
$this->upload_arr = $this->single_file_upload->do_upload('img5','5000','5000','5000','1') ;
if($this->upload_arr['upload']==1)
{
$this->img5 = $this->upload_arr['filename'] ;
//$this->tmb_img2 = $this->upload_arr['thumb'] ;
return true;
}
else
{
$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
return false;
}
}
else
{return true;}
}



public function image_upload6()
{
 if( isset($_FILES['img6']['name']) && !empty($_FILES['img6']['name']) )
{
$this->load->library('single_file_upload') ;
$this->upload_arr = $this->single_file_upload->do_upload('img6','5000','5000','5000','1') ;
if($this->upload_arr['upload']==1)
{
$this->img6 = $this->upload_arr['filename'] ;
//$this->tmb_img2 = $this->upload_arr['thumb'] ;
return true;
}
else
{
$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
return false;
}
}
else
{return true;}
}





public function image_upload7()
{
 if( isset($_FILES['img7']['name']) && !empty($_FILES['img7']['name']) )
{
$this->load->library('single_file_upload') ;
$this->upload_arr = $this->single_file_upload->do_upload('img7','5000','5000','5000','1') ;
if($this->upload_arr['upload']==1)
{
$this->img7 = $this->upload_arr['filename'] ;
//$this->tmb_img2 = $this->upload_arr['thumb'] ;
return true;
}
else
{
$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
return false;
}
}
else
{return true;}
}



public function image_upload8()
{
 if( isset($_FILES['img8']['name']) && !empty($_FILES['img8']['name']) )
{
$this->load->library('single_file_upload') ;
$this->upload_arr = $this->single_file_upload->do_upload('img8','5000','5000','5000','1') ;
if($this->upload_arr['upload']==1)
{
$this->img8 = $this->upload_arr['filename'] ;
//$this->tmb_img2 = $this->upload_arr['thumb'] ;
return true;
}
else
{
$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
return false;
}
}
else
{return true;}
}




	
	
	
	
		
	public function data_excel()
	{  echo "gewgre";
		
	$this->load->view('admin/data_in_exel');
		
	}
	
	
		private function _userid()
	{
		$user_det = $this->Db_model->get_one('shop_user',array('uname'=>$this->user));//print_r($user_det);
		if($user_det!==FALSE)
			return $user_det->uid ;
		else
			return '';
	}
	
	
	
	
	
}//end class