ldc-supportweb/app/Http/Controllers/ScheduleController.php
2021-12-10 12:18:03 +01:00

167 lines
5.8 KiB
PHP

<?php
namespace App\Http\Controllers;
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) {
if ($prevUserId != $schedule->user_id) {
//nouveau user
$arSchedules[$schedule->user_id] = array(
$schedule->slot => array(
$schedule->date->format('d/m/Y') => array(
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $schedule->id_lie,
'model_lie' => $schedule->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(
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $schedule->id_lie,
'model_lie' => $schedule->model_lie,
),
);
} else {
//same slot
if ($prevDate != $schedule->date) {
//new date
$arSchedules[$schedule->user_id][$schedule->slot][$schedule->date->format('d/m/Y')] = array(
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $schedule->id_lie,
'model_lie' => $schedule->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'));
}
/**
* Migrates data from week_plannings
* to schedules table
*
* @author Olivier Legat <olivier.legat@ldc.fr>
*
*/
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;
}
}