shareit/app/Http/Controllers/HomeController.php

73 lines
1.8 KiB
PHP
Raw Normal View History

2018-09-09 04:03:41 +00:00
<?php
namespace App\Http\Controllers;
use \App\User;
2018-09-09 04:03:41 +00:00
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Get a name from a specific id in a array
2018-09-21 03:42:19 +00:00
*
* @param array $array The array of objects with ID and Names
* @param int $id The ID of the user
2018-09-21 03:42:19 +00:00
*
* @return string The username of the specified id
*/
protected function findName($array, $id)
{
foreach ($array as $object) {
if ($object->id == $id) {
return $object->name;
}
}
}
2018-09-21 03:42:19 +00:00
/**
* 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]));
}
2018-09-09 04:03:41 +00:00
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = [];
$items = User::loggedIn()->items()->with('users')->get();
foreach ($items as $item) {
2018-09-21 03:42:19 +00:00
if ($this->isNewUser($users, $item->used_by)) {
$users[$item->used_by] = $this->findName($item->users, $item->used_by);
}
2018-09-21 03:42:19 +00:00
if ($this->isNewUser($users, $item->waiting_user_id)) {
2018-09-15 05:09:07 +00:00
$users[$item->waiting_user_id] = $this->findName($item->users, $item->waiting_user_id);
}
}
2018-09-21 03:42:19 +00:00
$products = $items->sortBy('product.name', SORT_NATURAL | SORT_FLAG_CASE)->groupBy('product.name');
return view('home', compact('products', 'users'));
2018-09-09 04:03:41 +00:00
}
}