Skip to content

Commit 0e72ddb

Browse files
committed
Admin - Clients Accounts with Laravel Paginator and Ajax pages and filter
1 parent b0d6574 commit 0e72ddb

File tree

16 files changed

+648
-512
lines changed

16 files changed

+648
-512
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ A Laravel CMS Starter project with AdminLTE theme and core features.
1111
- Password: github
1212

1313
### What is New?
14-
- Upgraded to Laravel 5.6 and added many new 'components (blog, news, banners, etc)'
15-
- Page Builder (CRUD website pages with 3 different components)
14+
- Upgraded to Laravel 5.6'
1615
- Add config/app.php - "is_preview". This is to prevent users to delete pages and users from the demo site.
16+
- Add admin/accounts. The clients index.blade.php is a good example when working with 1+K entries. It use Laravel Paginator via Ajax pages and filter.
1717

1818
## Features / What it includes
1919
- Admin LTE admin theme
2020
- Members (website and admin users)
2121
- Google Analytics Reports (with charts)
22-
- Website Page Builder
22+
- Website Page Builder with 3 components (page content, photos, documents)
2323
- Log Website Activities (if contact us was submitted, etc)
2424
- Notifications (Laravel notifications)
2525
- Log Admin Activities (when admin create,edit,delete a resource)
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin\Accounts;
4+
5+
use Illuminate\Validation\Rule;
6+
use Password;
7+
use App\User;
8+
use App\Models\Role;
9+
use App\Http\Requests;
10+
use Illuminate\Http\Request;
11+
use Illuminate\Pagination\LengthAwarePaginator;
12+
use App\Http\Controllers\Admin\AdminController;
13+
14+
class ClientsController extends AdminController
15+
{
16+
/**
17+
* Display a listing of client.
18+
*
19+
* @return Response
20+
* @throws \Throwable
21+
*/
22+
public function index()
23+
{
24+
// flush filters if not ajax
25+
if (!request()->ajax()) {
26+
session()->forget('filtered');
27+
session()->forget('filter_fullname');
28+
session()->forget('filter_email');
29+
session()->forget('filter_cellphone');
30+
}
31+
32+
// paginator
33+
$paginator = $this->getPaginator();
34+
35+
// if pagination ajax
36+
if (request()->ajax()) {
37+
return response()->json(view('admin.accounts.clients.pagination')
38+
->with('paginator', $paginator)
39+
->render());
40+
}
41+
42+
save_resource_url();
43+
44+
return $this->view("accounts.clients.index")->with('paginator', $paginator);
45+
}
46+
47+
/**
48+
* @return \Illuminate\Http\JsonResponse
49+
*/
50+
public function filter()
51+
{
52+
session()->put('filtered', true);
53+
session()->put('filter_fullname', input('name'));
54+
session()->put('filter_email', input('email'));
55+
session()->put('filter_cellphone', input('cellphone'));
56+
57+
return json_response();
58+
}
59+
60+
/**
61+
* @param User $user
62+
* @return $this
63+
*/
64+
public function show(User $user)
65+
{
66+
return $this->view("accounts.clients.show", compact('user'));
67+
}
68+
69+
/**
70+
* @param User $user
71+
* @return $this
72+
*/
73+
public function edit(User $user)
74+
{
75+
$item = $user;
76+
$roles = Role::getAllLists();
77+
78+
return $this->view('accounts.clients.create_edit')
79+
->with('item', $user)
80+
->with('roles', $roles);
81+
}
82+
83+
/**
84+
* Update the specified client in storage.
85+
*
86+
* @param User $user
87+
* @return Response
88+
*/
89+
public function update($user)
90+
{
91+
$user = User::find($user);
92+
if (!$user) {
93+
return redirect_to_resource();
94+
}
95+
96+
$attributes = request()->validate([
97+
'firstname' => 'required',
98+
'lastname' => 'required',
99+
'cellphone' => 'required',
100+
'telephone' => 'nullable',
101+
'born_at' => 'nullable',
102+
'etokens' => 'nullable',
103+
'email' => 'required|email|' . Rule::unique('users')->ignore($user->id),
104+
'roles' => 'required|array',
105+
]);
106+
107+
unset($attributes['roles']);
108+
109+
$this->updateEntry($user, $attributes);
110+
111+
$user->roles()->sync(input('roles'));
112+
113+
return redirect_to_resource();
114+
}
115+
116+
/**
117+
* Remove the specified client from storage.
118+
*
119+
* @param User $user
120+
* @return Response
121+
*/
122+
public function destroy($user)
123+
{
124+
$user = User::find($user);
125+
if (!$user) {
126+
return redirect_to_resource();
127+
}
128+
129+
$this->deleteEntry($user, request());
130+
131+
return redirect_to_resource();
132+
}
133+
134+
/**
135+
* Send a reset link to the given user.
136+
*
137+
* @param \Illuminate\Http\Request $request
138+
* @return \Illuminate\Http\RedirectResponse
139+
*/
140+
public function sendResetLinkEmail(Request $request)
141+
{
142+
$this->validate($request, ['email' => 'required|email']);
143+
144+
//We will send the password reset link to this user. Once we have attempted
145+
//to send the link, we will examine the response then see the message we
146+
//need to show to the user. Finally, we'll send out a proper response.
147+
$response = Password::broker()->sendResetLink($request->only('email'));
148+
149+
if ($response == 'passwords.sent') {
150+
notify()->success('Success!', 'Password email sent to client.');
151+
}
152+
else {
153+
notify()->error('Oops', 'Something went wrong', 'warning shake animated');
154+
}
155+
156+
return redirect()->back();
157+
}
158+
159+
/**
160+
* Fetch the client paginator
161+
* @return LengthAwarePaginator
162+
*/
163+
private function getPaginator()
164+
{
165+
$perPage = 40;
166+
$page = input('page', 1);
167+
$itemsObj = $this->fetchEntries();
168+
$items = $itemsObj['items'];
169+
$total = $itemsObj['total'];
170+
$baseUrl = config('app.url') . "/admin/accounts/clients";
171+
172+
// paginator
173+
$paginator = new LengthAwarePaginator($items->forPage($page, $perPage), count($items),
174+
$perPage, $page, ['path' => $baseUrl, 'originalEntries' => $total]);
175+
176+
return $paginator;
177+
}
178+
179+
/**
180+
* Fetch the users
181+
*/
182+
private function fetchEntries()
183+
{
184+
// query to get the products
185+
//$items = User::orderBy('firstname')->get();
186+
$items = User::whereRole(Role::$USER)->orderBy('firstname')->get();
187+
188+
$total = $items->count();
189+
// if filtered
190+
if (session('filtered')) {
191+
// filter entries on the filter criteria
192+
$client = session('filter_fullname', '');
193+
if (strlen($client) >= 2) {
194+
$items = $items->filter(function ($item) use ($client) {
195+
return (stristr($item->fullname, $client) !== false);
196+
});
197+
}
198+
199+
$cellphone = session('filter_cellphone', '');
200+
if (strlen($cellphone) >= 2) {
201+
$items = $items->filter(function ($item) use ($cellphone) {
202+
return (stristr($item->cellphone, $cellphone) !== false);
203+
});
204+
}
205+
206+
$email = session('filter_email', '');
207+
if (strlen($email) >= 2) {
208+
$items = $items->filter(function ($item) use ($email) {
209+
return (stristr($item->email, $email) !== false);
210+
});
211+
}
212+
}
213+
214+
return ['items' => $items, 'total' => $total];
215+
}
216+
}

app/Http/Controllers/Admin/Settings/AdministratorsController.php

Lines changed: 0 additions & 137 deletions
This file was deleted.

public/js/admin.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)