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

class Partnership extends Admin_Controller {
	
	   function __construct()
	   {
			parent::__construct();
			$this->load->model("mod_partnership");
			$this->load->helper(array('file','string','text'));
	   }
	   
	   private $passwd = 'cs2022';		

       function index($page=1)
       {	
	   		$tab['activeTab'] = "partnership";
			$result = $this->mod_partnership->getPartnership();
			$data["result"] = $result->result_array(); 
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/partnership/view',$data);
			$this->load->view('admin/footer');
       }
	
	   function add()
       {	
	   		$tab['activeTab'] = "partnership";

			$this->form_validation->set_rules('par_code', 'Partner code', 'required');
			$this->form_validation->set_rules('par_name', 'Partner Name', 'required');
			$this->form_validation->set_rules('par_phone', 'Partner Phone', 'required');
			//$this->form_validation->set_rules('par_address', 'Partner Address', 'required');
			if ($this->form_validation->run() == TRUE)
			{
				//Altering Database
				$user_id = $this->session->userdata('userId');
				/*if (!empty($_FILES['userfile']['name']))
				{
					$path = './../CHANNELLIMOUSINE/assets/img/';
					$img_name = $this->move_file($_FILES['userfile'],$path);
				}else{
					$img_name = 'noimage.png';
				}*/

				$data = array(
					'par_code'		=> $this->input->post('par_code'),
					'par_name'		=> $this->input->post('par_name'),
					'par_email'		=> $this->input->post('par_email'),
					'par_phone'		=> $this->input->post('par_phone'),
					'par_website'	=> $this->input->post('par_website'),
					'par_address'	=> $this->input->post('par_address'),
					'remark'		=> $this->input->post('remark'),
					'status'		=> 1,
					'createdby'		=> $user_id,
					'createddate'	=> date('Y-m-d'),
				);
				
				$msg = $this->mod_partnership->add($data);
				//End Altering Database
				if($msg==1 || $msg==0){
					$this->session->set_flashdata('message',"New Partnership is Successfully Added");
					redirect('admin/partnership/add');
					exit();
				}else{
					$this->session->set_flashdata('message',"New Partnership is Failure Added!");
				    redirect('admin/partnership/add');
					exit();
				}
			}	
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/partnership/add');
			$this->load->view('admin/footer');
       }
	
		
	   public function edit()
	   {
			$tab['activeTab'] = "partnership";
			$id = $this->uri->segment(4);
			$result = $this->mod_partnership->getById($id);
			if(!$result)
			{
				redirect("admin/partnership");
				exit();
			}
	
			if(@$_POST['update_partnership'])
			{	
				/*if (!empty($_FILES['userfile']['name']))
		    	{
					$path = './../CHANNELLIMOUSINE/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;*/
				
				$postdata = array(
					'par_code'		=> $this->input->post('par_code'),
					'par_name'		=> $this->input->post('par_name'),
					'par_email'		=> $this->input->post('par_email'),
					'par_phone'		=> $this->input->post('par_phone'),
					'par_website'	=> $this->input->post('par_website'),
					'par_address'	=> $this->input->post('par_address'),
					'remark'		=> $this->input->post('remark'),
					'status'		=> $this->input->post('status'),
					'modifiedby'	=> $user_id,
					'modifieddate'	=> date('Y-m-d'),
				);
				
				$msg = $this->mod_partnership->update($postdata,$id);
				$this->session->set_flashdata('message',"Partnership is Successfully Update");
				redirect("admin/partnership");
				exit();
			}
		
		    $data['result'] = $result;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/partnership/edit',$data);
			$this->load->view('admin/footer');	
	   }
	   
	   
	   function delete()
	   {
			$id = $this->uri->segment(4);
			$this->mod_partnership->delete($id);
			$this->session->set_flashdata('message',"Partnership deleted successfully");
			redirect("admin/partnership");
			exit();
	   }

	   function detail()
	   {
			$tab['activeTab'] = "partnership";
			$this->load->helper('my_function');
			$id = $this->uri->segment(4);
			$result = $this->mod_partnership->getById($id);
			if(!$result)
			{
				redirect("admin/partnership");
				exit();
			}
			$data['result'] = $result;
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/partnership/detail',$data);
			$this->load->view('admin/footer');	
	   }
	   
	   function add_user()
       {   
	   		if($this->input->server('REQUEST_METHOD') == 'POST')
			{
				$this->form_validation->set_rules('username', 'Username', 'required');
				if ($this->form_validation->run() == TRUE)
				{
					$user_id = $this->session->userdata('userId');
					$data = array(
						'username' 	 => $this->input->post('username'),
						'password'   => sha1($this->input->post('password')),
						'fullname'   => $this->input->post('fullname'),
						'email' 	 => $this->input->post('email'),
						'phone' 	 => $this->input->post('phone'),
						'desc' 		 => $this->input->post('remark'),
						'status'	 => 1,
						'createdate' => date('y-m-d'),
						'createby' 	 => $user_id,
						'par_id' 	 => $this->input->post('par_id'),
					);
					$msg = $this->mod_partnership->add_user($data);
					if($msg==1 || $msg==0){
						echo"New User is Successfully Added";
					}else{
						echo"New User is Failure Added!";
					}	
				}
			}  
       }	
	   
	   function update_user()
       {   
	   		if($this->input->server('REQUEST_METHOD') == 'POST')
			{
				$this->form_validation->set_rules('username', 'Username', 'required');
				if ($this->form_validation->run() == TRUE)
				{
					$user_id = $this->session->userdata('userId');
					$id = $this->input->post('id');
					$data = array(
						'username' 	   => $this->input->post('username'),
						'fullname'     => $this->input->post('fullname'),
						'email' 	   => $this->input->post('email'),
						'phone' 	   => $this->input->post('phone'),
						'desc' 		   => $this->input->post('remark'),
						'status'	   => 1,
						'modifieddate' => date('y-m-d'),
						'modifiedby'   => $user_id,
					);
					$msg = $this->mod_partnership->update_user($data,$id);
					if($msg==1 || $msg==0){
						echo"User is Successfully Update";
					}else{
						echo"User is Failure Update!";
					}	
				}
			}  
       }
	   
	   function reset_password()
	   {
		    $this->load->library('user_agent');
		    $url = $this->agent->referrer();
			$id = $this->uri->segment(4);
			$this->mod_partnership->reset_password($id,sha1($this->passwd));
			$this->session->set_flashdata('message',"User reset password successfully");
			if($url){
				redirect($url);
				exit();
			}else{
				redirect("admin/partnership");
				exit();
			}   
	   }
	   
	   function delete_user()
	   {
		    $this->load->library('user_agent');
		    $url = $this->agent->referrer();	
	   		$id = $this->uri->segment(4);
			$this->mod_partnership->delete_user($id);
			$this->session->set_flashdata('message',"User deleted successfully");
			if($url){
				redirect($url);
				exit();
			}else{
				redirect("admin/partnership");
				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;
		}
		
}