268 lines
8.5 KiB
PHP
268 lines
8.5 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class Addresses extends Admin_Controller
|
|
{
|
|
public $viewData = array();
|
|
public $countries = array();
|
|
public $pagePerItem = 10;
|
|
private $geocode;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
// Load model
|
|
$this->load->model('combo_model');
|
|
$this->load->model('address_model', 'address');
|
|
$this->load->model('Geofence_area_city_model', 'geofence_area_city');
|
|
$this->viewData['selectedTab'] = 'address';
|
|
|
|
$this->load->helper('pagination');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$page = $this->input->get('page', true);
|
|
$params = [];
|
|
if ($page) {
|
|
$params = ['page' => $page];
|
|
};
|
|
|
|
$this->viewData['city'] = $this->combo_model->getCityCombo('city', trim($this->input->get('city')));
|
|
|
|
$data = $this->address->getAddresses($params);
|
|
$this->viewData['radius_default'] = 'on';
|
|
$this->viewData['list'] = $data['list'];
|
|
$this->viewData['pagination'] = initPagination($this->pagePerItem, $data['total'], $data['page'], '/addresses');
|
|
$this->renderAdminPage('addresses/list', $this->viewData);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->library('form_validation');
|
|
$this->viewData['errMsg'] = null;
|
|
|
|
$this->setFormRule();
|
|
if ($this->form_validation->run() == false) {
|
|
$this->renderAdminPage('addresses/create', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$params = $this->getFormValue();
|
|
|
|
$res = $this->address->createAddress($params);
|
|
if ($res['success']) {
|
|
$this->session->set_flashdata('success', $res['message']);
|
|
redirect('/addresses');
|
|
} else {
|
|
$this->viewData['errMsg'] = $res['error'];
|
|
$this->viewData['address'] = $params;
|
|
}
|
|
|
|
$this->load->view('addresses/create', $this->viewData);
|
|
}
|
|
|
|
public function getParamsRequest()
|
|
{
|
|
return [
|
|
'city' => trim($this->input->get('city_id', true)),
|
|
'city_name' => trim($this->input->get('city_name', true)),
|
|
'address' => trim($this->input->get('address', true)),
|
|
'radius' => trim($this->input->get('radius', true)),
|
|
'longitude' => trim($this->input->get('longitude', true)),
|
|
'latitude' => trim($this->input->get('latitude', true)),
|
|
'radius_default' => $this->input->get('radius_default'),
|
|
];
|
|
}
|
|
|
|
public function search()
|
|
{
|
|
$params = $this->getParamsRequest();
|
|
$page = $this->input->get('page', true);
|
|
|
|
$params = array_filter($params, function ($val) {
|
|
return $val !== '';
|
|
});
|
|
|
|
$pagingUrl = '/addresses/search?' . http_build_query($params);
|
|
|
|
if ($page) {
|
|
$params['page'] = $page;
|
|
}
|
|
|
|
$data = $this->address->getAddresses($params);
|
|
$this->viewData['city'] = $this->combo_model->getCityCombo('city', $this->input->get('city'));
|
|
$this->viewData['list'] = $data['list'];
|
|
$this->viewData['pagination'] = initPagination($this->pagePerItem, $data['total'], $data['page'], $pagingUrl);
|
|
$this->viewData = array_merge($this->viewData, $params);
|
|
$this->renderAdminPage('addresses/list', $this->viewData);
|
|
}
|
|
|
|
public function addressEdit($param)
|
|
{
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->library('form_validation');
|
|
|
|
// Check if user edit the id through browser inspect
|
|
$this->viewData['address'] = ['id' => $param];
|
|
$id = $this->input->post('id');
|
|
if (isset($id) && $id != $param) {
|
|
redirect('/addresses');
|
|
}
|
|
|
|
$res = $this->address->getAddressById($param);
|
|
$this->viewData['address'] = $res['data'];
|
|
|
|
if (isset($res['error'])) {
|
|
redirect('/addresses');
|
|
return;
|
|
}
|
|
|
|
$this->viewData['errMsg'] = null;
|
|
|
|
$this->setFormRule();
|
|
|
|
if ($this->form_validation->run() == false) {
|
|
$this->viewData['errMsg'] = validation_errors();
|
|
$this->renderAdminPage('addresses/edit', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$params = $this->getFormValue();
|
|
$updateParams = http_build_query($params);
|
|
|
|
$res = $this->address->updateAddressById($id, $updateParams);
|
|
|
|
if ($res['success']) {
|
|
$this->session->set_flashdata('success', $res['message']);
|
|
redirect('/addresses');
|
|
} else {
|
|
$this->viewData['errMsg'] = $res['error'];
|
|
$this->viewData['address'] = $params;
|
|
}
|
|
|
|
$this->viewData['address'] = $params;
|
|
|
|
$this->renderAdminPage('addresses/edit', $this->viewData);
|
|
}
|
|
|
|
public function remove($param)
|
|
{
|
|
$res = $this->address->removeAddressById($param);
|
|
|
|
if ($res['success']) {
|
|
$this->session->set_flashdata('success', $res['message']);
|
|
} else {
|
|
$this->session->set_flashdata('error', $res['error']);
|
|
}
|
|
|
|
redirect('/addresses');
|
|
}
|
|
|
|
private function setFormRule()
|
|
{
|
|
$this->form_validation->set_rules('address', 'Address', 'required|callback_isValidAddress');
|
|
$this->form_validation->set_rules('geocoding_date', 'Geocoding date', 'required|callback_isDate');
|
|
$this->form_validation->set_rules('description', 'Description', 'max_length[100]');
|
|
}
|
|
|
|
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 getFormValue()
|
|
{
|
|
return [
|
|
'address' => trim($this->input->post('address')),
|
|
'geocoding_date' => trim($this->input->post('geocoding_date')),
|
|
'description' => trim($this->input->post('description')),
|
|
'latitude' => $this->geocode['lat'],
|
|
'longitude' => $this->geocode['lng'],
|
|
'timezone' => $this->geocode['timezone'],
|
|
'postal' => $this->geocode['postal'],
|
|
'country' => $this->geocode['country'],
|
|
'geometry' => $this->convertToGeometry($this->geocode),
|
|
'city' => $this->geocode['city_id'],
|
|
];
|
|
}
|
|
|
|
public function isDate($date)
|
|
{
|
|
$matches = [];
|
|
$result = preg_match_all("/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date, $matches, PREG_SET_ORDER);
|
|
|
|
if ($result == 0 || $result == false) {
|
|
$this->form_validation->set_message('isDate', 'Geocoding date is invalid');
|
|
return false;
|
|
}
|
|
|
|
$day = $matches[0][3];
|
|
$month = $matches[0][2];
|
|
$year = $matches[0][1];
|
|
|
|
if (checkdate($month, $day, $year)) {
|
|
return true;
|
|
} else {
|
|
$this->form_validation->set_message('isDate', 'Geocoding date is invalid');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function isValidAddress($address)
|
|
{
|
|
$this->geocode = SQEAPI::geocode($address)['geocode'];
|
|
|
|
if (!$this->geocode) {
|
|
$this->form_validation->set_message('isValidAddress', 'Address is invalid');
|
|
return false;
|
|
}
|
|
|
|
$this->city = $this->geofence_area_city->getCityByCoordindates([
|
|
'latitude' => $this->geocode['lat'],
|
|
'longitude' => $this->geocode['lng'],
|
|
'radius' => 5, // km
|
|
]);
|
|
|
|
if (!$this->city) {
|
|
$this->form_validation->set_message('isValidAddress', 'Address is invalid');
|
|
return false;
|
|
}
|
|
|
|
$this->geocode['city_id'] = $this->city[0]['id'];
|
|
return true;
|
|
}
|
|
|
|
public function getAddressesAjax()
|
|
{
|
|
if (!$this->input->is_ajax_request()) {
|
|
exit('No direct script access allowed');
|
|
}
|
|
|
|
$result = $this->address->getAddressesWithUsingQuery([
|
|
'address' => trim($this->input->get('name')),
|
|
'page' => $this->input->get('page'),
|
|
]);
|
|
|
|
echo json_encode([
|
|
'data' => $result['data'],
|
|
'total' => $result['total'],
|
|
]);
|
|
}
|
|
|
|
private function convertToGeometry($params)
|
|
{
|
|
$query = "";
|
|
|
|
$query = "SELECT(ST_GeomFromText('"
|
|
. "POINT(" . $params['lat'] . " "
|
|
. $params['lng'] . ")', 4326)) AS geometry";
|
|
|
|
$rs = $this->read_replica->query($query);
|
|
return $rs->row_array()['geometry'];
|
|
}
|
|
}
|