Skip to content

Commit

Permalink
Admin - Clients Accounts with Laravel Paginator and Ajax pages and fi…
Browse files Browse the repository at this point in the history
…lter
  • Loading branch information
bpocallaghan committed Jun 1, 2018
1 parent b0d6574 commit 0e72ddb
Show file tree
Hide file tree
Showing 16 changed files with 648 additions and 512 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ A Laravel CMS Starter project with AdminLTE theme and core features.
- Password: github

### What is New?
- Upgraded to Laravel 5.6 and added many new 'components (blog, news, banners, etc)'
- Page Builder (CRUD website pages with 3 different components)
- Upgraded to Laravel 5.6'
- Add config/app.php - "is_preview". This is to prevent users to delete pages and users from the demo site.
- 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.

## Features / What it includes
- Admin LTE admin theme
- Members (website and admin users)
- Google Analytics Reports (with charts)
- Website Page Builder
- Website Page Builder with 3 components (page content, photos, documents)
- Log Website Activities (if contact us was submitted, etc)
- Notifications (Laravel notifications)
- Log Admin Activities (when admin create,edit,delete a resource)
Expand Down
216 changes: 216 additions & 0 deletions app/Http/Controllers/Admin/Accounts/ClientsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?php

namespace App\Http\Controllers\Admin\Accounts;

use Illuminate\Validation\Rule;
use Password;
use App\User;
use App\Models\Role;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Controllers\Admin\AdminController;

class ClientsController extends AdminController
{
/**
* Display a listing of client.
*
* @return Response
* @throws \Throwable
*/
public function index()
{
// flush filters if not ajax
if (!request()->ajax()) {
session()->forget('filtered');
session()->forget('filter_fullname');
session()->forget('filter_email');
session()->forget('filter_cellphone');
}

// paginator
$paginator = $this->getPaginator();

// if pagination ajax
if (request()->ajax()) {
return response()->json(view('admin.accounts.clients.pagination')
->with('paginator', $paginator)
->render());
}

save_resource_url();

return $this->view("accounts.clients.index")->with('paginator', $paginator);
}

/**
* @return \Illuminate\Http\JsonResponse
*/
public function filter()
{
session()->put('filtered', true);
session()->put('filter_fullname', input('name'));
session()->put('filter_email', input('email'));
session()->put('filter_cellphone', input('cellphone'));

return json_response();
}

/**
* @param User $user
* @return $this
*/
public function show(User $user)
{
return $this->view("accounts.clients.show", compact('user'));
}

/**
* @param User $user
* @return $this
*/
public function edit(User $user)
{
$item = $user;
$roles = Role::getAllLists();

return $this->view('accounts.clients.create_edit')
->with('item', $user)
->with('roles', $roles);
}

/**
* Update the specified client in storage.
*
* @param User $user
* @return Response
*/
public function update($user)
{
$user = User::find($user);
if (!$user) {
return redirect_to_resource();
}

$attributes = request()->validate([
'firstname' => 'required',
'lastname' => 'required',
'cellphone' => 'required',
'telephone' => 'nullable',
'born_at' => 'nullable',
'etokens' => 'nullable',
'email' => 'required|email|' . Rule::unique('users')->ignore($user->id),
'roles' => 'required|array',
]);

unset($attributes['roles']);

$this->updateEntry($user, $attributes);

$user->roles()->sync(input('roles'));

return redirect_to_resource();
}

/**
* Remove the specified client from storage.
*
* @param User $user
* @return Response
*/
public function destroy($user)
{
$user = User::find($user);
if (!$user) {
return redirect_to_resource();
}

$this->deleteEntry($user, request());

return redirect_to_resource();
}

/**
* Send a reset link to the given user.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function sendResetLinkEmail(Request $request)
{
$this->validate($request, ['email' => 'required|email']);

//We will send the password reset link to this user. Once we have attempted
//to send the link, we will examine the response then see the message we
//need to show to the user. Finally, we'll send out a proper response.
$response = Password::broker()->sendResetLink($request->only('email'));

if ($response == 'passwords.sent') {
notify()->success('Success!', 'Password email sent to client.');
}
else {
notify()->error('Oops', 'Something went wrong', 'warning shake animated');
}

return redirect()->back();
}

/**
* Fetch the client paginator
* @return LengthAwarePaginator
*/
private function getPaginator()
{
$perPage = 40;
$page = input('page', 1);
$itemsObj = $this->fetchEntries();
$items = $itemsObj['items'];
$total = $itemsObj['total'];
$baseUrl = config('app.url') . "/admin/accounts/clients";

// paginator
$paginator = new LengthAwarePaginator($items->forPage($page, $perPage), count($items),
$perPage, $page, ['path' => $baseUrl, 'originalEntries' => $total]);

return $paginator;
}

/**
* Fetch the users
*/
private function fetchEntries()
{
// query to get the products
//$items = User::orderBy('firstname')->get();
$items = User::whereRole(Role::$USER)->orderBy('firstname')->get();

$total = $items->count();
// if filtered
if (session('filtered')) {
// filter entries on the filter criteria
$client = session('filter_fullname', '');
if (strlen($client) >= 2) {
$items = $items->filter(function ($item) use ($client) {
return (stristr($item->fullname, $client) !== false);
});
}

$cellphone = session('filter_cellphone', '');
if (strlen($cellphone) >= 2) {
$items = $items->filter(function ($item) use ($cellphone) {
return (stristr($item->cellphone, $cellphone) !== false);
});
}

$email = session('filter_email', '');
if (strlen($email) >= 2) {
$items = $items->filter(function ($item) use ($email) {
return (stristr($item->email, $email) !== false);
});
}
}

return ['items' => $items, 'total' => $total];
}
}
137 changes: 0 additions & 137 deletions app/Http/Controllers/Admin/Settings/AdministratorsController.php

This file was deleted.

4 changes: 2 additions & 2 deletions public/js/admin.js

Large diffs are not rendered by default.

Loading

0 comments on commit 0e72ddb

Please sign in to comment.