Groups HOME itesm by Product

This commit is contained in:
Bruno F. Fontes 2018-09-21 00:42:19 -03:00
parent d87d4ba4bf
commit 0cd807169e
Signed by: brunofontes
GPG Key ID: EE3447CE80048495
2 changed files with 27 additions and 16 deletions

View File

@ -3,7 +3,6 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use \App\User; use \App\User;
use Illuminate\Http\Request;
class HomeController extends Controller class HomeController extends Controller
{ {
@ -19,10 +18,10 @@ class HomeController extends Controller
/** /**
* Get a name from a specific id in a array * Get a name from a specific id in a array
* *
* @param array $array The array of objects with ID and Names * @param array $array The array of objects with ID and Names
* @param int $id The ID of the user * @param int $id The ID of the user
* *
* @return string The username of the specified id * @return string The username of the specified id
*/ */
protected function findName($array, $id) protected function findName($array, $id)
@ -34,6 +33,19 @@ class HomeController extends Controller
} }
} }
/**
* Check if the user_id alreay exists on $users array.
*
* @param array $users The array with users
* @param int $user_id The user_id to try to find on array
*
* @return boolean
*/
public function isNewUser($users, $user_id)
{
return ($user_id && !isset($users[$user_id]));
}
/** /**
* Show the application dashboard. * Show the application dashboard.
* *
@ -45,14 +57,16 @@ class HomeController extends Controller
$items = User::loggedIn()->items()->with('users')->get(); $items = User::loggedIn()->items()->with('users')->get();
foreach ($items as $item) { foreach ($items as $item) {
if ($item->used_by && !isset($users[$item->used_by])) { if ($this->isNewUser($users, $item->used_by)) {
$users[$item->used_by] = $this->findName($item->users, $item->used_by); $users[$item->used_by] = $this->findName($item->users, $item->used_by);
} }
if ($item->waiting_user_id && !isset($users[$item->waiting_user_id])) { if ($this->isNewUser($users, $item->waiting_user_id)) {
$users[$item->waiting_user_id] = $this->findName($item->users, $item->waiting_user_id); $users[$item->waiting_user_id] = $this->findName($item->users, $item->waiting_user_id);
} }
} }
return view('home', compact('items', 'users'));
$products = $items->sortBy('product.name', SORT_NATURAL | SORT_FLAG_CASE)->groupBy('product.name');
return view('home', compact('products', 'users'));
} }
} }

View File

@ -4,16 +4,11 @@
<div class="container"> <div class="container">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-md-8"> <div class="col-md-8">
<div class="card"> @forelse ($products as $items)
<div class="card-header">Your items</div> <div class="card mb-4">
<div class="card-header">{{$items->first()->product->name}}</div>
<div class="card-body"> <div class="card-body">
@if (session('status')) @forelse ($items->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE) as $item)
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
@forelse ($items as $item)
@if (!$loop->first) @if (!$loop->first)
<hr class="m-3"> <hr class="m-3">
@endif @endif
@ -23,7 +18,7 @@
<a href="{{$item->product->url}}" class="link-unstyled" target="_blank" rel="noopener noreferrer"> <a href="{{$item->product->url}}" class="link-unstyled" target="_blank" rel="noopener noreferrer">
@endif @endif
<strong>{{$item->name}}</strong> ({{$item->product->name}}) <strong>{{$item->name}}</strong>
@if ($item->product->url) @if ($item->product->url)
</a> </a>
@ -42,6 +37,8 @@
@endforelse @endforelse
</div> </div>
</div> </div>
@empty
@endforelse
</div> </div>
</div> </div>
</div> </div>