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


class Product_admin extends CI_Controller {
	
	
	private $img			= 	'' 		;
	private $tmb_img		= 	'' 		;
	private $upload_arr		=	array() ; 
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('loginfunctions');
		
		$this->load->helper(array('form', 'url'));
		
		//logged out
		if($this->loginfunctions->checkloggedin()===FALSE)
		{
			redirect('admin/') ;
		}
		else
		{
			$this->load->model('Db_model') ;
			
			$this->load->library(array('form_validation'));
		}
		

	}
	
	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()
	{
		
		
	$category_result= array();
	$type_result= array();

	
		
		
		
		if($this->Db_model->get_all_by_order('shop_category','title','asc') !== FALSE )
		$category_result = $this->Db_model->get_all_by_order('shop_category','title','asc') ;
		
		if($this->Db_model->get_all_by_order('shop_type','title','asc') !== FALSE )
		$type_result = $this->Db_model->get_all_by_order('shop_type','title','asc') ;
		
		/*$category_result= array();
		$subcategory_result = array() ;
		$brand_result= array();	*/
		
		
		/*if($this->Db_model->get_all_by_order('shop_category','title','asc') !== FALSE )
		$category_result = $this->Db_model->get_all_by_order('shop_category','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_where_by_order('shop_pages',array('type'=>'brand'),'title','asc') !== FALSE )
		$brand_result = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'brand'),'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('entry','entry','trim|required|numeric');
			//$this->form_validation->set_rules('category_id', 'Category', 'trim|required|numeric');
			$this->form_validation->set_rules('type_id', 'Type', 'trim|required|numeric');
			$this->form_validation->set_rules('title', 'Product Name', 'trim|required|is_unique[shop_product.title]');
			$this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');
			$this->form_validation->set_rules('img1', 'Image1', 'callback_image_upload');
			$this->form_validation->set_rules('img2', 'Image2', 'callback_image_upload');
			
			
			
			//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' =>  'Product' ,
							'heading' => 'Product' ,
						    'top_link_name' => 'View' ,
							'top_link' => site_url('Product_admin/view') ,
							'category_result' => $category_result,
							'type_result' => $type_result,
							/*'brand_result' => $brand_result,*/
							'aid'=>'aid',
							'featured'=>$this->input->post('featured'),
							//'offer'=>$this->input->post('offer'),
							//'offer_det'=>$this->input->post('offer_det'),
							'order'=>$this->input->post('order'),
							/*'entry'=>$this->input->post('entry'),*/
							'type_id'=>$this->input->post('type_id'),
							'category_id'=>$this->input->post('category_id'),
							/*'subcategory_id'=>$this->input->post('subcategory_id'),
							'brand_id'=>$this->input->post('brand_id'),*/
							'title' => $this->input->post('title'),
							'text'=>$this->input->post('text'),
							'specification'=>$this->input->post('specification'),
							//'activity_control'=>$this->input->post('activity_control'),
							'price'=>$this->input->post('price'),
							'uprice'=>$this->input->post('uprice'),
							//'image_result'=>array(),
							'img' => $this->img ,
							'img1' => $this->img1,
							'img2' => $this->img2,
							'tmb_img' => $this->tmb_img ,
							'str' => ''
							);
				
				$this->_page($data);
				
			}

			else 
			{
								
				$data = array(
				            
				            //'category_id' => $cars,
							'aid'=>$this->input->post('aid',TRUE),
             			    'featured'=>$this->input->post('featured',TRUE),
							//'offer'=>$this->input->post('offer',TRUE),
							//'offer_det'=>$this->input->post('offer_det',TRUE),
							'order'=>$this->input->post('order',TRUE),
							/*'entry'=>$this->input->post('entry',TRUE),
							/*'unit_id'=>$this->input->post('unit_id',TRUE),*/
							'type_id'=>$this->input->post('type_id',TRUE),
							/*'subcategory_id'=>$this->input->post('subcategory_id',TRUE),
							'brand_id'=>$this->input->post('brand_id',TRUE),*/
							'title' => $this->input->post('title',TRUE),
							'text'=>$this->input->post('text',TRUE),
							'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'),
							'img' => $this->img,
							'img1' => $this->img1,
							'img2' => $this->img2,
							'tmb_img' => $this->tmb_img,
							'active'=>'Y'
							 );
							 

	 
$insert_id = $this->Db_model->insert('shop_product',$data) ;
$product_id = $this->db->insert_id();

              
				
$temp =count($this->input->post('category_id'));
$category_id= $this->input->post('category_id');


  		 
for($i=0; $i<$temp;$i++){
$data2 = array(

'cat_id' =>$category_id[$i],
'product_id' =>$product_id,

      );
   $this->db->insert('shop_relproduct', $data2);
  }	

				
				
	
			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' =>  'Product' ,
							'heading' => 'Product' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Product_admin/view') ,
							'category_result' => $category_result,
							'type_result' => $type_result,
							'aid'=>'',
							'featured'=>0,
							//'offer'=>0,
							//'offer_det'=>'',
							'order'=>'',
							//'entry'=>'',
							//'unit_id'=>'',
							'type_id'=>'',
							//'subcategory_id'=>'',
							//'brand_id'=>'',
							'title' => '',
							'text'=>'',
							'specification'=>'',
							//'activity_control'=>'',
							'price'=>'',
							'uprice'=>'',
							//'image_result'=>array(),
							'img'=>'',
							'img1'=>'',
							'img2'=>'',
							'tmb_img'=>'',
							'str' => ''
							);
				
			$this->_page($data);
		}

	}
	
	/**
	*
	*   Product modification
	*
	*/
	
	public function edit($id='')
	{
		
		$featured = '' ;
		//$offer = 0 ;
		//$offer_det = '' ;
		$order = '' ;
		$category_id = array();
		$type_id = '';
		/*$entry = '';
		$brand_id = '' ;
		$unit_id = '';
		
		$subcategory_id = '' ;*/
		$title = '';
		$text = ''; 
		$specification = '';
		//$activity_control = '';
		$price = '';
		$category_result = array() ;
		$type_result = array() ;	
		/*$subcategory_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);
		
		$details2 = $this->Db_model->get_by_id('shop_relproduct','product_id',$id);
		
		//$details2 = $this->Db_model->get_where_by_order('shop_relproduct',array('product_id'=>$id),'id','asc') ;
		
		$temp =count($details2);
				
		
		if($details!==FALSE)
		{
			$featured = $details->featured ;
			//$offer = $details->offer ;
			//$offer_det = $details->offer_det ;
			$order = $details->order ;
			$category_id = $details2->cat_id ;
			$type_id = $details->type_id ;
			/*$entry = $details->entry ;
			$brand_id = $details->brand_id ;
			$unit_id = $details->unit_id ;
			$subcategory_id = $details->subcategory_id ;*/
			$title = $details->title ;
			$text = $details->text ;
			$specification = $details->specification ;
			//$activity_control = $details->activity_control ;
			$price = $details->price ;	
			$uprice = $details->uprice ;	
			$this->img = $details->img ;	
			$this->img1 = $details->img1 ;
			$this->img2 = $details->img2 ;
			$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_all_by_order('shop_category','title','asc') ;
			
			if($this->Db_model->get_all_by_order('shop_type','title','asc') !== FALSE )
			$type_result = $this->Db_model->get_all_by_order('shop_type','title','asc') ;
			
			
			if($this->Db_model->get_where_by_order('shop_relproduct',array('product_id'=>$id),'id','asc') !== FALSE )
			$subcategory_result = $this->Db_model->get_where_by_order('shop_relproduct',array('product_id'=>$id),'id','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_where_by_order('shop_pages',array('type'=>'brand'),'title','asc') !== FALSE )
			$brand_result = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'brand'),'title','asc') ;*/
			
			
			
		}
		
	
		$data = array(
					'page_name' => 'Product' ,
					'heading' => 'Product - Edit' ,
					'top_link_name' => 'View' ,
					'top_link' => site_url('Product_admin/view') ,
					'category_result' => $category_result,
					'subcategory_result' => $subcategory_result,
					'type_result' => $type_result,
					/*'brand_result' => $brand_result,
					'unit_result' => $unit_result,*/
					'aid'=>$id,
					'featured'=>$featured,
					//'offer'=>$offer,
					//'offer_det'=>$offer_det,
					'order'=>$order,
					'category_id'=>$category_id,
					/*'entry'=>$entry,
					'unit_id'=>$unit_id,
					'subcategory_id'=>$subcategory_id,
					'brand_id'=>$brand_id,*/
					'type_id'=>$type_id,
					'title' => $title,
					'text'=>$text,
					'specification'=>$specification,
					//'activity_control'=>$activity_control,
					'price'=>$price,
					'uprice'=>$uprice,
					//'image_result'=>$image_result,
					'img'=>$this->img,
					'img1'=>$this->img1,
					'img2'=>$this->img2,
					'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('type_id', 'Category', 'trim|required|numeric');
			$this->form_validation->set_rules('type_id', 'Type', 'trim|required|numeric');
			/*$this->form_validation->set_rules('entry', 'entry', 'trim|required|numeric');
			$this->form_validation->set_rules('unit_id', 'unit', '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('img', 'Image', 'callback_image_upload');
			$this->form_validation->set_rules('img1', 'Image1', 'callback_image_upload');
			$this->form_validation->set_rules('img2', 'Image2', 'callback_image_upload');

			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{
				
				
			/*if($this->Db_model->get_where_by_order('shop_subcategory',array('type_id'=>$this->input->post('type_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' => 'Product' ,
							'heading' => 'Product - Edit' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Product_admin/view') ,
							'category_result' => $category_result,
							'subcategory_result' => $subcategory_result,
							'type_result' => $type_result,
							/*'brand_result' => $brand_result,
							'unit_result' => $unit_result,*/
							'aid'=>$id,
							'featured'=>$this->input->post('featured'),
							//'offer'=>$this->input->post('offer'),
							//'offer_det'=>$this->input->post('offer_det'),
							'order'=>$this->input->post('order'),
							/*'entry'=>$this->input->post('entry'),
							'unit_id'=>$this->input->post('unit_id'),
							'category_id'=>$this->input->post('category_id'),
							'subcategory_id'=>$this->input->post('subcategory_id'),
							'brand_id'=>$this->input->post('brand_id'),*/
							'type_id'=>$this->input->post('type_id'),
							'category_id'=>$this->input->post('category_id'),
							'title' => $this->input->post('title'),
							'text'=>$this->input->post('text'),
							'specification'=>$this->input->post('specification'),
							//'activity_control'=>$this->input->post('activity_control'),
							'price'=>$this->input->post('price'),
							'uprice'=>$this->input->post('uprice'),
							//'image_result'=>$image_result,
							'img'=>$this->img,
							'img1'=>$this->img1,
							'img2'=>$this->img2,
							'tmb_img'=>$this->tmb_img,
							'str' => ''
							);

				
				$this->_page($data);
				
			}
			
			
			else 
			{
								
				$data = array(
							'featured'=>$this->input->post('featured',TRUE), 
							//'offer'=>$this->input->post('offer',TRUE), 
							//'offer_det'=>$this->input->post('offer_det',TRUE), 
							'order'=>$this->input->post('order',TRUE),
							'type_id'=>$this->input->post('type_id',TRUE),
							/*'entry'=>$this->input->post('entry',TRUE),
							'unit_id'=>$this->input->post('unit_id',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),*/
							'title' => $this->input->post('title',TRUE),
							'text'=>$this->input->post('text',TRUE),
							'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),
							'img'=>$this->img,
							'img1'=>$this->img1,
							'img2'=>$this->img2,
							'tmb_img'=>$this->tmb_img
							 );
				
				//update data
				$this->Db_model->update('shop_product',$data,array('aid'=>$id)) ;

              $this->Db_model->delete('shop_relproduct',array('product_id'=>$id));
				
$temp =count($this->input->post('category_id'));
$category_id= $this->input->post('category_id');


  		 
for($i=0; $i<$temp;$i++){
$data2 = array(

'cat_id' =>$category_id[$i],
'product_id' =>$id,

      );
   $this->db->insert('shop_relproduct', $data2);
  }	
				
				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');

		if( $this->Db_model->get_all_by_order('shop_product','aid','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_all_by_order('shop_product','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' => 'Product Management' ,
					  'heading' => 'Product 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]['img1'] != '' )
				{
					unlink('assets/images/'.$details[0]['img1']) ;
				}
				
				if(  $details[0]['img2'] != '' )
				{
					unlink('assets/images/'.$details[0]['img2']) ;
				}
			
			}
			
			$this->db->update('shop_product',array('img'=>'','img1'=>'','img2'=>'','tmb_img'=>''),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('img','512','1024','720','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;
			}
			
			}
			
			
		elseif( isset($_FILES['img1']['name']) && !empty($_FILES['img1']['name']) ) 
		{
		$this->load->library('single_file_upload') ;
			$this->upload_arr = $this->single_file_upload->do_upload('img1','512','1024','720','2') ;
			
				if($this->upload_arr['upload']==1)
			{ 
				$this->img1 = $this->upload_arr['filename'] ;
				return true;
			}
			else
			{
				$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
        		return false;
			}
			}
			
			
			
	elseif( isset($_FILES['img2']['name']) && !empty($_FILES['img2']['name']) ) 
		{
		$this->load->library('single_file_upload') ;
			
			$this->upload_arr = $this->single_file_upload->do_upload('img2','512','1024','720','3') ;
			
			if($this->upload_arr['upload']==1)
			{ 
				$this->img2 = $this->upload_arr['filename'];
				return true;
			}
			else
			{
				$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
        		return false;
			}
			}
		
		else
		{
			return true;
		}
				
	}
	
	
}//end class