337 lines
9.8 KiB
PHP
337 lines
9.8 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class Transport_provider_accounts extends Admin_Controller
|
|
{
|
|
|
|
public $viewData = array();
|
|
public $pagePerItem = 50;
|
|
public $statusOptions = [
|
|
'Inactive' => '0',
|
|
'Active' => '1',
|
|
];
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
// Load model
|
|
$this->load->model('transport_provider_account_model', 'transport_provider_account');
|
|
$this->load->model('transport_provider_model', 'transport_provider');
|
|
$this->load->helper('pagination');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->load->model('combo_model');
|
|
$this->load->helper(array('url'));
|
|
|
|
//get query string filter
|
|
$filterData = $this->input->get();
|
|
$queryString = $this->input->server('QUERY_STRING');
|
|
|
|
$pagingUrl = empty($queryString) ? uri_string() : uri_string() . '?' . $queryString;
|
|
$pagingUrl = remove_querystring_var($pagingUrl, 'page');
|
|
|
|
$filterData['perPage'] = $this->pagePerItem;
|
|
$data = $this->transport_provider_account->getTransportProviderAccount($filterData);
|
|
$data['list'] = array_map(function($item) {
|
|
|
|
$item['added'] = $this->formatDate($item['added']);
|
|
$item['used'] = $this->formatDate($item['used']);
|
|
$item['updated'] = $this->formatDate($item['updated']);
|
|
return $item;
|
|
|
|
}, $data['list']);
|
|
|
|
$this->viewData['list'] = $data['list'];
|
|
$this->viewData['statusSelection'] = $this->combo_model->getStatusComboWithAll(
|
|
'status',
|
|
$filterData['status'] ?? $this->statusOptions['Active']
|
|
);
|
|
$this->viewData['filterData'] = $filterData;
|
|
$this->viewData['pagination'] = initPagination($this->pagePerItem, $data['totalItems'], $data['page'], $pagingUrl);
|
|
$this->renderAdminPage('transport_provider_accounts/list', $this->viewData);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->viewData['statusOptions'] = $this->statusOptions;
|
|
$this->viewData['transport_provider_account'] = null;
|
|
$this->renderAdminPage('transport_provider_accounts/create', $this->viewData);
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->library('form_validation');
|
|
|
|
$inputData = $this->getFormData();
|
|
|
|
$this->viewData['transport_provider_account'] = $inputData;
|
|
$this->viewData['statusOptions'] = $this->statusOptions;
|
|
$this->setTransportProviderAccountCreationRules();
|
|
|
|
if ($this->form_validation->run() == false) {
|
|
$this->renderAdminPage('transport_provider_accounts/create', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$res = $this->transport_provider_account->createTransportProviderAccount($inputData);
|
|
|
|
if ($res['status'] == 'success') {
|
|
$this->session->set_flashdata('success', isset($res['message']) ? $res['message'] : 'Create success.');
|
|
redirect('/transport_provider_accounts');
|
|
} else {
|
|
$this->session->set_flashdata('error', isset($res['message']) ? $res['message'] : 'Create fail.');
|
|
$this->renderAdminPage('transport_provider_accounts/create', $this->viewData);
|
|
return;
|
|
}
|
|
}
|
|
|
|
private function formatDate($date) {
|
|
if (!empty($date)) {
|
|
return (new DateTime($date))->format('Y-m-d');
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->load->helper(array('form', 'url'));
|
|
$res = $this->transport_provider_account->getTransportProviderAccountByID($id);
|
|
|
|
if ($res['data']) {
|
|
$res['data']['added'] = $this->formatDate($res['data']['added']);
|
|
$res['data']['updated'] = $this->formatDate($res['data']['updated']);
|
|
$res['data']['used'] = $this->formatDate($res['data']['used']);
|
|
}
|
|
|
|
$this->viewData['transport_provider_account'] = isset($res['data']) ? $res['data'] : [];
|
|
$this->viewData['statusOptions'] = $this->statusOptions;
|
|
$this->viewData['id'] = $id;
|
|
|
|
$this->renderAdminPage('transport_provider_accounts/edit', $this->viewData);
|
|
}
|
|
|
|
public function update($id)
|
|
{
|
|
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->library('form_validation');
|
|
|
|
$inputData = $this->getFormData();
|
|
|
|
$this->viewData['statusOptions'] = $this->statusOptions;
|
|
$this->viewData['transport_provider_account'] = $inputData;
|
|
$this->viewData['id'] = $id;
|
|
$this->setTransportProviderAccountUpdateRules();
|
|
// $this->setTransportProviderAccountCreationRules();
|
|
|
|
if ($this->form_validation->run() == false) {
|
|
$this->renderAdminPage('transport_provider_accounts/edit', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$inputData = array_filter($inputData, function($value) {
|
|
return (isset($value) && trim(''.$value) !== '');
|
|
});
|
|
|
|
$res = $this->transport_provider_account->updateTransportProviderAccountById($id, $inputData);
|
|
|
|
if ($res['status'] == 'success') {
|
|
$this->session->set_flashdata('success', isset($res['message']) ? $res['message'] : 'Create success');
|
|
redirect('/transport_provider_accounts');
|
|
} else {
|
|
$this->session->set_flashdata('error', $res['message']);
|
|
$this->renderAdminPage('transport_provider_accounts/edit', $this->viewData);
|
|
return;
|
|
}
|
|
}
|
|
|
|
public function remove($param)
|
|
{
|
|
$res = $this->transport_provider_account->removeTransportProviderAccountById($param);
|
|
if (!$res['success'] != 'success') {
|
|
$this->viewData['errMsg'] = isset($res['message']) ? $res['message'] : 'Something went wrong. Please try again later.';
|
|
$this->renderAdminPage('transport_provider_accounts', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
redirect('/transport_provider_accounts');
|
|
}
|
|
|
|
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 [
|
|
'transport_provider_id' => $this->input->post('transport_provider_id'),
|
|
'transport_provider_name' => $this->input->post('transport_provider_name'),
|
|
'email' => $this->input->post('email'),
|
|
'phone' => $this->input->post('phone'),
|
|
'name' => $this->input->post('name'),
|
|
'added' => $this->input->post('added'),
|
|
'updated' => $this->input->post('updated'),
|
|
'active' => (boolean)$this->input->post('active'),
|
|
'parameters' => json_decode($this->input->post('parameters'), true),
|
|
'used' => $this->input->post('used'),
|
|
'pool' => (int)$this->input->post('pool'),
|
|
];
|
|
}
|
|
|
|
private function setTransportProviderAccountCreationRules()
|
|
{
|
|
$config = [
|
|
[
|
|
'field' => 'transport_provider_id',
|
|
'label' => 'Transport Provider ID',
|
|
'rules' => 'required',
|
|
'errors' => array(
|
|
'max_length' => 'Max length is 50.'
|
|
),
|
|
],
|
|
[
|
|
'field' => 'email',
|
|
'label' => 'Email',
|
|
'rules' => 'required|max_length[200]|callback_valid_email',
|
|
'errors' => array(
|
|
'max_length' => 'Max length is 200.',
|
|
'valid_email' => 'Email is invalid !!!'
|
|
)
|
|
],
|
|
[
|
|
'field' => 'phone',
|
|
'label' => 'Phone',
|
|
'rules' => 'required|max_length[20]', // TODO: regex
|
|
'errors' => array(
|
|
'max_length' => 'Max length is 200.'
|
|
)
|
|
],
|
|
[
|
|
'field' => 'name',
|
|
'label' => 'Name',
|
|
'rules' => 'required|max_length[50]',
|
|
'errors' => array(
|
|
'max_length' => 'Max length is 50.'
|
|
)
|
|
],
|
|
|
|
/* [
|
|
'field' => 'added',
|
|
'label' => 'Added',
|
|
'rules' => 'required', // TODO: timestamp
|
|
],
|
|
[
|
|
'field' => 'updated',
|
|
'label' => 'Updated', // TODO: timestamp
|
|
'rules' => 'required|max_length[200]',
|
|
'errors' => array(
|
|
'max_length' => 'Max length is 200.'
|
|
)
|
|
],*/
|
|
|
|
[
|
|
'field' => 'timeout',
|
|
'label' => 'Timeout',
|
|
'rules' => 'd', // TODO: timestamp
|
|
],
|
|
[
|
|
'field' => 'active',
|
|
'label' => 'Active',
|
|
'rules' => 'required', // TODO: boolean
|
|
],
|
|
/* [
|
|
'field' => 'used',
|
|
'label' => 'Used',
|
|
'rules' => 'required',
|
|
],
|
|
*/
|
|
[
|
|
'field' => 'pool',
|
|
'label' => 'Pool',
|
|
'rules' => 'required|callback_valid_pool',
|
|
],
|
|
[
|
|
'field' => 'parameters',
|
|
'label' => 'Parameters',
|
|
'rules' => 'required|callback_is_json',
|
|
'errors' => array(
|
|
'is_json' => 'Only input json.'
|
|
)
|
|
]
|
|
];
|
|
|
|
$this->form_validation->set_rules($config);
|
|
}
|
|
|
|
private function setTransportProviderAccountUpdateRules() {
|
|
$config = [
|
|
[
|
|
'field' => 'transport_provider_id',
|
|
'label' => 'Transport Provider ID',
|
|
'rules' => 'required',
|
|
'errors' => array(
|
|
'max_length' => 'Max length is 50.'
|
|
),
|
|
],
|
|
[
|
|
'field' => 'pool',
|
|
'label' => 'Pool',
|
|
'rules' => 'required|callback_valid_pool',
|
|
],
|
|
];
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* Valid Email
|
|
*
|
|
* @access public
|
|
* @param string
|
|
* @return bool
|
|
*/
|
|
public function valid_email($str)
|
|
{
|
|
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
|
|
}
|
|
|
|
public function valid_pool($number) {
|
|
return is_numeric($number) && $number > 0 && $number < 1000;
|
|
}
|
|
|
|
|
|
public function getTransportProviderByName() {
|
|
|
|
$name = $this->input->get()['q'];
|
|
$res = $this->transport_provider->getTransportProviderByName([ 'name' => $name ]);
|
|
|
|
echo json_encode($res);
|
|
}
|
|
}
|