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

316 lines
10 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Geofence_area_anchor extends Admin_Controller
{
public $viewData = [];
public function __construct()
{
parent::__construct();
$this->load->library('table');
$this->table->set_template($this->template);
$this->load->model('combo_model');
$this->load->model('address_model', 'address');
$this->load->model('Geofence_area_anchor_model', 'geofence_area_anchor');
$this->load->model('Geofence_area_model', 'geofence_area');
$this->load->model('Geofence_area_city_model', 'geofence_area_city');
// Load Pagination library
$this->load->library('pagination');
$this->viewData['city_card'] = $this->combo_model->getCityCombo('city_card', $this->input->get('city'));
$this->viewData['geofence_area_city_card'] = $this->combo_model->getCityCombo('geofence_area_city_card', '');
$this->viewData['country_card'] = $this->combo_model->getCountryCombo('country_card', '');
$this->viewData['msg'] = null;
}
protected function renderToolsPage($page_name, $data)
{
$this->load->view('admin/view_admin_header', $data);
$this->load->view('geofence_area_anchor/' . $page_name, $data);
$this->load->view('admin/view_admin_footer', $data);
}
public function index()
{
$this->renderToolsPage("view_geofence_area_anchor", $this->viewData);
}
private function getFormValue()
{
return [
'geofence_area_city_card' => $this->input->post('geofence_area_city_card'),
'geofence_area_id' => $this->input->post('geofence_area_card'),
'address_id' => $this->input->post('address_id'),
'title' => $this->input->post('title'),
'address' => $this->input->post('address'),
'city_filter' => $this->input->post('city_filter'),
'geo_area_filter' => $this->input->post('geo_area_filter'),
'country_filter' => $this->input->post('country_filter'),
];
}
public function create()
{
$this->load->helper(array('form', 'url'));
$this->load->database();
$this->load->library('form_validation');
$this->setFormRuleCreate();
$params = $this->getFormValue();
if ($this->validateForm($params) === FALSE) {
return;
}
if (!$this->geofence_area_anchor->insertGeoAreaAnchor($params)) {
$this->viewData['msg'] = "Insert Failed";
} else {
$this->viewData['msg'] = "Insert Succesful";
}
$this->renderToolsPage('view_geofence_area_anchor', $this->viewData);
}
public function loadRecord()
{
$rowno = $this->input->get('rowno');
$filters = $this->input->get('filters');
$filters = $filters ? $filters : [];
$filters = array_filter($filters, function ($ele) {
return $ele !== '';
});
// Row per page
$rowperpage = 10;
$cur_page = $rowno;
// Row position
if ($rowno != 0) {
$rowno = ($rowno - 1) * $rowperpage;
}
// All records count
$allcount = count($this->geofence_area_anchor->getGeoAreaAnchorRecord($filters));
// Get records
$record = $this->geofence_area_anchor->getGeoAreaAnchorRecord($filters, $rowperpage, $rowno);
// Pagination Configuration
$config['base_url'] = '/Geofence_area_anchor/loadRecord';
$config['use_page_numbers'] = TRUE;
$config['total_rows'] = $allcount;
$config['per_page'] = $rowperpage;
$config['cur_page'] = $cur_page;
$config['suffix'] = '?' . http_build_query($filters);
$config['first_url'] = '/Geofence_area_anchor/loadRecord/0?' . http_build_query($filters);
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] = "</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
// Initialize
$this->pagination->initialize($config);
// Initialize $data Array
$data['pagination'] = $this->pagination->create_links();
$data['result'] = $record;
$data['row'] = $rowno;
echo json_encode($data);
}
public function update($id)
{
$this->load->helper(array('form', 'url'));
$this->load->database();
$this->load->library('form_validation');
$params = $this->getFormValue();
$params = array_merge($params, [
'id' => $id
]);
$this->setFormRuleUpdate($params);
if ($this->validateForm($params) === FALSE) {
return;
}
if (!$this->geofence_area_anchor->updateGeoAreaAnchor($params)) {
$this->viewData['msg'] = "Update Failed";
} else {
$this->viewData['msg'] = "Updated Succesful";
}
$this->renderToolsPage('view_geofence_area_anchor', $this->viewData);
}
public function validateForm(&$params)
{
$this->form_validation->set_data($params);
if ($this->form_validation->run() == false) {
$this->viewData = array_merge($this->viewData, $params);
$this->viewData['geofence_area_city_card'] = $this->combo_model->getCityCombo('geofence_area_city_card', $params['geofence_area_city_card'] ?? '');
$this->viewData['geofence_area_card'] = $this->combo_model->getGeofenceAreaCombo('geofence_area_card', $params['geofence_area_id'] ?? '');
// Rerender filter if validate fail
$this->viewData['geofence_area_filter_card'] = $this->combo_model->getCityCombo('geofence_area_filter_card', $params['geo_area_filter'] ?? '');
$this->viewData['city_card'] = $this->combo_model->getCityCombo('city_card', $params['city_filter'] ?? '');
$this->viewData['country_card'] = $this->combo_model->getCountryCombo('country_card', $params['country_filter'] ?? '');
$this->viewData['msg'] = validation_errors();
$this->renderToolsPage('view_geofence_area_anchor', $this->viewData);
return FALSE;
}
unset($params['geofence_area_city_card']);
unset($params['address']);
unset($params['city_filter']);
unset($params['geo_area_filter']);
unset($params['country_filter']);
return TRUE;
}
public function setFormRuleCreate()
{
$this->form_validation->set_rules('geofence_area_id', 'Geofence Area', 'required|numeric|callback_isExistsGeofenceArea');
$this->form_validation->set_rules('address_id', 'Address', 'required|numeric|callback_isExistsAddress|callback_isExistsGeoAreaAndAdress');
$this->form_validation->set_rules('title', 'Title', 'max_length[100]');
}
public function setFormRuleUpdate($params)
{
// prevent wrong id of geofene_area_anchor
$this->form_validation->set_rules('id', 'Geofence Area Anchor', 'required|numeric|callback_isExistsGeofenceAreaAnchor');
$this->form_validation->set_rules('geofence_area_id', 'Geofence Area', 'required|numeric|callback_isExistsGeofenceArea');
$this->form_validation->set_rules('title', 'Title', 'max_length[100]');
// prevent duplication of unique key
$old_geo_area_id = $this->input->post('old_geofence_area_id');
$old_address_id = $this->input->post('old_address_id');
if ($old_geo_area_id === $params['geofence_area_id'] && $old_address_id === $params['address_id']) {
$this->form_validation->set_rules('address_id', 'Address', 'required|numeric|callback_isExistsAddress');
} else {
$this->form_validation->set_rules('address_id', 'Address', 'required|numeric|callback_isExistsAddress|callback_isExistsGeoAreaAndAdress');
}
}
private function setFormRuleDelete()
{
$this->form_validation->set_rules('id', 'Geofence Area Anchor', 'required|numeric|callback_isExistsGeofenceAreaAnchor');
}
public function destroy($id = null)
{
$this->load->helper(array('form', 'url'));
$this->load->database();
$this->load->library('form_validation');
$params = ['id' => $id];
$this->setFormRuleDelete();
if ($this->validateForm($params) === FALSE) {
return;
}
if (!$this->geofence_area_anchor->deleteGeoAreaAnchor($id)) {
$this->viewData['msg'] = "Delete Failed";
} else {
$this->viewData['msg'] = "Delete Succesful";
}
$this->renderToolsPage('view_geofence_area_anchor', $this->viewData);
}
public function getLocationByAddress()
{
$location = $this->address->getAddresses([
'address' => $this->input->get('address'),
'page' => $this->input->get('page')
]);
echo json_encode([
'data' => $location['list'],
'total' => $location['total']
]);
}
public function isExistsAddress($id)
{
if (!$this->address->getAddresses(['id' => $id])) {
$this->form_validation->set_message('isExistsAddress', 'Please enter an existing address');
return FALSE;
} else {
return TRUE;
}
}
public function isExistsGeofenceArea($id)
{
if (!$this->geofence_area->getLocationByID($id)) {
$this->form_validation->set_message('isExistsGeofenceArea', 'Please enter an existing geofence area');
return FALSE;
} else {
return TRUE;
}
}
public function isExistsGeofenceAreaAnchor($id)
{
if (!$this->geofence_area_anchor->getLocationByID($id)) {
$this->form_validation->set_message('isExistsGeofenceAreaAnchor', 'Please enter an existing geofence area anchor');
return FALSE;
} else {
return TRUE;
}
}
public function isExistsGeoAreaAndAdress()
{
$geo_area_id = $this->input->post('geofence_area_card');
$address_id = $this->input->post('address_id');
if (!$geo_area_id || !$address_id) {
$this->form_validation->set_message('isExistsGeoAreaAndAdress', '');
return FALSE;
}
if ($this->geofence_area_anchor->getRecordByGeoAreaAndAddress([
'geofence_area_id' => $geo_area_id,
'address_id' => $address_id
])) {
$this->form_validation->set_message('isExistsGeoAreaAndAdress', 'The data already input');
return FALSE;
} else {
return TRUE;
}
}
public function loadGeoAreaByCity($city_id = NULL)
{
if (is_numeric($city_id) === FALSE) {
echo json_encode([]);
return;
}
echo json_encode($this->geofence_area->getLocationByCityID($city_id));
}
public function loadCityByCountry($country_id = NULL) {
$condition="country='" . pg_escape_string($country_id) . "'";
echo json_encode(['contries' => $this->geofence_area_city->getCities($condition)]);
}
}