2018-09-15 05:09:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-10-03 02:53:35 +00:00
|
|
|
use Auth;
|
2018-10-10 03:46:28 +00:00
|
|
|
use Mail;
|
2018-09-15 05:09:07 +00:00
|
|
|
use \App\User;
|
2018-09-15 05:33:21 +00:00
|
|
|
use \App\Mail\UserWaiting;
|
2018-09-15 05:09:07 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class AlertController extends Controller
|
|
|
|
{
|
2018-10-10 03:46:28 +00:00
|
|
|
/**
|
|
|
|
* Store the waiting_user_id on db
|
|
|
|
* so the user can be alerted when
|
|
|
|
* the item is free
|
|
|
|
*
|
|
|
|
* @param Request $request Form data
|
|
|
|
*
|
|
|
|
* @return redirect to home
|
|
|
|
*/
|
2018-09-15 05:09:07 +00:00
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2018-09-19 17:58:10 +00:00
|
|
|
$item = User::loggedIn()->items()->find(request('item'));
|
2018-10-21 14:57:23 +00:00
|
|
|
if (!$item->used_by) {
|
|
|
|
session()->flash(
|
|
|
|
FlashMessage::PRIMARY,
|
|
|
|
__('Oh! This item has just being returned. Take it before anyone else!')
|
|
|
|
);
|
|
|
|
return redirect('home');
|
|
|
|
}
|
2018-10-21 15:36:02 +00:00
|
|
|
|
|
|
|
if ($item->used_by == Auth::id()) {
|
|
|
|
return redirect('home');
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->storeAlert();
|
2018-09-15 05:33:21 +00:00
|
|
|
|
2018-10-03 02:53:35 +00:00
|
|
|
$loggedUser = Auth::user()->name;
|
2018-09-15 05:33:21 +00:00
|
|
|
$userWithItem = User::find($item->used_by);
|
2018-10-10 03:46:28 +00:00
|
|
|
Mail::to($userWithItem)
|
2018-10-03 02:53:35 +00:00
|
|
|
->locale($userWithItem->language)
|
|
|
|
->send(new UserWaiting($loggedUser, $userWithItem->name, $item));
|
2018-09-15 05:33:21 +00:00
|
|
|
|
2018-09-15 05:09:07 +00:00
|
|
|
return redirect('home');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(Request $request)
|
|
|
|
{
|
2018-09-19 17:58:10 +00:00
|
|
|
$item = User::loggedIn()->items()->find(request('item'));
|
2018-09-15 05:09:07 +00:00
|
|
|
|
2018-10-21 15:36:02 +00:00
|
|
|
if ($item->waiting_user_id != Auth::id()) {
|
|
|
|
return redirect('home');
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->removeAlert();
|
2018-09-15 05:09:07 +00:00
|
|
|
return redirect('home');
|
|
|
|
}
|
|
|
|
}
|