Files
FloatBackOfffice/application/controllers/Trips.php
T
dev-chiefworks f76abffdcd first commit
2022-05-31 16:21:53 -04:00

217 lines
7.3 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
use Carbon\Carbon;
class Trips extends Admin_Controller
{
public $viewData = array();
public $pagePerItem = 10;
public function __construct()
{
parent::__construct();
// Load model
$this->load->model('parsedEmailItem_model', 'parsedEmailItem');
$this->viewData['selectedTab'] = 'trip';
$this->load->helper('pagination');
}
public function index()
{
$this->load->helper(array('form', 'url'));
//get query string filter
$page = $this->input->get('page', true);
$travelDate = $this->input->get('travel_date', true);
$duration = $this->input->get('duration', true);
$cost = $this->input->get('cost', true);
$transportProviderId = $this->input->get('transport_provider_id', true);
$distance = $this->input->get('distance', true);
$filterData = $this->input->get();
$queryString = $this->input->server('QUERY_STRING');
// remove empty value
$filterData = array_filter($filterData, function ($v, $k) {
return $v != '';
}, ARRAY_FILTER_USE_BOTH);
$pagingUrl = empty($queryString) ? uri_string() : uri_string() . '?' . $queryString;
$pagingUrl = remove_querystring_var($pagingUrl, 'page');
$data = $this->parsedEmailItem->getParsedEmailItemList($filterData);
$this->viewData['list'] = $data['list'];
$this->viewData['filterData'] = $filterData;
$this->viewData['pagination'] = initPagination($this->pagePerItem, $data['itemTotal'], $data['page'], $pagingUrl);
//$this->template->load('trips/trips', $this->viewData);
$this->renderAdminPage('trips/trips', $this->viewData);
}
public function create()
{
$this->load->helper(array('form', 'url'));
$this->viewData['tripItem'] = null;
$this->template->load('trips/create', $this->viewData);
}
public function store()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->viewData['tripItem'] = null;
$this->setTripCreationRules();
if ($this->form_validation->run() == false) {
$this->template->load('trips/create', $this->viewData);
return;
}
$inputData = [
'travel_date' => Carbon::parse($this->input->post('travel_date'))->format('Y-m-d'),
'travel_date_end' => Carbon::parse($this->input->post('travel_date_end'))->format('Y-m-d'),
'cost' => $this->input->post('cost'),
'cost_raw' => $this->input->post('cost_raw'),
'duration' => $this->input->post('duration'),
'distance' => $this->input->post('distance'),
'transport_provider_id' => $this->input->post('transport_provider_id'),
'location_start_id' => $this->input->post('location_start_id'),
'location_end_id' => $this->input->post('location_end_id'),
'scheduled' => Carbon::now()->format('Y-m-d')
];
$res = $this->parsedEmailItem->createParsedEmailItem($inputData);
if ($res['success']) {
$this->session->set_flashdata('success', $res['message']);
redirect('/trips');
} else {
$this->session->set_flashdata('error', $res['message']);
$this->template->load('trips/create', $this->viewData);
return;
}
}
public function edit($id)
{
$this->load->helper(array('form', 'url'));
$tripItem = $this->parsedEmailItem->getParsedEmailItem($id);
$this->viewData['tripItem'] = $tripItem;
$this->viewData['tripId'] = $id;
$this->template->load('trips/edit', $this->viewData);
}
public function update($id)
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->viewData['tripItem'] = null;
$this->viewData['tripId'] = $id;
$this->setTripCreationRules();
if ($this->form_validation->run() == false) {
$this->template->load('trips/edit', $this->viewData);
return;
}
$inputData = [
'travel_date' => Carbon::parse($this->input->post('travel_date'))->format('Y-m-d'),
'travel_date_end' => Carbon::parse($this->input->post('travel_date_end'))->format('Y-m-d'),
'cost' => $this->input->post('cost'),
'cost_raw' => $this->input->post('cost_raw'),
'duration' => $this->input->post('duration'),
'distance' => $this->input->post('distance'),
'transport_provider_id' => $this->input->post('transport_provider_id'),
'location_start_id' => $this->input->post('location_start_id'),
'location_end_id' => $this->input->post('location_end_id')
];
$res = $this->parsedEmailItem->updateParsedEmailItem($id, $inputData);
if ($res['success']) {
$this->session->set_flashdata('success', $res['message']);
redirect('/trips');
} else {
$this->session->set_flashdata('error', $res['message']);
$this->template->load('trips/edit', $this->viewData);
return;
}
}
public function delete($id)
{
$res = $this->parsedEmailItem->deleteParsedEmailItem($id);
if (!$res['success']) {
$this->session->set_flashdata('success', $res['message']);
} else {
$this->session->set_flashdata('error', $res['error']);
}
redirect('/trips');
}
private function setTripCreationRules()
{
$config = [
[
'field' => 'travel_date',
'label' => 'Travel date',
'rules' => 'required',
],
[
'field' => 'travel_date_end',
'label' => 'Travel date end',
'rules' => 'required',
],
[
'field' => 'location_start_id',
'label' => 'Location start',
'rules' => 'required',
],
[
'field' => 'location_end_id',
'label' => 'Location end',
'rules' => 'required',
],
[
'field' => 'duration',
'label' => 'Duration',
'rules' => 'required',
],
[
'field' => 'distance',
'label' => 'Distance',
'rules' => 'required',
],
[
'field' => 'cost_raw',
'label' => 'Cost raw',
'rules' => 'required',
],
[
'field' => 'cost',
'label' => 'Cost',
'rules' => 'required',
],
[
'field' => 'transport_provider_id',
'label' => 'Transport provider',
'rules' => 'required',
]
];
$this->form_validation->set_rules($config);
}
protected function renderAdminPage($page_name, $data) {
$this->load->view('admin/view_admin_header', $data);
$this->load->view($page_name, $data);
$this->load->view('admin/view_admin_footer', $data);
}
}