<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Offerrides extends CI_Controller
{
public function __construct()
{
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('Offer_model');
$this->load->helper('access');
if (!$this->session->userdata('logged_in')) {
redirect(base_url());
}
$class = $this->router->fetch_class();
$method = $this->router->fetch_method();
$r = check_access($class,$method);
// $r);exit();
if($r == false)
{
redirect(base_url().'welcome/error_404');
}
}
public function add_offer_rides(){
$template['page'] = 'Offerrides/add_offerrides';
$template['page_title'] = "Create offers";
$template['data'] = $this->Offer_model->get_allusers();
$this->load->view('template', $template);
if ($_POST) {
$request = $_POST;
// $request);die;
/////////////////////////duration and time calculation//////////////////////////
$from = $request['source'];
$remFrom = str_replace(',', '', $from); //Remove Commas
$from = urlencode($remFrom);
$to = $request['destination'];
$remTo = str_replace(',', '', $to); //Remove Commas
$to = urlencode($remTo);
$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
$data = json_decode($data, true);
if($data['rows'][0]['elements'][0]['status']=='ZERO_RESULTS'){
$response=array('status' => 'error','message'=>'No Routes Found!!!');
}else{
$time = $data['rows'][0]['elements'][0]['duration']['text']; //Text for String and Value for INT
$distance = $data['rows'][0]['elements'][0]['distance']['text'];//Text for String and Value for INT
$date=date("H:i:s ",strtotime($request['detour_time']));
$request['detour_time'] =$date;
$totaltime=date("H:i:s ", strtotime("$date + $time"));
$request['reached_time']=$totaltime;
}
$carexist= $this->Offer_model->car_exist($request);
if($carexist){
$this->session->set_flashdata('message', array(
'message' => 'Car Not available for this day!!',
'class' => 'danger'
));
}else{
$result = $this->Offer_model->offer_departure($request);
unset($data['submit']);
if ($result) {
$this->session->set_flashdata('message', array(
'message' => 'offer ride successfully',
'class' => 'success'
));
} else {
$this->session->set_flashdata('message', array(
'message' => 'Something Went wrong.Try Again!!',
'class' => 'danger'
));
}
}
redirect(base_url() . 'Offerrides/view_offer_rides');
}
}
public function get_user_car_details(){
if ($_POST) {
$data = $_POST;
$result=$this->Offer_model->get_allusers_car($data);
print json_encode($result);
}
}
public function view_offer_rides(){
$template['page'] = 'Offerrides/view_offerrides';
$template['page_title'] = "view offers";
$template['data'] = $this->Offer_model->get_offer_details();
$this->load->view('template', $template);
}
public function driver_viewpopup() {
$id=$_POST['ride_id'];
$template['data'] = $this->Offer_model->view_offer_details_pop($id);
$this->load->view('Offerrides/view_offer_popup',$template);
}
public function delete_Offerrides(){
$id = $this->uri->segment(3);
$result= $this->Offer_model->offer_delete($id);
$this->session->set_flashdata('message', array('message' => 'Requested Ride Deleeted Successfully','class' => 'success'));
redirect(base_url() . 'Offerrides/view_offer_rides');
//echo "tinu";die;
}
public function Edit_Offerrides(){
$id = $this->uri->segment(3);
$template['page'] = 'Offerrides/edit_offerrides';
$template['page_title'] = "view offers";
$template['data'] = $this->Offer_model->get_allusers();
$template['edit_data'] = $this->Offer_model->edit_offer_details($id);
$template['car_details'] = $this->Offer_model->get_car_name($template['edit_data']->car_id);
// $template['edit_data']);die;
if ($_POST) {
$request = $_POST;
// $request);die;
/////////////////////////duration and time calculation//////////////////////////
$from = $request['source'];
$remFrom = str_replace(',', '', $from); //Remove Commas
$from = urlencode($remFrom);
$to = $request['destination'];
$remTo = str_replace(',', '', $to); //Remove Commas
$to = urlencode($remTo);
$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
$data = json_decode($data, true);
if($data['rows'][0]['elements'][0]['status']=='ZERO_RESULTS'){
$response=array('status' => 'error','message'=>'No Routes Found!!!');
}else{
$time = $data['rows'][0]['elements'][0]['duration']['text']; //Text for String and Value for INT
$distance = $data['rows'][0]['elements'][0]['distance']['text'];//Text for String and Value for INT
$date=date("H:i:s ",strtotime($request['detour_time']));
$request['detour_time'] =$date;
$totaltime=date("H:i:s ", strtotime("$date + $time"));
$request['reached_time']=$totaltime;
}
$carexist= $this->Offer_model->car_exist_edit($request,$id);
//echo $this->db->last_query();die;
if($carexist){
$this->session->set_flashdata('message', array(
'message' => 'Car Not available for this day!!',
'class' => 'danger'
));
}else{
$result = $this->Offer_model->offer_departure_update($request,$id);
unset($data['submit']);
if ($result) {
$this->session->set_flashdata('message', array(
'message' => 'offer ride updated successfully',
'class' => 'success'
));
} else {
$this->session->set_flashdata('message', array(
'message' => 'Something Went wrong.Try Again!!',
'class' => 'danger'
));
}
}
redirect(base_url() . 'Offerrides/view_offer_rides');
}
$this->load->view('template', $template);
}
}