public function cloneEvent(Request $request, Event $event)
 {
     $this->authorize('create-event');
     $this->validate($request, ['start_date' => 'required|date_format:Y-m-d']);
     // Set up event information
     $startDate = new Carbon($event->start_date);
     $endDate = new Carbon($event->end_date);
     $newStartDate = new Carbon($request->input('start_date'));
     $departments = $event->departments;
     // Find the difference of the start dates
     $difference = $startDate->diffInSeconds($newStartDate);
     // Create new event from old event data
     $newEvent = Event::create(['name' => $event->name, 'description' => $event->description, 'start_date' => $startDate->addSeconds($difference)->format('Y-m-d'), 'end_date' => $endDate->addSeconds($difference)->format('Y-m-d')]);
     // Add the image manually because it's not automatically fillable
     if ($event->image) {
         $newEvent->image = $event->image;
         $newEvent->save();
     }
     // Loop through event departments
     foreach ($departments as $department) {
         // Create new department
         $newDepartment = new Department();
         $newDepartment->event_id = $newEvent->id;
         $newDepartment->save();
         // Because the event_id isn't fillable, we have to define it first and then update
         $newDepartment->update(['name' => $department->name, 'description' => $department->description, 'roles' => $department->roles]);
         // Loop through shifts
         $shifts = $department->shifts;
         foreach ($shifts as $shift) {
             // Adjust shift dates
             $shift->start_date = new Carbon($shift->start_date);
             $shift->end_date = new Carbon($shift->end_date);
             $shift->start_date = $shift->start_date->addSeconds($difference)->format('Y-m-d');
             $shift->end_date = $shift->end_date->addSeconds($difference)->format('Y-m-d');
             // Update the department ID
             $shift->department_id = $newDepartment->id;
             $newShift = Shift::create(['department_id' => $newDepartment->id, 'name' => $shift->name, 'start_date' => $shift->start_date, 'end_date' => $shift->end_date, 'start_time' => $shift->start_time, 'end_time' => $shift->end_time, 'duration' => $shift->duration, 'roles' => $shift->roles]);
             Slot::generate($newShift);
         }
     }
     $request->session()->flash('success', 'Event has been cloned.');
     return redirect('/event/' . $newEvent->id);
 }
 public function edit(ShiftRequest $request, Shift $shift)
 {
     $this->authorize('edit-shift');
     $input = $request->all();
     $department = Department::find($input['department_id']);
     // Convert roles into JSON
     $input['roles'] = json_encode($input['roles']);
     // Check if the current roles match the department roles
     if ($input['roles'] == $department->roles) {
         // Unset the roles, use department as default instead
         unset($input['roles']);
     }
     // Make sure dates and times are set properly and formatted
     $input = Shift::setDates($department, $input);
     $input = Shift::setTimes($input);
     $shift->formatTimes();
     // Check if the start time, end time, or duration are changing
     $regenerateSlots = false;
     if ($shift->start_date != $input['start_date'] || $shift->end_date != $input['end_date'] || $shift->start_time != $input['start_time'] || $shift->end_time != $input['end_time'] || $shift->duration != $input['duration']) {
         $regenerateSlots = true;
     }
     $shift->update($input);
     // Regenerate slots after the updated shift information is saved
     if ($regenerateSlots) {
         Slot::generate($shift);
     }
     event(new EventChanged($shift->event, ['type' => 'shift', 'status' => 'edited']));
     $request->session()->flash('success', 'Shift has been updated.');
     return redirect('/event/' . $shift->event->id);
 }