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

179 lines
6.4 KiB
PHP
Raw Normal View History

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');
}
2021-12-04 17:09:22 +01:00
public function index(Request $request)
{
2022-03-29 16:57:54 +02:00
$date = CarbonImmutable::now()->startOfWeek();
$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
}
2021-12-05 20:34:01 +01:00
$dbusers = User::where('role', Role::developper())->get();
2022-03-29 16:57:54 +02:00
$users = [];
2021-12-05 20:34:01 +01:00
foreach ($dbusers as $user) {
$users[$user->id] = ['name' => $user->name];
}
2022-08-22 15:38:36 +02:00
$schedules = Schedule::whereHas('user', function(Builder $query) {
$query->where('deleted_at', NULL);
})
->whereDate('date', '>=', $date)
2021-12-05 20:34:01 +01:00
->whereDate('date', '<=', $date->addDays($nbweeks * 7 - 1))
2021-12-05 15:01:32 +01:00
->orderBy('user_id')
->orderBy('slot')
2021-12-05 20:34:01 +01:00
->orderBy('date')
2021-12-05 15:01:32 +01:00
->get();
2022-08-22 15:38:36 +02:00
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:52:55 +01:00
2021-12-05 15:01:32 +01:00
if ($prevUserId != $schedule->user_id) {
2022-03-29 16:57:54 +02:00
// nouveau 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,
// $schedule->id_lie,
2021-12-18 18:20:57 +01:00
'model_lie' => $model_lie,
2021-12-05 15:01:32 +01:00
),
)
);
} else {
2022-03-29 16:57:54 +02:00
// same user
2021-12-05 20:34:01 +01:00
if ($prevSlot != $schedule->slot) {
2022-03-29 16:57:54 +02: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(
'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 {
2022-03-29 16:57:54 +02:00
// same slot
2021-12-05 20:34:01 +01:00
if ($prevDate != $schedule->date) {
2022-03-29 16:57:54 +02:00
// 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,
// $schedule->id_lie,
2021-12-18 18:20:57 +01:00
'model_lie' => $model_lie,
2021-12-05 15:01:32 +01:00
);
} else {
2022-03-29 16:57:54 +02:00
// same date
// should never be here
2021-12-05 15:01:32 +01: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
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();
2022-03-29 16:57:54 +02:00
$id_lie = array('label' => null, 'value' => null);
if (!is_null($schedule->model_lie)) {
if (isset($schedule->relatedModel->name)) {
2022-03-29 16:57:54 +02:00
$id_lie = array('label' => $schedule->relatedModel->name, 'value' => $schedule->id_lie);
} else {
2022-03-29 16:57:54 +02:00
$id_lie = array('label' => 'Ticket ' . $schedule->id_lie, 'value' => $schedule->id_lie);
}
$model_lie = $schedule->model_lie;
} else {
2022-03-29 16:57:54 +02:00
$model_lie = array('label' => 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
public function eraseCellContent(Request $request)
{
$schedule = Schedule::findOrFail($request->id);
$schedule->delete();
return \response()->json(['status' => true]);
}
2021-12-04 17:09:22 +01:00
}