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

class Drivers extends Admin_Controller {
	
	   function __construct()
	   {
			parent::__construct();
			$this->load->model("mod_drivers","drivers");
			$this->load->helper(array('file','string','text'));
	   }		

       public function index($page=1)
       {	
	   		$tab['activeTab'] = "drivers";
			$data["drivers"] = $this->drivers->getdrivers();
			$data["drivers"] = $data["drivers"]->result_array(); 
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/drivers/view',$data);
			$this->load->view('admin/footer');
       }
	
	   public function add()
       {	
	   		$tab['activeTab'] = "drivers";
			$this->form_validation->set_rules('name', 'Name', 'required');
		    $this->form_validation->set_rules('remark','Remark','required');
			if ($this->form_validation->run() == TRUE)
			{
			//Altering Database
				$user_id = $this->session->userdata('userId');
				if (!empty($_FILES['userfile']['name']))
				{
					$path = './../7ced114865b41ca35/assets/img/';
					$img_name = $this->move_file($_FILES['userfile'],$path);
				}else{
					$img_name = 'noimage.png';
				}
				$data['name'] = trim($_POST['name']);
				$data['status'] = $_POST['status'];
				$data['remark'] = $_POST['remark'];
				$data['images'] = $img_name;
				$data['createdate'] = date('y-m-d');
				$data['createby'] = $user_id;
				
				$msg = $this->drivers->add($data);
				//End Altering Database
				if($msg==1 || $msg==0){
					$this->session->set_flashdata('message',"New Drivers is Successfully Added");
					redirect('admin/drivers/add');
					exit();
				}else{
					$this->session->set_flashdata('message',"New Drivers is Failure Added!");
				    redirect('admin/drivers/add');
					exit();
				}
			}	
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/drivers/add');
			$this->load->view('admin/footer');
       }
	
		
	   public function edit()
	   {
			$tab['activeTab'] = "drivers";
			$id = $this->uri->segment(4);
			$driver = $this->drivers->getById($id);
			if(!$driver)
			{
				redirect("admin/drivers");
				exit();
			}
	
			if(@$_POST['update_drivers'])
			{	
				if (!empty($_FILES['userfile']['name']))
		    	{
					$path = './../7ced114865b41ca35/assets/img/';
					$img_name = $this->move_file($_FILES['userfile'],$path);
		    	}
			    $user_id = $this->session->userdata('userId');
				$postdata['name'] = $_POST['name'];
				$postdata['remark'] = $_POST['remark'];
				$postdata['status'] = $_POST['status'];
				if (!empty($_FILES['userfile']['name']))
		    	{
					$postdata['images'] = $img_name;
				}
				$postdata['modifydate'] = date('y-m-d');
				$postdata['modifyby'] = $user_id;
				
				$msg = $this->drivers->update($postdata,$id);
				
				$this->session->set_flashdata('message',"Drivers is Successfully Update");
				redirect("admin/drivers");
				exit();
			}
		
		    $data['drivers'] = $driver;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/drivers/edit',$data);
			$this->load->view('admin/footer');	
	   }
	   
	   
	   function delete()
	   {
			$id = $this->uri->segment(4);
			$this->drivers->delete($id);
			$this->session->set_flashdata('message',"Drivers deleted successfully");
			redirect("admin/drivers");
			exit();
	   }
	   
	   private function move_file($fieldName,$path)
	   {
			if(isset($fieldName["type"]))
			{
				//ini_set('upload-max-filesize', '512000');
				@ini_set('move_uploaded_file' , '5M');
				//move_uploaded_file
				//upload_max_filesize = 5097152;
				$newFilename = '';
				$validextensions = array("jpeg", "jpg", "png","pdf");
				$temporary = explode(".", $fieldName["name"]);
				$file_extension = end($temporary);
				if ((($fieldName["type"] == "image/png") || ($fieldName["type"] == "image/jpg") || ($fieldName["type"] == "image/jpeg")|| ($fieldName["type"] == "application/pdf")
			) && ($fieldName["size"] < 512000)//Approx. 100kb files can be uploaded.
			&& in_array($file_extension, $validextensions)) {
					if ($fieldName["error"] > 0)
					{
						echo "Return Code: " . $fieldName["error"] . "<br/><br/>";
					}
					else
					{
						if (file_exists($path . $fieldName["name"])) {
						echo $fieldName["name"] . " <b>already exists.</b>";
					}
					else
					{
						$sourcePath = $fieldName['tmp_name']; // Storing source path of the file in a variable
						$fname = md5(random_string('alnum', 16));
						$fExt = substr(strrchr($fieldName['name'],'.'),1);
						$newFilename = $fname.".".$fExt;
						$targetPath = $path.$newFilename; // Target path where file is to be stored
						move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
						//echo "Image Uploaded Successfully...!!";
					}
				}
			}else{
				echo "***Invalid file Size or Type***";
			}
			}
			return $newFilename;
		}
		
}