mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
306 lines
12 KiB
PHP
306 lines
12 KiB
PHP
<?php namespace App\Controllers\Tarifas;
|
|
|
|
|
|
use App\Entities\Tarifas\TarifaacabadoEntity;
|
|
|
|
class Tarifaacabado extends \App\Controllers\GoBaseController {
|
|
|
|
use \CodeIgniter\API\ResponseTrait;
|
|
|
|
protected static $primaryModelName = 'App\Models\Tarifas\TarifaacabadoModel';
|
|
|
|
protected static $singularObjectNameCc = 'tarifaacabado';
|
|
protected static $singularObjectName = 'Tarifaacabado';
|
|
protected static $pluralObjectName = 'Tarifasacabado';
|
|
protected static $controllerSlug = 'tarifaacabado';
|
|
|
|
protected static $viewPath = 'themes/backend/vuexy/form/tarifas/acabado/';
|
|
protected $indexRoute = 'tarifaacabadoList';
|
|
|
|
|
|
|
|
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) {
|
|
$this->viewData['pageTitle'] = lang('Tarifaacabado.moduleTitle');
|
|
$this->isDelete = true;
|
|
parent::initController($request, $response, $logger);
|
|
|
|
}
|
|
|
|
public function index() {
|
|
|
|
$this->viewData['usingClientSideDataTable'] = true;
|
|
|
|
$this->viewData['pageSubTitle'] = lang('Basic.global.ManageAllRecords', [lang('Tarifaacabado.tarifaacabado')]);
|
|
parent::index();
|
|
|
|
}
|
|
|
|
public function add() {
|
|
|
|
$session = session();
|
|
|
|
$requestMethod = $this->request->getMethod();
|
|
|
|
if ($requestMethod === 'post') :
|
|
|
|
$nullIfEmpty = true; // !(phpversion() >= '8.1');
|
|
|
|
$postData = $this->request->getPost();
|
|
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
|
|
$sanitizedData['user_created_id'] = $session->id_user;
|
|
$sanitizedData['user_update_id'] = $session->id_user;
|
|
|
|
$noException = true;
|
|
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
|
|
|
|
|
|
if ($this->canValidate()) :
|
|
try {
|
|
$successfulResult = $this->model->skipValidation(true)->save($sanitizedData);
|
|
} catch (\Exception $e) {
|
|
$noException = false;
|
|
$this->dealWithException($e);
|
|
}
|
|
else:
|
|
$this->viewData['errorMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Tarifaacabado.tarifaacabado'))]);
|
|
$this->session->setFlashdata('formErrors', $this->model->errors());
|
|
endif;
|
|
|
|
$thenRedirect = true; // Change this to false if you want your user to stay on the form after submission
|
|
endif;
|
|
if ($noException && $successfulResult) :
|
|
|
|
$id = $this->model->db->insertID();
|
|
|
|
$message = lang('Basic.global.saveSuccess', [mb_strtolower(lang('Tarifaacabado.tarifaacabado'))]).'.';
|
|
$message .= anchor(route_to('editTarifaacabado', $id), lang('Basic.global.continueEditing').'?');
|
|
$message = ucfirst(str_replace("'", "\'", $message));
|
|
|
|
if ($thenRedirect) :
|
|
if (!empty($this->indexRoute)) :
|
|
return redirect()->to(route_to($this->indexRoute))->with('successMessage', $message);
|
|
else:
|
|
return $this->redirect2listView('successMessage', $message);
|
|
endif;
|
|
else:
|
|
$this->viewData['successMessage'] = $message;
|
|
endif;
|
|
|
|
endif; // $noException && $successfulResult
|
|
|
|
endif; // ($requestMethod === 'post')
|
|
|
|
$this->viewData['tarifaacabado_'] = isset($sanitizedData) ? new TarifaacabadoEntity($sanitizedData) : new TarifaacabadoEntity();
|
|
|
|
$this->viewData['formAction'] = route_to('createTarifaacabado');
|
|
|
|
$this->viewData['boxTitle'] = lang('Basic.global.addNew').' '.lang('Tarifaacabado.tarifaacabado').' '.lang('Basic.global.addNewSuffix');
|
|
|
|
|
|
return $this->displayForm(__METHOD__);
|
|
} // end function add()
|
|
|
|
public function edit($requestedId = null) {
|
|
|
|
$session = session();
|
|
|
|
if ($requestedId == null) :
|
|
return $this->redirect2listView();
|
|
endif;
|
|
$id = filter_var($requestedId, FILTER_SANITIZE_URL);
|
|
$tarifaacabado_ = $this->model->find($id);
|
|
|
|
if ($tarifaacabado_ == false) :
|
|
$message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Tarifaacabado.tarifaacabado')), $id]);
|
|
return $this->redirect2listView('errorMessage', $message);
|
|
endif;
|
|
|
|
$requestMethod = $this->request->getMethod();
|
|
|
|
if ($requestMethod === 'post') :
|
|
|
|
$nullIfEmpty = true; // !(phpversion() >= '8.1');
|
|
|
|
$postData = $this->request->getPost();
|
|
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
|
|
|
|
$sanitizedData['user_update_id'] = $session->id_user;
|
|
|
|
$noException = true;
|
|
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
|
|
|
|
|
|
|
|
if ($this->canValidate()) :
|
|
try {
|
|
$successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData);
|
|
} catch (\Exception $e) {
|
|
$noException = false;
|
|
$this->dealWithException($e);
|
|
}
|
|
else:
|
|
$this->viewData['warningMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Tarifaacabado.tarifaacabado'))]);
|
|
$this->session->setFlashdata('formErrors', $this->model->errors());
|
|
|
|
endif;
|
|
|
|
$tarifaacabado_->fill($sanitizedData);
|
|
|
|
$thenRedirect = true;
|
|
endif;
|
|
if ($noException && $successfulResult) :
|
|
$id = $tarifaacabado_->id ?? $id;
|
|
$message = lang('Basic.global.updateSuccess', [mb_strtolower(lang('Tarifaacabado.tarifaacabado'))]).'.';
|
|
$message .= anchor(route_to('editTarifaacabado', $id), lang('Basic.global.continueEditing').'?');
|
|
$message = ucfirst(str_replace("'", "\'", $message));
|
|
|
|
if ($thenRedirect) :
|
|
if (!empty($this->indexRoute)) :
|
|
return redirect()->to(route_to($this->indexRoute))->with('successMessage', $message);
|
|
else:
|
|
return $this->redirect2listView('successMessage', $message);
|
|
endif;
|
|
else:
|
|
$this->viewData['successMessage'] = $message;
|
|
endif;
|
|
|
|
endif; // $noException && $successfulResult
|
|
endif; // ($requestMethod === 'post')
|
|
|
|
$this->viewData['tarifaacabado_'] = $tarifaacabado_;
|
|
|
|
$this->viewData['formAction'] = route_to('updateTarifaacabado', $id);
|
|
|
|
$this->viewData['boxTitle'] = lang('Basic.global.edit2').' '.lang('Tarifaacabado.tarifaacabado').' '.lang('Basic.global.edit3');
|
|
|
|
|
|
return $this->displayForm(__METHOD__, $id);
|
|
} // end function edit(...)
|
|
|
|
|
|
|
|
public function allItemsSelect() {
|
|
if ($this->request->isAJAX()) {
|
|
$onlyActiveOnes = true;
|
|
$reqVal = $this->request->getPost('val') ?? 'id';
|
|
$menu = $this->model->getAllForMenu($reqVal.', Select a field...', 'Select a field...', $onlyActiveOnes, false);
|
|
$nonItem = new \stdClass;
|
|
$nonItem->id = '';
|
|
$nonItem->Selectafield = '- '.lang('Basic.global.None').' -';
|
|
array_unshift($menu , $nonItem);
|
|
|
|
$newTokenHash = csrf_hash();
|
|
$csrfTokenName = csrf_token();
|
|
$data = [
|
|
'menu' => $menu,
|
|
$csrfTokenName => $newTokenHash
|
|
];
|
|
return $this->respond($data);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function menuItems() {
|
|
if ($this->request->isAJAX()) {
|
|
$searchStr = goSanitize($this->request->getPost('searchTerm'))[0];
|
|
$reqId = goSanitize($this->request->getPost('id'))[0];
|
|
$reqText = goSanitize($this->request->getPost('text'))[0];
|
|
$onlyActiveOnes = false;
|
|
$columns2select = [$reqId ?? 'id', $reqText ?? 'Select a field...'];
|
|
$onlyActiveOnes = false;
|
|
$menu = $this->model->getSelect2MenuItems($columns2select, $columns2select[1], $onlyActiveOnes, $searchStr);
|
|
$nonItem = new \stdClass;
|
|
$nonItem->id = '';
|
|
$nonItem->text = '- '.lang('Basic.global.None').' -';
|
|
array_unshift($menu , $nonItem);
|
|
|
|
$newTokenHash = csrf_hash();
|
|
$csrfTokenName = csrf_token();
|
|
$data = [
|
|
'menu' => $menu,
|
|
$csrfTokenName => $newTokenHash
|
|
];
|
|
return $this->respond($data);
|
|
} else {
|
|
return $this->failUnauthorized('Invalid request', 403);
|
|
}
|
|
}
|
|
|
|
public function delete($requestedId, bool $deletePermanently = false) {
|
|
|
|
if (is_string($requestedId)) :
|
|
if (is_numeric($requestedId)) :
|
|
$id = filter_var($requestedId, FILTER_SANITIZE_NUMBER_INT);
|
|
else:
|
|
$onlyAlphaNumeric = true;
|
|
$fromGetRequest = true;
|
|
$idSanitization = goSanitize($requestedId, $onlyAlphaNumeric, $fromGetRequest); // filter_var(trim($requestedId), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
|
|
$id = $idSanitization[0];
|
|
endif;
|
|
else:
|
|
$id = intval($requestedId);
|
|
endif;
|
|
|
|
if (empty($id) || $id === 0) :
|
|
$error = 'Invalid identifier provided to delete the object.';
|
|
endif;
|
|
|
|
$rawResult = null;
|
|
|
|
if (!isset($error)) :
|
|
try {
|
|
if ($deletePermanently) :
|
|
if (is_numeric($id)) :
|
|
$rawResult = $this->primaryModel->delete($id);
|
|
else:
|
|
$rawResult = $this->primaryModel->where($this->primaryModel->getPrimaryKeyName(), $id)->delete();
|
|
endif;
|
|
else:
|
|
$datetime = (new \CodeIgniter\I18n\Time("now"));
|
|
$rawResult = $this->primaryModel->where('id',$id)
|
|
->set(['deleted_at' => $datetime->format('Y-m-d H:i:s')])
|
|
->update();
|
|
endif;
|
|
} catch (\Exception $e) {
|
|
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()) ;
|
|
}
|
|
}
|
|
if (isset($dbError['code']) && isset($dbError['message'])) {
|
|
log_message('error', $dbError['code'].' '.$dbError['message']);
|
|
} else {
|
|
$dbError = ['code' => '', 'message'=>''];
|
|
}
|
|
|
|
$result = ['persisted'=>$ar>0, 'ar'=>$ar, 'persistedId'=>null, 'affectedRows'=>$ar, 'errorCode'=>$dbError['code'], 'error'=>$dbError['message']];
|
|
|
|
$nameOfDeletedObject = static::$singularObjectNameCc;
|
|
|
|
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';
|
|
$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");
|
|
endif;
|
|
$message = str_replace("'", "\'", $message);
|
|
return $this->redirect2listView($fdKey, $message);
|
|
endif;
|
|
}
|
|
|
|
}
|