|
Server : Apache System : Linux server.mata-lashes.com 3.10.0-1160.90.1.el7.x86_64 #1 SMP Thu May 4 15:21:22 UTC 2023 x86_64 User : matalashes ( 1004) PHP Version : 8.1.29 Disable Function : NONE Directory : /home/matalashes/.trash/app.bak.8/Controllers/Ps/ |
Upload File : |
<?php
namespace App\Controllers\Ps;
use App\Libraries\Ps\Loglib;
use App\Libraries\Ps\Msglib;
use App\Libraries\Ps\Mylib;
use App\Libraries\Ps\Render;
use App\Models\PsAdmAccess;
use App\Models\PsAdmRole;
use App\Models\PsAdmUser;
use App\Models\PsAdmSetting;
use App\Models\PsAdmSettingProfile;
use App\Models\PsStoreAccess;
use App\Models\PsStoreMain;
use App\Models\PsStoreRole;
use App\Models\PsStoreUser;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Files\File;
use Config\Services;
use Psr\Log\LoggerInterface;
class PS_Controller extends BaseController
{
protected $view_data = array();
protected $download = false;
protected $active_menu = '';
protected $setting_system = '';
protected $breadcrumb = '';
protected $pagetitle = '';
protected $content_views = array();
protected $validation_input = array();
protected $upload_data = array(
'upload_status' => false,
'upload_field_name' => null,
'upload_path' => '',
'upload_field_name2' => null,
'upload_path2' => '',
);
protected $upload_data_validation_rule = [];
protected $models;
protected $returned;
protected $class;
protected $access;
protected $sess_user_id_name;
protected $sess_role_id_name;
public $session;
public $validation;
public $myLib;
public $msgLib;
public $logLib;
public $render;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger); // TODO: Change the autogenerated stub
helper(['form', 'htmlcustom_helper', 'jscustom_helper', 'url', 'email_helper']);
/* == Simplify Code == */
$uri = $request->uri;
$uri = $uri->getPath();
$this->session = Services::session();
$this->validation = Services::validation();
$system = new PsAdmSetting();
$profile = new PsAdmSettingProfile();
$store_main = new PsStoreMain();
$this->myLib = new Mylib();
$this->msgLib = new Msglib();
$this->logLib = new Loglib();
$this->render = new Render();
$this->initViewData();
if (strpos($uri, 'admin') !== false) {
$this->access = 'admin';
$this->session->set('access', 'admin');
} else if (strpos($uri, 'master') !== false) {
$this->access = 'master';
$this->session->set('access', 'master');
} else {
$this->access = 'report';
$this->session->set('access', 'report');
}
// For Admin Profile & Setting
$this->setting_system = $system->first();
$this->view_data['core_settings'] = $this->setting_system;
$this->view_data['core_profile'] = $profile->first();
// $this->view_data['store_main'] = $store_main->where('ps_store_id',$this->session->get('ps_store_id'))->first();
$this->view_data['store_main'] = $store_main
->select('ps_store_main.sm_slug,ps_store_account.sa_plan')
->join('ps_store_account', 'ps_store_account.ps_sa_id = ps_store_main.ps_sa_id')
->where('ps_store_main.ps_store_id',$this->session->get('ps_store_id'))
->first();
/* == Simplify Code == */
if ($this->session->get('last_uri') == '') {
$this->session->set('last_uri', $this->myLib->getOnlyControllersName());
}
$this->initialAccess($uri, $this->access);
$this->logLib->log();
}
protected function initViewData()
{
$this->view_data['error'] = false;
$this->view_data['error_list'] = array();
$this->view_data['success'] = false;
$this->view_data['warning'] = false;
$this->view_data['logout'] = false;
$this->view_data['prompt'] = false;
$this->view_data['state_error_uploaddata'] = false;
$this->view_data['message'] = '';
$this->view_data['redirect'] = false;
$this->view_data['access_menu'] = '';
$this->view_data['myLib'] = $this->myLib;
}
protected function initialAccess($uri, $access)
{
$mdlUser = '';
$mdlRole = '';
$aksesMenu = '';
if ($access == 'admin') {
$this->sess_user_id_name = 'ps_au_id';
$this->sess_role_id_name = 'ps_ar_id';
$mdlUser = new PsAdmUser();
$mdlRole = new PsAdmRole();
$aksesMenu = new PsAdmAccess();
if ($uri != 'authdev' && $uri != 'forgotdev' && $uri != 'resetdev' && $uri != 'override404' && $uri != 'maintenance') {
$this->setClassCurrentURILastURI();
}
} else {
if (null !== ($this->session->get('ps_au_id'))) {
$this->sess_user_id_name = 'ps_au_id';
$this->sess_role_id_name = 'ps_ar_id';
$mdlUser = new PsAdmUser();
$mdlRole = new PsAdmRole();
$aksesMenu = new PsAdmAccess();
if ($uri != 'authdev' && $uri != 'forgotdev' && $uri != 'resetdev' && $uri != 'override404' && $uri != 'maintenance') {
$this->setClassCurrentURILastURI();
}
} else {
$this->sess_user_id_name = 'ps_su_id';
$this->sess_role_id_name = 'ps_sr_id';
$mdlUser = new PsStoreUser();
$mdlRole = new PsStoreRole();
$aksesMenu = new PsStoreAccess();
if ($uri != 'authstore' && $uri != 'forgotstore' && $uri != 'resetstore' && $uri != 'override404' && $uri != 'maintenance') {
$this->setClassCurrentURILastURI();
}
}
}
$this->view_data['access'] = $this->access;
$this->view_data['sess_user_id_name'] = $this->sess_user_id_name;
$this->view_data['sess_role_id_name'] = $this->sess_role_id_name;
$this->active_menu = $this->class != '' ? strtolower($this->class) : '';
$this->user = $this->session->get($this->sess_user_id_name) ? $mdlUser->find($this->session->get($this->sess_user_id_name)) : false;
if ($this->user) {
$this->view_data['user'] = $this->user;
$role = $this->session->get($this->sess_role_id_name);
$this->view_data['role'] = $mdlRole->find($role);
if (isset($role)) {
$this->view_data['access_menu'] = $aksesMenu::get_by_role_id($role)->list_with_action;
}
$this->session->set('username', $this->user->username);
$this->session->set('role', $this->view_data['role']->role_name);
} else {
}
}
protected function setClassCurrentURILastURI()
{
$this->class = $this->myLib->getOnlyControllersName();
$this->session->set('current_uri', $this->class);
$this->myLib->check_last_uri();
}
protected function filterStoreLoad()
{
$store = new PsStoreMain();
if ($this->session->has('ps_ar_id')) {
$this->view_data['_store'] = $store->findAll();
} else {
$ps_sa_id = $this->session->get('ps_sa_id');
$this->view_data['_store'] = $store->where('ps_sa_id', $ps_sa_id)->findAll();
}
if ($this->request->getPost()) {
if ($this->request->getVar('filtersubmit')) {
$this->stateActiveStore();
}
}
}
protected function stateAdd($override = false)
{
$this->view_data['state'] = 'add';
$this->view_data['button'] = 'Simpan';
$this->view_data['upload_data'] = $this->upload_data;
if ($_POST) {
if (!$this->validation_input->withRequest($this->request)->run()) {
$this->view_data['error'] = true;
$this->view_data['message'] = 'Ada kesalahan dalam pengisian form!';
$this->view_data['error_list'] = $this->validation_input->getErrors();
} else {
unset($_POST['id']);
$_mdl = $this->myLib->getNewModel($this->models['base_model']);
$_datamodel = array();
foreach ($_POST as $index => $arr) {
$prefix = substr($index, 0, 2);
//skip field from inserting to table
if ($prefix != '__') {
if (strpos($index, 'texteditor')) {
$index = explode('__', $index);
$index = $index[0];
$_datamodel[$index] = $this->request->getVar($index);
} else {
$_datamodel[$index] = $this->request->getVar($index);
}
}
}
if ($this->upload_data['upload_status']) {
$field_name = $this->upload_data['upload_field_name'];
$_datamodel[$field_name] = $this->session->get($field_name) !== null ? $this->session->get($field_name) : '';
$this->session->remove($field_name);
}
try {
if (!$_mdl->save($_datamodel)) {
throw new \CodeIgniter\Database\Exceptions\DatabaseException();
} else {
if (!$override) {
$this->setMessage(false, true, $this->msgLib->scs_add());
} else {
return $this->setReturn(true, $_mdl->getInsertID(), NULL, NULL, NULL);
}
}
} catch (Exception $e) {
$this->setMessage(true, false, $this->msgLib->err_add());
}
}
}
$this->render->viewAdmin($this->content_views['form_add_view'], $this);
}
public function setMessage($err_state, $scs_state, $msg)
{
if ($err_state) {
$this->view_data['error'] = true;
}
if ($scs_state) {
$this->view_data['success'] = true;
}
if (!$err_state && !$scs_state) {
$this->view_data['warning'] = true;
}
$this->view_data['message'] = $msg;
}
public function setMessageWithRedirect($err_state, $scs_state, $msg, $redirect)
{
if ($err_state) {
$this->view_data['error'] = true;
}
if ($scs_state) {
$this->view_data['success'] = true;
}
if (!$err_state && !$scs_state) {
$this->view_data['warning'] = true;
}
$this->view_data['base_controller'] = $redirect;
$this->view_data['message'] = $msg;
}
public function setMessageAndLogout($err_state, $scs_state, $msg)
{
if ($err_state) {
$this->view_data['error'] = true;
}
if ($scs_state) {
$this->view_data['success'] = true;
}
$this->view_data['logout'] = true;
$this->view_data['message'] = $msg;
}
protected function setReturn($continue = false, $created_data = NULL, $updated_data = NULL, $deleted_data = NULL, $restored_data = NULL)
{
return array(
'continue' => $continue,
'created_data' => $created_data,
'updated_data' => $updated_data,
'deleted_data' => $deleted_data,
'restored_data' => $restored_data
);
}
protected function stateEdit($id, $override = false)
{
$mdl = $this->myLib->getNewModel($this->models['base_model']);
$_data = $mdl->find($id);
$this->view_data['state'] = 'edit';
$this->view_data['button'] = 'Simpan';
$this->view_data['id'] = $id;
$this->view_data['_data'] = $_data;
$this->view_data['upload_data'] = $this->upload_data;
if ($_POST) {
if (!$this->validation_input->withRequest($this->request)->run()) {
$this->view_data['error'] = true;
$this->view_data['message'] = 'Ada kesalahan dalam pengisian form!';
$this->view_data['error_list'] = $this->validation_input->getErrors();
} else {
unset($_POST['id']);
$_datamodel = array();
foreach ($_POST as $index => $arr) {
//disable xss clean to insert html using texteditor
if (strpos($index, 'texteditor')) {
$index = explode('__', $index);
$index = $index[0];
$_datamodel[$index] = $this->request->getVar($index);
} else {
$_datamodel[$index] = $this->request->getVar($index);
}
}
if ($this->upload_data['upload_status']) {
$field_name = $this->upload_data['upload_field_name'];
if ($_data->$field_name != '') {
$this->session->set($field_name, $_data->$field_name);
}
$_datamodel[$field_name] = $this->session->get($field_name) !== null ? $this->session->get($field_name) : '';
$this->session->remove($field_name);
}
try {
if (!$mdl->update($id, $_datamodel)) {
throw new \CodeIgniter\Database\Exceptions\DatabaseException();
} else {
if (!$override) {
$this->setMessage(false, true, $this->msgLib->scs_edit());
} else {
return $this->setReturn(true, NULL, $_data, NULL, NULL);
}
}
} catch (Exception $e) {
$this->setMessage(true, false, $this->msglib->err_edit());
}
}
}
$this->render->viewAdmin($this->content_views['form_edit_view'], $this);
}
protected function stateDelete($id = 0, $override = false)
{
$this->view_data['state'] = 'delete';
$mdl = $this->myLib->getNewModel($this->models['base_model']);
$_data = $mdl->find($id);
$this->view_data['upload_data'] = $this->upload_data;
try {
if (!$mdl->delete($id)) {
throw new \CodeIgniter\Database\Exceptions\DatabaseException();
} else {
if (!$override) {
$this->setMessage(false, true, $this->msgLib->scs_del());
} else {
return $this->setReturn(true, NULL, $_data, NULL, NULL);
}
}
} catch (Exception $e) {
$this->setMessage(false, true, $this->msglib->err_del());
}
$this->render->viewAdmin($this->content_views['form_delete_view'], $this);
}
protected function stateView($id = 0)
{
$mdl = $this->myLib->getNewModel($this->models['base_model']);
$_data = $mdl->find($id);
$this->view_data['state'] = 'view';
$this->view_data['page_title'] = 'View ' . $this->pagetitle;
$this->view_data['_data'] = $_data;
$this->view_data['models'] = $this->models;
$this->render->viewAdmin($this->content_views['form_data_view'], $this);
}
protected function stateActiveStore()
{
$this->session->set('ps_store_id', $this->request->getVar('active_store'));
$this->setMessage(false, true, $this->msgLib->scs_activestore());
}
protected function stateRestore($id_column_name = '', $id = 0, $override = false)
{
$basedir = $this->myLib->getBaseDir($this->class) . '/';
$mdl = $this->myLib->getNewModel($this->models['base_model']);
$this->view_data['state'] = 'restore';
$this->view_data['base_controller'] = site_url($basedir . $this->class . '/trash');
$this->view_data['upload_data'] = $this->upload_data;
$_data = $mdl->onlyDeleted()->where($id_column_name, $id);
try {
if (!$mdl->update($id, ['deleted_at' => NULL])) {
exit;
throw new \CodeIgniter\Database\Exceptions\DatabaseException();
} else {
if (!$override) {
$this->setMessage(false, true, $this->msgLib->scs_restore());
} else {
return $this->setReturn(true, NULL, NULL, NULL, $_data);
}
}
} catch (Exception $e) {
$this->setMessage(true, false, $this->msglib->err_restore());
}
$this->render->viewAdmin($this->content_views['form_restore_view'], $this);
}
protected function stateDeletePermanent($id_column_name = '', $id = 0, $override = false)
{
$basedir = $this->myLib->getBaseDir($this->class) . '/';
$mdl = $this->myLib->getNewModel($this->models['base_model']);
$this->view_data['state'] = 'deletepermanent';
$this->view_data['base_controller'] = site_url($basedir . $this->class . '/trash');
$this->view_data['upload_data'] = $this->upload_data;
$_data = $mdl->onlyDeleted()->where($id_column_name, $id);
try {
if (!$_data->purgeDeleted()) {
throw new \CodeIgniter\Database\Exceptions\DatabaseException();
} else {
if (!$override) {
$this->setMessage(false, true, $this->msgLib->scs_del_permanent());
} else {
return $this->setReturn(true, NULL, NULL, $_data, NULL);
}
}
} catch (Exception $e) {
$this->setMessage(true, false, $this->msglib->err_del_permanent());
}
$this->render->viewAdmin($this->content_views['form_delete_permanent_view'], $this);
}
protected function deleteImageUploaded()
{
$p = $_POST['path'];
$id = $_POST['id'];
if (isset($_POST['path'])) {
$field = $this->upload_data['upload_field_name'];
$mdl = $this->myLib->getNewModel($this->models['base_model']);
$data = $mdl->where($field, $p)->first();
if (isset($data)) {
$mdl->update($id, [$this->upload_data['upload_field_name'] => NULL]);
}
if (file_exists(FCPATH . $this->upload_data['upload_path'] . $p)) {
unlink(FCPATH . $this->upload_data['upload_path'] . $p);
unset($_SESSION[$this->upload_data['upload_field_name']]);
}
} else {
if (isset($_SESSION[$this->upload_data['upload_field_name']]) && file_exists(FCPATH . $_SESSION[$this->upload_data['upload_field_name']])) {
unlink(FCPATH . $_SESSION[$this->upload_data['upload_field_name']]);
unset($_SESSION[$this->upload_data['upload_field_name']]);
}
}
$result = array(
'success' => FCPATH . $this->upload_data['upload_path'] . $p,
);
echo json_encode($result);
// exit;
}
protected function uploadImage()
{
if (!$this->validate($this->upload_data_validation_rule)) {
$this->post['warning'] = 'gagal';
$this->post['success'] = false;
$this->post['error'] = $this->validator->getErrors();
$this->post['tambah'] = 'tambah';
$data = array(
'is_success' => false,
'message' => $this->validator->getErrors()[$this->upload_data['upload_field_name']] . 'Please try again!'
);
echo json_encode($data);
} else {
$img_file = $this->request->getFile($this->upload_data['upload_field_name']);
$uploaded_data = '';
if (!$img_file->hasMoved()) {
$filename = $img_file->getRandomName();
$img_file->move(FCPATH . $this->upload_data['upload_path'], $filename);
$_SESSION[$this->upload_data['upload_field_name']] = $filename;
$data = array(
'is_success' => true,
'message' => 'Berhasil mengupload file!',
'filename' => $filename
);
echo json_encode($data);
}
}
// exit;
}
protected function indexLoad($is_where = false, $where_cond = '', $where_v1_var = '', $where_v1_op = '', $where_v1_string = '',
$is_where2 = false, $where_v2_var = '', $where_v2_op = '', $where_v2_string = '', $order_by = '', $sort = '')
{
if ($this->upload_data['upload_status']) {
$this->view_data['upload_data'] = $this->upload_data;
if ($this->session->get($this->upload_data['upload_field_name']) !== null && file_exists(FCPATH . $this->session->get($this->upload_data['upload_field_name']))) {
// unlink(FCPATH . $this->session->get($this->upload_data['upload_field_name']));
$this->session->remove($this->upload_data['upload_field_name']);
}
if (isset($this->upload_data['upload_field_name2'])) {
if ($this->session->get($this->upload_data['upload_field_name2']) !== null && file_exists(FCPATH . $this->session->get($this->upload_data['upload_field_name2']))) {
// unlink(FCPATH . $_SESSION[$this->upload_data['upload_field_name2']]);
$this->session->remove($this->upload_data['upload_field_name2']);
}
}
}
$this->session->set('trash', false);
$this->session->set('keyword', '');
$pagination = $this->myLib->get_all_data($this->models, $is_where, $where_v1_var, $where_v1_op, $where_v1_string, $is_where2, $where_v2_var, $where_v2_op, $where_v2_string, $order_by, $sort);
$this->view_data['_data'] = $pagination['_data'];
$this->view_data['models'] = $this->models;
$this->render->viewAdmin($this->content_views['table_view'], $this);
}
//BELUM DICEK
protected function indexLoadQuery($query)
{
if ($this->upload_data['upload_status']) {
if (isset($_SESSION[$this->upload_data['upload_field_name']]) && file_exists(FCPATH . $_SESSION[$this->upload_data['upload_field_name']])) {
unlink(FCPATH . $_SESSION[$this->upload_data['upload_field_name']]);
unset($_SESSION[$this->upload_data['upload_field_name']]);
}
if (isset($this->upload_data['upload_field_name2'])) {
if (isset($_SESSION[$this->upload_data['upload_field_name2']]) && file_exists(FCPATH . $_SESSION[$this->upload_data['upload_field_name2']])) {
unlink(FCPATH . $_SESSION[$this->upload_data['upload_field_name2']]);
unset($_SESSION[$this->upload_data['upload_field_name2']]);
}
}
}
$this->session->set('trash', false);
$this->session->set('keyword', '');
$pagination = $this->db->query($query)->result();
$this->view_data['_data'] = $pagination;
$this->view_data['models'] = $this->models;
$this->content_view = $this->content_views['table_view'];
}
protected function trashView()
{
$session = Services::session();
$myLib = new Mylib();
$session->set('trash', true);
$session->set('keyword', '');
$pagination = $myLib->get_all_trash($this->models['base_model']);
$this->view_data['_data'] = $pagination['_data'];
$this->view_data['page_title'] = $this->view_data['page_title'] . ' (Trashed)';
$render = new Render();
$render->viewAdmin($this->content_views['table_view'], $this);
}
protected function setURLActionTable($class, $arr = array())
{
$basedir = $this->myLib->getBaseDir($class) . '/';
$url_action_table = array(
'restore' => site_url($basedir . $class . '/action/restore/'),
'deletepermanent' => site_url($basedir . $class . '/action/deletepermanent/'),
'add' => site_url($basedir . $class . '/action/add'),
'edit' => site_url($basedir . $class . '/action/edit/'),
'delete' => site_url($basedir . $class . '/action/delete/'),
'view' => site_url($basedir . $class . '/action/view/'),
'trash' => site_url($basedir . $class . '/trash'),
'main' => site_url($basedir . $class),
'activestore' => site_url($basedir . $class),
);
$this->view_data['url_action_table'] = $url_action_table + $arr;
}
protected function setContentView($class, $arr = array())
{
$basedir = $this->myLib->getBaseDir($class) . '/';
$content_views = array(
'table_view' => 'admin/pages/custom/' . $basedir . $class . '/' . $class,
'form_add_view' => 'admin/pages/custom/' . $basedir . $class . '/' . $class . '_form',
'form_edit_view' => 'admin/pages/custom/' . $basedir . $class . '/' . $class . '_form',
'form_delete_view' => 'admin/pages/custom/' . $basedir . $class . '/' . $class . '_form',
'form_restore_view' => 'admin/pages/custom/' . $basedir . $class . '/' . $class . '_form',
'form_delete_permanent_view' => 'admin/pages/custom/' . $basedir . $class . '/' . $class . '_form',
'form_data_view' => 'admin/pages/custom/' . $basedir . $class . '/' . $class . '_view',
);
$this->content_views = $content_views + $arr;
}
protected function setPageTitleBreadcrumb($trash = false, $pagetitle = '', $breadcrumb = array())
{
if (!$trash) {
$this->session->set('trash', false);
}
$this->pagetitle = $pagetitle;
$this->breadcrumb = $breadcrumb;
$this->view_data['page_title'] = $pagetitle;
$this->view_data['breadcrumb'] = $breadcrumb;
}
protected function setBaseContAndModel($class, $base_model = '', $other_models = '')
{
$basedir = $this->myLib->getBaseDir($class) . '/';
$this->class = $class;
$this->view_data['base_controller'] = site_url($basedir . $class);
$this->models = array(
'base_model' => $base_model, // Base model controller.
'other_models' => $other_models, // Other models for JOIN query
);
}
protected function deleteFileUploaded($id, $state)
{
if (isset($_POST['path'])) {
$p = $_POST['path'];
$field = $this->upload_data['upload_field_name2'];
if ($state == 'edit') {
//Update DB
$_data = $this->myLib->getNewModel($this->models['base_model']);
$_datamodel = array();
$_datamodel[$field] = NULL;
$_data->update($id, $_datamodel);
}
if (file_exists(FCPATH . $p)) {
unlink(FCPATH . $p);
$this->session->remove($this->upload_data['upload_field_name2']);
}
} else {
if ($this->session->get($this->upload_data['upload_field_name2']) !== null && file_exists(FCPATH . $this->session->get($this->upload_data['upload_field_name2']))) {
unlink(FCPATH . $this->session->get($this->upload_data['upload_field_name2']));
$this->session->remove($this->upload_data['upload_field_name2']);
}
}
exit;
}
protected function uploadFile($id, $state)
{
$folder = $this->upload_data['upload_path2'];
$path = FCPATH . $folder;
$validated = $this->validate([
'popup_file' => [
'uploaded[popup_file]',
'mime_in[popup_file,application/pdf]',
'max_size[popup_file,5120]',
],
]);
if ($validated) {
$fileuploaded = $this->request->getFile('popup_file');
$newname = $fileuploaded->getRandomName();
if (!$fileuploaded->move($path, $newname)) {
$this->post['warning'] = 'gagal';
$this->post['success'] = false;
$this->post['error'] = 'Error during uploading data!';
$this->post['tambah'] = 'tambah';
$data = array(
'is_success' => false,
'message' => 'Error during uploading data!'
);
echo json_encode($data);
} else {
$file_path = $folder . $fileuploaded->getName();
if ($state == 'edit') {
//Update DB
$_data = $this->myLib->getNewModel($this->models['base_model']);
$_datamodel = array();
$_datamodel[$this->upload_data['upload_field_name2']] = $file_path;
$_data->update($id, $_datamodel);
}
$this->session->set($this->upload_data['upload_field_name2'], $file_path);
$data = array(
'is_success' => true,
'message' => 'File successfuly uploaded!'
);
echo json_encode($data);
}
} else {
$data = array(
'is_success' => false,
'message' => 'File is not allowed/oversized!'
);
echo json_encode($data);
}
exit;
}
}