131 lines
4.8 KiB
PHP
131 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\Planning\ActivityType;
|
|
use App\Enums\Role;
|
|
use App\Models\Schedule;
|
|
use App\Models\User;
|
|
use App\Models\WeekPlanning;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
/**
|
|
* Alternate planner for Dev Team
|
|
*
|
|
* @author Olivier Legat <olivier.legat@ldc.fr>
|
|
*
|
|
*/
|
|
class ScheduleController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$date = CarbonImmutable::now()->startOfWeek();
|
|
$nbweeks = 2;
|
|
if ($request->p) {
|
|
$date = CarbonImmutable::createFromFormat('d/m/Y', $request->p)->startOfWeek();
|
|
}
|
|
|
|
|
|
$dbusers = User::where('role', Role::developper())->get();
|
|
$users = [];
|
|
foreach ($dbusers as $user) {
|
|
$users[$user->id] = ['name' => $user->name];
|
|
}
|
|
|
|
$schedules = Schedule::whereDate('date', '>=', $date)
|
|
->whereDate('date', '<=', $date->addDays($nbweeks * 7 - 1))
|
|
->orderBy('user_id')
|
|
->orderBy('slot')
|
|
->orderBy('date')
|
|
->get();
|
|
|
|
$arSchedules = array();
|
|
$prevUserId = null;
|
|
$prevDate = null;
|
|
$prevSlot = null;
|
|
foreach ($schedules as $schedule) {
|
|
$id_lie = array('label'=> null, 'value'=> null);
|
|
if(!is_null($schedule->model_lie)) {
|
|
if (isset($schedule->relatedModel->name)) {
|
|
$id_lie = array('label'=>$schedule->relatedModel->name, 'value'=>$schedule->id_lie);
|
|
} else {
|
|
$id_lie = array('label'=>'Ticket '.$schedule->id_lie, 'value'=>$schedule->id_lie);
|
|
}
|
|
$model_lie = $schedule->model_lie;
|
|
} else {
|
|
$model_lie = array('label'=> null, 'value'=> null);
|
|
}
|
|
|
|
if ($prevUserId != $schedule->user_id) {
|
|
//nouveau user
|
|
$arSchedules[$schedule->user_id] = array(
|
|
$schedule->slot => array(
|
|
$schedule->date->format('d/m/Y') => array(
|
|
'id' => $schedule->id,
|
|
'type' => $schedule->activity,
|
|
'text' => $schedule->description,
|
|
'id_lie' => $id_lie, //$schedule->id_lie,
|
|
'model_lie' => $model_lie,
|
|
),
|
|
)
|
|
);
|
|
} else {
|
|
//same user
|
|
if ($prevSlot != $schedule->slot) {
|
|
//new slot
|
|
$arSchedules[$schedule->user_id][$schedule->slot] = array(
|
|
$schedule->date->format('d/m/Y') => array(
|
|
'id' => $schedule->id,
|
|
'type' => $schedule->activity,
|
|
'text' => $schedule->description,
|
|
'id_lie' => $id_lie, //$schedule->id_lie,
|
|
'model_lie' => $model_lie,
|
|
),
|
|
);
|
|
} else {
|
|
//same slot
|
|
if ($prevDate != $schedule->date) {
|
|
//new date
|
|
$arSchedules[$schedule->user_id][$schedule->slot][$schedule->date->format('d/m/Y')] = array(
|
|
'id' => $schedule->id,
|
|
'type' => $schedule->activity,
|
|
'text' => $schedule->description,
|
|
'id_lie' => $id_lie, //$schedule->id_lie,
|
|
'model_lie' => $model_lie,
|
|
);
|
|
} else {
|
|
//same date
|
|
//should never be here
|
|
}
|
|
}
|
|
}
|
|
$prevUserId = $schedule->user_id;
|
|
$prevDate = $schedule->date;
|
|
$prevSlot = $schedule->slot;
|
|
}
|
|
|
|
$weekCells = array();
|
|
for ($i=0; $i < $nbweeks; $i++) {
|
|
$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'));
|
|
}
|
|
|
|
public function setCellContent(Request $request)
|
|
{
|
|
$date = CarbonImmutable::createFromFormat('d/m/Y', $request->date);
|
|
$fill = $request->all();
|
|
$fill['date'] = $date;
|
|
|
|
$schd = Schedule::updateOrCreate(['id' => $request->id],$fill);
|
|
$schd->saveOrFail();
|
|
return \response()->json(['status' => true, 'sched'=>$schd]);
|
|
}
|
|
}
|