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

315 lines
11 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Geofence_area extends Admin_Controller
{
public $viewData = array();
public $pagePerItem = 10;
public function __construct()
{
parent::__construct();
// Load model
$this->load->model('geofence_area_model');
$this->load->model('country_model');
$this->load->helper('pagination');
}
public function index()
{
$this->load->helper(array('form', 'url'));
//get query string filter
$queryString = $this->input->server('QUERY_STRING');
$filterData = $this->input->get();
$page = $this->input->get('page');
// 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->geofence_area_model->getAllWithPagination($page, $this->pagePerItem, $filterData);
$this->viewData['list'] = $data['data'];
$this->viewData['filterData'] = $filterData;
$this->viewData['pagination'] = initPagination($this->pagePerItem, $data['total'], $page, $pagingUrl);
$this->read_replica->order_by('city', 'ASC');
$this->viewData['cityList'] = $this->read_replica->get('geofence_area_city')->result();
$this->renderAdminPage('geofence_area/list', $this->viewData);
}
public function create()
{
$this->load->helper(array('form', 'url'));
$this->viewData['countryList'] = $this->country_model->getAll();
$this->viewData['cityList'] = $this->read_replica->get('geofence_area_city')->result();
$this->viewData['geofencingAreaTypes'] = Geofence_area_model::GEOFENCING_AREA_TYPES;
$this->viewData['item'] = null;
$this->renderAdminPage('geofence_area/create', $this->viewData);
}
public function store()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->viewData['item'] = null;
$this->viewData['countryList'] = $this->country_model->getAll();
$this->viewData['cityList'] = $this->read_replica->get('geofence_area_city')->result();
$this->viewData['geofencingAreaTypes'] = Geofence_area_model::GEOFENCING_AREA_TYPES;
$this->setCreationRules();
if ($this->form_validation->run() == false) {
$this->renderAdminPage('geofence_area/create', $this->viewData);
return;
}
$inputData = [
'name' => $this->input->post('name'),
'area' => $this->input->post('area'),
'city_id' => $this->input->post('city_id'),
'country' => $this->input->post('country_code'),
'latitude' => $this->input->post('latitude'),
'longitude' => $this->input->post('longitude'),
'type' => $this->input->post('type'),
'boundaries' => $this->input->post('boundaries') ?: null
];
$this->geofence_area_model->create($inputData);
$this->session->set_flashdata('success', 'Created geofence area successfully.');
redirect('/geofence_area');
}
public function edit($id)
{
$this->load->helper(array('form', 'url'));
$this->viewData['item'] = $this->geofence_area_model->getItem($id);
$this->viewData['itemId'] = $id;
$this->viewData['countryList'] = $this->country_model->getAll();
$this->viewData['cityList'] = $this->read_replica->get('geofence_area_city')->result();
$this->viewData['geofencingAreaTypes'] = Geofence_area_model::GEOFENCING_AREA_TYPES;
$this->renderAdminPage('geofence_area/edit', $this->viewData);
}
public function update($id)
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->viewData['item'] = $this->geofence_area_model->getItem($id);
$this->viewData['itemId'] = $id;
$this->viewData['countryList'] = $this->country_model->getAll();
$this->viewData['cityList'] = $this->read_replica->get('geofence_area_city')->result();
$this->viewData['geofencingAreaTypes'] = Geofence_area_model::GEOFENCING_AREA_TYPES;
$this->setCreationRules();
if ($this->form_validation->run() == false) {
$this->renderAdminPage('geofence_area/edit', $this->viewData);
return;
}
$inputData = [
'name' => $this->input->post('name'),
'area' => $this->input->post('area'),
'city_id' => $this->input->post('city_id'),
'country' => $this->input->post('country_code'),
'latitude' => $this->input->post('latitude'),
'longitude' => $this->input->post('longitude'),
'type' => $this->input->post('type'),
'boundaries' => $this->input->post('boundaries') ?: null
];
$this->geofence_area_model->update($id, $inputData);
$this->session->set_flashdata('success', 'Updated geofence area successfully.');
redirect('/geofence_area');
}
public function delete($id)
{
$res = $this->geofence_area_model->delete($id);
$this->session->set_flashdata('success', 'Deleted geofence area successfully.');
redirect('/geofence_area');
}
public function comparePriceBetweenAreas()
{
$filterData = $this->input->get();
$fromAreaId = $filterData['from_area_id'] ?? null;
$toAreaId = $filterData['to_area_id'] ?? null;
$this->viewData['polygons'] = [];
$this->viewData['priceComparison'] = [];
if ($fromAreaId && $toAreaId) {
$priceComparisonData = $this->geofence_area_model->priceComparison($fromAreaId, $toAreaId);
if ($priceComparisonData['success']) {
$startArea = $priceComparisonData['from_area'];
$endArea = $priceComparisonData['to_area'];
$startBoundaries = json_decode($startArea['boundaries'], true);
$endBoundaries = json_decode($endArea['boundaries'], true);
if (isset($startBoundaries['polygon'])) {
$startPolygon = $startBoundaries['polygon'];
}
if (isset($endBoundaries['polygon'])) {
$endPolygon = $endBoundaries['polygon'];
}
$polygons = [
"startPolygon" => !empty($startPolygon) ? $startPolygon : [],
"endPolygon" => !empty($endPolygon) ? $endPolygon : []
];
$this->viewData['polygons'] = json_encode($polygons);
$this->viewData['priceComparison'] = $priceComparisonData;
}
}
$this->read_replica->order_by('city', 'ASC');
$this->viewData['cityList'] = $this->read_replica->get('geofence_area_city')->result();
$this->viewData['areaList'] = $this->geofence_area_model->getAllAreas();
$this->viewData['filterData'] = $filterData;
// add css/js
$this->viewData['extra_styles'] = [
'/assets/css/geofence_area/price_comparison.css'
];
$this->viewData['js'] = [
'https://maps.googleapis.com/maps/api/js?key=AIzaSyDvjiRTxngOQyBP4zpqFlZuiquc0ROvo9c&libraries=geometry'
];
$this->renderAdminPage('geofence_area/price-comparison', $this->viewData);
}
public function getCityListDependOnCountryCodeAjax()
{
$inputData = $this->input->get();
$this->read_replica->where('country', $inputData['country_code']);
$this->read_replica->order_by('city', 'ASC');
$query = $this->read_replica->get('geofence_area_city');
$data = $query->result_array();
$response = [
'success' => true,
'data' => !empty($data) ? $data : []
];
echo json_encode($response);
}
public function getAreaListDependOnCityIdAjax()
{
$inputData = $this->input->get();
$cityId = $inputData['city_id'] ?? null;
if ($cityId) {
$this->read_replica->where('city_id', $cityId);
}
$this->read_replica->order_by('name', 'ASC');
$query = $this->read_replica->get('geofence_area');
$data = $query->result_array();
$response = [
'success' => true,
'data' => !empty($data) ? $data : []
];
echo json_encode($response);
}
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 setCreationRules()
{
$config = [
[
'field' => 'name',
'label' => 'Name',
'rules' => 'required',
],
[
'field' => 'area',
'label' => 'Area name',
'rules' => 'required',
],
[
'field' => 'country_code',
'label' => 'Country',
'rules' => 'required',
],
[
'field' => 'city_id',
'label' => 'City',
'rules' => 'required',
],
[
'field' => 'latitude',
'label' => 'Latitude',
'rules' => 'required|numeric',
],
[
'field' => 'longitude',
'label' => 'Longitude',
'rules' => 'required|numeric',
],
[
'field' => 'type',
'label' => 'Geofencing area types',
'rules' => 'required',
],
[
'field' => 'boundaries',
'label' => 'Boundaries',
'rules' => 'callback_jsonRegex', // The input have to be json format
'errors' => [
'jsonRegex' => 'Please enter a json format.',
],
],
];
$this->form_validation->set_rules($config);
}
public function jsonRegex($input)
{
if (empty(trim($input))) {
return;
}
// json format regex
$pcreRegex = '/
(?(DEFINE)
(?<number> -? (?= [1-9]|0(?!\d) ) \d+ (\.\d+)? ([eE] [+-]? \d+)? )
(?<boolean> true | false | null )
(?<string> " ([^"\n\r\t\\\\]* | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* " )
(?<array> \[ (?: (?&json) (?: , (?&json) )* )? \s* \] )
(?<pair> \s* (?&string) \s* : (?&json) )
(?<object> \{ (?: (?&pair) (?: , (?&pair) )* )? \s* \} )
(?<json> \s* (?: (?&number) | (?&boolean) | (?&string) | (?&array) | (?&object) ) \s* )
)
\A (?&json) \Z
/six';
if (preg_match($pcreRegex, $input)) {
return true;
} else {
return false;
}
}
}