mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Eliminados ajustes de sistemas. Ahora estan en variables del ERP
This commit is contained in:
@ -17,10 +17,10 @@ namespace App\Controllers;
|
||||
*/
|
||||
use CodeIgniter\Controller;
|
||||
use CodeIgniter\Database\Query;
|
||||
use App\Models\NotificationModel;
|
||||
|
||||
|
||||
abstract class GoBaseController extends Controller {
|
||||
abstract class GoBaseController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
@ -102,7 +102,7 @@ abstract class GoBaseController extends Controller {
|
||||
* @var array
|
||||
*/
|
||||
public $viewData;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* JJO: Variable para indicar si el controlador hace soft_delete o no
|
||||
@ -139,14 +139,15 @@ abstract class GoBaseController extends Controller {
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $helpers = ['session', 'go_common', 'text', 'general','jwt', 'rbac']; //JJO
|
||||
protected $helpers = ['session', 'go_common', 'text', 'general', 'jwt', 'rbac']; //JJO
|
||||
|
||||
public static $queries = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) {
|
||||
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
|
||||
{
|
||||
// Do Not Edit This Line
|
||||
parent::initController($request, $response, $logger);
|
||||
|
||||
@ -155,9 +156,9 @@ abstract class GoBaseController extends Controller {
|
||||
//--------------------------------------------------------------------
|
||||
// E.g.:
|
||||
$this->session = \Config\Services::session();
|
||||
|
||||
|
||||
if ((!isset($this->viewData['pageTitle']) || empty($this->viewData['pageTitle']) ) && isset(static::$pluralObjectName) && !empty(static::$pluralObjectName)) {
|
||||
|
||||
if ((!isset($this->viewData['pageTitle']) || empty($this->viewData['pageTitle'])) && isset(static::$pluralObjectName) && !empty(static::$pluralObjectName)) {
|
||||
$this->viewData['pageTitle'] = ucfirst(static::$pluralObjectName);
|
||||
}
|
||||
|
||||
@ -171,7 +172,7 @@ abstract class GoBaseController extends Controller {
|
||||
if (empty(static::$controllerSlug)) {
|
||||
$reflect = new \ReflectionClass($this);
|
||||
$className = $reflect->getShortName();
|
||||
$this->viewData['currentModule'] = slugify(convertToSnakeCase(str_replace('Controller','',$className)));
|
||||
$this->viewData['currentModule'] = slugify(convertToSnakeCase(str_replace('Controller', '', $className)));
|
||||
|
||||
} else {
|
||||
$this->viewData['currentModule'] = strtolower(static::$controllerSlug);
|
||||
@ -185,43 +186,31 @@ abstract class GoBaseController extends Controller {
|
||||
$this->model = &$this->primaryModel;
|
||||
}
|
||||
|
||||
// Preload any models, libraries, etc, here.
|
||||
// Preload any models, libraries, etc, here.
|
||||
|
||||
|
||||
|
||||
// Language Validate
|
||||
$language = \Config\Services::language();
|
||||
$language->setLocale($this->session->lang);
|
||||
|
||||
// Set TimeZone
|
||||
if(empty($this->session->get('settings'))){
|
||||
$settingsModel = new SettingsModel();
|
||||
$settings = $settingsModel->select('default_timezone')->first()??[];
|
||||
date_default_timezone_set($this->$settings['default_timezone']??'America/Sao_Paulo');
|
||||
}else{
|
||||
date_default_timezone_set($this->session->get('settings')['default_timezone']??'America/Sao_Paulo');
|
||||
if (empty($this->session->get('settings'))) {
|
||||
$time_zone = model('App\Models\Configuracion\ConfigVariableModel')->getVariable('default_timezone')->value;
|
||||
date_default_timezone_set($time_zone ?? 'Europe/Madrid');
|
||||
} else {
|
||||
date_default_timezone_set($this->session->get('settings')['default_timezone'] ?? 'Europe/Madrid');
|
||||
}
|
||||
|
||||
// Get notification
|
||||
if(!empty($this->session->get('token'))) {
|
||||
$notificationModel = new NotificationModel();
|
||||
$pulse = $notificationModel->where('user_recipient',$this->session->get('token'))->where('is_read',false)->countAllResults() ?? 0;
|
||||
$notification = $notificationModel->select('token,title,is_read,created_at')->where('user_recipient',$this->session->get('token'))->orderBy('created_at','desc')->findAll(5) ?? [];
|
||||
$this->session->set('notification', $notification);
|
||||
$this->session->set('pulse', $pulse);
|
||||
}else{
|
||||
$this->session->set('notification', []);
|
||||
$this->session->set('pulse', 0);
|
||||
}
|
||||
|
||||
$this->viewData['currentLocale'] = $this->request->getLocale();
|
||||
|
||||
}
|
||||
|
||||
public function index() {
|
||||
public function index()
|
||||
{
|
||||
|
||||
helper('text');
|
||||
|
||||
if ((!isset($this->viewData['boxTitle']) || empty($this->viewData['boxTitle']) ) && isset(static::$pluralObjectName) && !empty(static::$pluralObjectName)) {
|
||||
if ((!isset($this->viewData['boxTitle']) || empty($this->viewData['boxTitle'])) && isset(static::$pluralObjectName) && !empty(static::$pluralObjectName)) {
|
||||
$this->viewData['boxTitle'] = ucfirst(static::$pluralObjectName);
|
||||
}
|
||||
|
||||
@ -236,10 +225,10 @@ abstract class GoBaseController extends Controller {
|
||||
|
||||
// if $this->currentView is assigned a view name, use it, otherwise assume the view something like 'viewSingleObjectList'
|
||||
$viewFilePath = static::$viewPath . (empty($this->currentView) ? 'view' . ucfirst(static::$singularObjectNameCc) . 'List' : $this->currentView);
|
||||
|
||||
|
||||
echo view($viewFilePath, $this->viewData);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -248,12 +237,13 @@ abstract class GoBaseController extends Controller {
|
||||
* @param null $objId
|
||||
* @return string
|
||||
*/
|
||||
protected function displayForm($forMethod, $objId = null) {
|
||||
protected function displayForm($forMethod, $objId = null)
|
||||
{
|
||||
|
||||
helper('form');
|
||||
$this->viewData['usingSelect2'] = true;
|
||||
|
||||
$validation = \Config\Services::validation();
|
||||
|
||||
$validation = \Config\Services::validation();
|
||||
|
||||
$action = str_replace(static::class . '::', '', $forMethod);
|
||||
$actionSuffix = ' ';
|
||||
@ -270,13 +260,13 @@ abstract class GoBaseController extends Controller {
|
||||
}
|
||||
|
||||
if (!isset($this->viewData['formAction'])) {
|
||||
$this->viewData['formAction'] = base_url(strtolower($this->viewData['currentModule']) . '/' . $action . '/' . $formActionSuffix);
|
||||
$this->viewData['formAction'] = base_url(strtolower($this->viewData['currentModule']) . '/' . $action . '/' . $formActionSuffix);
|
||||
}
|
||||
|
||||
if ((!isset($this->viewData['boxTitle']) || empty($this->viewData['boxTitle']) ) && isset(static::$singularObjectName) && !empty(static::$singularObjectName)) {
|
||||
if ((!isset($this->viewData['boxTitle']) || empty($this->viewData['boxTitle'])) && isset(static::$singularObjectName) && !empty(static::$singularObjectName)) {
|
||||
$this->viewData['boxTitle'] = ucfirst($action) . $actionSuffix . ucfirst(static::$singularObjectName);
|
||||
}
|
||||
|
||||
|
||||
$this->viewData['validation'] = $validation;
|
||||
|
||||
$viewFilePath = static::$viewPath . 'view' . ucfirst(static::$singularObjectNameCc) . 'Form';
|
||||
@ -284,7 +274,8 @@ abstract class GoBaseController extends Controller {
|
||||
return view($viewFilePath, $this->viewData);
|
||||
}
|
||||
|
||||
protected function redirect2listView($flashDataKey = null, $flashDataValue = null) {
|
||||
protected function redirect2listView($flashDataKey = null, $flashDataValue = null)
|
||||
{
|
||||
|
||||
if (!empty($this->indexRoute)) {
|
||||
$uri = base_url(route_to($this->indexRoute));
|
||||
@ -304,9 +295,9 @@ abstract class GoBaseController extends Controller {
|
||||
} else {
|
||||
$getHandlingRoutes = $routes->getRoutes('get');
|
||||
|
||||
$indexMethod = array_search('\\App\\Controllers\\'.$className.'::index', $getHandlingRoutes);
|
||||
$indexMethod = array_search('\\App\\Controllers\\' . $className . '::index', $getHandlingRoutes);
|
||||
if ($indexMethod) {
|
||||
$uri = route_to('App\\Controllers\\'.$className.'::index');
|
||||
$uri = route_to('App\\Controllers\\' . $className . '::index');
|
||||
} else {
|
||||
$uri = base_url(static::$controllerSlug);
|
||||
}
|
||||
@ -315,7 +306,7 @@ abstract class GoBaseController extends Controller {
|
||||
$uri = base_url($className);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($flashDataKey != null && $flashDataValue != null) {
|
||||
return redirect()->to($uri)->with($flashDataKey, $flashDataValue);
|
||||
} else {
|
||||
@ -323,10 +314,11 @@ abstract class GoBaseController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
public function delete($requestedId, bool $deletePermanently = true) {
|
||||
public function delete($requestedId, bool $deletePermanently = true)
|
||||
{
|
||||
|
||||
if (is_string($requestedId)) :
|
||||
if (is_numeric($requestedId)) :
|
||||
if (is_string($requestedId)):
|
||||
if (is_numeric($requestedId)):
|
||||
$id = filter_var($requestedId, FILTER_SANITIZE_NUMBER_INT);
|
||||
else:
|
||||
$onlyAlphaNumeric = true;
|
||||
@ -338,64 +330,66 @@ abstract class GoBaseController extends Controller {
|
||||
$id = intval($requestedId);
|
||||
endif;
|
||||
|
||||
if (empty($id) || $id === 0) :
|
||||
if (empty($id) || $id === 0):
|
||||
$error = 'Invalid identifier provided to delete the object.';
|
||||
endif;
|
||||
|
||||
$rawResult = null;
|
||||
|
||||
if (!isset($error)) :
|
||||
if (!isset($error)):
|
||||
try {
|
||||
if ($deletePermanently && !$this->soft_delete) :
|
||||
if (is_numeric($id)) :
|
||||
$rawResult = $this->primaryModel->delete($id);
|
||||
else:
|
||||
$rawResult = $this->primaryModel->where($this->primaryModel->getPrimaryKeyName(), $id)->delete();
|
||||
endif;
|
||||
elseif ($this->soft_delete):
|
||||
$datetime = (new \CodeIgniter\I18n\Time("now"));
|
||||
$rawResult = $this->primaryModel->where('id',$id)
|
||||
->set(['deleted_at' => $datetime->format('Y-m-d H:i:s'),
|
||||
'is_deleted' => $this->delete_flag])
|
||||
->update();
|
||||
if ($deletePermanently && !$this->soft_delete):
|
||||
if (is_numeric($id)):
|
||||
$rawResult = $this->primaryModel->delete($id);
|
||||
else:
|
||||
$rawResult = $this->primaryModel->where($this->primaryModel->getPrimaryKeyName(), $id)->delete();
|
||||
endif;
|
||||
elseif ($this->soft_delete):
|
||||
$datetime = (new \CodeIgniter\I18n\Time("now"));
|
||||
$rawResult = $this->primaryModel->where('id', $id)
|
||||
->set([
|
||||
'deleted_at' => $datetime->format('Y-m-d H:i:s'),
|
||||
'is_deleted' => $this->delete_flag
|
||||
])
|
||||
->update();
|
||||
|
||||
else:
|
||||
$rawResult = $this->primaryModel->update($id, ['deleted' => true]);
|
||||
endif;
|
||||
else:
|
||||
$rawResult = $this->primaryModel->update($id, ['deleted' => true]);
|
||||
endif;
|
||||
} catch (\Exception $e) {
|
||||
log_message('error', "Exception: Error deleting object named '".(static::$singularObjectName ?? 'unknown')."' with $id :\r\n".$e->getMessage());
|
||||
log_message('error', "Exception: Error deleting object named '" . (static::$singularObjectName ?? 'unknown') . "' with $id :\r\n" . $e->getMessage());
|
||||
}
|
||||
endif;
|
||||
|
||||
$ar = $this->primaryModel->db->affectedRows();
|
||||
|
||||
|
||||
try {
|
||||
$dbError = $this->primaryModel->db->error();
|
||||
} catch (\Exception $e2) {
|
||||
if ($e2->getMessage() != "Trying to get property 'errno' of non-object") {
|
||||
log_message('error', $e2->getCode() . ' : ' . $e2->getMessage()) ;
|
||||
log_message('error', $e2->getCode() . ' : ' . $e2->getMessage());
|
||||
}
|
||||
}
|
||||
if (isset($dbError['code']) && isset($dbError['message'])) {
|
||||
log_message('error', $dbError['code'].' '.$dbError['message']);
|
||||
log_message('error', $dbError['code'] . ' ' . $dbError['message']);
|
||||
} else {
|
||||
$dbError = ['code' => '', 'message'=>''];
|
||||
$dbError = ['code' => '', 'message' => ''];
|
||||
}
|
||||
|
||||
$result = ['persisted'=>$ar>0, 'ar'=>$ar, 'persistedId'=>null, 'affectedRows'=>$ar, 'errorCode'=>$dbError['code'], 'error'=>$dbError['message']];
|
||||
|
||||
$result = ['persisted' => $ar > 0, 'ar' => $ar, 'persistedId' => null, 'affectedRows' => $ar, 'errorCode' => $dbError['code'], 'error' => $dbError['message']];
|
||||
|
||||
$nameOfDeletedObject = static::$singularObjectNameCc;
|
||||
|
||||
if ($ar < 1) :
|
||||
|
||||
if ($ar < 1):
|
||||
$errorMessage = lang('Basic.global.deleteError', [$nameOfDeletedObject]); // 'No ' . static::$singularObjectName . ' was deleted now, because it probably had already been deleted.';
|
||||
$fdKey = isset($this->viewData['usingSweetAlert'] ) && $this->viewData['usingSweetAlert'] ? 'sweet-error' : 'errorMessage';
|
||||
$fdKey = isset($this->viewData['usingSweetAlert']) && $this->viewData['usingSweetAlert'] ? 'sweet-error' : 'errorMessage';
|
||||
$errorMessage = str_replace("'", "\'", $errorMessage);
|
||||
return $this->redirect2listView($fdKey, str_replace("'", '', $errorMessage));
|
||||
else:
|
||||
$message = lang('Basic.global.deleteSuccess', [$nameOfDeletedObject]); // 'The ' . static::$singularObjectName . ' was successfully deleted.';
|
||||
$fdKey = isset($this->viewData['usingSweetAlert'] ) && $this->viewData['usingSweetAlert'] ? 'sweet-success' : 'successMessage';
|
||||
if ($result['affectedRows']>1) :
|
||||
log_message('warning', "More than one row has been deleted in attempt to delete row for object named '".(static::$singularObjectName ?? 'unknown')."' with id: $id");
|
||||
$fdKey = isset($this->viewData['usingSweetAlert']) && $this->viewData['usingSweetAlert'] ? 'sweet-success' : 'successMessage';
|
||||
if ($result['affectedRows'] > 1):
|
||||
log_message('warning', "More than one row has been deleted in attempt to delete row for object named '" . (static::$singularObjectName ?? 'unknown') . "' with id: $id");
|
||||
endif;
|
||||
$message = str_replace("'", "\'", $message);
|
||||
return $this->redirect2listView($fdKey, $message);
|
||||
@ -403,7 +397,7 @@ abstract class GoBaseController extends Controller {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Convenience method to validate form submission
|
||||
* @return bool
|
||||
*/
|
||||
@ -416,14 +410,15 @@ abstract class GoBaseController extends Controller {
|
||||
return true;
|
||||
}
|
||||
|
||||
$validationErrorMessages = $this->model->validationMessages ?? $this->formValidationErrorMessagess ?? null;;
|
||||
$validationErrorMessages = $this->model->validationMessages ?? $this->formValidationErrorMessagess ?? null;
|
||||
;
|
||||
|
||||
if ($validationErrorMessages != null) {
|
||||
$valid = $this->validate($validationRules, $validationErrorMessages);
|
||||
} else {
|
||||
$valid = $this->validate($validationRules);
|
||||
}
|
||||
|
||||
|
||||
$this->validationErrors = $valid ? '' : $this->validator->getErrors();
|
||||
|
||||
/*
|
||||
@ -443,7 +438,8 @@ abstract class GoBaseController extends Controller {
|
||||
* @param array|null $postData
|
||||
* @return array
|
||||
*/
|
||||
protected function sanitized(array $postData = null, bool $nullIfEmpty = false) {
|
||||
protected function sanitized(array $postData = null, bool $nullIfEmpty = false)
|
||||
{
|
||||
if ($postData == null) {
|
||||
$postData = $this->request->getPost();
|
||||
}
|
||||
@ -462,29 +458,31 @@ abstract class GoBaseController extends Controller {
|
||||
* Convenience method for common exception handling
|
||||
* @param \Exception $e
|
||||
*/
|
||||
protected function dealWithException(\Exception $e) {
|
||||
protected function dealWithException(\Exception $e)
|
||||
{
|
||||
// using another try / catch block to prevent to avoid CodeIgniter bug throwing trivial exceptions for querying DB errors
|
||||
try {
|
||||
$query = $this->model->db->getLastQuery();
|
||||
$queryStr = !is_null($query) ? $query->getQuery() : '';
|
||||
$dbError = $this->model->db->error();
|
||||
$userFriendlyErrMsg = lang('Basic.global.persistErr1', [static::$singularObjectNameCc]);
|
||||
if (isset($dbError['code']) && $dbError['code'] == 1062) :
|
||||
$userFriendlyErrMsg .= PHP_EOL.lang('Basic.global.persistDuplErr', [static::$singularObjectNameCc]);
|
||||
if (isset($dbError['code']) && $dbError['code'] == 1062):
|
||||
$userFriendlyErrMsg .= PHP_EOL . lang('Basic.global.persistDuplErr', [static::$singularObjectNameCc]);
|
||||
endif;
|
||||
// $userFriendlyErrMsg = str_replace("'", "\'", $userFriendlyErrMsg); // Uncomment if experiencing unescaped single quote errors
|
||||
log_message('error', $userFriendlyErrMsg.PHP_EOL.$e->getMessage().PHP_EOL.$queryStr);
|
||||
if (isset($dbError['message']) && !empty($dbError['message'])) :
|
||||
log_message('error', $dbError['code'].' : '.$dbError['message']);
|
||||
log_message('error', $userFriendlyErrMsg . PHP_EOL . $e->getMessage() . PHP_EOL . $queryStr);
|
||||
if (isset($dbError['message']) && !empty($dbError['message'])):
|
||||
log_message('error', $dbError['code'] . ' : ' . $dbError['message']);
|
||||
endif;
|
||||
$this->viewData['errorMessage'] = $userFriendlyErrMsg;
|
||||
} catch (\Exception $e2) {
|
||||
log_message('debug', 'You can probably safely ignore this: In attempt to check DB errors, CodeIgniter threw: '.PHP_EOL.$e2->getMessage());
|
||||
log_message('debug', 'You can probably safely ignore this: In attempt to check DB errors, CodeIgniter threw: ' . PHP_EOL . $e2->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Collect the queries so something can be done with them later.
|
||||
public static function collect(Query $query) {
|
||||
public static function collect(Query $query)
|
||||
{
|
||||
static::$queries[] = $query;
|
||||
}
|
||||
|
||||
@ -495,7 +493,8 @@ abstract class GoBaseController extends Controller {
|
||||
* @param object $sourceObject
|
||||
* @return object
|
||||
*/
|
||||
function cast($destination, $sourceObject) {
|
||||
function cast($destination, $sourceObject)
|
||||
{
|
||||
if (is_string($destination)) {
|
||||
$destination = new $destination();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user