Skip to content

Commit 342eedf

Browse files
committed
added query filter for users component
1 parent ec66047 commit 342eedf

File tree

4 files changed

+67
-35
lines changed

4 files changed

+67
-35
lines changed

src/Darryldecode/Backend/Components/User/Commands/QueryUsersCommand.php

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,41 +121,26 @@ public function handle(User $user, Group $group, Dispatcher $dispatcher)
121121

122122
$results = null;
123123

124-
// if group Id has been provided, we will query
125-
// the users by group, or else, we will query
126-
// users regardless of its group
124+
$q = $user->with(array_merge(array('groups'),$this->with))
125+
->ofFirstName($this->firstName)
126+
->ofLastName($this->lastName)
127+
->ofEmail($this->email);
128+
127129
if( is_int($this->groupId) )
128130
{
129-
$q = $this->app['db']->table('user_group_pivot_table')
130-
->select('*')
131-
->where('group_id',$this->groupId)
132-
->join('users','user_group_pivot_table.user_id','=','users.id')
133-
->join('groups','user_group_pivot_table.group_id','=','groups.id');
134-
135-
if( $this->paginated )
136-
{
137-
$results = $q->paginate($this->perPage);
138-
}
139-
else
131+
$q->whereHas('groups', function($q)
140132
{
141-
$results = $q->get();
142-
}
133+
$q->where('groups.id',$this->groupId);
134+
});
135+
}
136+
137+
if( $this->paginated )
138+
{
139+
$results = $q->paginate($this->perPage);
143140
}
144141
else
145142
{
146-
$q = $user->with(array_merge(array('groups'),$this->with))
147-
->ofFirstName($this->firstName)
148-
->ofLastName($this->lastName)
149-
->ofEmail($this->email);
150-
151-
if( $this->paginated )
152-
{
153-
$results = $q->paginate($this->perPage);
154-
}
155-
else
156-
{
157-
$results = $q->get();
158-
}
143+
$results = $q->get();
159144
}
160145

161146
// fire after query event

src/Darryldecode/Backend/Components/User/Models/User.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,19 @@ public function getValidationRules()
296296

297297
public function scopeOfFirstName($query, $firstName)
298298
{
299-
if( $firstName === null ) return false;
299+
if( $firstName === null || $firstName === '' ) return false;
300300

301-
return $query->where('first_name','like',"%{$firstName}%");
301+
return $query->where('first_name','LIKE',"%{$firstName}%");
302302
}
303303
public function scopeOfLastName($query, $lastName)
304304
{
305-
if( $lastName === null ) return false;
305+
if( $lastName === null || $lastName === '' ) return false;
306306

307-
return $query->where('last_name','like',"%{$lastName}%");
307+
return $query->where('last_name','LIKE',"%{$lastName}%");
308308
}
309309
public function scopeOfEmail($query, $email)
310310
{
311-
if( $email === null ) return false;
311+
if( $email === null || $email === '' ) return false;
312312

313313
return $query->where('email','=',$email);
314314
}

src/Darryldecode/Backend/Components/User/Views/users.blade.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,27 @@
1616
<div class="control-panel text-right">
1717
<button data-ng-click="drawer.show('drawer-create')" class="btn btn-default"><i class="fa fa-plus"></i> New User</button>
1818
<a href="{{route('backend.groups')}}" class="btn btn-default"><i class="fa fa-wrench"></i> Manage Groups</a>
19+
<button data-ng-click="filter.toggle()" class="btn btn-default" title="Open Query Filter"><i class="fa fa-filter"></i></button>
1920
</div>
2021
</div>
2122
</div>
23+
<div class="row" data-ng-if="filter.isOpen">
24+
<div class="col-lg-12 col-md-12">
25+
<table class="table">
26+
<tr>
27+
<td><input data-ng-model="filter.firstName" type="text" class="form-control" placeholder="First Name"></td>
28+
<td><input data-ng-model="filter.lastName" type="text" class="form-control" placeholder="Last Name"></td>
29+
<td><input data-ng-model="filter.email" type="text" class="form-control" placeholder="Email"></td>
30+
<td>
31+
<select data-ng-model="filter.group" data-ng-options="g.id as g.name for g in groups">
32+
<option value="">--group--</option>
33+
</select>
34+
</td>
35+
<td><button class="btn btn-default" data-ng-click="filter.filter()" data-ng-disabled="filter.isQuerying">Filter</button></td>
36+
</tr>
37+
</table>
38+
</div>
39+
</div>
2240
<div class="table-responsive animated fadeIn">
2341
<table class="table table-hover">
2442
<tr>
@@ -28,7 +46,7 @@
2846
<th colspan="2" class="action text-center"><i class="fa fa-bolt"></i></th>
2947
</tr>
3048
<tr data-ng-show="!users.data.length">
31-
<td colspan="4" class="text-center">Loading <i class="fa fa-spinner fa-spin"></i></td>
49+
<td colspan="4" class="text-center">No Results Found.</td>
3250
</tr>
3351
<tr data-ng-repeat="u in users.data" class="user-item-@{{::$index}}" >
3452
<td>[@{{::u.id}}]</td>

src/Darryldecode/Backend/Public/backend/cb/app/users/controller.manage.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,35 @@ angular.module('cb.user').controller('ManageUsersController', ['$scope','$window
198198
});
199199
};
200200

201+
// filter results
202+
$scope.filter = {};
203+
$scope.filter.isQuerying = false;
204+
$scope.filter.isOpen = false;
205+
$scope.filter.firstName = '';
206+
$scope.filter.lastName = '';
207+
$scope.filter.email = '';
208+
$scope.filter.group = '';
209+
$scope.filter.toggle = function () {
210+
if( $scope.filter.isOpen ) queryInitialData();
211+
$scope.filter.isOpen = !$scope.filter.isOpen;
212+
};
213+
$scope.filter.filter = function () {
214+
$scope.filter.isQuerying = true;
215+
UsersFactory.get({
216+
perPage: $scope.pagination.perPage,
217+
groupId: $scope.filter.group,
218+
firstName: $scope.filter.firstName,
219+
lastName: $scope.filter.lastName,
220+
email: $scope.filter.email
221+
}).then(function (success) {
222+
$scope.filter.isQuerying = false;
223+
$scope.pagination.current = 1;
224+
$scope.users = success.data.data;
225+
}, function (error) {
226+
GlobalLoaderService.show(error.data.message || 'An error has occurred.', 'danger').hide(4000);
227+
});
228+
};
229+
201230
// watch if password match
202231
$scope.$watch('user.passwordConfirm', function () {
203232
if( $scope.user.password == '' ) return;

0 commit comments

Comments
 (0)