Skip to content

Commit

Permalink
Serverside datatable user, role and permission
Browse files Browse the repository at this point in the history
  • Loading branch information
agungsugiarto committed Apr 18, 2020
1 parent a197be7 commit bf0dd80
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 92 deletions.
26 changes: 16 additions & 10 deletions src/Controllers/Users/PermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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'),
]);
}

/**
Expand Down
28 changes: 17 additions & 11 deletions src/Controllers/Users/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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(),
]);
}

/**
Expand Down
31 changes: 18 additions & 13 deletions src/Controllers/Users/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'),
]);
}

/**
Expand Down Expand Up @@ -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),
];

Expand Down
23 changes: 23 additions & 0 deletions src/Entities/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace agungsugiarto\boilerplate\Entities;

class Collection
{
/**
* Return data to colection map datatable.
*
* @param array $data
*
* @return array
*/
public function toColection(array $data, int $recordsTotal, int $recordsFiltered)
{
return [
'draw' => service('request')->getGet('draw'),
'recordsTotal' => $recordsTotal,
'recordsFiltered' => $recordsFiltered,
'data' => $data,
];
}
}
34 changes: 0 additions & 34 deletions src/Models/Group.php

This file was deleted.

74 changes: 74 additions & 0 deletions src/Models/GroupModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace agungsugiarto\boilerplate\Models;

use Myth\Auth\Authorization\GroupModel as BaseModel;

/**
* Class Group.
*/
class GroupModel extends BaseModel
{
/**
* Returns an array of all groups that a user is a member of.
*
* @param $userId
*
* @return object
*/
public function getGroupsForUser(int $userId)
{
$group = $this->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();
}
}
48 changes: 48 additions & 0 deletions src/Models/PermissionModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace agungsugiarto\boilerplate\Models;

use Myth\Auth\Authorization\PermissionModel as BaseModel;

class PermissionModel extends BaseModel
{
/**
* 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();
}
}
49 changes: 49 additions & 0 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace agungsugiarto\boilerplate\Models;

use Myth\Auth\Models\UserModel as BaseModel;

class UserModel extends BaseModel
{
/**
* 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('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();
}
}
Loading

0 comments on commit bf0dd80

Please sign in to comment.