Files
dev-chiefworks f76abffdcd first commit
2022-05-31 16:21:53 -04:00

266 lines
8.7 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Transport_provider extends Admin_Controller
{
public $viewData = array();
public $pagePerItem = 20;
public $statusOptions = [
'Inactive' => '0',
'Active' => '1',
];
public function __construct()
{
parent::__construct();
// Load model
$this->load->model('transport_provider_model', 'transport_provider');
$this->load->helper('pagination');
}
public function index()
{
$this->load->helper(array('url'));
//get query string filter
$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->transport_provider->getTransportProvider($filterData);
$this->viewData['list'] = $data['list'];
$this->viewData['statusOptions'] = $this->statusOptions;
$this->viewData['filterData'] = $filterData;
$this->viewData['pagination'] = initPagination($this->pagePerItem, $data['totalItems'], $data['page'], $pagingUrl);
$this->renderAdminPage('transport_providers/list', $this->viewData);
}
public function create()
{
$this->load->helper(array('form', 'url'));
$this->viewData['statusOptions'] = $this->statusOptions;
$this->viewData['transport_provider'] = null;
$this->renderAdminPage('transport_providers/create', $this->viewData);
}
public function store()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->viewData['transport_provider'] = null;
$this->viewData['statusOptions'] = $this->statusOptions;
$this->setTransportProviderCreationRules();
if ($this->form_validation->run() == false) {
$this->renderAdminPage('transport_providers/create', $this->viewData);
return;
}
$inputData = $this->getFormData();
$res = $this->transport_provider->createTransportProvider($inputData);
if ($res['status'] == 'success') {
$this->session->set_flashdata('success', isSet($res['message']) ? $res['message'] : 'Create success.');
redirect('/transport_provider');
} else {
$this->session->set_flashdata('error', isSet($res['message']) ? $res['message'] : 'Create fail.');
$this->renderAdminPage('transport_providers/create', $this->viewData);
return;
}
}
public function edit($id)
{
$this->load->helper(array('form', 'url'));
$res= $this->transport_provider->getTransportByID($id);
$this->viewData['transport_provider'] = isSet($res['data']) ? $res['data'] : [];
$this->viewData['statusOptions'] = $this->statusOptions;
$this->viewData['id'] = $id;
$this->renderAdminPage('transport_providers/edit', $this->viewData);
}
public function update($id)
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->viewData['statusOptions'] = $this->statusOptions;
$this->viewData['transport_provider'] = null;
$this->viewData['id'] = $id;
$this->setTransportProviderCreationRules();
if ($this->form_validation->run() == false) {
$this->renderAdminPage('transport_providers/edit', $this->viewData);
return;
}
$inputData = $this->getFormData();
$inputData['id'] = (int) $id;
$res = $this->transport_provider->updateTransportProviderById($id, $inputData);
if ($res['status'] == 'success') {
$this->session->set_flashdata('success', isSet($res['message']) ? $res['message'] : 'Create success');
redirect('/transport_provider');
} else {
$this->session->set_flashdata('error', $res['message']);
$this->renderAdminPage('transport_provider/edit', $this->viewData);
return;
}
}
public function remove($param)
{
$res = $this->transport_provider->removeTransportProviderById($param);
if (!$res['success'] != 'success') {
$this->viewData['errMsg'] = isSet($res['message']) ? $res['message'] : 'Something went wrong. Please try again later.';
$this->renderAdminPage('transport_provider', $this->viewData);
return;
}
redirect('/transport_provider');
}
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);
}
private function getFormData() {
return [
'name' => $this->input->post('name'),
'client' => $this->input->post('client'),
'token' => $this->input->post('token'),
'code' => $this->input->post('code'),
'access_token' => $this->input->post('access_token'),
'timeout' => $this->input->post('timeout'),
'retries' => $this->input->post('retries'),
'active' => (int)$this->input->post('active'),
'custom' => $this->input->post('custom'),
];
}
private function setTransportProviderCreationRules()
{
$config = [
[
'field' => 'name',
'label' => 'Name',
'rules' => 'required|max_length[50]',
'errors' => array(
'max_length' => 'Max length is 50.'
),
],
[
'field' => 'client',
'label' => 'Client',
'rules' => 'required|max_length[100]',
'errors' => array(
'max_length' => 'Max length is 100.'
)
],
[
'field' => 'token',
'label' => 'Token',
'rules' => 'required|max_length[200]',
'errors' => array(
'max_length' => 'Max length is 200.'
)
],
[
'field' => 'code',
'label' => 'Code',
'rules' => 'required|max_length[100]',
'errors' => array(
'max_length' => 'Max length is 100.',
)
],
[
'field' => 'access_token',
'label' => 'Access Token',
'rules' => 'required|max_length[200]',
'errors' => array(
'max_length' => 'Max length is 200.'
)
],
[
'field' => 'timeout',
'label' => 'Timeout',
'rules' => 'required|numeric|callback_is_out_of_range_int2',
'errors' => array(
'is_out_of_range_int2' => 'Please input the value between ' . MIN_INT_2 . ' and ' . MAX_INT_2
)
],
[
'field' => 'retries',
'label' => 'Retries',
'rules' => 'required|numeric|callback_is_out_of_range_int4',
'errors' => array(
'is_out_of_range_int4' => 'Please input the value between ' . MIN_INT_4 . ' and ' . MAX_INT_4
)
],
[
'field' => 'active',
'label' => 'Active',
'rules' => 'required|numeric|callback_is_out_of_range_int4',
'errors' => array(
'is_out_of_range_int4' => 'Please input the value between ' . MIN_INT_4 . ' and ' . MAX_INT_4
)
],
[
'field' => 'custom',
'label' => 'Custom',
'rules' => 'required|callback_is_json',
'errors' => array(
'is_json' => 'Only input json.'
)
]
];
$this->form_validation->set_rules($config);
}
public function is_json($data) {
return (json_decode($data) != NULL) ? true : false;
}
public function is_out_of_range_int4($number) {
return is_numeric($number) && $number >= MIN_INT_4 && $number <= MAX_INT_4;
}
public function is_out_of_range_int2($number) {
return is_numeric($number) && $number >= MIN_INT_2 && $number <= MAX_INT_2;
}
public function searchTransportProvider() {
$filterData = $this->input->get();
// remove empty value
$filterData = array_filter($filterData, function ($v, $k) {
return $v != '';
}, ARRAY_FILTER_USE_BOTH);
$res = $this->transport_provider->getTransportProviderByName($filterData);
echo json_encode($res);
}
}