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


class Product_related extends CI_Controller {


	private $img			= 	'' 		;
	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'));
		}


	}

	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/relatedproduct',$data);
		$this->load->view('admin/footer');
	}

	public function onchange($onchange='')
	{
	echo $onchange;
	}
	
	
	public function create()
	{		
		$product_result= array();
		$related_result = array() ;
		$result_array = array();
		
		if( $this->Db_model->get_all_by_order('shop_relproduct','id','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_all_by_order('shop_relproduct','id','desc') ;
		}

		$uniq_id = uniqid();

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

		$this->session->set_userdata('uniq_id',$uniq_id);
		
		if($this->Db_model->get_all_by_order('shop_product','title','asc') !== FALSE )
		$product_result = $this->Db_model->get_all_by_order('shop_product','title','asc') ;
		
		if($this->Db_model->get_all_by_order('shop_category','title','asc') !== FALSE )
		$related_result = $this->Db_model->get_all_by_order('shop_category','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('product_id', 'title', 'trim|required|numeric');
			$this->form_validation->set_rules('cat_id', 'title', 'trim|required|numeric');
			/*$this->form_validation->set_rules('location', 'location', 'trim|required|is_unique[shop_relproduct.location]');
			$this->form_validation->set_rules('pin', 'Pin', 'trim|required|numeric');
			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');*/

			//if validation errors
			if($this->form_validation->run() === FALSE)
			{
			
			
				$data = array(
							'page_name' =>  'related products' ,
							'heading' => 'related products' ,
							'top_link_name' => '' ,
							'top_link' => '' ,
							'product_result' => $product_result,
							'related_result' => $related_result,
							'result_array'=>$result_array,
							'uniq_id' => $uniq_id,
							'id'=>'',
							//'entry'=>$this->input->post('entry'),
							'order'=>$this->input->post('order'),
							'product_id'=>$this->input->post('product_id'),
							'cat_id'=>$this->input->post('cat_id'),
							//'location' => $this->input->post('location'),
							//'text'=>$this->input->post('text'),
							//'pin'=>$this->input->post('pin'),
							'str' => ''
							);

				$this->_page($data);

			}

			else
			{
					 
				$data = array(
             			    //'entry'=>$this->input->post('entry',TRUE),
							'order'=>$this->input->post('order',TRUE),
							'product_id'=>$this->input->post('product_id',TRUE),
							'cat_id'=>$this->input->post('cat_id',TRUE),
							/*'location' => $this->input->post('location',TRUE),
							'text'=>$this->input->post('text',TRUE),
							'pin'=>$this->input->post('pin',TRUE),*/
							'active'=>'Y'
							 );


				//insert data
				$insert_id = $this->Db_model->insert('shop_relproduct',$data) ;
			

				redirect('Product_related/create');

			}

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

		else
		{

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

			$data = array(
							'page_name' =>  'Link Category' ,
							'heading' => 'Link Category' ,
							'top_link_name' => '' ,
							'top_link' => '' ,
							'product_result' => $product_result,
							'related_result' => $related_result,
							'result_array'=>$result_array,
							'uniq_id' => $uniq_id,
							'id'=>'',
							//'entry'=>0,
							'order'=>'',
							'product_id'=>'',
							'cat_id'=>'',
							/*'location' => '',
							'text'=>'',
							'pin'=>'',
							*/'str' => ''
							);

			$this->_page($data);
		}

	}

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

		
		//$entry = 0 ;
		$order = '' ;
		$product_id = '' ;
		$cat_id = '' ;
		/*$location = '';
		$text = '';
		$pin = '';*/
		$uniq_id = uniqid();
		$product_result = array() ;
		$related_result = array() ;
		$result_array = 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_relproduct','id',$id);

		if( $this->Db_model->get_all_by_order('shop_relproduct','id','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_all_by_order('shop_relproduct','id','desc') ;
		}

		$uniq_id = uniqid();

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

		$this->session->set_userdata('uniq_id',$uniq_id);
		
		if($this->Db_model->get_all_by_order('shop_product','title','asc') !== FALSE )
		$product_result = $this->Db_model->get_all_by_order('shop_product','title','asc') ;
		
		if($this->Db_model->get_all_by_order('shop_category','title','asc') !== FALSE )
		$related_result = $this->Db_model->get_all_by_order('shop_category','title','asc') ;

		if($details!==FALSE)
		{
			//$entry = $details->entry ;
			$order = $details->order ;
			$product_id = $details->product_id ;
			$cat_id = $details->cat_id ;
			
			/*$location = $details->location ;
			$text = $details->text ;
			$pin = $details->pin ;
			*/

			if($this->Db_model->get_all_by_order('shop_product','title','asc') !== FALSE )
			$product_result = $this->Db_model->get_all_by_order('shop_product','title','asc') ;

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

			
		
		}

		$data = array(
					'page_name' => 'Link Category' ,
					'heading' => 'Link Category- Edit' ,
					'top_link_name' => '' ,
					'top_link' => '' ,
					'product_result' => $product_result,
					'related_result' => $related_result,
					'result_array'=>$result_array,
					'uniq_id' => $uniq_id,
					'id'=>$id,
					//'entry'=>$entry,
					'order'=>$order,
					'product_id'=>$product_id,
					'cat_id'=>$cat_id,
					/*'location' => $location,
					'text'=>$text,
					'pin'=>$pin,*/
					'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('product_id', 'title', 'trim|required|numeric');
			$this->form_validation->set_rules('cat_id', 'title', 'trim|required|numeric');
			/*$this->form_validation->set_rules('location', 'location', 'trim|required');
			$this->form_validation->set_rules('pin', 'Pin', 'trim|required|numeric');
			*/
			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{
			

				$data = array(
							'page_name' => 'related products' ,
							'heading' => 'related products - Edit' ,
							'top_link_name' => '' ,
							'top_link' => '',
							'product_result' => $product_result,
							'related_result' => $related_result,
							'result_array'=>$result_array,
							'uniq_id' => $uniq_id,
							'id'=>$id,
							//'entry'=>$this->input->post('entry'),
							'order'=>$this->input->post('order'),
							'product_id'=>$this->input->post('product_id'),
							'cat_id'=>$this->input->post('cat_id'),
							/*'location' => $this->input->post('location'),
							'text'=>$this->input->post('text'),
							'pin'=>$this->input->post('pin'),*/
							'str' => ''
							);


				$this->_page($data);

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

			}
			else
			{
	
				$data = array(
							//'entry'=>$this->input->post('entry',TRUE),
							'order'=>$this->input->post('order',TRUE),
							'product_id'=>$this->input->post('product_id',TRUE),
							'cat_id'=>$this->input->post('cat_id',TRUE),
							/*'location' => $this->input->post('location',TRUE),
							'text'=>$this->input->post('text',TRUE),
							'pin'=>$this->input->post('pin',TRUE),*/
							 );

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

				redirect('Product_related/create');

			}

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

		else
		{
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			$this->_page($data);
			//redirect('Product_related');
		}



	}


	/*public function view()
	{

		$result_array = array() ;
		$image_result = array() ;
		$this->load->helpers('basic_helper');
		
		if($this->Db_model->get_all_by_order('shop_product','title','asc') !== FALSE )
		$product_result = $this->Db_model->get_all_by_order('shop_product','title','asc') ;

		if($this->Db_model->get_all_by_order('shop_product','title','asc') !== FALSE )
		$related_result = $this->Db_model->get_all_by_order('shop_product','title','asc') ;


		if( $this->Db_model->get_all_by_order('shop_relproduct','id','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_all_by_order('shop_relproduct','id','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' => 'related products Management' ,
					  'heading' => 'related products Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('Product_related/create') ,
					  'product_result' => $product_result,
					  'related_result' => $related_result,
					  '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_relatedview',$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_relproduct',array('id'=>$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_relproduct',array('id'=>$aid));

				//redirect page to hide the id
				redirect('Product_related/create') ;

			}

		}
		else
		{
			redirect('Product_related/create') ;
		}

	}


	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_relproduct',array('id'=>$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_relproduct',array('img'=>'','tmb_img'=>''),array('id'=>$id)) ;

			//redirect page to hide the id
			redirect('Product_related/edit/'.$id);


		}

	}
	
	public function email_exist()
	{
	
	$data['resultarray']=array();
	
		if($this->Db_model->get_all_by_order('shop_product','title','asc') !== FALSE )
		$product_result = $this->Db_model->get_all_by_order('shop_product','title','asc') ;

		if($this->Db_model->get_all_by_order('shop_category','title','asc') !== FALSE )
		$related_result = $this->Db_model->get_all_by_order('shop_category','title','asc') ;
		$uniq_id = uniqid();

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

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


		
		if(isset($_POST['product_id']) && $_POST['product_id']!='')
		{
			$product_id = $_POST['product_id'] ;
		
			$status = $this->Db_model->get_where_by_order('shop_relproduct',array('product_id'=>$product_id),'id','asc') ;
			$i=0;
	
			if($status!==FALSE)
			{?>
			</br>
            
			<table width="800" align="center"   cellpadding="6" cellspacing="2">

<tr class="tdcolor">
<td width="5%" align="center">No.</td>
<td width="45%" align="center">category</td>
<td width="15%" align="center">Order</td>
<td width="20%" align="center">Status</td>
<td width="15%" align="center">Delete</td>
</tr>
<?php 

$i=1;
foreach($status as $row):

?>
<?php //if($data[$i]==$data[$i-1]):?>
<tr class="tdcolor2">
<td align="center"><?=$i?></td>
<td align="center"><?php foreach($related_result as $row2):?>
<?php if($row['cat_id']==$row2['id']){ echo $row2['title']; }?><?php endforeach; ?></td>

<!--<td align="center">
<?php if($row['img']!=''): ?>
<img src="<?php echo base_url("assets/images/".$row['tmb_img']) ; ?>" width="60" height="60"/> 
<?php endif; ?>
</td>-->
<td align="center"><?=$row['order']?></td>
<td align="center">

<?php if($row['active']=="N"): ?>

<img src="<?php echo base_url('assets/web_images/incorrect.png'); ?>" width="20" height="20" />
<br />
<a href="<?php echo site_url('Basic_functions/change_status/shop_relproduct/active/Y/id/'.$row['id'].'/Product_related/create' ); ?>">Activate?</a>

<?php elseif($row['active']=="Y") : ?>

<img src="<?php echo base_url('assets/web_images/ok.png'); ?>" width="20" height="20" />
<br />
<a href="<?php echo site_url('Basic_functions/change_status/shop_relproduct/active/N/id/'.$row['id'].'/Product_related/create' ); ?>">Suspend?</a>

<?php endif; ?>

</td>    



<td align="center">
<a href="<?php echo site_url('Product_related/delete/'.$row['id'].'/1/'.$uniq_id) ; ?>" onClick="return confirm('Delete content ?')">
<img src="<?php echo base_url('assets/web_images/delete.gif'); ?>" width="20" height="20" />
</a> 
</td>
</tr>

<?php /*endif;*/ $i++; endforeach ; ?>

</table><?php
			
				}
			
		
		}
		
		
		
	}
	

	/*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;
			}

		}
		else
		{
			return true;
		}
		
	}
*/








}//end class