1811 lines
74 KiB
PHP
1811 lines
74 KiB
PHP
<?php
|
|
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class Bkoadmin extends Admin_Controller
|
|
{
|
|
public $template = array(
|
|
'table_open' => "<table datatable-scroll-y class='table table-striped table-hover table-bordered table-condensed'>",
|
|
'thead_open' => '<thead class=\'bg-indigo\'>',
|
|
'thead_close' => '</thead>',
|
|
'heading_row_start' => '<tr>',
|
|
'heading_row_end' => '</tr>',
|
|
'heading_cell_start' => '<th>',
|
|
'heading_cell_end' => '</th>',
|
|
'tbody_open' => '<tbody>',
|
|
'tbody_close' => '</tbody>',
|
|
'row_start' => '<tr>',
|
|
'row_end' => '</tr>',
|
|
'cell_start' => '<td>',
|
|
'cell_end' => '</td>',
|
|
'row_alt_start' => '<tr>',
|
|
'row_alt_end' => '</tr>',
|
|
'cell_alt_start' => '<td>',
|
|
'cell_alt_end' => '</td>',
|
|
'table_close' => '</table>',
|
|
);
|
|
|
|
public function index()
|
|
{
|
|
$this->load->helper('url');
|
|
$data = array();
|
|
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
$data['backoffice_users'] = "";
|
|
$mysql = "SELECT '<button type=\"button\" class=\"btn\">Edit</button>' AS edit,firstname,lastname,email,username,status,last_login,loc FROM bko_users ORDER by username ASC";
|
|
$query = $this->read_replica->query($mysql);
|
|
//$this->table->set_heading( array('data' => 'ID', 'style' => 'width:50px'),'Email', 'Firstname', 'Lastname','Last Login','Location', array('data' => 'ACTION', 'style' => 'width:40px'), array('data' => 'Select', 'style' => 'width:40px'));
|
|
$data['backoffice_users'] = $this->table->generate($query);
|
|
|
|
$this->renderAdminPage('view_admin_users', $data);
|
|
// echo 'Ameye Olu';
|
|
}
|
|
|
|
protected function renderAdminPage($page_name, $data)
|
|
{
|
|
$this->load->view('admin/view_admin_header', $data);
|
|
$this->load->view('admin/' . $page_name, $data);
|
|
$this->load->view('admin/view_admin_footer', $data);
|
|
}
|
|
|
|
public function profile()
|
|
{
|
|
global $savvyext;
|
|
$data = array();
|
|
$data['storage'] = $savvyext->cfgReadChar('system.storage_url');
|
|
|
|
//$data['js'] = array('https://maps.googleapis.com/maps/api/js?key=AIzaSyDvjiRTxngOQyBP4zpqFlZuiquc0ROvo9c&callback=initMap');
|
|
$this->renderAdminPage('cardimages', $data);
|
|
}
|
|
|
|
public function offers()
|
|
{
|
|
global $savvyext;
|
|
//echo "";
|
|
$data = array();
|
|
$data['storage'] = $savvyext->cfgReadChar('system.storage_url');
|
|
|
|
$data["images"] = array();
|
|
$this->renderAdminPage('view_offers', $data);
|
|
}
|
|
|
|
public function globals()
|
|
{
|
|
$this->load->model('combo_model');
|
|
$this->load->model('bkoadmin_model');
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
$data = array("message" => "");
|
|
if ($this->input->post()) {
|
|
$data = $this->countryImageUpload($data);
|
|
}
|
|
$data["images"] = array();
|
|
$data["page_title"] = "Country";
|
|
|
|
$params = [];
|
|
$params = $this->input->get();
|
|
|
|
$query = $this->bkoadmin_model->getGlobalSettingsQuery($params);
|
|
$tableData = $this->returnAdminTable(
|
|
[
|
|
'count_query' => $query,
|
|
'query' => $query,
|
|
],
|
|
'/bkoadmin/globals',
|
|
[
|
|
'per_page' => 20,
|
|
'reuse_query_string' => TRUE,
|
|
]
|
|
);
|
|
|
|
$data['status_dropdown'] = $this->combo_model->getStatusComboWithAll('status', $params['status'] ?? -1);
|
|
$data['filterData'] = $params;
|
|
$data['global_table'] = $tableData['output_table'];
|
|
$data['links'] = $tableData['links'];
|
|
|
|
$this->renderAdminPage('view_globals', $data);
|
|
}
|
|
|
|
public function appsettings()
|
|
{
|
|
$this->load->model('bkoadmin_model');
|
|
|
|
$data = array("message" => "");
|
|
if ($this->input->post()) {
|
|
$data = $this->countryImageUpload($data);
|
|
}
|
|
$data["images"] = array();
|
|
$data["page_title"] = "Country";
|
|
|
|
$params = [];
|
|
$params = $this->input->get();
|
|
|
|
$this->load->library('table');
|
|
$this->table->set_heading(
|
|
['data' => 'Order', 'style' => 'width:10px'],
|
|
['data' => 'Key', 'style' => 'width:50px'],
|
|
['data' => 'Name', 'style' => 'width:120px'],
|
|
'Value',
|
|
['data' => 'Update', 'style' => 'width:50px']
|
|
);
|
|
|
|
$query = $this->bkoadmin_model->getAppSettingsQuery($params);
|
|
$tableData = $this->returnAdminTable(
|
|
[
|
|
'count_query' => $query,
|
|
'query' => $query,
|
|
],
|
|
'/bkoadmin/appsettings',
|
|
[
|
|
'per_page' => 20,
|
|
'reuse_query_string' => TRUE,
|
|
]
|
|
);
|
|
|
|
$data['filterData'] = $params;
|
|
$data['app_setttings_table'] = $tableData['output_table'];
|
|
$data['links'] = $tableData['links'];
|
|
$this->renderAdminPage('view_app_settings', $data);
|
|
}
|
|
|
|
public function getValueOfPoints() {
|
|
return [
|
|
'from_value' => trim($this->input->get('from_value') ?? ''),
|
|
'to_value' => trim($this->input->get('to_value') ?? ''),
|
|
'key' => trim($this->input->get('key') ?? ''),
|
|
'name' => trim($this->input->get('name') ?? ''),
|
|
'from_date' => trim($this->input->get('from_date') ?? ''),
|
|
'to_date' => trim($this->input->get('to_date') ?? ''),
|
|
'activated' => trim($this->input->get('card_activated')
|
|
?? ($this->input->get('activated') ?? -1)),
|
|
];
|
|
}
|
|
|
|
public function setComboForPoints($params) {
|
|
$this->load->model('combo_model');
|
|
|
|
$combo['card_activated'] = $this->combo_model->getStatusComboFromZeroToNine(
|
|
'card_activated',
|
|
$params['activated']
|
|
);
|
|
|
|
return $combo;
|
|
}
|
|
|
|
public function getValueCombo($val) {
|
|
$status_value = range(0, 9);
|
|
return in_array($val, $status_value)
|
|
? $val
|
|
: '';
|
|
}
|
|
|
|
public function setFormRuleForPoints() {
|
|
$status_pattern = 'regex_match[/^(?:[0-9])$/]';
|
|
$date_pattern = 'regex_match[/\d{4}-\d{2}-\d{2}/]';
|
|
|
|
$this->form_validation->set_rules('from_value', 'Value', 'numeric');
|
|
$this->form_validation->set_rules('to_value', 'Value', 'numeric');
|
|
$this->form_validation->set_rules('from_date', 'Created date', $date_pattern);
|
|
$this->form_validation->set_rules('to_date', 'Created date', $date_pattern);
|
|
$this->form_validation->set_rules('activated', 'Activated', $status_pattern);
|
|
}
|
|
|
|
public function validateValueForPoints($params) {
|
|
$this->load->library('form_validation');
|
|
$this->form_validation->set_data($params);
|
|
$this->setFormRuleForPoints();
|
|
|
|
$errors = [];
|
|
if ($this->form_validation->run() === FALSE) {
|
|
$errors = $this->form_validation->error_array();
|
|
}
|
|
return $errors;
|
|
}
|
|
|
|
public function load_pagination($all_record, $params, $action) {
|
|
// pagination
|
|
$this->load->library('pagination');
|
|
$config["total_rows"] = count($all_record);
|
|
$config["base_url"] = base_url() . "/bkoadmin/" . $action;
|
|
$config["per_page"] = 10;
|
|
$config["uri_segment"] = 3;
|
|
$config["num_links"] = 5;
|
|
$config["suffix"] =
|
|
"?"
|
|
. http_build_query($params);
|
|
$config["first_url"] =
|
|
"/bkoadmin/{$action}/0?"
|
|
. http_build_query($params);
|
|
$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>";
|
|
|
|
$this->pagination->initialize($config);
|
|
$page = ( $this->uri->segment(3) ) ? $this->uri->segment(3) : 0;
|
|
$offset = is_numeric($page) ? $page : 0;
|
|
|
|
return [
|
|
'link' => $this->pagination->create_links(),
|
|
'offset' => $offset,
|
|
'limit' => $config["per_page"]
|
|
];
|
|
}
|
|
|
|
public function points()
|
|
{
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
$data = array("message" => "");
|
|
if ($this->input->post()) {
|
|
$data = $this->countryImageUpload($data);
|
|
}
|
|
$data["images"] = array();
|
|
$data["page_title"] = "Points Settings";
|
|
|
|
$this->load->model('bkoadmin_model');
|
|
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
$this->table->set_heading([
|
|
['data' => 'ID', 'style' => 'width:10px'],
|
|
['data' => 'Order', 'style' => 'width:10px'],
|
|
['data' => 'Key', 'style' => 'width:50px'],
|
|
'Name',
|
|
['data' => 'Value', 'style' => 'width:120px'],
|
|
['data' => 'Update', 'style' => 'width:50px'],
|
|
['data' => 'Status', 'style' => 'width:10px'],
|
|
['data' => 'Added', 'style' => 'width:50px'],
|
|
['data' => 'Activated', 'style' => 'width:25px']
|
|
]);
|
|
|
|
$params = $this->getValueOfPoints();
|
|
|
|
$data = array_merge($data, $this->setComboForPoints($params));
|
|
$params['activated'] = $this->getValueCombo($params['activated']);
|
|
|
|
$params = array_filter($params, function($ele) {
|
|
return $ele !== "";
|
|
});
|
|
$errors = $this->validateValueForPoints($params);
|
|
$params = array_diff_key($params, $errors);
|
|
|
|
$data = array_merge(
|
|
$data,
|
|
$params,
|
|
$this->load_pagination(
|
|
$this->bkoadmin_model->get_point_records($params),
|
|
$params,
|
|
'points'
|
|
)
|
|
);
|
|
$data['points_table'] = $this->table->generate(
|
|
$this->bkoadmin_model->get_point_records(
|
|
$params,
|
|
$data['limit'],
|
|
$data['offset']
|
|
)
|
|
);
|
|
|
|
$this->renderAdminPage('view_points', $data);
|
|
}
|
|
|
|
public function pointsupdate()
|
|
{
|
|
$id = (int) $this->input->get('id');
|
|
$points = (int) $this->input->get('value');
|
|
$q = "UPDATE points_settings SET value=${points} WHERE id=${id} RETURNING *";
|
|
$r = $this->db->query($q);
|
|
if ($r->num_rows()) {
|
|
echo "Point settings updated!";
|
|
} else {
|
|
echo "Failed to update point settings!";
|
|
}
|
|
}
|
|
|
|
public function cards()
|
|
{
|
|
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
//echo "";
|
|
$data = array();
|
|
$data["page_title"] = "Cards";
|
|
$data["form_button"] = "Add";
|
|
$data["images"] = array();
|
|
|
|
$this->load->model('combo_model');
|
|
|
|
$data['card_category_vaue'] = trim($this->input->post('card_category'));
|
|
$data['card_can_save_value'] = trim($this->input->post('card_can_save'));
|
|
$data['card_pictures_value'] = trim($this->input->post('card_pictures'));
|
|
$data['card_status_value'] = trim($this->input->post('card_status'));
|
|
$data['card_template_value'] = trim($this->input->post('card_template'));
|
|
$data['card_canexpire_value'] = trim($this->input->post('card_canexpire'));
|
|
$data['card_notify_value'] = trim($this->input->post('card_notify'));
|
|
$data['card_country_value'] = trim($this->input->post('card_country'));
|
|
$data['card_titleshow_value'] = trim($this->input->post('card_titleshow'));
|
|
|
|
$card_category = ($this->uri->segment(3)) ? $this->uri->segment(3) : '0';
|
|
if ($card_category != '' && $card_category != '0') {
|
|
$data['card_category_vaue'] = trim($card_category);
|
|
} else {
|
|
$card_category = $data['card_category_vaue'];
|
|
}
|
|
|
|
$data['card_category'] = $this->combo_model->getCardCategoryCombo('card_category', $data['card_category_vaue']);
|
|
$data['card_can_save'] = $this->combo_model->getYesNoCombo('card_can_save', $data['card_can_save_value']);
|
|
$data['card_pictures'] = $this->combo_model->getCardPicturesCombo('card_pictures', $data['card_pictures_value']);
|
|
$data['card_status'] = $this->combo_model->getStatusCombo('card_status', $data['card_status_value']);
|
|
$data['card_template'] = $this->combo_model->getCardTemplateCombo('card_template', $data['card_template_value']);
|
|
$data['card_canexpire'] = $this->combo_model->getYesNoCombo('card_canexpire', $data['card_canexpire_value']);
|
|
$data['card_notify'] = $this->combo_model->getYesNoCombo('card_notify', $data['card_notify_value']);
|
|
$data['card_country'] = $this->combo_model->getCountryCombo('card_country', $data['card_country_value']);
|
|
|
|
$data['card_titleshow'] = $this->combo_model->getTitleShowCombo('card_titleshow', $data['card_titleshow_value']);
|
|
|
|
$data['card_name'] = trim($this->input->post('card_name'));
|
|
$data['card_title'] = trim($this->input->post('card_title'));
|
|
$data['short_title'] = trim($this->input->post('short_title'));
|
|
$data['description'] = trim($this->input->post('description'));
|
|
$data['background_picture'] = trim($this->input->post('background_picture'));
|
|
$data['button1'] = trim($this->input->post('button1'));
|
|
$data['button1_text'] = trim($this->input->post('button1_text'));
|
|
$data['button1_action'] = trim($this->input->post('button1_action'));
|
|
$data['card_expiration'] = trim($this->input->post('card_expiration'));
|
|
//$data['card_template'] = trim($this->input->post('card_template'));
|
|
|
|
$message = '';
|
|
if ($_POST) {
|
|
$id = $this->input->post('id');
|
|
if ($id > 0) {
|
|
$in['action'] = SAVVY_BKO_MAINCARD_UPDATE;
|
|
$in['id'] = $id;
|
|
$data["form_button"] = "Update";
|
|
$data["card_id"] = $id;
|
|
} else {
|
|
$in['action'] = SAVVY_BKO_MAINCARD_CREATE;
|
|
}
|
|
//SAVVY_BKO_MAINCARD_UPDATE
|
|
$in['name'] = $data['card_name'];
|
|
$in['title'] = $data['card_title'];
|
|
$in['short_title'] = $data['short_title'];
|
|
$in['description'] = $data['description'];
|
|
$in['background_picture'] = $data['card_pictures_value'];
|
|
$in['button1'] = $data['button1'];
|
|
$in['button1_text'] = $data['button1_text'];
|
|
$in['button1_action'] = $data['card_category_vaue']; // note category drives this action
|
|
$in['status'] = (int) $data['card_status_value'];
|
|
$in['can_save'] = (int) $data['card_can_save_value'];
|
|
$in['template'] = (int) $data['card_template_value'];
|
|
$in['card_canexpire'] = (int) $data['card_canexpire_value'];
|
|
$in['card_expiration'] = $data['card_expiration'];
|
|
$in['card_notify'] = (int) $data['card_notify_value'];
|
|
$in['titleshow'] = (int) $data['card_titleshow_value'];
|
|
|
|
if ($in['name'] == '' || strlen($in['name']) > 25) {
|
|
$message .= "<br/>Invalid name";
|
|
}
|
|
if ($in['title'] == '' || strlen($in['title']) > 100) {
|
|
$message .= "<br/>Invalid title";
|
|
}
|
|
if ($in['short_title'] == '' || strlen($in['short_title']) > 35) {
|
|
$message .= "<br/>Invalid short title";
|
|
}
|
|
if ($in['description'] == '' || strlen($in['description']) > 250) {
|
|
$message .= "<br/>Invalid description";
|
|
}
|
|
if ($in['background_picture'] == '' || strlen($in['background_picture']) > 150) {
|
|
$message .= "<br/>Invalid background picture";
|
|
}
|
|
if ($in['button1'] == '' || strlen($in['button1']) > 35) {
|
|
$message .= "<br/>Invalid button1";
|
|
}
|
|
if ($in['button1_text'] == '' || strlen($in['button1_text']) > 35) {
|
|
$message .= "<br/>Invalid button1 text";
|
|
}
|
|
if ($in['button1_action'] == '' || strlen($in['button1_action']) > 15) {
|
|
$message .= "<br/>Invalid category";
|
|
}
|
|
if ($data['card_canexpire_value'] != '' && $data['card_canexpire_value'] == 1 &&
|
|
($data['card_expiration'] == '' || strtotime($data['card_expiration']) < time())) {
|
|
$message .= "<br/>Invalid expiration date";
|
|
}
|
|
if ($message == '') {
|
|
$out = array();
|
|
$ret = $this->savvy_api($in, $out);
|
|
if ($ret == PHP_API_OK) {
|
|
$message = $id > 0 ? 'Updated!' : 'Created!';
|
|
} else {
|
|
$message = 'Failed to ' . ($id > 0 ? 'update' : 'create') . ' card: ' . $out["status"];
|
|
}
|
|
}
|
|
} else {
|
|
$in['card_expiration'] = date("Y-m-d", strtotime("+3 months"));
|
|
}
|
|
$data['message'] = $message;
|
|
|
|
$card_category = ($this->uri->segment(3)) ? $this->uri->segment(3) : '0';
|
|
|
|
$this->load->library('pagination');
|
|
$config = array();
|
|
$q = "SELECT * FROM main_cards WHERE status=1";
|
|
if ($card_category != '' && $card_category != '0') {
|
|
$q .= " AND button1_action='" . pg_escape_string($card_category) . "'";
|
|
}
|
|
$query = $this->read_replica->query($q);
|
|
$config["total_rows"] = $query->num_rows();
|
|
$config["base_url"] = '/bkoadmin/cards/' . $card_category . '/';
|
|
$config["per_page"] = 6;
|
|
$config["uri_segment"] = 4;
|
|
$config["num_links"] = 5;
|
|
$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>";
|
|
$this->pagination->initialize($config);
|
|
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
|
|
$page = is_numeric($page) ? $page : 0;
|
|
$data["links"] = $this->pagination->create_links();
|
|
|
|
$mysql = "SELECT '<button id=\"acc'||id||'\" type=\"button\" class=\"btn btn-primary btn-xs\" block onclick=\"viewCard('||id||');\">View-'||id||'</button>' ||'<br><hr>'|| "
|
|
. "'<button id=\"edit'||id||'\" type=\"button\" class=\"btn btn-warning btn-xs\" block onclick=\"editCard('||id||');\">Edit-'||id||'</button>' AS Edit,"
|
|
. " '<b>Name:</b>'|| name||'<br><b>Title:</b>'||title||'<br><b>Short Title</b>'||short_title||'<br><b>Desc :</b>'||description
|
|
||'<br><b>Button :</b>'||button1||'<br><b>Button Text :</b>'
|
|
||button1_text||'<br><b>Action :</>'||button1_action AS description, '<img onclick=\"viewCard('||id||');\" style=\"height:120px; \" src=\"'||background_picture||'\" ><br>
|
|
<div id=\"del_form'||id||'\"><a href=\"#\" onclick=\"deleteCard('||id||');\" class=\"text-danger\">archive</a></div> ' As button FROM main_cards WHERE status = 1 ";
|
|
if ($card_category != '' && $card_category != '0') {
|
|
$mysql .= " AND button1_action='" . pg_escape_string($card_category) . "'";
|
|
}
|
|
$mysql .= " ORDER BY id DESC LIMIT " . $config["per_page"] . " OFFSET " . $page;
|
|
$query = $this->read_replica->query($mysql);
|
|
$this->table->set_heading(array('data' => 'View', 'style' => 'width:50px'), 'Card', array('data' => 'Image', 'style' => 'width:120px'));
|
|
$data["card_table"] = $this->table->generate($query);
|
|
|
|
$this->renderAdminPage('view_cards', $data);
|
|
}
|
|
|
|
public function cardsettings()
|
|
{
|
|
|
|
echo 'ffffffff';
|
|
return 0;
|
|
}
|
|
|
|
//set_form
|
|
public function updatesettings()
|
|
{
|
|
//echo 'uuu';
|
|
if ($_GET) {
|
|
$setting_id = $this->input->get('setting_id');
|
|
$setting_key = $this->input->get('setting_key');
|
|
$setting_value = $this->input->get("setting_value");
|
|
|
|
$mysql = "UPDATE app_settings SET value = '" . pg_escape_string($setting_value) . "' WHERE id = " . $setting_id . " AND setting_key = '$setting_key' ";
|
|
//echo $mysql;
|
|
$this->db->query($mysql);
|
|
echo "Updated.";
|
|
} else {
|
|
echo "Invalid request.";
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function cardarchive()
|
|
{
|
|
if ($_GET) {
|
|
$card_id = $this->input->get('card_id');
|
|
$mysql = "UPDATE main_cards SET status = 0 WHERE id = " . $card_id;
|
|
// echo $sql;
|
|
$this->db->query($mysql);
|
|
echo "Archived.";
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function cardactivate()
|
|
{
|
|
if ($_GET) {
|
|
$card_id = $this->input->get('card_id');
|
|
$mysql = "UPDATE main_cards SET status = 1 WHERE id = " . $card_id;
|
|
// echo $sql;
|
|
$this->db->query($mysql);
|
|
echo "Activated.";
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function cardactions()
|
|
{
|
|
$data = array();
|
|
$data["page_title"] = "Cards";
|
|
$data["form_button"] = "Add";
|
|
$data["links"] = "";
|
|
$data["card_category"] = "";
|
|
|
|
$this->load->model('combo_model');
|
|
|
|
$message = "";
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
$data["card_action_id"] = 0;
|
|
$data["name"] = $this->input->post('name');
|
|
$data["type"] = $this->input->post('type');
|
|
$data["type_combo"] = $this->combo_model->getCardActionTypeCombo('type', $data['type']);
|
|
$data["data"] = $this->input->post('data');
|
|
if ($_POST) {
|
|
$message = 'Not implemented!';
|
|
$id = $this->input->post('id');
|
|
if ($id > 0) {
|
|
$in['action'] = SAVVY_BKO_MAINCARD_UPDATE; // TODO
|
|
$in['id'] = $id;
|
|
$data["form_button"] = "Update";
|
|
$data["card_action_id"] = $id;
|
|
} else {
|
|
$in['action'] = SAVVY_BKO_MAINCARD_CREATE; // TODO
|
|
}
|
|
}
|
|
// id, name, type, data
|
|
//echo "";
|
|
/*
|
|
<button id=\"acc'||id||'\" type=\"button\" class=\"btn btn-primary btn-xs\" block onclick=\"viewCardAction('||id||');\">View-'||id||'</button>' ||'<br><hr>'|| "
|
|
. "'<button id=\"edit'||id||'\" type=\"button\" class=\"btn btn-warning btn-xs\" block onclick=\"editCardAction('||id||');\">Edit-'||id||'</button>' AS Edit,"
|
|
. "
|
|
*/
|
|
$mysql = "SELECT id, name, type, data FROM card_actions ORDER BY name";
|
|
$query = $this->read_replica->query($mysql);
|
|
$data["card_action_table"] = $this->table->generate($query);
|
|
|
|
$data["message"] = $message;
|
|
|
|
$this->renderAdminPage('view_cardactions', $data);
|
|
}
|
|
|
|
public function transport()
|
|
{
|
|
$this->load->model('bkoadmin_model');
|
|
|
|
$data = [
|
|
"id" => "0",
|
|
"message" => "",
|
|
"name" => "",
|
|
"name_alias" => "",
|
|
"client" => "",
|
|
"token" => "",
|
|
"code" => "",
|
|
"access_token" => "",
|
|
"form_button" => "Create",
|
|
];
|
|
try {
|
|
$this->load->model('combo_model');
|
|
$data['provider_status_value'] = (int) $this->input->post('card_status');
|
|
$data['provider_status'] = $this->combo_model->getStatusCombo('card_status', $data['provider_status_value']);
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
|
|
$id = $this->input->post('id');
|
|
$data['name'] = trim($this->input->post('name'));
|
|
$name_alias = trim($this->input->post('name_alias'));
|
|
$name_alias = rtrim($name_alias,',');
|
|
$data['name_alias'] = ltrim($name_alias,',');
|
|
$data['client'] = trim($this->input->post('client'));
|
|
$data['token'] = trim($this->input->post('token'));
|
|
$data['code'] = trim($this->input->post('code'));
|
|
$data['access_token'] = trim($this->input->post('access_token'));
|
|
$data['active'] = $data['provider_status_value'];
|
|
/*
|
|
name | character varying(50) | not null
|
|
client | character varying(100) |
|
|
token | character varying(200) |
|
|
code | character varying(100) |
|
|
access_token | character varying(200) |
|
|
active | smallint | default 1
|
|
*/
|
|
if ($data['name'] == '' || strlen($data['name']) > 50) {
|
|
$data['message'] .= "<br/>Invalid name";
|
|
}
|
|
if (strlen($data['client']) > 100) {
|
|
$data['message'] .= "<br/>Invalid client ID";
|
|
}
|
|
if (strlen($data['token']) > 200) {
|
|
$data['message'] .= "<br/>Invalid token";
|
|
}
|
|
if (strlen($data['code']) > 100) {
|
|
$data['message'] .= "<br/>Invalid access code";
|
|
}
|
|
if (strlen($data['access_token']) > 200) {
|
|
$data['message'] .= "<br/>Invalid access token";
|
|
}
|
|
// deep check name_alias
|
|
if(!empty($data['name_alias'])){
|
|
list($checkAliasValid, $message) = $this->fnValidateNameAlias($data['name_alias'], $id);
|
|
if(!$checkAliasValid){
|
|
$data['message'] .= '<br/>- '.$message;
|
|
}
|
|
}
|
|
if ($id > 0) {
|
|
// Update
|
|
$q = "UPDATE transport_providers SET name='" . pg_escape_string($data['name']) . "',
|
|
name_alias='".pg_escape_string($data['name_alias'])."',
|
|
client='" . pg_escape_string($data['client']) . "',token='" . pg_escape_string($data['token']) . "',
|
|
code='" . pg_escape_string($data['code']) . "',access_token='" . pg_escape_string($data['access_token']) . "',
|
|
active=" . $data["active"] . " WHERE id=${id} RETURNING id";
|
|
} else {
|
|
// Insert
|
|
$q = "INSERT INTO transport_providers (name,name_alias,client,token,code,access_token,active) VALUES(
|
|
'" . pg_escape_string($data['name']) . "',
|
|
'" . pg_escape_string($data['name_alias']) . "',
|
|
'" . pg_escape_string($data['client']) . "',
|
|
'" . pg_escape_string($data['token']) . "',
|
|
'" . pg_escape_string($data['code']) . "',
|
|
'" . pg_escape_string($data['access_token']) . "',
|
|
" . $data["active"] . "
|
|
) RETURNING id";
|
|
}
|
|
if ($data['message'] == "") {
|
|
$r = $this->db->query($q);
|
|
$f = $r->row_array();
|
|
if ($f != null && isset($f["id"]) && $f["id"] > 0) {
|
|
$data["id"] = $f["id"];
|
|
$data['message'] = 'Provider ' . ($id > 0 ? 'updated' : 'created') . '!';
|
|
$data["form_button"] = 'Update';
|
|
} else {
|
|
$data['message'] = 'Failed to ' . ($id > 0 ? 'update' : 'create') . ' provider!';
|
|
}
|
|
}
|
|
} else {
|
|
$data['provider_status'] = $this->combo_model->getStatusCombo('card_status', 1);
|
|
}
|
|
|
|
$params = [];
|
|
$params = $this->input->get();
|
|
|
|
$this->load->library('table');
|
|
$this->table->set_heading(
|
|
['data' => 'ID', 'style' => 'width:50px'],
|
|
['data' => 'Name', 'style' => 'width:190px'],
|
|
'Credentials',
|
|
['data' => 'Active?', 'style' => 'width:50px'],
|
|
['data' => 'Action', 'style' => 'width:90px']
|
|
);
|
|
|
|
$query = $this->bkoadmin_model->getTransportProviderQuery($params);
|
|
$tableData = $this->returnAdminTable(
|
|
[
|
|
'count_query' => $query,
|
|
'query' => $query,
|
|
],
|
|
'bkoadmin/transport',
|
|
[
|
|
'per_page' => 20,
|
|
'reuse_query_string' => TRUE,
|
|
]
|
|
);
|
|
|
|
$data['filterData'] = $params;
|
|
$data['transporter_table'] = $tableData['output_table'];
|
|
$data['transporter_links'] = $tableData['links'];
|
|
|
|
$data["transport_providers"] = [];
|
|
$q = "SELECT * FROM transport_providers";
|
|
$r = $this->read_replica->query($q);
|
|
foreach ($r->result() as $row) {
|
|
$data["transport_providers"][$row->{'id'}] = (array) $row;
|
|
}
|
|
$q = "select b.name,a.country,a.ios_app_id,a.android_app_id,concat('<button type=\"button\" class=\"btn btn-info btn-xs\" onclick=\"document.location=''/bkoadmin/transportApps?id=',b.id,'&app_id=',a.id,''';return false;\">Edit</button>') as Action
|
|
from transport_provider_apps a, transport_providers b where b.id=a.transport_provider_id order by b.name,a.country";
|
|
$r = $this->read_replica->query($q);
|
|
$data["transporter_apps"] = $this->table->generate($r);
|
|
} catch (Exception $e) {
|
|
$data["transporter_table"] = "";
|
|
$data["transporter_apps"] = "";
|
|
$data["message"] = $e->getMessage();
|
|
}
|
|
$this->renderAdminPage('view_transporter', $data);
|
|
return 0;
|
|
}
|
|
|
|
public function transportApps()
|
|
{
|
|
$data = [];
|
|
$id = (int) $this->input->get('id');
|
|
$app_id = (int) $this->input->get('app_id');
|
|
if ($id > 0) {
|
|
$q = "SELECT * FROM transport_providers WHERE id=${id}";
|
|
$r = $this->read_replica->query($q);
|
|
$f = $r->row_array();
|
|
if ($f != null && isset($f["id"]) && $f["id"] > 0) {
|
|
$data = $f;
|
|
}
|
|
}
|
|
if (count($data) < 1) {
|
|
$this->load->helper('url');
|
|
redirect('/bkoadmin/transport', 'refresh');
|
|
return 0;
|
|
}
|
|
$data["message"] = "";
|
|
$data["form_button"] = "Add";
|
|
$data["app_id"] = $app_id;
|
|
$data["country"] = "SG";
|
|
$data["ios_app_id"] = "";
|
|
$data["android_app_id"] = "";
|
|
|
|
if ($_POST) {
|
|
$data['country_value'] = trim($this->input->post('country'));
|
|
$data['ios_app_id'] = trim($this->input->post('ios_app_id'));
|
|
$data['android_app_id'] = trim($this->input->post('android_app_id'));
|
|
|
|
if (strlen($data['country_value']) != 2) {
|
|
$data['message'] .= "<br/>Invalid country";
|
|
}
|
|
if (strlen($data['ios_app_id']) > 100) {
|
|
$data['message'] .= "<br/>Invalid iOS app ID";
|
|
}
|
|
if (strlen($data['android_app_id']) > 100) {
|
|
$data['message'] .= "<br/>Invalid Android app ID";
|
|
}
|
|
$id = (int) $this->input->post('id');
|
|
$app_id = (int) $this->input->post('app_id');
|
|
if ($app_id > 0) {
|
|
// Update
|
|
$q = "UPDATE transport_provider_apps SET country='" . pg_escape_string($data["country_value"]) . "',
|
|
ios_app_id='" . pg_escape_string($data["ios_app_id"]) . "',android_app_id='" . pg_escape_string($data["android_app_id"]) . "'
|
|
WHERE id=${app_id} AND transport_provider_id=${id} RETURNING id";
|
|
} else {
|
|
// Insert
|
|
$q = "INSERT INTO transport_provider_apps (transport_provider_id,country,ios_app_id,android_app_id) VALUES(
|
|
${id},
|
|
'" . pg_escape_string($data["country_value"]) . "',
|
|
'" . pg_escape_string($data["ios_app_id"]) . "',
|
|
'" . pg_escape_string($data["android_app_id"]) . "'
|
|
) RETURNING id";
|
|
}
|
|
if ($data['message'] == "") {
|
|
$r = $this->db->query($q);
|
|
$f = $r->row_array();
|
|
if ($f != null && isset($f["id"]) && $f["id"] > 0) {
|
|
$data["app_id"] = $f["id"];
|
|
$data['message'] = 'Provider app ' . ($id > 0 ? 'updated' : 'created') . '!';
|
|
$data["form_button"] = 'Update';
|
|
} else {
|
|
$data['message'] = 'Failed to ' . ($id > 0 ? 'update' : 'create') . ' provider app!';
|
|
}
|
|
}
|
|
} else {
|
|
$data['country_value'] = 'SG';
|
|
}
|
|
|
|
$this->load->model('combo_model');
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
$data["country_select"] = $this->combo_model->getCountryCombo('country', $data['country_value']);
|
|
|
|
$q = "SELECT country,ios_app_id AS \"iOS App ID\",android_app_id AS \"Android App ID\",'<input type=\"button\" class=\"btn btn-info btn-xs\" onclick=\"return EditTransportProviderApp('||id||');\" value=\"Edit\" />' as action FROM transport_provider_apps WHERE transport_provider_id=${id}";
|
|
$r = $this->read_replica->query($q);
|
|
$data["transporter_apps"] = $this->table->generate($r);
|
|
$data["transport_apps"] = [];
|
|
$q = "SELECT * FROM transport_provider_apps WHERE transport_provider_id=${id}";
|
|
$r = $this->read_replica->query($q);
|
|
foreach ($r->result() as $row) {
|
|
$data["transport_apps"][$row->{'id'}] = (array) $row;
|
|
}
|
|
$this->renderAdminPage('view_transporter_apps', $data);
|
|
return 0;
|
|
}
|
|
|
|
public function viewcard()
|
|
{
|
|
|
|
$card_id = $this->input->get('card_id');
|
|
|
|
if ($card_id != '' && $card_id > 0) {
|
|
|
|
$mysql = "SELECT * FROM main_cards WHERE id = $card_id";
|
|
$query = $this->read_replica->query($mysql);
|
|
$selected_card = $query->row_array();
|
|
|
|
echo "<table style=\"width: 414px !important; height:736px !important; background-color:aliceblue;\">"
|
|
. "<tr style='height:50%; background-size: contain; background-image:url(" . $selected_card["background_picture"] . "); background-repeat: no-repeat; '><td style='text-align:top;'> </td></tr>"
|
|
. "<tr style='height:10%;'><td><h2>" . $selected_card["title"] . "</h2></td></tr>"
|
|
. "<tr style=' vertical-align: top; padding:10px;'><td> <h4>" . $selected_card["description"] . "</h4></td></tr>"
|
|
. "<tr><td style='height: 80px; background-color: rgb(55, 117, 187); text-align: center; vertical-align: middle; color:white;'><h3>" . $selected_card["button1_text"] . "
|
|
</h3></td></tr>"
|
|
. "</table>";
|
|
|
|
// print_r( $selected_card );
|
|
}
|
|
}
|
|
|
|
public function editcard()
|
|
{
|
|
|
|
$data = array();
|
|
$card_id = $this->input->get('card_id');
|
|
|
|
if ($card_id != '' && $card_id > 0) {
|
|
|
|
$mysql = "SELECT * FROM main_cards WHERE id = $card_id";
|
|
$query = $this->read_replica->query($mysql);
|
|
$selected_card = $query->row_array();
|
|
|
|
$data["images"] = array();
|
|
$data["form_button"] = "Update";
|
|
|
|
$this->load->model('combo_model');
|
|
|
|
$data['card_category_value'] = $selected_card['button1_action'];
|
|
$data['card_can_save_value'] = $selected_card['can_save'];
|
|
$data['card_pictures_value'] = $selected_card['background_picture'];
|
|
$data['card_status_value'] = $selected_card['status'];
|
|
$data['card_template_value'] = $selected_card['template'];
|
|
$data['card_canexpire_value'] = $selected_card['card_canexpire'];
|
|
|
|
$data['card_notify_value'] = $selected_card['notify'];
|
|
$data['card_country_value'] = $selected_card['card_country'];
|
|
|
|
$data['card_titleshow_value'] = $selected_card['titleshow'];
|
|
|
|
$data['card_category'] = $this->combo_model->getCardCategoryCombo('card_category', $data['card_category_value']);
|
|
$data['card_can_save'] = $this->combo_model->getYesNoCombo('card_can_save', $data['card_can_save_value']);
|
|
$data['card_pictures'] = $this->combo_model->getCardPicturesCombo('card_pictures', $data['card_pictures_value']);
|
|
$data['card_status'] = $this->combo_model->getStatusCombo('card_status', $data['card_status_value']);
|
|
$data['card_template'] = $this->combo_model->getCardTemplateCombo('card_template', $data['card_template_value']);
|
|
$data['card_canexpire'] = $this->combo_model->getYesNoCombo('card_canexpire', $data['card_canexpire_value']);
|
|
|
|
$data['card_notify'] = $this->combo_model->getYesNoCombo('card_notify', $data['card_notify_value']);
|
|
$data['card_country'] = $this->combo_model->getCountryCombo('card_country', $data['card_country_value']);
|
|
|
|
$data['card_titleshow'] = $this->combo_model->getTitleShowCombo('card_titleshow', $data['card_titleshow_value']);
|
|
|
|
$data['card_name'] = $selected_card['name'];
|
|
$data['card_title'] = $selected_card['title'];
|
|
$data['short_title'] = $selected_card['short_title'];
|
|
$data['description'] = $selected_card['description'];
|
|
$data['background_picture'] = $selected_card['background_picture'];
|
|
$data['button1'] = $selected_card['button1'];
|
|
$data['button1_text'] = $selected_card['button1_text'];
|
|
$data['button1_action'] = $selected_card['button1_action'];
|
|
$data['card_expiration'] = $selected_card['card_expiration'];
|
|
|
|
$data['card_id'] = $card_id;
|
|
|
|
$this->load->view('admin/common/card_form', $data);
|
|
}
|
|
}
|
|
|
|
public function mytransport()
|
|
{
|
|
$data = array();
|
|
$data["images"] = array();
|
|
$this->renderAdminPage('view_mytransport', $data);
|
|
}
|
|
|
|
public function compare()
|
|
{
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
$show = false;
|
|
$data_from = 2; //"Marina Bay Sands, 10 Bayfront Ave, Singapore 018956";
|
|
$data_to = 3; //"97 Meyer Road, Singapore";
|
|
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
|
|
$get_from = (int) $this->input->get('from');
|
|
$get_to = (int) $this->input->get('to');
|
|
if ($get_from > 0 && $get_to > 0) {
|
|
$data_from = $get_from;
|
|
$data_to = $get_to;
|
|
$_POST['from'] = $get_from;
|
|
$_POST['to'] = $get_to;
|
|
$_POST['all'] = 1;
|
|
$_POST['daterange'] = date("Y-m-1", strtotime("-2 months")) . ' - ' . date("Y-m-d");
|
|
$show = true;
|
|
}
|
|
}
|
|
|
|
$data = array();
|
|
$data["from"] = $data_from;
|
|
$data["to"] = $data_to;
|
|
$data["all"] = 1;
|
|
$data["date_from"] = date("Y-m-1", strtotime("-2 months"));
|
|
$data["date_to"] = date("Y-m-d");
|
|
if ($this->input->post() || $show) {
|
|
$data["from"] = $this->input->post('from');
|
|
$data["to"] = $this->input->post('to');
|
|
$data["all"] = (int) $this->input->post('all');
|
|
$dates = explode(' - ', $this->input->post('daterange'));
|
|
$data["date_from"] = date("Y-m-d", strtotime($dates[0]));
|
|
$data["date_to"] = date("Y-m-d", strtotime($dates[1]));
|
|
|
|
$db_from = (int) ($data["from"]);
|
|
$db_to = (int) ($data["to"]);
|
|
|
|
$q = "select distinct
|
|
a.travel_date AS \"Travel Date\",
|
|
a.travel_date_end \"Travel Date End\",
|
|
CASE WHEN a.cost IS NULL THEN a.cost_raw ELSE ROUND(a.cost,2)::TEXT END AS \"Cost\",
|
|
a.duration AS \"Duration\",
|
|
a.distance AS \"Distance\",
|
|
a.transport_provider_id as \"Transport\",
|
|
b.name as \"Provider Name\",
|
|
'No' as \"Prefil\"
|
|
from parsedemail_item a
|
|
left join transport_providers b on (b.id=a.transport_provider_id)
|
|
where a.dup_id is null and (a.cost>0 or a.cost_raw<>'') and
|
|
a.location_start_id='${db_from}' and a.location_end_id='${db_to}'";
|
|
if ($data["all"] != 1) {
|
|
$q .= " and a.travel_date between '" . $data["date_from"] . "' and '" . $data["date_to"] . "'";
|
|
}
|
|
$q .= " order by a.travel_date";
|
|
$query = $this->read_replica->query($q);
|
|
//$this->table->set_heading(array('data' => 'Count', 'style' => 'width:10px'), 'Start Location', 'End Location');
|
|
$data['past_trips'] = $this->table->generate($query);
|
|
|
|
$bulk_data = [];
|
|
$by_vendor = [];
|
|
$day_hour = [];
|
|
foreach ($query->result() as $row) {
|
|
$bulk_data[$row->{'Travel Date'}] = $row->{'Cost'};
|
|
$by_vendor[$row->{'Travel Date'}][$row->{'Transport'}] = $row->{'Cost'};
|
|
$hr = date("H", strtotime($row->{'Travel Date'}));
|
|
if (!isset($day_hour[$hr])) {
|
|
$day_hour[$hr] = [];
|
|
}
|
|
|
|
$day_hour[$hr][] = $row->{'Cost'};
|
|
}
|
|
|
|
$q = "select distinct
|
|
to_char(a.completed, 'yyyy-mm-dd HH24:MI:SS') AS \"Quote Date\",
|
|
to_char(a.created, 'yyyy-mm-dd HH24:MI:SS') \"Request Date\",
|
|
ROUND(a.cost,2) AS \"Cost\",
|
|
'N/A' AS \"Duration\",
|
|
'N/A' AS \"Distance\",
|
|
a.transport_provider_id as \"Transport\",
|
|
d.name as \"Provider Name\",
|
|
CASE WHEN a.prefill='t' THEN 'Yes' ELSE 'No' END AS \"Prefill\"
|
|
from address b, address c, quotes a
|
|
left join transport_providers d on (d.id=a.transport_provider_id)
|
|
where a.cost>0 and a.completed is not null and
|
|
b.id=a.location_start_id and c.id=a.location_end_id and
|
|
b.id='${db_from}' and c.id='${db_to}'";
|
|
|
|
if ($data["all"] != 1) {
|
|
$q .= " and a.completed::date between '" . $data["date_from"] . "' and '" . $data["date_to"] . "'";
|
|
}
|
|
$q .= " order by \"Quote Date\"";
|
|
$query = $this->read_replica->query($q);
|
|
$data['quotes'] = $this->table->generate($query);
|
|
|
|
foreach ($query->result() as $row) {
|
|
$bulk_data[$row->{'Quote Date'}] = $row->{'Cost'};
|
|
$by_vendor[$row->{'Quote Date'}][$row->{'Transport'}] = $row->{'Cost'};
|
|
$hr = date("H", strtotime($row->{'Quote Date'}));
|
|
if (!isset($day_hour[$hr])) {
|
|
$day_hour[$hr] = [];
|
|
}
|
|
|
|
$day_hour[$hr][] = $row->{'Cost'};
|
|
}
|
|
foreach ($day_hour as $hr => $val) {
|
|
asort($val);
|
|
$pk = null;
|
|
$pv = null;
|
|
foreach ($val as $k => $v) {
|
|
if ($v == $pv) {
|
|
unset($day_hour[$hr][$k]);
|
|
} else {
|
|
$pk = $k;
|
|
$pv = $v;
|
|
}
|
|
}
|
|
}
|
|
asort($day_hour);
|
|
$data["bulk_data"] = $bulk_data;
|
|
$data["by_vendor"] = $by_vendor;
|
|
$data["day_hour"] = $day_hour;
|
|
} else {
|
|
$data['past_trips'] = "";
|
|
$data['quotes'] = "";
|
|
$data["bulk_data"] = array();
|
|
$data["by_vendor"] = array();
|
|
$data["day_hour"] = array();
|
|
}
|
|
|
|
$q = "SELECT id,address FROM address ORDER BY address";
|
|
$query = $this->read_replica->query($q);
|
|
$address = [];
|
|
$address[0] = '<<All>>';
|
|
foreach ($query->result() as $row) {
|
|
$address[$row->{"id"}] = $row->{"address"} == '' ? 'Empty address' : $row->{"address"};
|
|
}
|
|
$data["address"] = $address;
|
|
|
|
$this->renderAdminPage('view_compare', $data);
|
|
}
|
|
|
|
public function country()
|
|
{
|
|
global $savvyext;
|
|
$data['storage'] = $savvyext->cfgReadChar('system.storage_url');
|
|
//echo "";
|
|
$data["message"] = "";
|
|
$search_text = "";
|
|
$whereQuery = "1=1";
|
|
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
|
|
$page = is_numeric($page) ? $page : 0;
|
|
if ($this->input->post()) {
|
|
$data = $this->countryImageUpload($data);
|
|
$search_text = trim($this->input->post('search_text'));
|
|
$search_text = $this->read_replica->escape_like_str($search_text);
|
|
$whereQuery .= ' AND a.country ILIKE \'%' . $search_text . '%\'';
|
|
//$page =0;
|
|
}
|
|
$data["images"] = array();
|
|
$data["page_title"] = "Country";
|
|
|
|
$this->load->library('pagination');
|
|
$config = array();
|
|
$q = "SELECT * FROM country a LEFT JOIN card_images b ON (b.id=a.top_image) WHERE " . $whereQuery . " ORDER BY a.country";
|
|
//var_dump($q);
|
|
$query = $this->read_replica->query($q);
|
|
$config["total_rows"] = $query->num_rows();
|
|
$config["base_url"] = '/bkoadmin/country';
|
|
$config["per_page"] = 10;
|
|
$config["uri_segment"] = 3;
|
|
$config["num_links"] = 5;
|
|
$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>";
|
|
$this->pagination->initialize($config);
|
|
|
|
$data["links"] = $this->pagination->create_links();
|
|
|
|
$q = "SELECT a.*,b.uniqueid,b.name,b.format FROM country a LEFT JOIN card_images b ON (b.id=a.top_image) WHERE " . $whereQuery . " ORDER BY a.country";
|
|
$q .= " LIMIT " . $config["per_page"] . " OFFSET " . $page;
|
|
$r = $this->read_replica->query($q);
|
|
$countries = [];
|
|
foreach ($r->result() as $row) {
|
|
$countries[] = (array) $row;
|
|
}
|
|
$data["search_text"] = $search_text;
|
|
$data["countries"] = $countries;
|
|
$this->renderAdminPage('view_country', $data);
|
|
}
|
|
|
|
public function countrycity()
|
|
{
|
|
|
|
$data = array("message" => "");
|
|
$country = $this->input->get('country');
|
|
$page = preg_replace('/[^0-9]/', '', $this->input->get('per_page'));
|
|
$this->load->model('geofence_area_city_model');
|
|
if ($this->input->post()) {
|
|
$data = $this->countryImageUpload($data);
|
|
}
|
|
$data["images"] = array();
|
|
$data["page_title"] = "City in " . $country;
|
|
|
|
$this->load->library('pagination');
|
|
$config = array();
|
|
$q = "SELECT id FROM geofence_area_city WHERE country='" . $country . "'";
|
|
$query = $this->read_replica->query($q);
|
|
|
|
$config["total_rows"] = $query->num_rows();
|
|
$config["base_url"] = '/bkoadmin/countrycity?country=US';
|
|
$config["per_page"] = 10;
|
|
$config["uri_segment"] = 3;
|
|
$config["num_links"] = 5;
|
|
$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>";
|
|
$config['page_query_string'] = true;
|
|
$this->pagination->initialize($config);
|
|
|
|
$page = (int)($page);
|
|
$data["links"] = $this->pagination->create_links();
|
|
|
|
$q = "SELECT * FROM geofence_area_city WHERE country='".$country."'ORDER BY city";
|
|
$q .= " LIMIT " . $config["per_page"] . " OFFSET " . $page;
|
|
$r = $this->read_replica->query($q);
|
|
$data["cities"] = $r->result();
|
|
$this->renderAdminPage('view_country_city', $data);
|
|
}
|
|
|
|
private function countryImageUpload($data)
|
|
{
|
|
$id = (int) $this->input->post('id');
|
|
$catid = (int) $this->input->post('catid'); // 4 - adminsavvy_card_image_category_country
|
|
if ($id > 0 && $catid > 0) {
|
|
$this->load->library('uploadhelper');
|
|
$q = "SELECT code FROM country WHERE id=" . $id;
|
|
$r = $this->read_replica->query($q);
|
|
$f = $r->result_array()[0];
|
|
ob_start();
|
|
$data = $this->uploadhelper->cardimagesPost($data, $this, $f["code"]);
|
|
$str = ob_get_clean();
|
|
if (substr($str, 0, 8) == 'Uploaded' && (!isset($data["card_image_id"]) || $data["card_image_id"] < 1)) {
|
|
if (preg_match('/^Uploaded ([a-zA-Z0-9]+) to gs:\/\/(.*)\/(.*)\.(.*)$/', $str, $arr) == 1 &&
|
|
count($arr) > 4 && $arr[2] == 'adminsavvy_card_image_category_country') {
|
|
$uniqueId = pg_escape_string($arr[3]);
|
|
$fmt = $arr[4];
|
|
$q = "SELECT id FROM card_images WHERE uniqueid='${uniqueId}'";
|
|
$r = $this->read_replica->query($q);
|
|
$f = $r->row_array();
|
|
if ($f != null && is_array($f) && $f["id"] > 0) {
|
|
$data["card_image_id"] = $f["id"];
|
|
}
|
|
}
|
|
}
|
|
// Uploaded phpGUIDjh to gs://adminsavvy_card_image_category_country/RATEMKrK9B.jpg
|
|
if (isset($data["card_image_id"]) && $data["card_image_id"] > 0) {
|
|
$q = "UPDATE country SET top_image='" . $data["card_image_id"] . "' WHERE id=${id}";
|
|
$this->db->query($q);
|
|
} else if (!isset($data["message"]) || $data["message"] == "") {
|
|
$data["message"] = "Failed to save image";
|
|
}
|
|
} else {
|
|
$data["message"] = "Invalid ID";
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function countryStatus()
|
|
{
|
|
$id = (int) $this->input->get('id');
|
|
$status = (int) $this->input->get('status');
|
|
if ($id > 0) {
|
|
$q = "UPDATE country SET status=${status} WHERE id=${id}";
|
|
$r = $this->db->query($q);
|
|
echo "Status updated";
|
|
} else {
|
|
echo "Invalid ID!";
|
|
}
|
|
}
|
|
|
|
public function countryImageDelete()
|
|
{
|
|
$data = [];
|
|
$id = (int) $this->input->get('id');
|
|
if ($id > 0) {
|
|
$q = "SELECT top_image FROM country WHERE id='${id}'";
|
|
$r = $this->read_replica->query($q);
|
|
$f = $r->row_array();
|
|
if ($f != null && is_array($f) && $f["top_image"] > 0) {
|
|
$data["id"] = $f["top_image"];
|
|
$this->load->library('uploadhelper');
|
|
$data = $this->uploadhelper->deleteCardimageFile($data, $this, 'country', 'top_image');
|
|
if (isset($data["deleted"]) && $data["deleted"] > 0) {
|
|
$q = "UPDATE country SET top_image=NULL WHERE top_image='" . $data["deleted"] . "'";
|
|
$this->db->query($q);
|
|
echo "Image deleted";
|
|
} else if (!isset($data["message"]) || $data["message"] == "") {
|
|
echo "Delete failed!";
|
|
} else {
|
|
echo $data["message"];
|
|
}
|
|
} else {
|
|
echo "Not top image found";
|
|
}
|
|
} else {
|
|
echo "Invalid ID!";
|
|
}
|
|
}
|
|
|
|
private function getAddressByAddress($addr)
|
|
{
|
|
$db_addr = pg_escape_string($addr);
|
|
$q = "SELECT * FROM address WHERE lower(address)=lower('${db_addr}')";
|
|
$r = $this->read_replica->query($q);
|
|
if ($row = $r->result()) {
|
|
//var_dump($row[0]);
|
|
return $row[0];
|
|
}
|
|
// TODO: Geocoding
|
|
return null;
|
|
}
|
|
|
|
private function getAddress($id)
|
|
{
|
|
$db_id = (int) $id;
|
|
$q = "SELECT * FROM address WHERE id=${id}";
|
|
$r = $this->read_replica->query($q);
|
|
if ($row = $r->result()) {
|
|
//var_dump($row[0]);
|
|
return $row[0];
|
|
}
|
|
// TODO: Geocoding
|
|
return null;
|
|
}
|
|
|
|
private function geofenceTrip($areas, $data_from, $data_to, $data)
|
|
{
|
|
$str = "Geofencing failed!";
|
|
$travel_time = 0;
|
|
$distance = 0;
|
|
$min_cost = PHP_INT_MAX;
|
|
$max_cost = 0;
|
|
$avg_cost = 0;
|
|
$avg_cnt = 0;
|
|
$route_overlay = [];
|
|
// Get Area
|
|
$postal_from = substr($data_from->{"postal"}, 0, 2);
|
|
$postal_to = substr($data_to->{"postal"}, 0, 2);
|
|
$area_from = array();
|
|
$area_to = array();
|
|
foreach ($areas as $f) {
|
|
$codes = json_decode($f->{"postal_code"}, true);
|
|
foreach ($codes["postal_code"] as $code) {
|
|
if ($postal_from == $code) {
|
|
$area_from = $f;
|
|
$str_from = "Travelling from '" . $f->{"name"} . "' (" . $f->{"id"} . ") ";
|
|
}
|
|
if ($postal_to == $code) {
|
|
$area_to = $f;
|
|
$str_to = " to '" . $f->{"name"} . "' (" . $f->{"id"} . ") ";
|
|
}
|
|
}
|
|
}
|
|
$history = [];
|
|
if (isset($area_from->{"postal_code"}) && isset($area_to->{"postal_code"})) {
|
|
try {
|
|
// Get distance
|
|
$resp = $this->geocode($data_from->{"address"}, $data_to->{"address"});
|
|
$route_overlay = $resp["options"]["route_overlay"];
|
|
$travel_time = $resp["options"]["travel_time"] / 60; //840;
|
|
$distance = $resp["options"]["travel_distance"] / 1000.0; //7043
|
|
|
|
if ($distance < $data["threshold"]) {
|
|
$distance = 1.4 * $this->distance_between_two_gps_coordinates(
|
|
$data_from->{"latitude"}, $data_from->{"longitude"}, $data_to->{"latitude"}, $data_to->{"longitude"}, 'K');
|
|
$travel_time = (int) (1.5 * $distance);
|
|
}
|
|
|
|
$str = $str_from . $str_to . ' for ' . sprintf("%0.02f", $distance) . ' km';
|
|
$str .= ' and ' . ((int) $travel_time) . 'minutes';
|
|
// Get history
|
|
$postals_from = json_decode($area_from->{"postal_code"}, true)["postal_code"];
|
|
$postals_to = json_decode($area_to->{"postal_code"}, true)["postal_code"];
|
|
$postals_from_str = implode("','", $postals_from);
|
|
$postals_to_str = implode("','", $postals_to);
|
|
$q = "SELECT a.*, b.postal AS spostal, b.address AS saddress, c.postal AS epostal, c.address AS eaddress
|
|
FROM parsedemail_item a
|
|
LEFT JOIN address b ON (b.id=a.location_start_id)
|
|
LEFT JOIN address c ON (c.id=a.location_end_id)
|
|
WHERE substring(b.postal from 1 for 2) IN ('${postals_from_str}')
|
|
AND substring(c.postal from 1 for 2) IN ('${postals_to_str}')
|
|
AND a.dup_id IS NULL AND a.cost>0
|
|
ORDER BY a.distance";
|
|
|
|
$r = $this->read_replica->query($q);
|
|
$k1 = $distance - $data["threshold"];
|
|
$k2 = $distance + $data["threshold"];
|
|
//echo "$k1 / $k2 <br/>\n";
|
|
foreach ($r->result() as $row) {
|
|
$history[] = (array) $row;
|
|
$c = $row->{"cost"};
|
|
$d = $row->{"distance"};
|
|
if (is_numeric($d) && is_numeric($c) && $c > 0 && $k1 <= $d && $d <= $k2) {
|
|
$avg_cnt++;
|
|
$avg_cost += $c;
|
|
if ($max_cost < $c) {
|
|
$max_cost = $c;
|
|
}
|
|
|
|
if ($min_cost > $c) {
|
|
$min_cost = $c;
|
|
}
|
|
|
|
}
|
|
}
|
|
$avg_cost = $avg_cnt > 0 ? $avg_cost / $avg_cnt : 0;
|
|
$str .= '<br/>';
|
|
$str .= 'Average trip cost is SGD$' . sprintf("%0.02f", $avg_cost) . ' based of ' . $avg_cnt . ' trips matching +/- ' . $data["threshold"] . 'km';
|
|
$str .= '<br/>';
|
|
$str .= 'Minimal trip cost is SDG$' . sprintf("%0.02f", $min_cost) . ', maximal trip cost is SDG$' . sprintf("%0.02f", $max_cost);
|
|
} catch (Exception $e) {
|
|
error_log($e->getMessage());
|
|
}
|
|
}
|
|
$data["geofence"] = $str;
|
|
$data["area_from"] = $area_from;
|
|
$data["area_to"] = $area_to;
|
|
$data["history"] = $history;
|
|
$data["distance"] = $distance;
|
|
$data["travel_time"] = $travel_time;
|
|
$data["route_overlay"] = $route_overlay;
|
|
$data["min_cost"] = $min_cost;
|
|
$data["max_cost"] = $max_cost;
|
|
$data["avg_cost"] = $avg_cost;
|
|
$data["avg_cnt"] = $avg_cnt;
|
|
return $data;
|
|
}
|
|
|
|
public function geofencing()
|
|
{
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
|
|
$data = array("geofence" => "", "message" => "");
|
|
$data["autofrom"] = "Marina Bay Sands, 10 Bayfront Ave, Singapore 018956";
|
|
$data["from"] = 2; //"Marina Bay Sands, 10 Bayfront Ave, Singapore 018956";
|
|
$data["autoto"] = "97 Meyer Road, Singapore";
|
|
$data["to"] = 3; //"97 Meyer Road, Singapore";
|
|
$data["threshold"] = 1.1; // +/- km
|
|
|
|
$areas = array();
|
|
$q = 'SELECT a.*,b.name AS sector,c.name AS region ';
|
|
$q .= ' FROM singapore_districts a, singapore_sectors b, singapore_regions c ';
|
|
$q .= ' WHERE b.id=a.sector_id AND c.id=a.region_id';
|
|
$query = $this->read_replica->query($q);
|
|
foreach ($query->result() as $row) {
|
|
$areas[] = $row;
|
|
}
|
|
$data['areas'] = $areas;
|
|
|
|
if ($this->input->post()) {
|
|
$data["autofrom"] = $this->input->post('autofrom');
|
|
$data["from"] = $this->input->post('from');
|
|
$data["autoto"] = $this->input->post('autoto');
|
|
$data["to"] = $this->input->post('to');
|
|
$data["threshold"] = $this->input->post('threshold');
|
|
$data["data_from"] = $this->getAddress($data["from"]);
|
|
$data["data_to"] = $this->getAddress($data["to"]);
|
|
$data = $this->geofenceTrip($areas, $data["data_from"], $data["data_to"], $data);
|
|
}
|
|
|
|
$q = "SELECT id, address FROM address ORDER BY address";
|
|
$query = $this->read_replica->query($q);
|
|
$address = [];
|
|
foreach ($query->result() as $row) {
|
|
$address[$row->{"id"}] = $row->{"address"};
|
|
}
|
|
$data["address"] = $address;
|
|
|
|
$this->renderAdminPage('view_geofencing', $data);
|
|
}
|
|
|
|
private function distance_between_two_gps_coordinates($lat1, $lon1, $lat2, $lon2, $unit)
|
|
{
|
|
if (($lat1 == $lat2) && ($lon1 == $lon2)) {
|
|
return 0;
|
|
} else {
|
|
$theta = $lon1 - $lon2;
|
|
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
|
|
$dist = acos($dist);
|
|
$dist = rad2deg($dist);
|
|
$miles = $dist * 60 * 1.1515;
|
|
$unit = strtoupper($unit);
|
|
|
|
if ($unit == "K") {
|
|
return ($miles * 1.609344);
|
|
} else if ($unit == "N") {
|
|
return ($miles * 0.8684);
|
|
} else {
|
|
return $miles;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function geocode($addrFrom, $addrTo)
|
|
{
|
|
global $savvyext;
|
|
$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
|
|
$encryptionAlg = $savvyext->cfgReadChar('encryption.algorithm');
|
|
$encryptionKey = $savvyext->cfgReadChar('encryption.key');
|
|
$encryptionIV = $savvyext->cfgReadChar('encryption.iv');
|
|
$api_url = $savvyext->cfgReadChar('system.api_url');
|
|
|
|
$payload = "{
|
|
\"addresses\":[
|
|
{\"address\":\"${addrFrom}\",\"type\":1},
|
|
{\"address\":\"${addrTo}\",\"type\":2}
|
|
],
|
|
\"options\":{
|
|
\"travel_time\": true,
|
|
\"route_overlay\": true
|
|
},
|
|
\"gps_country_code\": \"SG\",
|
|
\"country\": \"SG\",
|
|
\"member_id\": \"3\"
|
|
}";
|
|
$encrypted_payload = bin2hex(
|
|
openssl_encrypt(
|
|
$payload, $encryptionAlg, $encryptionKey, OPENSSL_RAW_DATA, $encryptionIV
|
|
));
|
|
|
|
$postdata = "{\"encrypted_payload\": \"${encrypted_payload}\"}";
|
|
|
|
$url = $api_url . "/trips/api/geocode";
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_VERBOSE, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
'Content-Type: application/json',
|
|
'Content-Length: ' . strlen($postdata),
|
|
'Authorization: Server-Token ' . $httpAuthToken,
|
|
"client_id: BackOffice"
|
|
)
|
|
);
|
|
|
|
$body = curl_exec($ch);
|
|
$result = json_decode($body, true);
|
|
|
|
$payload = openssl_decrypt(
|
|
hex2bin(
|
|
$result['payload']
|
|
), $encryptionAlg, $encryptionKey, OPENSSL_RAW_DATA, $encryptionIV
|
|
);
|
|
return json_decode($payload, true);
|
|
}
|
|
|
|
public $template2 = array(
|
|
'table_open' => "<table style='background-color:aliceblue' class='table table-striped table-hover table-bordered table-condensed'>",
|
|
'thead_open' => '<thead class=\'bg-indigo\'>',
|
|
'thead_close' => '</thead>',
|
|
'heading_row_start' => '<tr>',
|
|
'heading_row_end' => '</tr>',
|
|
'heading_cell_start' => '<th>',
|
|
'heading_cell_end' => '</th>',
|
|
'tbody_open' => '<tbody>',
|
|
'tbody_close' => '</tbody>',
|
|
'row_start' => '<tr style=\'padding:3px;\'>',
|
|
'row_end' => '</tr>',
|
|
'cell_start' => '<td>',
|
|
'cell_end' => '</td>',
|
|
'row_alt_start' => '<tr style=\'padding:3px;\'>',
|
|
'row_alt_end' => '</tr>',
|
|
'cell_alt_start' => '<td>',
|
|
'cell_alt_end' => '</td>',
|
|
'table_close' => '</table>',
|
|
);
|
|
|
|
public function userManagerment()
|
|
{
|
|
$this->load->model('bkoadmin_model');
|
|
|
|
$data = [
|
|
"pid" => "",
|
|
"firstname" => "",
|
|
"lastname" => "",
|
|
"email" => "",
|
|
"username" => "",
|
|
"password" => "",
|
|
"confirm_password" => "",
|
|
"permission" => "",
|
|
"plevel" => "",
|
|
"status" => "",
|
|
"last_login" => "",
|
|
"loc" => "",
|
|
"form_button" => "Create",
|
|
"message" => "",
|
|
];
|
|
|
|
try {
|
|
if ($_POST) {
|
|
$data['pid'] = trim($this->input->post('pid'));
|
|
$data['firstname'] = trim($this->input->post('firstname'));
|
|
$data['lastname'] = trim($this->input->post('lastname'));
|
|
$data['email'] = trim($this->input->post('email'));
|
|
$data['username'] = trim($this->input->post('username'));
|
|
$data['password'] = trim($this->input->post('password'));
|
|
$data['confirm_password'] = trim($this->input->post('confirm_password'));
|
|
$data['permission'] = trim($this->input->post('permission'));
|
|
$data['last_login'] = trim($this->input->post('last_login'));
|
|
$data['loc'] = trim($this->input->post('loc'));
|
|
|
|
/*
|
|
pid | integer | | |
|
|
firstname | character varying(50) | | not null |
|
|
lastname | character varying(50) | | not null |
|
|
email | character varying(150) | | not null |
|
|
username | character varying(25) | | not null |
|
|
password | character varying(125) | | not null |
|
|
plevel | integer | | |
|
|
status | integer | | | 1
|
|
added | timestamp without time zone | | | now()
|
|
last_login | timestamp without time zone | | | now()
|
|
loc | inet | | |
|
|
*/
|
|
// Validate
|
|
if (empty($data['firstname']) || strlen($data['firstname']) > 50) {
|
|
$data['message'] .= "<br/>Invalid first name";
|
|
}
|
|
if (empty($data['lastname']) || strlen($data['lastname']) > 50) {
|
|
$data['message'] .= "<br/>Invalid last name";
|
|
}
|
|
if (empty($data['email']) || strlen($data['email']) > 150 || $this->validateEmail($data['email']) == false) {
|
|
$data['message'] .= "<br/>Invalid email";
|
|
}
|
|
if (empty($data['username']) || strlen($data['username']) > 25) {
|
|
$data['message'] .= "<br/>Invalid username";
|
|
}
|
|
|
|
// Validate password
|
|
$query = "SELECT COUNT(*) AS count
|
|
FROM bko_users
|
|
WHERE username = '" . pg_escape_string($data['username']) . "'";
|
|
$result = $this->read_replica->query($query)->result_array();
|
|
|
|
if ($result[0]['count'] > 0) {
|
|
|
|
if (strlen($data['password']) > 125) {
|
|
$data['message'] .= "<br/>Invalid password";
|
|
}
|
|
if (strlen($data['confirm_password']) > 125) {
|
|
$data['message'] .= "<br/>Invalid confirm password";
|
|
}
|
|
if ($data['confirm_password'] != $data['password']) {
|
|
$data['message'] .= "<br/>Password doesn't match";
|
|
}
|
|
} else {
|
|
|
|
if (empty($data['password']) || strlen($data['password']) > 125) {
|
|
$data['message'] .= "<br/>Invalid password";
|
|
}
|
|
if (empty($data['confirm_password']) || strlen($data['confirm_password']) > 125) {
|
|
$data['message'] .= "<br/>Invalid confirm password";
|
|
}
|
|
if ($data['confirm_password'] != $data['password']) {
|
|
$data['message'] .= "<br/>Password doesn't match";
|
|
}
|
|
}
|
|
|
|
$data['permission'] = explode(',', $data['permission']);
|
|
if (count($data['permission']) != 2) {
|
|
|
|
$data['message'] .= "<br/>Invalid permission";
|
|
} else {
|
|
|
|
$data['plevel'] = $data['permission'][0];
|
|
$data['status'] = $data['permission'][1];
|
|
|
|
if (is_numeric($data['plevel']) == false || is_numeric($data['status']) == false) {
|
|
$data['message'] .= "<br/>Invalid permission";
|
|
}
|
|
}
|
|
|
|
if (is_numeric($data['pid']) == false) {
|
|
$data['message'] .= "<br/>Invalid pid";
|
|
}
|
|
if (!empty($data['last_login']) && $this->validateDate($data['last_login']) == false) {
|
|
$data['message'] .= "<br/>Invalid last login";
|
|
}
|
|
if (!empty($data['loc']) && $this->validateIpAddress($data['loc']) == false) {
|
|
$data['message'] .= "<br/>Invalid loc";
|
|
}
|
|
|
|
$q = $this->bkoadmin_model->insertOrUpdateAdminQuery($data);
|
|
|
|
if ($data['message'] == "") {
|
|
$r = $this->db->query($q);
|
|
$f = $r->row_array();
|
|
$active = $f["active"];
|
|
if ($f != null && isset($active)) {
|
|
$data['message'] = 'User management ' . $active . '!';
|
|
$data["form_button"] = $active == 'inserted' ? 'Insert' : 'Update';
|
|
} else {
|
|
$data['message'] = 'Failed to ' . $active . ' user management!';
|
|
}
|
|
}
|
|
}
|
|
|
|
$params = [];
|
|
$params = $this->input->get();
|
|
|
|
$query = $this->bkoadmin_model->getAdminListQuery($params);
|
|
$tableData = $this->returnAdminTable(
|
|
[
|
|
'count_query' => $query,
|
|
'query' => $query,
|
|
],
|
|
'/bkoadmin/usermanagerment',
|
|
[
|
|
'per_page' => 10,
|
|
'reuse_query_string' => TRUE,
|
|
]
|
|
);
|
|
|
|
$data['links'] = $tableData['links'];
|
|
$data['filterData'] = $params;
|
|
|
|
//drop down
|
|
$query = "SELECT pid,
|
|
name
|
|
FROM platform_user";
|
|
|
|
$platform = $this->read_replica->query($query);
|
|
|
|
$query = "SELECT name,
|
|
plevel,
|
|
status
|
|
FROM bko_permission_level";
|
|
|
|
$permission = $this->read_replica->query($query);
|
|
|
|
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
|
|
$page = is_numeric($page) ? $page : 0;
|
|
|
|
$data = array_merge($data, [
|
|
"user_management_table" => $tableData['limited_data'],
|
|
"page" => $page,
|
|
"platform" => $platform->result_array(),
|
|
"permission" => $permission->result_array(),
|
|
]);
|
|
} catch (Exception $e) {
|
|
$data["message"] = $e->getMessage();
|
|
}
|
|
|
|
$this->renderAdminPage('view_user_management', $data);
|
|
}
|
|
|
|
public function validateDate($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) {
|
|
return false;
|
|
}
|
|
|
|
$day = $matches[0][3];
|
|
$month = $matches[0][2];
|
|
$year = $matches[0][1];
|
|
|
|
return checkdate($month, $day, $year);
|
|
}
|
|
|
|
public function validateEmail($email)
|
|
{
|
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
|
}
|
|
|
|
public function validateIpAddress($ip)
|
|
{
|
|
|
|
return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $ip);
|
|
}
|
|
|
|
public function userManagementDelete()
|
|
{
|
|
$data = [];
|
|
$id = (int) $this->input->get('id');
|
|
header('Content-Type: application/json');
|
|
|
|
if ($id > 0) {
|
|
$q = "DELETE FROM bko_users WHERE id=${id}";
|
|
$r = $this->db->query($q);
|
|
if ($r) {
|
|
echo json_encode([
|
|
'state' => 'successful',
|
|
'message' => 'User management deleted',
|
|
'user_id' => $id,
|
|
]);
|
|
} else {
|
|
echo json_encode([
|
|
'state' => 'failure',
|
|
'message' => 'Delete failed',
|
|
]);
|
|
}
|
|
} else {
|
|
echo json_encode([
|
|
'state' => 'failure',
|
|
'message' => 'Invalid ID',
|
|
]);
|
|
}
|
|
}
|
|
|
|
/*** name alias validation function
|
|
* @param string $name_alias
|
|
* @param int $id 0 => check when create, >0 check when update
|
|
* @return array tupple result 0 => result true or false, 1 => error message if fasle
|
|
*/
|
|
public function fnValidateNameAlias($name_alias = '', $id = 0)
|
|
{
|
|
//step 1. trim and upper item
|
|
if (empty($name_alias) || strlen($name_alias) > 100)
|
|
return array(false, 'Alias invalid Name Alias, Name Alias length should be 0-100');
|
|
|
|
$tmp_alias = array_map(function ($v) {
|
|
$v = trim($v);
|
|
$v = strtoupper($v);
|
|
return $v;
|
|
}, explode(',', $name_alias));
|
|
|
|
//step 2. each string item max string length is 50
|
|
foreach ($tmp_alias as $item)
|
|
if (strlen($item) > 50)
|
|
return array(false, 'Invalid Name Alias, max of Name Alias item max length is 50');
|
|
|
|
//step 3: string item not include special character
|
|
$tmp_alias_str = implode(',', $tmp_alias);
|
|
if (!preg_match('/^[0-9a-zA-Z, -_]+$/', $tmp_alias_str))
|
|
return array(false, 'Invalid Name Alias, Name Alias item should not have special character');
|
|
|
|
//step 4: check unique in db
|
|
$sql_alias_similar_to = pg_escape_string(strtolower(str_replace(',', '|', $tmp_alias_str)));
|
|
$sqlFirstCheck = '';
|
|
if($id>0){
|
|
// if edit => except this
|
|
$sqlFirstCheck = 'id<>'.pg_escape_string($id).' AND ';
|
|
}
|
|
$sql_check = "SELECT name_alias FROM transport_providers WHERE $sqlFirstCheck LOWER(name_alias) SIMILAR TO '%(" . $sql_alias_similar_to . ")%'";
|
|
$check_alias_exists = $this->read_replica->query($sql_check)->result_array();
|
|
if(!empty($check_alias_exists)){
|
|
$ali_exists = [];
|
|
foreach ($check_alias_exists as $ali){
|
|
$ex = array_map(function ($v) {
|
|
$v = trim($v);
|
|
$v = strtoupper($v);
|
|
return $v;
|
|
}, explode(',', $ali['name_alias']));
|
|
$ali_exists = array_merge($ali_exists,$ex);
|
|
}
|
|
$rs_check_exists= array_intersect($tmp_alias,$ali_exists);
|
|
if(!empty($rs_check_exists))
|
|
return array(false, 'aliases already exist in the system: '.implode(',', $rs_check_exists));
|
|
}
|
|
return array(true, 'Success!');
|
|
}
|
|
|
|
//
|
|
public function gas_station_map()
|
|
{
|
|
$data = array();
|
|
$this->renderAdminPage('view_gas_station_map', $data);
|
|
}
|
|
}
|