2021-12-04 17:09:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Enums\Role;
|
2022-01-02 13:03:40 +01:00
|
|
|
use App\Http\Requests\StoreScheduleRequest;
|
2021-12-05 15:01:32 +01:00
|
|
|
use App\Models\Schedule;
|
2021-12-04 17:09:22 +01:00
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Carbon\CarbonImmutable;
|
2022-08-22 15:38:36 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2021-12-04 17:09:22 +01:00
|
|
|
|
2021-12-05 15:01:32 +01:00
|
|
|
/**
|
|
|
|
|
* Alternate planner for Dev Team
|
|
|
|
|
*
|
2022-08-30 13:21:07 +00:00
|
|
|
* TODO: Refactor this controller.
|
|
|
|
|
*
|
2021-12-05 15:01:32 +01:00
|
|
|
* @author Olivier Legat <olivier.legat@ldc.fr>
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-12-04 17:09:22 +01:00
|
|
|
class ScheduleController extends Controller
|
|
|
|
|
{
|
2022-05-26 13:18:47 +02:00
|
|
|
/**
|
|
|
|
|
* ScheduleController
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->middleware('is-admin');
|
|
|
|
|
}
|
2023-06-22 11:08:17 +00:00
|
|
|
/**
|
|
|
|
|
* Display schedule.
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse
|
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException RouteNotFoundException.
|
|
|
|
|
*/
|
2021-12-04 17:09:22 +01:00
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
2023-06-22 11:08:17 +00:00
|
|
|
if (!\auth()->user()->role->equals(Role::developper()) && !is_null($request->cookie('userAuthSchedule'))) {
|
|
|
|
|
return $this->showAllSchedules();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 16:57:54 +02:00
|
|
|
$date = CarbonImmutable::now()->startOfWeek();
|
2022-03-08 11:38:07 +01:00
|
|
|
$nbweeks = config('schedules.shownWeeks');
|
2021-12-04 17:09:22 +01:00
|
|
|
if ($request->p) {
|
2021-12-05 15:01:32 +01:00
|
|
|
$date = CarbonImmutable::createFromFormat('d/m/Y', $request->p)->startOfWeek();
|
2021-12-04 17:09:22 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 11:08:17 +00:00
|
|
|
$users = [];
|
|
|
|
|
if (!is_null($request->cookie('userAuthSchedule'))) {
|
|
|
|
|
$dbusers = User::where('id', auth()->user()->id)->get();
|
|
|
|
|
} else {
|
|
|
|
|
$dbusers = User::where('role', Role::developper())->get();
|
|
|
|
|
}
|
2021-12-05 20:34:01 +01:00
|
|
|
|
|
|
|
|
foreach ($dbusers as $user) {
|
|
|
|
|
$users[$user->id] = ['name' => $user->name];
|
|
|
|
|
}
|
2023-06-22 11:08:17 +00:00
|
|
|
$schedules = Schedule::whereHas('user', function (Builder $query) use ($request) {
|
|
|
|
|
$query->where('deleted_at', null);
|
|
|
|
|
$query->where('role', Role::developper());
|
|
|
|
|
if (!is_null($request->cookie('userAuthSchedule'))) {
|
|
|
|
|
$query->where('id', auth()->user()->id);
|
|
|
|
|
}
|
2022-08-22 15:38:36 +02:00
|
|
|
})
|
2023-06-22 11:08:17 +00:00
|
|
|
->whereDate('date', '>=', $date)
|
|
|
|
|
->whereDate('date', '<=', $date->addDays($nbweeks * 7 - 1))
|
|
|
|
|
->orderBy('user_id')
|
|
|
|
|
->orderBy('slot')
|
|
|
|
|
->orderBy('date')
|
|
|
|
|
->get();
|
|
|
|
|
|
2021-12-05 15:01:32 +01:00
|
|
|
$arSchedules = array();
|
2022-03-29 16:57:54 +02:00
|
|
|
$prevUserId = null;
|
|
|
|
|
$prevDate = null;
|
|
|
|
|
$prevSlot = null;
|
2021-12-05 15:01:32 +01:00
|
|
|
foreach ($schedules as $schedule) {
|
2022-03-29 16:57:54 +02:00
|
|
|
$id_lie = array('label' => null, 'value' => null);
|
|
|
|
|
if (!is_null($schedule->model_lie)) {
|
2021-12-18 16:52:55 +01:00
|
|
|
if (isset($schedule->relatedModel->name)) {
|
2022-03-29 16:57:54 +02:00
|
|
|
$id_lie = array('label' => $schedule->relatedModel->name, 'value' => $schedule->id_lie);
|
2021-12-18 16:52:55 +01:00
|
|
|
} else {
|
2022-03-29 16:57:54 +02:00
|
|
|
$id_lie = array('label' => 'Ticket ' . $schedule->id_lie, 'value' => $schedule->id_lie);
|
2021-12-18 16:52:55 +01:00
|
|
|
}
|
2021-12-18 18:20:57 +01:00
|
|
|
$model_lie = $schedule->model_lie;
|
|
|
|
|
} else {
|
2022-03-29 16:57:54 +02:00
|
|
|
$model_lie = array('label' => null, 'value' => null);
|
2021-12-18 16:46:50 +01:00
|
|
|
}
|
2021-12-18 16:52:55 +01:00
|
|
|
|
2021-12-05 15:01:32 +01:00
|
|
|
if ($prevUserId != $schedule->user_id) {
|
2023-06-22 11:08:17 +00:00
|
|
|
// New user.
|
2021-12-05 15:01:32 +01:00
|
|
|
$arSchedules[$schedule->user_id] = array(
|
2021-12-05 20:34:01 +01:00
|
|
|
$schedule->slot => array(
|
|
|
|
|
$schedule->date->format('d/m/Y') => array(
|
2021-12-12 15:56:42 +01:00
|
|
|
'id' => $schedule->id,
|
2021-12-05 15:01:32 +01:00
|
|
|
'type' => $schedule->activity,
|
|
|
|
|
'text' => $schedule->description,
|
2022-03-29 16:57:54 +02:00
|
|
|
'id_lie' => $id_lie,
|
|
|
|
|
// $schedule->id_lie,
|
2021-12-18 18:20:57 +01:00
|
|
|
'model_lie' => $model_lie,
|
2021-12-05 15:01:32 +01:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2023-06-22 11:08:17 +00:00
|
|
|
// Same user.
|
2021-12-05 20:34:01 +01:00
|
|
|
if ($prevSlot != $schedule->slot) {
|
2023-06-22 11:08:17 +00:00
|
|
|
// New slot.
|
2021-12-05 20:34:01 +01:00
|
|
|
$arSchedules[$schedule->user_id][$schedule->slot] = array(
|
|
|
|
|
$schedule->date->format('d/m/Y') => array(
|
2021-12-12 15:56:42 +01:00
|
|
|
'id' => $schedule->id,
|
2021-12-05 15:01:32 +01:00
|
|
|
'type' => $schedule->activity,
|
|
|
|
|
'text' => $schedule->description,
|
2022-03-29 16:57:54 +02:00
|
|
|
'id_lie' => $id_lie,
|
|
|
|
|
// $schedule->id_lie,
|
2021-12-18 18:20:57 +01:00
|
|
|
'model_lie' => $model_lie,
|
2021-12-05 15:01:32 +01:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2023-06-22 11:08:17 +00:00
|
|
|
// Same slot.
|
2021-12-05 20:34:01 +01:00
|
|
|
if ($prevDate != $schedule->date) {
|
2023-06-22 11:08:17 +00:00
|
|
|
// New date.
|
2021-12-05 20:34:01 +01:00
|
|
|
$arSchedules[$schedule->user_id][$schedule->slot][$schedule->date->format('d/m/Y')] = array(
|
2021-12-12 15:56:42 +01:00
|
|
|
'id' => $schedule->id,
|
2021-12-05 15:01:32 +01:00
|
|
|
'type' => $schedule->activity,
|
|
|
|
|
'text' => $schedule->description,
|
2022-03-29 16:57:54 +02:00
|
|
|
'id_lie' => $id_lie,
|
|
|
|
|
// $schedule->id_lie,
|
2021-12-18 18:20:57 +01:00
|
|
|
'model_lie' => $model_lie,
|
2021-12-05 15:01:32 +01:00
|
|
|
);
|
2023-06-22 11:08:17 +00:00
|
|
|
}
|
|
|
|
|
// else {
|
2022-03-29 16:57:54 +02:00
|
|
|
// same date
|
|
|
|
|
// should never be here
|
2023-06-22 11:08:17 +00:00
|
|
|
// }.
|
2022-03-29 16:57:54 +02:00
|
|
|
}//end if
|
|
|
|
|
}//end if
|
2021-12-05 15:01:32 +01:00
|
|
|
$prevUserId = $schedule->user_id;
|
2022-03-29 16:57:54 +02:00
|
|
|
$prevDate = $schedule->date;
|
|
|
|
|
$prevSlot = $schedule->slot;
|
|
|
|
|
}//end foreach
|
2021-12-04 17:09:22 +01:00
|
|
|
|
2021-12-05 20:34:01 +01:00
|
|
|
$weekCells = array();
|
2022-03-29 16:57:54 +02:00
|
|
|
for ($i = 0; $i < $nbweeks; $i++) {
|
2021-12-05 20:34:01 +01:00
|
|
|
$weekCells[] = array(
|
|
|
|
|
"num" => $date->addDays($i * 7)->weekOfYear,
|
|
|
|
|
"month" => $date->addDays($i * 7)->shortLocaleMonth,
|
|
|
|
|
"year" => $date->addDays($i * 7)->format('Y')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('schedule.index', compact('users', 'date', 'weekCells', 'arSchedules'));
|
2021-12-05 15:01:32 +01:00
|
|
|
}
|
2021-12-18 19:51:16 +01:00
|
|
|
|
2023-06-22 11:08:17 +00:00
|
|
|
/**
|
|
|
|
|
* Display cell content.
|
|
|
|
|
* @param \App\Http\Requests\StoreScheduleRequest $request
|
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
*/
|
2022-01-02 13:03:40 +01:00
|
|
|
public function setCellContent(StoreScheduleRequest $request)
|
2021-12-18 19:51:16 +01:00
|
|
|
{
|
2022-03-29 16:57:54 +02:00
|
|
|
$date = CarbonImmutable::createFromFormat('d/m/Y', $request->date);
|
|
|
|
|
$fill = $request->all();
|
2021-12-18 19:51:16 +01:00
|
|
|
$fill['date'] = $date;
|
|
|
|
|
|
2022-03-29 16:57:54 +02:00
|
|
|
$schedule = Schedule::updateOrCreate(['id' => $request->id], $fill);
|
2021-12-18 20:19:57 +01:00
|
|
|
$schedule->saveOrFail();
|
|
|
|
|
|
2022-03-29 16:57:54 +02:00
|
|
|
$id_lie = array('label' => null, 'value' => null);
|
|
|
|
|
if (!is_null($schedule->model_lie)) {
|
2021-12-18 20:19:57 +01:00
|
|
|
if (isset($schedule->relatedModel->name)) {
|
2022-03-29 16:57:54 +02:00
|
|
|
$id_lie = array('label' => $schedule->relatedModel->name, 'value' => $schedule->id_lie);
|
2021-12-18 20:19:57 +01:00
|
|
|
} else {
|
2022-03-29 16:57:54 +02:00
|
|
|
$id_lie = array('label' => 'Ticket ' . $schedule->id_lie, 'value' => $schedule->id_lie);
|
2021-12-18 20:19:57 +01:00
|
|
|
}
|
|
|
|
|
$model_lie = $schedule->model_lie;
|
|
|
|
|
} else {
|
2022-03-29 16:57:54 +02:00
|
|
|
$model_lie = array('label' => null, 'value' => null);
|
2021-12-18 20:19:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$arSchd = [
|
|
|
|
|
'id' => $schedule->id,
|
|
|
|
|
'user_id' => $schedule->user_id,
|
|
|
|
|
'date' => $schedule->date->format('d/m/Y'),
|
|
|
|
|
'activity' => $schedule->activity,
|
|
|
|
|
'description' => $schedule->description,
|
|
|
|
|
'id_lie' => $id_lie,
|
|
|
|
|
'model_lie' => $model_lie,
|
|
|
|
|
'slot' => $schedule->slot,
|
|
|
|
|
];
|
|
|
|
|
|
2022-03-29 16:57:54 +02:00
|
|
|
return \response()->json(['status' => true, 'sched' => $arSchd]);
|
2021-12-18 19:51:16 +01:00
|
|
|
}
|
2021-12-18 20:33:26 +01:00
|
|
|
|
2023-06-22 11:08:17 +00:00
|
|
|
/**
|
|
|
|
|
* Delet cell content.
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
*/
|
2021-12-18 20:33:26 +01:00
|
|
|
public function eraseCellContent(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$schedule = Schedule::findOrFail($request->id);
|
|
|
|
|
$schedule->delete();
|
|
|
|
|
return \response()->json(['status' => true]);
|
|
|
|
|
}
|
2023-06-22 11:08:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the auth user schedule.
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException RouteNotFoundException.
|
|
|
|
|
*/
|
|
|
|
|
public function showMySchedule()
|
|
|
|
|
{
|
|
|
|
|
return \redirect()->route('schedule.index')->withCookie(cookie()->forever('userAuthSchedule', 'true'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display all the dev schedule.
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException RouteNotFoundException.
|
|
|
|
|
*/
|
|
|
|
|
public function showAllSchedules()
|
|
|
|
|
{
|
|
|
|
|
return \redirect()->route('schedule.index')->withCookie(cookie()->forget('userAuthSchedule'));
|
|
|
|
|
}
|
2021-12-04 17:09:22 +01:00
|
|
|
}
|