From bf0dd80352edcc2d4a50b23b3dc3398d594908fc Mon Sep 17 00:00:00 2001 From: Agung Sugiarto Date: Sat, 18 Apr 2020 22:04:17 +0700 Subject: [PATCH] Serverside datatable user, role and permission --- .../Users/PermissionController.php | 26 ++++--- src/Controllers/Users/RoleController.php | 28 ++++--- src/Controllers/Users/UserController.php | 31 ++++---- src/Entities/Collection.php | 23 ++++++ src/Models/Group.php | 34 --------- src/Models/GroupModel.php | 74 +++++++++++++++++++ src/Models/PermissionModel.php | 48 ++++++++++++ src/Models/UserModel.php | 49 ++++++++++++ src/Views/Permission/index.php | 16 +--- src/Views/Role/index.php | 14 +--- src/Views/User/index.php | 4 +- 11 files changed, 255 insertions(+), 92 deletions(-) create mode 100644 src/Entities/Collection.php delete mode 100644 src/Models/Group.php create mode 100644 src/Models/GroupModel.php create mode 100644 src/Models/PermissionModel.php create mode 100644 src/Models/UserModel.php diff --git a/src/Controllers/Users/PermissionController.php b/src/Controllers/Users/PermissionController.php index 66a5bdc..57aabdc 100644 --- a/src/Controllers/Users/PermissionController.php +++ b/src/Controllers/Users/PermissionController.php @@ -3,6 +3,8 @@ namespace agungsugiarto\boilerplate\Controllers\Users; use agungsugiarto\boilerplate\Controllers\BaseController; +use agungsugiarto\boilerplate\Entities\Collection; +use agungsugiarto\boilerplate\Models\PermissionModel; use CodeIgniter\API\ResponseTrait; /** @@ -19,19 +21,23 @@ class PermissionController extends BaseController */ public function index() { - // Title and breadcrump - $data = [ - 'title' => lang('permission.title'), - 'subtitle' => lang('permission.subtitle'), - ]; - if ($this->request->isAJAX()) { - return $this->respond([ - 'data' => $this->authorize->permissions(), - ]); + $start = $this->request->getGet('start'); + $length = $this->request->getGet('length'); + $search = $this->request->getGet('search[value]'); + $collection = new Collection(); + + return $this->respond($collection->toColection( + model(PermissionModel::class)->findPaginatedData($length, $start, $search), + model(PermissionModel::class)->countAllResults(), + model(PermissionModel::class)->countFindData($search) + )); } - return view('agungsugiarto\boilerplate\Views\Permission\index', $data); + return view('agungsugiarto\boilerplate\Views\Permission\index', [ + 'title' => lang('permission.title'), + 'subtitle' => lang('permission.subtile'), + ]); } /** diff --git a/src/Controllers/Users/RoleController.php b/src/Controllers/Users/RoleController.php index 552e2de..bdde806 100644 --- a/src/Controllers/Users/RoleController.php +++ b/src/Controllers/Users/RoleController.php @@ -3,7 +3,8 @@ namespace agungsugiarto\boilerplate\Controllers\Users; use agungsugiarto\boilerplate\Controllers\BaseController; -use agungsugiarto\boilerplate\Models\Group; +use agungsugiarto\boilerplate\Entities\Collection; +use agungsugiarto\boilerplate\Models\GroupModel; use CodeIgniter\API\ResponseTrait; /** @@ -20,19 +21,24 @@ class RoleController extends BaseController */ public function index() { - $data = [ - 'title' => lang('role.title'), - 'subtitle' => lang('role.subtitle'), - 'data' => $this->authorize->permissions(), - ]; - if ($this->request->isAJAX()) { - return $this->response->setJSON([ - 'data' => $this->authorize->groups(), - ]); + $start = $this->request->getGet('start'); + $length = $this->request->getGet('length'); + $search = $this->request->getGet('search[value]'); + $collection = new Collection(); + + return $this->respond($collection->toColection( + model(GroupModel::class)->findPaginatedData($length, $start, $search), + model(GroupModel::class)->countAllResults(), + model(GroupModel::class)->countFindData($search) + )); } - return view('agungsugiarto\boilerplate\Views\Role\index', $data); + return view('agungsugiarto\boilerplate\Views\Role\index', [ + 'title' => lang('role.title'), + 'subtitle' => lang('role.subtitle'), + 'data' => $this->authorize->permissions(), + ]); } /** diff --git a/src/Controllers/Users/UserController.php b/src/Controllers/Users/UserController.php index b75b0a3..7460e86 100644 --- a/src/Controllers/Users/UserController.php +++ b/src/Controllers/Users/UserController.php @@ -3,12 +3,12 @@ namespace agungsugiarto\boilerplate\Controllers\Users; use agungsugiarto\boilerplate\Controllers\BaseController; -use agungsugiarto\boilerplate\Models\Group; +use agungsugiarto\boilerplate\Entities\Collection; +use agungsugiarto\boilerplate\Models\GroupModel; +use agungsugiarto\boilerplate\Models\UserModel; use CodeIgniter\API\ResponseTrait; -use Myth\Auth\Authorization\GroupModel; use Myth\Auth\Authorization\PermissionModel; use Myth\Auth\Entities\User; -use Myth\Auth\Models\UserModel; /** * Class UserController. @@ -31,18 +31,23 @@ public function __construct() */ public function index() { - $data = [ - 'title' => lang('user.title'), - 'subtitle' => lang('user.subtitle'), - ]; - if ($this->request->isAJAX()) { - return $this->respond([ - 'data' => $this->users->asObject()->findAll(), - ]); + $start = $this->request->getGet('start'); + $length = $this->request->getGet('length'); + $search = $this->request->getGet('search[value]'); + $collection = new Collection(); + + return $this->respond($collection->toColection( + model(UserModel::class)->findPaginatedData($length, $start, $search), + model(UserModel::class)->countAllResults(), + model(UserModel::class)->countFindData($search) + )); } - return view('agungsugiarto\boilerplate\Views\User\index', $data); + return view('agungsugiarto\boilerplate\Views\User\index', [ + 'title' => lang('user.title'), + 'subtitle' => lang('user.subtitle'), + ]); } /** @@ -199,7 +204,7 @@ public function edit($id) 'permissions' => $this->authorize->permissions(), 'permission' => (new PermissionModel())->getPermissionsForUser($id), 'roles' => $this->authorize->groups(), - 'role' => (new Group())->getGroupsForUser($id), + 'role' => (new GroupModel())->getGroupsForUser($id), 'user' => $this->users->asArray()->find($id), ]; diff --git a/src/Entities/Collection.php b/src/Entities/Collection.php new file mode 100644 index 0000000..12c75df --- /dev/null +++ b/src/Entities/Collection.php @@ -0,0 +1,23 @@ + service('request')->getGet('draw'), + 'recordsTotal' => $recordsTotal, + 'recordsFiltered' => $recordsFiltered, + 'data' => $data, + ]; + } +} \ No newline at end of file diff --git a/src/Models/Group.php b/src/Models/Group.php deleted file mode 100644 index ff693d9..0000000 --- a/src/Models/Group.php +++ /dev/null @@ -1,34 +0,0 @@ -builder() - ->join('auth_groups_users', 'auth_groups_users.group_id = auth_groups.id', 'left') - ->where('user_id', $userId) - ->get() - ->getResultObject(); - - $found = []; - foreach ($group as $row) { - $found[$row->id] = strtolower($row->name); - } - - return $found; - } -} diff --git a/src/Models/GroupModel.php b/src/Models/GroupModel.php new file mode 100644 index 0000000..69a40ea --- /dev/null +++ b/src/Models/GroupModel.php @@ -0,0 +1,74 @@ +builder() + ->join('auth_groups_users', 'auth_groups_users.group_id = auth_groups.id', 'left') + ->where('user_id', $userId) + ->get() + ->getResultObject(); + + $found = []; + foreach ($group as $row) { + $found[$row->id] = strtolower($row->name); + } + + return $found; + } + + /** + * FInd with paginate data. + * + * @param int $length + * @param int $start + * + * @return array + */ + public function findPaginatedData(int $length, int $start, string $keyword = ''): ?array + { + if ($keyword) { + $this->builder() + ->groupStart() + ->like('name', $keyword) + ->orLike('description', $keyword) + ->groupEnd(); + } + + return $this->builder()->limit($length, $start)->get()->getResultObject(); + } + + /** + * FInd with count all data. + * + * @param string $keyword + * + * @return int + */ + public function countFindData(string $keyword = ''): int + { + return $keyword ? $this->builder() + ->groupStart() + ->like('name', $keyword) + ->orLike('description', $keyword) + ->groupEnd() + ->countAllResults() + + : $this->builder()->countAllResults(); + } +} diff --git a/src/Models/PermissionModel.php b/src/Models/PermissionModel.php new file mode 100644 index 0000000..76dfa05 --- /dev/null +++ b/src/Models/PermissionModel.php @@ -0,0 +1,48 @@ +builder() + ->groupStart() + ->like('name', $keyword) + ->orLike('description', $keyword) + ->groupEnd(); + } + + return $this->builder()->limit($length, $start)->get()->getResultObject(); + } + + /** + * FInd with count all data. + * + * @param string $keyword + * + * @return int + */ + public function countFindData(string $keyword = ''): int + { + return $keyword ? $this->builder() + ->groupStart() + ->like('name', $keyword) + ->orLike('description', $keyword) + ->groupEnd() + ->countAllResults() + + : $this->builder()->countAllResults(); + } +} \ No newline at end of file diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php new file mode 100644 index 0000000..6c0b831 --- /dev/null +++ b/src/Models/UserModel.php @@ -0,0 +1,49 @@ +builder() + ->groupStart() + ->like('username', $keyword) + ->orLike('email', $keyword) + ->groupEnd(); + } + + return $this->builder()->where('deleted_at', null)->limit($length, $start)->get()->getResultObject(); + } + + /** + * FInd with count all data. + * + * @param string $keyword + * + * @return int + */ + public function countFindData(string $keyword = ''): int + { + return $keyword ? $this->builder() + ->groupStart() + ->like('username', $keyword) + ->orLike('email', $keyword) + ->groupEnd() + ->where('deleted_at', null) + ->countAllResults() + + : $this->builder()->where('deleted_at', null)->countAllResults(); + } +} \ No newline at end of file diff --git a/src/Views/Permission/index.php b/src/Views/Permission/index.php index 081fb6d..11c122b 100644 --- a/src/Views/Permission/index.php +++ b/src/Views/Permission/index.php @@ -46,19 +46,17 @@ $.get('https://www.google.com/'); var tablePermission = $('#table-permission').DataTable({ - paging: true, - lengthChange: true, - searching: true, + processing: true, + serverSide: true, ordering: false, - info: true, autoWidth: false, ajax : { url: '', - method: 'get' + method: 'GET' }, columns : [ - { 'data': null }, + { 'data': 'id' }, { 'data': 'name' }, { 'data': 'description' }, { @@ -74,12 +72,6 @@ ] }); - tablePermission.on('order.dt search.dt', () => { - tablePermission.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) { - cell.innerHTML = i+1; - }); - }).draw(); - $(document).on('click', '#btn-save-permission', () => { $('.text-danger').remove(); $('.is-invalid').removeClass('is-invalid'); diff --git a/src/Views/Role/index.php b/src/Views/Role/index.php index 2358198..a08d7e8 100644 --- a/src/Views/Role/index.php +++ b/src/Views/Role/index.php @@ -42,11 +42,9 @@ section('js') ?>