ldc-supportweb/app/Http/Controllers/Bo/ScheduleController.php

257 lines
9.5 KiB
PHP
Raw Normal View History

2021-12-04 17:09:22 +01:00
<?php
namespace App\Http\Controllers\Bo;
2021-12-04 17:09:22 +01:00
use App\Enums\Users\RoleEnum;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\StoreScheduleRequest;
2021-12-05 15:01:32 +01:00
use App\Models\Schedule;
use App\Models\Ticket;
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
class ScheduleController extends Controller
{
/**
2023-06-23 07:14:39 +00:00
* Create the date
*
* @param \Illuminate\Http\Request $request
2023-06-23 07:14:39 +00:00
* @return \Carbon\CarbonImmutable|false
*/
2023-06-23 07:14:39 +00:00
private function dateCreation(Request $request)
2021-12-04 17:09:22 +01:00
{
2023-06-23 07:14:39 +00:00
$date = CarbonImmutable::now()->startOfWeek();
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-23 07:14:39 +00:00
return $date;
}
2021-12-04 17:09:22 +01:00
2023-06-23 07:14:39 +00:00
/**
* Create the users table.
*
2023-06-23 07:14:39 +00:00
* @param \Illuminate\Http\Request $request
* @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
*/
private function createUsers(Request $request)
{
$users = [];
if (!is_null($request->cookie('userAuthSchedule'))) {
$dbusers = User::where('id', auth()->user()->id)->get();
} else {
$dbusers = User::where('role', RoleEnum::developper)->get();
}
2021-12-05 20:34:01 +01:00
foreach ($dbusers as $user) {
$users[$user->id] = ['name' => $user->name];
}
2023-06-23 07:14:39 +00:00
return $users;
}
/**
* Display schedule.
*
2023-06-23 07:14:39 +00:00
* @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.
*/
public function index(Request $request)
{
if (!\auth()->user()->role->equals(RoleEnum::developper) && !is_null($request->cookie('userAuthSchedule'))) {
2023-06-23 07:14:39 +00:00
return $this->showAllSchedules();
}
$date = $this->dateCreation($request);
$nbweeks = config('schedules.shownWeeks');
$users = $this->createUsers($request);
$schedules = Schedule::whereHas('user', function (Builder $query) use ($request) {
$query->where('deleted_at', null);
$query->where('role', RoleEnum::developper);
if (!is_null($request->cookie('userAuthSchedule'))) {
$query->where('id', auth()->user()->id);
}
2022-08-22 15:38:36 +02: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) {
$id_lie = ['label' => null, 'name' => null, 'value' => ''];
2022-03-29 16:57:54 +02:00
if (!is_null($schedule->model_lie)) {
2021-12-18 16:52:55 +01:00
if (isset($schedule->relatedModel->name)) {
$id_lie = [
'label' => $schedule->relatedModel->name,
'name' => $schedule->relatedModel->name,
'value' => $schedule->id_lie
];
2021-12-18 16:52:55 +01:00
} else {
$ticket = Ticket::find($schedule->id_lie);
$id_lie = [
'label' => 'Ticket ' . $schedule->id_lie,
'name' => $ticket->formatNameForSchedule($ticket),
'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 {
$model_lie = ['label' => null, 'value' => null];
}//end if
2021-12-18 16:52:55 +01:00
2021-12-05 15:01:32 +01:00
if ($prevUserId != $schedule->user_id) {
// 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(
'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,
2021-12-18 18:20:57 +01:00
'model_lie' => $model_lie,
2021-12-05 15:01:32 +01:00
),
)
);
} else {
// Same user.
2021-12-05 20:34:01 +01:00
if ($prevSlot != $schedule->slot) {
// New slot.
2021-12-05 20:34:01 +01:00
$arSchedules[$schedule->user_id][$schedule->slot] = array(
$schedule->date->format('d/m/Y') => array(
'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,
2021-12-18 18:20:57 +01:00
'model_lie' => $model_lie,
2021-12-05 15:01:32 +01:00
),
);
} else {
// Same slot.
2021-12-05 20:34:01 +01:00
if ($prevDate != $schedule->date) {
// New date.
2021-12-05 20:34:01 +01:00
$arSchedules[$schedule->user_id][$schedule->slot][$schedule->date->format('d/m/Y')] = array(
'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,
2021-12-18 18:20:57 +01:00
'model_lie' => $model_lie,
2021-12-05 15:01:32 +01: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')
);
2021-12-05 20:34:01 +01:00
}
return view('bo.pages.schedule.index', compact('users', 'date', 'weekCells', 'arSchedules'));
2021-12-05 15:01:32 +01:00
}
2021-12-18 19:51:16 +01:00
/**
* Display cell content.
*
* @param \App\Http\Requests\Bo\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);
$schedule->saveOrFail();
$id_lie = array('label' => null, 'name' => null, 'value' => null);
2022-03-29 16:57:54 +02:00
if (!is_null($schedule->model_lie)) {
if (isset($schedule->relatedModel->name)) {
$id_lie = array(
'label' => $schedule->relatedModel->name,
'name' => $schedule->relatedModel->name,
'value' => $schedule->id_lie
);
} else {
$id_lie = array(
'label' => 'Ticket ' . $schedule->id_lie,
'name' => Ticket::find($schedule->id_lie)->formatNameForSchedule(Ticket::find($schedule->id_lie)),
'value' => $schedule->id_lie
);
}
$model_lie = $schedule->model_lie;
} else {
$model_lie = array('label' => null, 'name' => null, 'value' => null);
}
$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
/**
* 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]);
}
/**
* 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('bo.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('bo.schedule.index')->withCookie(cookie()->forget('userAuthSchedule'));
}
2021-12-04 17:09:22 +01:00
}