* */ class ScheduleController extends Controller { public function index(Request $request) { $date = CarbonImmutable::now()->startOfWeek(); $nbweek = 2; $users = User::where('role', Role::developper())->get(); if ($request->p) { $date = CarbonImmutable::createFromFormat('d/m/Y', $request->p)->startOfWeek(); } $schedules = Schedule::whereDate('date', '>=', $date) ->whereDate('date', '<=', $date->addDays($nbweek * 7)) ->orderBy('user_id') ->orderBy('date') ->orderBy('slot') ->get(); $arSchedules = array(); $prevUserId = null; $prevDate = null; $prevSlot = null; foreach ($schedules as $schedule) { if ($prevUserId != $schedule->user_id) { //nouveau user $arSchedules[$schedule->user_id] = array( $schedule->date->format('d/m/Y') => array( $schedule->slot => array( 'type' => $schedule->activity, 'text' => $schedule->description, 'id_lie' => $schedule->id_lie, 'model_lie' => $schedule->model_lie, ), ) ); } else { //same user if ($prevDate != $schedule->date) { //new date $arSchedules[$schedule->user_id][$schedule->date->format('d/m/Y')] = array( $schedule->slot => array( 'type' => $schedule->activity, 'text' => $schedule->description, 'id_lie' => $schedule->id_lie, 'model_lie' => $schedule->model_lie, ), ); } else { //same date if ($prevSlot != $schedule->slot) { //new slot $arSchedules[$schedule->user_id][$schedule->date->format('d/m/Y')][$schedule->slot] = array( 'type' => $schedule->activity, 'text' => $schedule->description, 'id_lie' => $schedule->id_lie, 'model_lie' => $schedule->model_lie, ); } else { //same slot ? //should never en here } } } $prevUserId = $schedule->user_id; $prevDate = $schedule->date; $prevSlot = $schedule->slot; } return view('schedule.index', compact('users', 'date', 'nbweek', 'arSchedules')); } /** * Migrates data from week_plannings * to schedules table * * @author Olivier Legat * */ public function import(Request $request) { // $schedule_test = Schedule::first(); // dd($schedule_test->relatedModel); $week_plannings = WeekPlanning::all(); foreach ($week_plannings as $oneweek) { $user_id = $oneweek->user_id; $plan = $oneweek->planning; foreach ($plan as $time => $data) { $date = Carbon::createFromTimestamp($time); foreach ($data as $dayslot => $cell) { $activite = $this->convertColorToType($cell["type"]); $description = $cell["text"]; $id_objetlie = $cell["foreign"]; $type_objetlie = $cell["cellModel"]; //c'est une table d'après l'Enum CellModel $collected = [ 'user_id' => $user_id, 'date' => $date, 'slot' => $dayslot, 'activity' => $activite, 'description' => $description, 'id_lie' => $id_objetlie, 'model_lie' => $type_objetlie ]; //si activite null et description null ne pas enregistrer if (!is_null($activite) && !is_null($description)) { $schedule = new Schedule(); $schedule->fill($collected); $schedule->save(); } } } } dump('finished'); } /** * convertir une value de couleur en type */ private function convertColorToType($value) { $converter = array( '#c6e0b4' => 'support', '#b4c6e7' => 'rnd', '#00b0f0' => 'dev', '#ffe699' => 'projects', '#f4b084' => 'management', '#aeaaaa' => 'absence', 'abscence' => 'absence' ); return $converter[$value] ?? null; } }