<?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/service_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()
	{

		$service_result= array();
		$category_result = array() ;
		$result_array = array();
		
		if( $this->db_model->get_all_by_order('catpref','pcid','desc') !== FALSE )
		{
			$result_array = $this->db_model->get_all_by_order('catpref','pcid','desc') ;
		}

		$uniq_id = uniqid();

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

		$this->session->set_userdata('uniq_id',$uniq_id);
		
		if($this->db_model->get_all_by_order('pages','title','asc') !== FALSE )
		$service_result = $this->db_model->get_all_by_order('pages','title','asc') ;
		
		if($this->db_model->get_all_by_order('category','title','asc') !== FALSE )
		$category_result = $this->db_model->get_all_by_order('category','title','asc') ;

		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
			$this->form_validation->set_rules('aid', 'aid', 'trim|required|numeric');
			$this->form_validation->set_rules('pcat', 'title', 'trim|required|numeric');
			/*$this->form_validation->set_rules('location', 'location', 'trim|required|is_unique[catpref.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 services' ,
							'heading' => 'related services' ,
							'top_link_name' => '' ,
							'top_link' => '' ,
							'service_result' => $service_result,
							'category_result' => $category_result,
							'result_array'=>$result_array,
							'uniq_id' => $uniq_id,
							'pcid'=>'',
							//'entry'=>$this->input->post('entry')
							'aid'=>$this->input->post('aid'),
							'pcat'=>$this->input->post('pcat'),
							//'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),
							'aid'=>$this->input->post('aid',TRUE),
							'pcat'=>$this->input->post('pcat',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('catpref',$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' =>  'Related Products' ,
							'heading' => 'Related Products' ,
							'top_link_name' => '' ,
							'top_link' => '' ,
							'service_result' => $service_result,
							'category_result' => $category_result,
							'result_array'=>$result_array,
							'uniq_id' => $uniq_id,
							'pcid'=>'',
							//'entry'=>0,
							'aid'=>'',
							'pcat'=>'',
							/*'location' => '',
							'text'=>'',
							'pin'=>'',
							*/'str' => ''
							);

			$this->_page($data);
		}

	}

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

		
		//$entry = 0 ;
		$aid = '' ;
		$pcat = '' ;
		/*$location = '';
		$text = '';
		$pin = '';*/
		$uniq_id = uniqid();
		$service_result = array() ;
		$category_result = array() ;
		$result_array = array();
		$pcid = $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('catpref','pcid',$pcid);

		if( $this->db_model->get_all_by_order('catpref','pcid','desc') !== FALSE )
		{
			$result_array = $this->db_model->get_all_by_order('catpref','pcid','desc') ;
		}

		$uniq_id = uniqid();

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

		$this->session->set_userdata('uniq_id',$uniq_id);
		
		if($this->db_model->get_all_by_order('pages','title','asc') !== FALSE )
		$service_result = $this->db_model->get_all_by_order('pages','title','asc') ;
		
		if($this->db_model->get_all_by_order('category','title','asc') !== FALSE )
		$category_result = $this->db_model->get_all_by_order('category','title','asc') ;

		if($details!==FALSE)
		{
			//$entry = $details->entry ;
			$aid = $details->aid ;
			$pcat = $details->pcat ;
			
			/*$location = $details->location ;
			$text = $details->text ;
			$pin = $details->pin ;
			*/

			if($this->db_model->get_all_by_order('pages','title','asc') !== FALSE )
			$service_result = $this->db_model->get_all_by_order('pages','title','asc') ;

			if($this->db_model->get_all_by_order('category','title','asc') !== FALSE )
			$category_result = $this->db_model->get_all_by_order('category','title','asc') ;

			
		
		}

		$data = array(
					'page_name' => 'Related Products' ,
					'heading' => 'Related Products- Edit' ,
					'top_link_name' => '' ,
					'top_link' => '' ,
					'service_result' => $service_result,
					'category_result' => $category_result,
					'result_array'=>$result_array,
					'uniq_id' => $uniq_id,
					'pcid'=>$pcid,
					//'entry'=>$entry,
					'aid'=>$aid,
					'pcat'=>$pcat,
					/*'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('aid', 'title', 'trim|required|numeric');
			$this->form_validation->set_rules('pcat', '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 services' ,
							'heading' => 'Related services - Edit' ,
							'top_link_name' => '' ,
							'top_link' => '',
							'service_result' => $service_result,
							'category_result' => $category_result,
							'result_array'=>$result_array,
							'uniq_id' => $uniq_id,
							'pcid'=>$pcid,
							//'entry'=>$this->input->post('entry'),
							'aid'=>$this->input->post('aid'),
							'pcat'=>$this->input->post('pcat'),
							/*'location' => $this->input->post('location'),
							'text'=>$this->input->post('text'),
							'pin'=>$this->input->post('pin'),*/
							'str' => ''
							);


				$this->_page($data);

			//$this->db_model->update('catpref',$data,array('id'=>$id)) ;

			}
			else
			{
	
				$data = array(
							//'entry'=>$this->input->post('entry',TRUE),
							'aid'=>$this->input->post('aid',TRUE),
							'pcat'=>$this->input->post('pcat',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('catpref',$data,array('pcid'=>$pcid)) ;

				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('category','title','asc') !== FALSE )
		$product_result = $this->db_model->get_all_by_order('category','title','asc') ;

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


		if( $this->db_model->get_all_by_order('catpref','pcid','desc') !== FALSE )
		{
			$result_array = $this->db_model->get_all_by_order('catpref','pcid','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($pcid='',$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
				$pcid = $this->uri->segment(3,'');

				$details = $this->db_model->get_where('catpref',array('pcid'=>$pcid)) ;

			/*	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('catpref',array('pcid'=>$pcid));

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

			}

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

	}


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

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

			$this->pcid = $this->uri->segment(3,'');

			$details = $this->db_model->get_where('catpref',array('pcid'=>$pcid)) ;

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

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


		}

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

		if($this->db_model->get_all_by_order('category','title','asc') !== FALSE )
		$category_result = $this->db_model->get_all_by_order('category','title','asc') ;
		$uniq_id = uniqid();

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

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


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

<tr class="tdcolor">
<td width="15%" align="center">No.</td>
<td width="25%" align="center">service</td>
<td width="25%" align="center">Category</td>
<td width="20%" align="center">Status</td>
<td width="20%" 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($service_result as $row2):?>
<?php if($row['pcat']==$row2['aid']){ 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/catpref/active/Y/id/'.$row['pcid'].'/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/catpref/active/N/pcid/'.$row['pcid'].'/Product_related/create' ); ?>">Suspend?</a>

<?php endif; ?>

</td>    



<td align="center">
<a href="<?php echo site_url('Product_related/delete/'.$row['pcid'].'/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