306 lines
9.8 KiB
PHP
306 lines
9.8 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class Credit_cards extends Admin_Controller
|
|
{
|
|
|
|
public $viewData = array();
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
// load library
|
|
$this->load->library('session');
|
|
|
|
// Load model
|
|
$this->load->model('bank_model');
|
|
$this->load->model('credit_card_model');
|
|
$this->load->model('credit_card_benefit_model');
|
|
$this->load->library('pagination');
|
|
$this->viewData['benefits'] = [];
|
|
}
|
|
|
|
protected function renderToolsPage($page_name, $data)
|
|
{
|
|
$this->load->view('admin/view_admin_header', $data);
|
|
$this->load->view('credit_cards/' . $page_name, $data);
|
|
$this->load->view('admin/view_admin_footer', $data);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->renderToolsPage("view_credit_cards", $this->viewData);
|
|
}
|
|
|
|
public function loadRecord()
|
|
{
|
|
$rowno = $this->input->get('rowno');
|
|
parse_str($this->input->get('filters'), $filters);
|
|
$filters = array_filter($filters, function($val) {
|
|
return $val !== '';
|
|
});
|
|
|
|
// Row per page
|
|
$rowperpage = 10;
|
|
$cur_page = $rowno;
|
|
|
|
// Row position
|
|
if ($rowno != 0) {
|
|
$rowno = ($rowno - 1) * $rowperpage;
|
|
}
|
|
|
|
// All records count
|
|
$allcount = count($this->credit_card_model->getData($filters));
|
|
|
|
// Get records
|
|
$users_record = $this
|
|
->credit_card_model
|
|
->getData($filters, $rowperpage, $rowno);
|
|
|
|
foreach($users_record as &$ele) {
|
|
|
|
$ele['benefits'] = array_map(function($ele) {
|
|
$ele['expired_date'] = empty($ele['expired_date'])
|
|
? ''
|
|
: $ele['expired_date'];
|
|
|
|
return $ele;
|
|
}, $this
|
|
->credit_card_benefit_model
|
|
->getBenefitByCardID($ele['credit_card_id']));
|
|
|
|
}
|
|
|
|
// Pagination Configuration
|
|
$config['base_url'] = '/Credit_cards/loadRecord';
|
|
$config['use_page_numbers'] = TRUE;
|
|
$config['total_rows'] = $allcount;
|
|
$config['per_page'] = $rowperpage;
|
|
$config['cur_page'] = $cur_page;
|
|
$config['full_tag_open'] = "<ul class='pagination pb-20'>";
|
|
$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'] = $users_record;
|
|
$data['row'] = $rowno;
|
|
|
|
echo json_encode($data);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->library('form_validation');
|
|
|
|
$this->setFormRuleCreate();
|
|
$params = $this->getFormValue();
|
|
|
|
if ($this->validateForm($params) === FALSE) {
|
|
$this->renderToolsPage('view_credit_cards', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
if ($this->isExistBankNameAndCardType($params)) {
|
|
$this->viewData['msg'] = 'Bank Name and Card Type already input';
|
|
$this->renderToolsPage('view_credit_cards', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$this->insert_record($params);
|
|
redirect('/credit_cards');
|
|
}
|
|
|
|
public function setFormRuleCreate()
|
|
{
|
|
$this->form_validation->set_rules('bank_name', 'Bank Name', 'required|max_length[50]');
|
|
$this->form_validation->set_rules('card_name', 'Card Type', 'required|max_length[50]');
|
|
$this->form_validation->set_rules('benefits[]', 'Benefit', 'max_length[256]');
|
|
$this->form_validation->set_rules('expired_date[]', 'Expired Date', 'regex_match[/\d{4}-\d{2}-\d{2}/]');
|
|
}
|
|
|
|
private function getFormValue()
|
|
{
|
|
return [
|
|
'bank_name' => trim($this->input->post('bank_name') ?? ''),
|
|
'card_name' => trim($this->input->post('card_name') ?? ''),
|
|
'bank_id' => $this->input->post('bank_id'),
|
|
'credit_card_id' => $this->input->post('credit_card_id'),
|
|
'old_bank_name' => trim($this->input->post('old_bank_name') ?? ''),
|
|
'old_card_name' => trim($this->input->post('old_card_name') ?? ''),
|
|
'benefits[]' => $this->input->post('benefits[]') ?? [],
|
|
'expired_date[]' => $this->input->post('expired_date[]') ?? []
|
|
];
|
|
}
|
|
|
|
public function validateForm(&$params)
|
|
{
|
|
$this->form_validation->set_data($params);
|
|
|
|
if ($this->form_validation->run() == false) {
|
|
$this->viewData = array_merge($this->viewData, $params);
|
|
// Rerender filter if validate fail
|
|
$this->viewData['msg'] = validation_errors();
|
|
return FALSE;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
private function isExistBankNameAndCardType($data) {
|
|
return $this->credit_card_model->countRecordsByBankAndCardName([
|
|
'bank_name' => $data['bank_name'],
|
|
'card_name' => $data['card_name'],
|
|
]) > 0;
|
|
}
|
|
|
|
private function insert_record($params) {
|
|
$this->db->trans_start();
|
|
$this->db->trans_strict(FALSE);
|
|
|
|
$params['bank_id'] = $this->bank_model->insert($params);
|
|
$params['credit_card_id'] = $this->credit_card_model->insert($params);
|
|
|
|
$this->credit_card_benefit_model->insert($params);
|
|
|
|
$this->db->trans_complete();
|
|
|
|
if ($this->db->trans_status() === FALSE) {
|
|
$this->db->trans_rollback();
|
|
$this->viewData['msg'] = 'Something went wrong !!!';
|
|
return FALSE;
|
|
}
|
|
else {
|
|
$this->db->trans_commit();
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
public function setFormRuleUpdate()
|
|
{
|
|
$this->form_validation->set_rules('bank_name', 'Bank Name', 'required|max_length[50]');
|
|
$this->form_validation->set_rules('card_name', 'Card Type', 'required|max_length[50]');
|
|
$this->form_validation->set_rules('benefits[]', 'Benefit', 'max_length[256]');
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->library('form_validation');
|
|
|
|
$this->setFormRuleUpdate();
|
|
$params = $this->getFormValue();
|
|
|
|
if ($this->validateForm($params) === FALSE) {
|
|
// $this->renderToolsPage('view_credit_cards', $this->viewData);
|
|
$response = array(
|
|
'type' => 'errors',
|
|
'message' => validation_errors()
|
|
);
|
|
return $this->output->set_content_type('application/json')->set_output( json_encode( $response ) );
|
|
}
|
|
|
|
if (
|
|
strcmp($params['bank_name'], $params['old_bank_name']) !== 0 ||
|
|
strcmp($params['card_name'], $params['old_card_name']) !== 0
|
|
) {
|
|
|
|
if ($this->isExistBankNameAndCardType($params)) {
|
|
$this->viewData['msg'] = 'Bank Name and Card Type already input';
|
|
$this->renderToolsPage('view_credit_cards', $this->viewData);
|
|
return;
|
|
}
|
|
}
|
|
|
|
$update = $this->update_record($params);
|
|
|
|
return $this->output->set_content_type('application/json')->set_output( json_encode( $update ) );
|
|
}
|
|
|
|
private function update_record($params) {
|
|
$this->db->trans_start();
|
|
$this->db->trans_strict(FALSE);
|
|
|
|
$params['bank_id'] = $this->bank_model->insert($params);
|
|
$this->credit_card_model->update($params);
|
|
|
|
// delete old records
|
|
$this->credit_card_benefit_model->delete($params);
|
|
$this->credit_card_benefit_model->insert($params);
|
|
|
|
$this->db->trans_complete();
|
|
if ($this->db->trans_status() === FALSE) {
|
|
$this->db->trans_rollback();
|
|
$this->viewData['msg'] = 'Something went wrong !!!';
|
|
return FALSE;
|
|
}
|
|
else {
|
|
$this->db->trans_commit();
|
|
return $params;
|
|
}
|
|
}
|
|
|
|
public function destroy() {
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->database();
|
|
|
|
$params = $this->getFormValue();
|
|
$this->setFormRuleDelete();
|
|
|
|
if ($this->validateForm($params) === FALSE) {
|
|
$this->renderToolsPage('view_credit_cards', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$result = $this->delete_record($params);
|
|
|
|
$this->viewData['msg'] = $result ? "Delete Successful" : "Delete Fail";
|
|
|
|
$this->renderToolsPage('view_credit_cards', $this->viewData);
|
|
}
|
|
|
|
public function delete_record($params) {
|
|
$this->db->trans_start();
|
|
$this->db->trans_strict(FALSE);
|
|
|
|
$this->credit_card_benefit_model->delete($params);
|
|
$this->credit_card_model->delete($params);
|
|
|
|
$this->db->trans_complete();
|
|
if ($this->db->trans_status() === FALSE) {
|
|
$this->db->trans_rollback();
|
|
return FALSE;
|
|
}
|
|
else {
|
|
$this->db->trans_commit();
|
|
|
|
if ($this->credit_card_model->count_credit_card($params) === '0') {
|
|
$this->bank_model->delete($params);
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
private function setFormRuleDelete()
|
|
{
|
|
$this->form_validation->set_rules('credit_card_id', 'Credit', 'required|numeric|exist[credit_cards,id,credit_card_id]');
|
|
}
|
|
}
|