first commit
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Automation_jobs extends Admin_Controller
|
||||
{
|
||||
public $viewData = array();
|
||||
public $pagePerItem = 50;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
// Load model
|
||||
$this->load->model('automation_job_model', 'automation_job');
|
||||
$this->load->helper('pagination');
|
||||
}
|
||||
|
||||
public function setOnePastWeek(&$params) {
|
||||
if (
|
||||
(empty($params['from_complete']))
|
||||
&& (empty($params['to_complete']))
|
||||
) {
|
||||
$params['from_complete'] = date('Y-m-d', strtotime('-1 week'));
|
||||
$params['to_complete'] = date('Y-m-d');
|
||||
}
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->load->helper(array('url'));
|
||||
|
||||
//get query string filter
|
||||
$filterData = $this->input->get();
|
||||
$this->setOnePastWeek($filterData);
|
||||
$filterData['transport_provider_name'] = '';
|
||||
$queryString = $this->input->server('QUERY_STRING');
|
||||
|
||||
// remove empty value
|
||||
$filterData = array_map(function($ele) {
|
||||
return trim($ele);
|
||||
}, $filterData);
|
||||
$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->automation_job->getJobs($filterData);
|
||||
|
||||
$this->viewData['list'] = $data['list'];
|
||||
$this->viewData['filterData'] = $filterData;
|
||||
$this->viewData['pagination'] = initPagination($this->pagePerItem, $data['totalItems'], $data['page'], $pagingUrl);
|
||||
$this->viewData = array_merge($this->viewData, $filterData);
|
||||
$this->renderAdminPage('automation_jobs/list', $this->viewData);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->load->helper(array('form', 'url'));
|
||||
$this->viewData['job'] = null;
|
||||
$this->renderAdminPage('automation_jobs/create', $this->viewData);
|
||||
}
|
||||
|
||||
public function store()
|
||||
{
|
||||
$this->load->helper(array('form', 'url'));
|
||||
$this->load->library('form_validation');
|
||||
|
||||
$this->viewData['job'] = null;
|
||||
$this->setJobCreationRules();
|
||||
$inputData = $this->getFormData();
|
||||
|
||||
if ($this->form_validation->run() == false) {
|
||||
$this->viewData['job'] = $this->prepareTransportProviderData();
|
||||
$this->renderAdminPage('automation_jobs/create', $this->viewData);
|
||||
return;
|
||||
}
|
||||
|
||||
$res = $this->automation_job->createJob($inputData);
|
||||
if ($res['status'] == 'success') {
|
||||
$this->session->set_flashdata('success', isSet($res['message']) ? $res['message'] : 'Create success.');
|
||||
redirect('/automation_jobs');
|
||||
} else {
|
||||
$this->viewData['job'] = $this->prepareTransportProviderData();
|
||||
$this->session->set_flashdata('error', isSet($res['message']) ? $res['message'] : 'Create fail.');
|
||||
$this->renderAdminPage('automation_jobs/create', $this->viewData);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$this->load->helper(array('form', 'url'));
|
||||
$res= $this->automation_job->getById($id);
|
||||
|
||||
$job = isSet($res['data']) ? $res['data'] : [];
|
||||
$this->viewData['job'] = $job;
|
||||
$this->viewData['transport_provider_name'] = isset($job) ? $job['transport_provider_name'] : "";
|
||||
$this->viewData['jobId'] = $id;
|
||||
$this->renderAdminPage('automation_jobs/edit', $this->viewData);
|
||||
}
|
||||
|
||||
public function update($id)
|
||||
{
|
||||
$this->load->helper(array('form', 'url'));
|
||||
$this->load->library('form_validation');
|
||||
|
||||
$this->viewData['job'] = null;
|
||||
$this->viewData['jobId'] = $id;
|
||||
$this->setJobCreationRules();
|
||||
|
||||
if ($this->form_validation->run() == false) {
|
||||
$this->viewData['job'] = $this->prepareTransportProviderData();
|
||||
$this->renderAdminPage('automation_jobs/edit', $this->viewData);
|
||||
return;
|
||||
}
|
||||
|
||||
$inputData = $this->getFormData();
|
||||
$inputData['id'] = (int) $id;
|
||||
|
||||
$res = $this->automation_job->updateById($id, $inputData);
|
||||
|
||||
if ($res['status'] == 'success') {
|
||||
$this->session->set_flashdata('success', isSet($res['message']) ? $res['message'] : 'Create success');
|
||||
redirect('/automation_jobs');
|
||||
} else {
|
||||
$this->viewData['job'] = $this->prepareTransportProviderData();
|
||||
$this->session->set_flashdata('error', $res['message']);
|
||||
$this->renderAdminPage('automation_jobs/edit', $this->viewData);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function remove($param)
|
||||
{
|
||||
$res = $this->automation_job->removeById($param);
|
||||
if (!$res['success'] != 'success') {
|
||||
$this->viewData['errMsg'] = isSet($res['message']) ? $res['message'] : 'Something went wrong. Please try again later.';
|
||||
$this->renderAdminPage('automation_jobs', $this->viewData);
|
||||
return;
|
||||
}
|
||||
|
||||
redirect('/automation_jobs');
|
||||
}
|
||||
|
||||
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 [
|
||||
'location_start' => $this->input->post('location_start'),
|
||||
'location_end' => $this->input->post('location_end'),
|
||||
'location_start_lat' => $this->input->post('location_start_lat'),
|
||||
'location_start_lng' => $this->input->post('location_start_lng'),
|
||||
'location_end_lat' => $this->input->post('location_end_lat'),
|
||||
'location_end_lng' => $this->input->post('location_end_lng'),
|
||||
'cost_raw' => $this->input->post('cost_raw'),
|
||||
'transport_provider_id' => (int) $this->input->post('transport_provider_id'),
|
||||
'trackedemail_item_id' => $this->input->post('trackedemail_item_id'),
|
||||
'cost' => $this->input->post('cost'),
|
||||
'started' => $this->input->post('started'),
|
||||
'complete' => $this->input->post('complete'),
|
||||
'status' => $this->input->post('status'),
|
||||
'message' => $this->input->post('message'),
|
||||
'attempts' => (int) $this->input->post('attempts'),
|
||||
];
|
||||
}
|
||||
|
||||
private function setJobCreationRules()
|
||||
{
|
||||
$config = [
|
||||
[
|
||||
'field' => 'location_start',
|
||||
'label' => 'Location start',
|
||||
'rules' => 'required|max_length[200]',
|
||||
'errors' => array(
|
||||
'max_length' => 'Max length is 200.'
|
||||
),
|
||||
],
|
||||
[
|
||||
'field' => 'location_end',
|
||||
'label' => 'Location end',
|
||||
'rules' => 'required|max_length[200]',
|
||||
'errors' => array(
|
||||
'max_length' => 'Max length is 200.'
|
||||
)
|
||||
],
|
||||
[
|
||||
'field' => 'trackedemail_item_id',
|
||||
'label' => 'Trackedemail Item',
|
||||
'rules' => 'required'
|
||||
],
|
||||
[
|
||||
'field' => 'transport_provider_id',
|
||||
'label' => 'Transport provider',
|
||||
'rules' => 'required',
|
||||
],
|
||||
];
|
||||
|
||||
$this->form_validation->set_rules($config);
|
||||
}
|
||||
|
||||
private function prepareTransportProviderData() {
|
||||
return [
|
||||
'transport_provider_id' => (int) $this->input->post('transport_provider_id'),
|
||||
'transport_provider_name' => $this->input->post('transport_provider_name')
|
||||
];
|
||||
}
|
||||
|
||||
public function getAndroidAutomationJobsByTransportProvider() {
|
||||
|
||||
$transport_provider_id = (int)$this->input->get('transport_provider_id');
|
||||
|
||||
$data = $this->automation_job->getJobs(
|
||||
$transport_provider_id === 0 ? [] : [ 'transport_provider_id' => $transport_provider_id ]
|
||||
);
|
||||
|
||||
echo json_encode([
|
||||
'android_automation_jobs' => $data['list']
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user