terminadas respuesta. falta repasar todo y cabecera datatable y permisos menus y botones

This commit is contained in:
2025-02-17 20:56:32 +01:00
parent 809a705c88
commit 3cdc38b822
10 changed files with 193 additions and 95 deletions

View File

@ -16,7 +16,7 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
{
protected $modelName = TicketModel::class;
protected $format = 'json';
protected $format = 'json';
protected static $singularObjectNameCc = 'ticket';
protected static $singularObjectName = 'Ticket';
@ -25,13 +25,13 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
protected static $viewPath = 'themes/vuexy/form/soporte/';
protected $indexRoute = 'viewTicketList';
protected $indexRoute = 'ticketList';
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
$this->viewData['pageTitle'] = lang('Tickets.moduleTitle');
// Breadcrumbs
$this->viewData['breadcrumb'] = [
['title' => lang("App.menu_soporte"), 'route' => "javascript:void(0);", 'active' => false],
@ -49,20 +49,19 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
'currentModule' => static::$controllerSlug,
'pageSubTitle' => lang('Basic.global.ManageAllRecords', [lang('Tickets.tickets')]),
'usingServerSideDataTable' => true,
'userType' => auth()->user()->can('tickets.edit')? 1:0,
'userType' => auth()->user()->can('tickets.edit') ? 1 : 0,
];
$viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class
return view(static::$viewPath . 'viewTicketList', $viewData);
}
public function add()
{
//checkPermission('tickets.create', $this->indexRoute);
if ($this->request->getPost()) :
$nullIfEmpty = false; // !(phpversion() >= '8.1');
@ -97,33 +96,7 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
$id = $this->model->db->insertID();
$uploadPath = WRITEPATH . 'uploads/tickets/';
$fileModel = new ticketFileModel();
$files = $this->request->getFiles();
if ($files && isset($files['files'])) {
foreach ($files['files'] as $file) {
if ($file->isValid() && !$file->hasMoved()) {
$originalName = $file->getClientName();
$fileExt = $file->getExtension();
// Generar hash SHA-256 basado en el contenido del archivo
$fileHash = hash_file("sha256", $file->getTempName());
$newFileName = $fileHash . '.' . $fileExt;
// Mover el archivo con el nombre basado en el hash
$file->move($uploadPath, $newFileName);
// Guardar en la base de datos
$fileModel->insert([
'nombre' => $originalName,
'ticket_id' => $id,
'hash' => $fileHash,
'path' => 'uploads/tickets/' . $newFileName
]);
}
}
}
$this->saveImages($id, $this->request->getFiles());
$message = lang('Basic.global.saveSuccess', [lang('Basic.global.record')]) . '.';
@ -149,6 +122,8 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
$this->viewData['estados'] = $this->model->getEstados();
$this->viewData['secciones'] = $this->model->getSecciones();
$this->viewData['supportUsers'] = $this->getSupportUsers();
$this->viewData['boxTitle'] = lang('Basic.global.addNew') . ' ' . lang('Tickets.ticket') . ' ' . lang('Basic.global.addNewSuffix');
@ -157,7 +132,8 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
public function edit($requestedId = null)
{
$modelRespuesta = new \App\Models\Soporte\TicketRespuestaModel();
if ($requestedId == null) :
return $this->redirect2listView();
endif;
@ -169,16 +145,42 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
return $this->redirect2listView('errorMessage', $message);
endif;
if(!auth()->user()->can('Tickets.edit') && auth()->user()->id != $ticket->usuario_id){
return redirect()->to(route_to('TicketIndex'))->with('errorMessage', lang('Basic.global.noPermission'));
}
if ($this->request->getPost()) :
$postData = $this->request->getPost();
$sanitizedData = $this->sanitized($postData, false);
$sanitizedData = $this->sanitized($postData, false);
$noException = true;
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
if ($this->canValidate()) :
try {
$successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData);
$this->saveImages($id, $this->request->getFiles());
if(auth()->user()->can('Tickets.edit')){
$respuesta = $modelRespuesta->where('ticket_id', $id)->first();
if($respuesta == null){
$modelRespuesta->insert([
'ticket_id' => $id,
'usuario_id' => auth()->user()->id,
'mensaje' => $sanitizedData['respuesta_mensaje']
]);
}else{
$modelRespuesta->update($respuesta->id, [
'mensaje' => $sanitizedData['respuesta_mensaje'],
'usuario_id' => auth()->user()->id,
]);
}
}
} catch (\Exception $e) {
$noException = false;
$this->dealWithException($e);
@ -204,34 +206,22 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
return $this->redirect2listView('successMessage', $message);
endif;
else:
$this->session->setFlashData('sweet-success', $message);
return redirect()->to(route_to("editTicket", $id))->with('successMessage', $message);
//$this->session->setFlashData('sweet-success', $message);
endif;
endif; // $noException && $successfulResult
endif; // ($requestMethod === 'post')
$this->viewData['ticket'] = $ticket;
$this->viewData['respuesta'] = $modelRespuesta->where('ticket_id', $id)->first();
$this->viewData['formAction'] = route_to('updateTicket', $id);
$this->viewData['categorias'] = $this->model->getCategorias();
$this->viewData['estados'] = $this->model->getEstados();
$this->viewData['secciones'] = $this->model->getSecciones();
$defatulSoporteUserId = model('App\Models\Configuracion\ConfigVariableModel')->getVariable('default_soporte_user_id')->value;
$this->viewData['supportUsers'] = array(
array(
'id' => $defatulSoporteUserId,
'name' => model('App\Models\UserModel')->getFullName($defatulSoporteUserId)
),
array(
'id' => 2,
'name' => model('App\Models\UserModel')->getFullName(2)
),
array(
'id' => 1,
'name' => model('App\Models\UserModel')->getFullName(1)
),
);
$this->viewData['supportUsers'] = $this->getSupportUsers();
$this->viewData['imagesTicket'] = $this->getImages('ticket', $id);
$this->viewData['imagesRespuesta'] = $this->getImages('respuesta', $id);
@ -240,7 +230,6 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
return $this->displayForm(__METHOD__, $id);
} // end function edit(...)
public function datatable()
@ -259,7 +248,13 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
$searchValues = get_filter_datatables_columns($reqData);
$resourceData = $this->model->getResource($searchValues);
if(auth()->user()->can('tickets.edit')){
$user_id = null;
}else{
$user_id = auth()->user()->id;
}
$resourceData = $this->model->getResource($searchValues, $user_id);
foreach ($requestedOrder as $order) {
$column = $order['column'] ?? 0;
$dir = $order['dir'] ?? 'asc';
@ -269,7 +264,7 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
}
}
$resourceData = $resourceData->limit($length, $start)->get()->getResultObject();
return $this->respond(Collection::datatable(
$resourceData,
$this->model->getResource($searchValues)->countAllResults(),
@ -295,27 +290,57 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
->setBody(file_get_contents($filePath));
}
private function saveImages($ticket_id, $files = [])
{
$uploadPath = WRITEPATH . 'uploads/tickets/';
$fileModel = new ticketFileModel();
if ($files && isset($files['files'])) {
foreach ($files['files'] as $file) {
if ($file->isValid() && !$file->hasMoved()) {
$originalName = $file->getClientName();
$fileExt = $file->getExtension();
// Generar hash SHA-256 basado en el contenido del archivo
$fileHash = hash_file("sha256", $file->getTempName());
$newFileName = $fileHash . '.' . $fileExt;
// Mover el archivo con el nombre basado en el hash
$file->move($uploadPath, $newFileName);
// Guardar en la base de datos
$fileModel->insert([
'nombre' => $originalName,
'ticket_id' => $ticket_id,
'hash' => $fileHash,
'path' => 'uploads/tickets/' . $newFileName
]);
}
}
}
}
private function getImages($tipo = 'ticket', $id = null)
{
$images = [];
$model = new ticketFileModel();
if($tipo == 'ticket'){
if ($tipo == 'ticket') {
$files = $model->where('ticket_id', $id)->findAll();
foreach ($files as $file) {
$ext = pathinfo($file['nombre'], PATHINFO_EXTENSION);
array_push($images,array(
"path" => '/soporte/image/' . $file['hash'] .".". $ext,
array_push($images, array(
"path" => '/soporte/image/' . $file['hash'] . "." . $ext,
"name" => $file['nombre']
));
}
}
else{
} else {
$files = $model->where('respuesta_id', $id)->findAll();
foreach ($files as $file) {
$ext = pathinfo($file['nombre'], PATHINFO_EXTENSION);
array_push($images,array(
"path" => '/soporte/image/' . $file['hash'] .".". $ext,
array_push($images, array(
"path" => '/soporte/image/' . $file['hash'] . "." . $ext,
"name" => $file['nombre']
));
}
@ -323,4 +348,24 @@ class Ticketcontroller extends \App\Controllers\BaseResourceController
return $images;
}
private function getSupportUsers()
{
$defatulSoporteUserId = model('App\Models\Configuracion\ConfigVariableModel')->getVariable('default_soporte_user_id')->value;
$supportUsers = array(
array(
'id' => $defatulSoporteUserId,
'name' => model('App\Models\UserModel')->getFullName($defatulSoporteUserId)
),
array(
'id' => 2,
'name' => model('App\Models\UserModel')->getFullName(2)
),
array(
'id' => 1,
'name' => model('App\Models\UserModel')->getFullName(1)
),
);
return $supportUsers;
}
}