116 lines
2.7 KiB
PHP
116 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Auth\ForgotPasswordController;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Kwaadpepper\CrudPolicies\Traits\CrudController;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
use CrudController {
|
|
__construct as __CrudConstructor;
|
|
}
|
|
|
|
/**
|
|
* UserController
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->__CrudConstructor();
|
|
$this->authorizeResource(User::class, 'user');
|
|
}
|
|
|
|
/** @var User */
|
|
public static $modelClass = User::class;
|
|
|
|
/**
|
|
* Crud Layout
|
|
*
|
|
* @var string
|
|
*/
|
|
public static $layout = 'layout.app';
|
|
|
|
/**
|
|
* After store model middleware
|
|
*
|
|
* @param Model $model
|
|
* @return void
|
|
*/
|
|
public function storedModel(Model &$model)
|
|
{
|
|
/** @var \App\Models\User */
|
|
$model = $model;
|
|
$model->sendMailConfirm();
|
|
}
|
|
|
|
/**
|
|
* Password reset form
|
|
*
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function passwordReset()
|
|
{
|
|
return view('users.passwordReset');
|
|
}
|
|
|
|
/**
|
|
* Password reset submit
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function passwordResetSubmit()
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
$request = new Request(['email' => $user->email]);
|
|
return app(ForgotPasswordController::class)->sendResetLinkEmail($request);
|
|
}
|
|
|
|
/**
|
|
* Show stats
|
|
*
|
|
* TODO: refator this using carbon.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function stats()
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
$stats = [];
|
|
$curYear = date('Y');
|
|
|
|
for ($i = 2021; $i <= $curYear; $i++) {
|
|
$start = $i . '-03-01';
|
|
$end = $i + 1 . '-02-' . $this->getLastDayFebruary($i + 1);
|
|
|
|
$datas = $user->schedules()->selectRaw('count(id) as nb, activity')
|
|
->whereBetween('date', [$start, $end])
|
|
->groupBy('activity')
|
|
->orderBy('activity', 'ASC')
|
|
->get();
|
|
|
|
$stats[sprintf("%d/%d", $i, $i + 1)] = $datas;
|
|
}
|
|
|
|
return view('users.stats', ['stats' => array_reverse($stats)]);
|
|
}
|
|
|
|
/**
|
|
* Get february last day date of the year
|
|
*
|
|
* @param integer $year
|
|
* @return integer 28 or 29
|
|
*/
|
|
private function getLastDayFebruary(int $year): int
|
|
{
|
|
if ((is_int($year / 4) && !is_int($year / 100)) || is_int($year / 400)) {
|
|
return 29;
|
|
}
|
|
return 28;
|
|
}
|
|
}
|