From 719455567e9edc08764e0191e67c5f57cd5b8065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Mon, 9 Dec 2024 19:44:44 +0100 Subject: [PATCH] =?UTF-8?q?corregida=20busqueda=20de=20usuarios.=20tambi?= =?UTF-8?q?=C3=A9n=20el=20mail=20obligatorio=20y=20borrar=20los=20chat=20d?= =?UTF-8?q?epartaments=20con=20soft=20delete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ci4/app/Config/Routes.php | 1 + ci4/app/Controllers/Configuracion/Users.php | 50 +++++- .../Models/Chat/ChatDeparmentUserModel.php | 2 +- ci4/app/Models/UserModel.php | 40 +++++ .../themes/vuexy/form/user/viewUserList.php | 52 +++---- .../assets/js/safekat/pages/users/list.js | 144 ++++++++++++++++++ 6 files changed, 257 insertions(+), 32 deletions(-) create mode 100644 httpdocs/assets/js/safekat/pages/users/list.js diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 6e377cbc..c4c439e8 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -105,6 +105,7 @@ $routes->group('users', ['namespace' => 'App\Controllers\Configuracion'], functi $routes->get('delete/(:num)', 'Users::delete/$1', ['as' => 'deleteUser']); $routes->post('allmenuitems', 'Users::allItemsSelect', ['as' => 'select2ItemsOfUsers']); $routes->post('menuitems', 'Users::menuItems', ['as' => 'menuItemsOfUsers']); + $routes->post('datatable', 'Users::datatable', ['as' => 'datatableOfUsers']); $routes->get('getMenuComerciales', 'Users::getMenuComerciales', ['as' => 'menuItemsComerciales']); }); $routes->resource('users', ['namespace' => 'App\Controllers\Configuracion', 'controller' => 'Users', 'except' => 'show,new,create,update']); diff --git a/ci4/app/Controllers/Configuracion/Users.php b/ci4/app/Controllers/Configuracion/Users.php index 7864620f..8fcc153e 100755 --- a/ci4/app/Controllers/Configuracion/Users.php +++ b/ci4/app/Controllers/Configuracion/Users.php @@ -7,6 +7,8 @@ use App\Models\Usuarios\GroupModel; use App\Models\UserModel; use App\Models\Usuarios\GroupsUsersModel; +use App\Models\Collection; + use CodeIgniter\Shield\Entities\User; use function PHPUnit\Framework\isNull; @@ -22,7 +24,8 @@ class Users extends \App\Controllers\GoBaseController use \CodeIgniter\API\ResponseTrait; - protected static $primaryModelName = 'App\Models\UserModel'; + protected static $primaryModelName = UserModel::class; + protected $modelName = ClientePlantillaPreciosLineasModel::class; protected static $singularObjectNameCc = 'user'; protected static $singularObjectName = 'User'; @@ -58,10 +61,8 @@ class Users extends \App\Controllers\GoBaseController public function index() { - $this->viewData['usingClientSideDataTable'] = true; + $this->viewData['usingServerSideDataTable'] = true; $this->viewData['pageSubTitle'] = lang('Basic.global.ManageAllRecords', [lang('Users.user')]); - $this->viewData['user_model'] = $this->user_model; - $this->viewData['userList2'] = auth()->getProvider()->findAll(); parent::index(); } @@ -313,8 +314,10 @@ class Users extends \App\Controllers\GoBaseController return $this->redirect2listView('errorMessage', $message); endif; + $this->chat_department_user_model->where("user_id", $id)->delete(); + $users = auth()->getProvider(); - $users->delete($user->id, true); + $users->delete($user->id); $message = "Usuario eliminado correctamente"; return $this->redirect2listView('successMessage', $message); @@ -373,6 +376,43 @@ class Users extends \App\Controllers\GoBaseController } } + public function datatable(){ + + if($this->request->isAJAX()){ + + $reqData = $this->request->getPost(); + if (!isset($reqData['draw']) || !isset($reqData['columns'])) { + $errstr = 'No data available in response to this specific request.'; + $response = $this->respond(Collection::datatable([], 0, 0, $errstr), 400, $errstr); + return $response; + } + $start = $reqData['start'] ?? 0; + $length = $reqData['length'] ?? 5; + $searchValues = get_filter_datatables_columns($reqData); + $requestedOrder = $reqData['order'] ?? []; + + $resourceData = $this->model->getResource($searchValues); + foreach ($requestedOrder as $order) { + $column = $order['column'] ?? 0; + $dir = $order['dir'] ?? 'asc'; + $orderColumn = UserModel::SORTABLE[$column] ?? null; + if ($orderColumn) { + $resourceData->orderBy($orderColumn, $dir); + } + } + $resourceData = $resourceData->limit($length, $start)->get()->getResultObject(); + + return $this->respond(Collection::datatable( + $resourceData, + $this->model->getResource([])->countAllResults(), + $this->model->getResource($searchValues)->countAllResults() + )); + + } else { + return $this->failUnauthorized('Invalid request', 403); + } + } + public function getMenuComerciales() { if ($this->request->isAJAX()) { diff --git a/ci4/app/Models/Chat/ChatDeparmentUserModel.php b/ci4/app/Models/Chat/ChatDeparmentUserModel.php index 0c701d14..717c738a 100644 --- a/ci4/app/Models/Chat/ChatDeparmentUserModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentUserModel.php @@ -11,7 +11,7 @@ class ChatDeparmentUserModel extends Model protected $primaryKey = 'id'; protected $useAutoIncrement = true; protected $returnType = 'array'; - protected $useSoftDeletes = false; + protected $useSoftDeletes = true; protected $protectFields = true; protected $allowedFields = [ "chat_department_id", diff --git a/ci4/app/Models/UserModel.php b/ci4/app/Models/UserModel.php index 3c2fe960..57cb4e7a 100644 --- a/ci4/app/Models/UserModel.php +++ b/ci4/app/Models/UserModel.php @@ -22,6 +22,14 @@ class UserModel extends ShieldUserModel ]; } + const SORTABLE = [ + 0 => "t1.id", + 1 => "t1.first_name", + 2 => "t1.last_name", + 3 => "t2.secret", + 4 => "t1.last_active", + ]; + protected $returnType = UsersEntity::class; protected $useSoftDeletes = true; @@ -33,6 +41,7 @@ class UserModel extends ShieldUserModel protected $validationRules = [ "first_name" => "required|trim|max_length[150]", "last_name" => "required|trim|max_length[150]", + "email" => "required|valid_email|max_length[150]", 'new_pwd' => 'permit_empty|min_length[8]', 'new_pwd_confirm' => 'permit_empty|required_with[new_pwd]|matches[new_pwd]', "comments" => "permit_empty|trim|max_length[512]" @@ -56,9 +65,40 @@ class UserModel extends ShieldUserModel 'comments' => [ "max_length" => "Users.validation.last_name.max_length", ], + 'email' => [ + "required" => "Users.validation.email.required", + "valid_email" => "Users.validation.email.valid_email", + "max_length" => "Users.validation.email.max_length" + ] ]; + public function getResource($search = []) + { + $builder = $this->db + ->table($this->table . " t1") + ->select( + "t1.id as id, t1.first_name AS first_name, t1.last_name AS last_name, + t2.secret AS email, t1.last_active AS last_active" + ); + + $builder->join("auth_identities t2", "t1.id = t2.user_id", "left"); + $builder->where('t1.deleted_at', null)->groupBy("t1.id"); + + + + if (empty($search)) + return $builder; + else { + $builder->groupStart(); + foreach ($search as $col_search) { + $builder->like(self::SORTABLE[$col_search[0]], $col_search[2]); + } + $builder->groupEnd(); + return $builder; + } + } + public function getComerciales() { diff --git a/ci4/app/Views/themes/vuexy/form/user/viewUserList.php b/ci4/app/Views/themes/vuexy/form/user/viewUserList.php index b6adb138..f142c4f1 100644 --- a/ci4/app/Views/themes/vuexy/form/user/viewUserList.php +++ b/ci4/app/Views/themes/vuexy/form/user/viewUserList.php @@ -1,5 +1,6 @@ -include('themes/_commonPartialsBs/datatables') ?> +include('themes/_commonPartialsBs/datatables') ?> extend('themes/vuexy/main/defaultlayout') ?> + section('content'); ?>
@@ -16,36 +17,15 @@ style="width: 100%;"> + ID - - + + - - - - first_name) || strlen($item->first_name) < 51 ? esc($item->first_name) : character_limiter(esc($item->first_name), 50) ?> - - - last_name) || strlen($item->last_name) < 51 ? esc($item->last_name) : character_limiter(esc($item->last_name), 50) ?> - - - getProvider()->findById($item->id)->email) ? "" : character_limiter(esc(auth()->getProvider()->findById($item->id)->email), 50) ?> - - - last_active) ? '' : date('d/m/Y H:m:s', strtotime($item->last_active)) ?> - - - - id), "", ['class' => 'text-body', 'data-id' => $item->id,]); ?> - ", ['class' => 'text-body', 'data-href' => route_to('deleteUser', $item->id), 'data-bs-toggle' => 'modal', 'data-bs-target' => '#confirm2delete']); ?> - - - -
@@ -56,4 +36,24 @@
-endSection() ?> \ No newline at end of file +endSection() ?> + + +section('css') ?> + "> +endSection() ?> + +section('additionalExternalJs') ?> + + + + + + + + + + + + +endSection() ?> diff --git a/httpdocs/assets/js/safekat/pages/users/list.js b/httpdocs/assets/js/safekat/pages/users/list.js new file mode 100644 index 00000000..d5a4094c --- /dev/null +++ b/httpdocs/assets/js/safekat/pages/users/list.js @@ -0,0 +1,144 @@ +import Table from '../../components/table.js'; +import ConfirmDeleteModal from '../../components/ConfirmDeleteModal.js'; +import Ajax from '../../components/ajax.js'; +import { getToken } from '../../common/common.js'; + +class UserList { + + constructor() { + + this.domItem = $('.card-body'); + + this.csrf_token = getToken(); + this.csrf_hash = $('#mainContainer').find('input[name="' + this.csrf_token + '"]').val(); + + this.tableUsers = null; + this.deleteModal = null; + } + + init() { + + const self = this; + + this.headerSearcher(); + + this.deleteModal = new ConfirmDeleteModal('plantillasTarifasCliente'); + this.deleteModal.init(); + + this.#initTable(); + + // Editar en linea la fila + this.tableUsers.table.on('click', '.btn-edit-' + this.tableUsers.getAlias(), function (e) { + + const dataId = $(this).attr('data-id'); + + if (!Number.isNaN(Number(dataId))) { + window.location.href = '/users/edit/' + dataId; + } + }); + + + // Eliminar la fila + this.tableUsers.table.on('click', '.btn-delete-' + this.tableUsers.getAlias(), function (e) { + const row = $(this).closest('tr')[0]._DT_RowIndex; + const dataId = $(this).attr('data-id'); + self.deleteModal.setData($(this).attr('data-id')); + self.deleteModal.show(() => { + + if (!Number.isNaN(Number(self.deleteModal.getData()))) { + + new Ajax( + '/users/delete/' + dataId, + { + + }, + {}, + (data, textStatus, jqXHR) => { + + self.tableUsers.table.clearPipeline(); + self.tableUsers.table.row($(row)).invalidate().draw(); + + popSuccessAlert(data.msg ?? jqXHR.statusText); + }, + (error) => { + console.log(error); + } + + ).get(); + self.deleteModal.hide(); + } + }); + }); + } + + #initTable() { + + const self = this; + + const columns = [ + { 'data': 'id' }, + { 'data': 'first_name' }, + { 'data': 'last_name' }, + { 'data': 'email' }, + { 'data': 'last_active' }, + + ]; + + const actions = ['edit', 'delete']; + + this.tableUsers = new Table( + $('#tableOfUsers'), + 'users', + '/users/datatable', + columns, + [] + ); + + + this.tableUsers.init({ + actions: actions, + colVisibility: false, + buttonsExport: true, + }); + + + this.tableUsers.table.on('init.dt', function () { + self.tableUsers.table.page.len(50).draw(); + }); + + } + + headerSearcher() { + + const self = this; + + $('#tableOfUsers thead tr').clone(false).appendTo('#tableOfUsers thead'); + $('#tableOfUsers thead tr:eq(1) th').each(function (i) { + + if (!$(this).hasClass("noFilter")) { + + $(this).html(''); + + $('input', this).on('change clear', function () { + if (self.tableUsers.table.column(i).search() !== this.value) { + self.tableUsers.table + .column(i) + .search(this.value) + .draw(); + } + }); + } + else { + $(this).html(''); + } + }); + + + } + +} + +document.addEventListener('DOMContentLoaded', function () { + + new UserList().init(); +});