* */ class ScheduleController extends Controller { /** * ScheduleController */ public function __construct() { parent::__construct(); $this->middleware('is-admin'); } /** * Display schedule. * @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(Role::developper()) && !is_null($request->cookie('userAuthSchedule'))) { return $this->showAllSchedules(); } $date = CarbonImmutable::now()->startOfWeek(); $nbweeks = config('schedules.shownWeeks'); if ($request->p) { $date = CarbonImmutable::createFromFormat('d/m/Y', $request->p)->startOfWeek(); } $users = []; if (!is_null($request->cookie('userAuthSchedule'))) { $dbusers = User::where('id', auth()->user()->id)->get(); } else { $dbusers = User::where('role', Role::developper())->get(); } foreach ($dbusers as $user) { $users[$user->id] = ['name' => $user->name]; } $schedules = Schedule::whereHas('user', function (Builder $query) use ($request) { $query->where('deleted_at', null); $query->where('role', Role::developper()); if (!is_null($request->cookie('userAuthSchedule'))) { $query->where('id', auth()->user()->id); } }) ->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) { // New 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 // }. }//end if }//end if $prevUserId = $schedule->user_id; $prevDate = $schedule->date; $prevSlot = $schedule->slot; }//end foreach $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')); } /** * Display cell content. * @param \App\Http\Requests\StoreScheduleRequest $request * @return \Illuminate\Http\JsonResponse * @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException. */ public function setCellContent(StoreScheduleRequest $request) { $date = CarbonImmutable::createFromFormat('d/m/Y', $request->date); $fill = $request->all(); $fill['date'] = $date; $schedule = Schedule::updateOrCreate(['id' => $request->id], $fill); $schedule->saveOrFail(); $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); } $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, ]; return \response()->json(['status' => true, 'sched' => $arSchd]); } /** * Delet cell content. * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse * @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException. */ 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('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('schedule.index')->withCookie(cookie()->forget('userAuthSchedule')); } }