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


class Testimonials_admin extends CI_Controller {
	
	private $id				=	''		; 
	private $filename		=	''		;
	private $order			=	''		; 
	private $title			=	''		; 
	private $text			=	''		; 
	private $img			=	''		;
	private $type			=	''		; 
	private $active			=	''		;
	private $error			=	'' 		;
	private $error_count	=	0		;
	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/about_header',$data);
		$this->load->view('admin/menu');
		
		$this->load->view('admin/testimonials_content',$data);
		$this->load->view('admin/footer');
		
	}
	
	
	public function create()
	{

		//check if submit button clicked
		if($this->input->post('submit'))
		{
			$this->form_validation->set_rules('title', 'Title', 'trim|required|callback_record_not_exist');
			$this->form_validation->set_rules('type', 'Type', 'trim|required');
			$this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
			$this->form_validation->set_rules('text', 'Description', 'trim|required');
			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');
			

			//if validation errors
			if($this->form_validation->run() === FALSE)
			{
		
				$data = array(
							'page_name' =>  'Testimonials' ,
							'heading' => 'Testimonials' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Testimonials_admin/view') ,
							'id' => '',
							'type' => 'testi'   ,
							'order' => $this->input->post('order'),
							'title' => $this->input->post('title'),
							'text' => $this->input->post('text'),
							'img' =>'',
							'active' => 'Y',
							'str' => ''
							);
				
				$this->_page($data);
				
			}
			
			else
			{
				
					$data = array(
								  'type'=>$this->input->post('type'),
								  'order' => $this->input->post('order',TRUE),
								  'title' => $this->input->post('title',TRUE),
								  'text' => $this->input->post('text',TRUE),
								  'img' => $this->img,
								  'active' => 'Y'
								);
					
					//insert data
					$insert_id = $this->Db_model->insert('testimonials',$data) ;
					
					redirect('Testimonials_admin/view');
			}
			
		}//if($this->input->post('submit'))
		
		else
		{
			
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$data = array(
							'page_name' =>  'Testimonials' ,
							'heading' => 'Testimonials' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Testimonials_admin/view') ,
							'id' => '',
							'type' => 'testi' ,
							'order' => '',
							'title' => '',
							'text' => '',
							'img' =>'',
							'active' => 'Y',
							'str' => ''
							);
				
			$this->_page($data);
		}

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

		$this->id = $this->uri->segment(3,'');
		
		//retrieve details from database matching to the id
		$details = $this->Db_model->get_where('testimonials',array('aid'=>$this->id));
		
		if($details!==FALSE)
		{
		
		//$this->aid = $details[0]['aid'] ;
		$this->order = $details[0]['order'] ;
		$this->title = $details[0]['title'] ;
		$this->text = $details[0]['text'] ;
		$this->img = $details[0]['img'] ;
		$this->type = $details[0]['type'] ;
		$this->active = $details[0]['active'] ;

		}
		
		$data = array(
				'page_name' => 'Pages' ,
				'heading' => 'Pages - Edit' ,
				'top_link_name' => 'View' ,
				'top_link' => site_url('Testimonials_admin/view') ,
				'id' => $this->id,
				'type' => $this->type,
				'order' => $this->order,
				'title' => $this->title,
				'text' => $this->text,
				'img' => $this->img,
				//'active' => 'Y',
				'str' => ''
				);
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
			$this->form_validation->set_rules('type', 'Type', 'trim|required');
			$this->form_validation->set_rules('title', 'Title', 'trim|required');
			$this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
			$this->form_validation->set_rules('text', 'Description', 'trim|required');
			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');


			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{
				
				$data = array(
							'page_name' => 'Pages' ,
							'heading' => 'Pages - Edit' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Testimonials_admin/view') ,
							'id' => $this->id,
							'type' => 'testi' ,
							'order' => $this->input->post('order'),
							'title' => $this->input->post('title'),
							'text' => $this->input->post('text'),
							'img' => $this->img,
							//'active' => 'Y',
							'str' => ''
							);
				
			
				$this->_page($data);
				
			}
			else
			{
				
				$data = array(
							  'type' => $this->input->post('type',TRUE) ,
							  'title' => $this->input->post('title',TRUE) ,
							  'text' => $this->input->post('text',TRUE) ,
							  'order' => $this->input->post('order',TRUE) ,
							  'img' => $this->img ,
							  );
				
			
				
				//update data
				$this->Db_model->update('testimonials',$data,array('aid'=>$this->id)) ;
				
				redirect('Testimonials_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() ;
		
	
		/*if( $this->Db_model->get_where_by_order('testimonials',array('type'=>'service'),'aid','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_where_by_order('testimonials',array('type'=>'service'),'aid','desc') ;
		}*/
		if( $this->Db_model->get_where_by_order('testimonials',array(''),'aid','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_where_by_order('testimonials',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(
					  'type'=>'testi',
					  'page_name' => 'Testimonials Management' ,
					  'heading' => 'Testimonials Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('Testimonials_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/testimonials_view',$data);
		$this->load->view('admin/footer');
		
	}
	
	
	public function delete($id='',$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)
			{	
			
				$this->id = $this->uri->segment(3,'');
				
				$details = $this->Db_model->get_field('img','testimonials',array('aid'=>$this->id)) ;
				
				if($details !== FALSE )
				{
					
					if(  $details[0]['img'] != '' )
					{
						unlink('assets/images/'.$details[0]['img']) ;
					}
				
				}
				
				$this->Db_model->delete('testimonials',array('aid'=>$this->id));
				
				//redirect page to hide the id
				redirect('Testimonials_admin/view') ;
	
			}
			
		}
		else
		{
			redirect('Testimonials_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_field('img','testimonials',array('aid'=>$this->id)) ;
			
			if($details !== FALSE )
			{
				
				if(  $details[0]['img'] != '' )
				{
					unlink('assets/images/'.$details[0]['img']) ;
				}
			
			}
			
			$this->db->update('testimonials',array('img'=>''),array('aid'=>$this->id)) ;
			
			//redirect page to hide the id
			redirect('Testimonials_admin/edit/'.$this->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)
			*	return value is an array
			*	if $arr['upload']==1 uploaded , $arr['filename'] = name of file,$arr[''thumb'] = thumb filename
			*	if $arr['upload']==0 not uploaded , $arr['errors'] = upload errors
			*
			*/
			
			$this->upload_arr = $this->single_file_upload->do_upload('img') ;
			
			if($this->upload_arr['upload']==1)
			{ 
				$this->img = $this->upload_arr['filename'] ;
				return true;
			}
			
			else
			{
				$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
        		return false;
			}
			
		}
		else
		{
			return true;
		}
				
	}
	
	
	public function record_not_exist()
	{
		if($this->Db_model->get_where( 'testimonials', array('type'=>'testi','title'=> $this->input->post('title'))) === FALSE)
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('record_not_exist', 'Title name exists.');
			
			return false;
		}
	}
	
	
	
	
	


	

	
}//end class Service_admin

