/** * Responds to requests to GET /schedules/getCalendar/{id} from the calendar view. This creates the json * object with the array of calendar activities to be rendered in the FullCalendar jquery plugin calendar. */ public function getCalendar($id = null) { $calendarArray = []; $schedule = \App\Schedule::with('activities')->find($id); // The toughest part of this was creating the input data in the json format that FullCalendar required. I didn't exactly design // the data to plug right in. // First grab each activity in the schedule foreach ($schedule->activities as $activity) { $activity_dow = \App\Activities_Dow::where('activity_id', '=', $activity->id)->get(); // Then find each day of the week that the activity is on. Add the DOW number - 1 to the start date (Monday is DOW 2, but start date is always // Sunday so need to subtract 1) foreach ($activity_dow as $dow) { $start = $schedule->start_dt; $inputDate = $start . " + " . ($dow->day_of_week - 1) . " days"; // format the time of the activity to 4 digits i.e. in case someone entered 800 instead of 0800. $activityTime = sprintf("%04d", $activity->default_time); // Split string into an array of 2 digit elements $chunks = str_split($activityTime, 2); // Convert array to string with : between the elements. Makes 0800 into 08:00 which is the format I need. $activityTime = implode(':', $chunks); // Take the input date that I calculated earlier and make it a date in the right format $activityDate = date('Y-m-d', strtotime($inputDate)); // Add on the time and reformat $activityStartDate = date('Y-m-d H:i', strtotime($activityDate . " " . $activityTime)); // Calculate the time of the activity end by adding the duration in minutes. Format that too. $activityEnd = date('Y-m-d H:i', strtotime($activityStartDate . " + " . $activity->duration_minutes . " minutes")); // Phew! Now build my array element. $calendarArray[] = array("title" => $activity->name, "start" => $activityStartDate, "end" => $activityEnd); } } // convert the array to json return response()->json($calendarArray); }
/** * Responds to requests to POST /activities/edit */ public function postEdit(Request $request) { $this->validate($request, ['name' => 'required|min:2', 'duration_minutes' => 'required|min:1', 'group' => 'required', 'days' => 'required', 'default_time' => 'required|digits:4']); $activity = \App\Activity::find($request->id); $activity->name = $request->name; $activity->description = $request->description; $activity->group_id = $request->group; $activity->duration_minutes = $request->duration_minutes; $activity->default_time = $request->default_time; if ($request->days) { $days = $request->days; } else { $days = []; } $activity_days = ''; foreach ($days as $day) { $activity_days = $activity_days . ' ' . $this->days_for_checkbox[$day]; } $activity->days = $activity_days; $activity->save(); // Get rid of all of the current activity->day_of_week rows in Activities_dow $old_activity_dow = []; $old_activity_dow = \App\Activities_Dow::where('activity_id', '=', $activity->id)->get(); foreach ($old_activity_dow as $odow) { $odow->delete(); } // Add back the new ones foreach ($days as $day) { $activity_dow = new \App\Activities_Dow(); $activity_dow->day_of_week = $day; $activity->activities_dow()->save($activity_dow); } \Session::flash('flash_message', 'Your activity was updated.'); return redirect('/activities/show'); }