diff --git a/src/Controller/ShiftScheduleController.php b/src/Controller/ShiftScheduleController.php index 532089d8..23ba1a76 100644 --- a/src/Controller/ShiftScheduleController.php +++ b/src/Controller/ShiftScheduleController.php @@ -90,8 +90,12 @@ class ShiftScheduleController extends Controller $hour_shifts = []; foreach ($shifts as $shift) { + // convert to DateTime then format + $shift_start = DateTime::createFromFormat('H:i', $shift['start']); + $shift_end = DateTime::createFromFormat('H:i', $shift['end']); + $hour_shifts[] = [ - $shift['start'] . ' - ' . $shift['end'] + $shift_start->format('g:i A') . ' - ' . $shift_end->format('g:i A') ]; } @@ -133,6 +137,7 @@ class ShiftScheduleController extends Controller $params = []; $params['obj'] = new ShiftSchedule(); $params['mode'] = 'create'; + $params['shift_entries'] = []; // response return $this->render('shift-schedule/form.html.twig', $params); @@ -177,6 +182,87 @@ class ShiftScheduleController extends Controller ]); } + /** + * @Menu(selected="shift_schedule_list") + */ + public function updateForm($id, EntityManagerInterface $em) + { + $this->denyAccessUnlessGranted('shift_schedule.update', null, 'No access.'); + + $params = []; + + // find shift schedule + $obj = $em->getRepository(ShiftSchedule::class)->find($id); + + // make sure this row exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + // get the shift entries + $shifts = $obj->getHourShifts(); + $hour_shifts = []; + foreach ($shifts as $shift) + { + // convert to DateTime then format + $shift_start = DateTime::createFromFormat('H:i', $shift['start']); + $shift_end = DateTime::createFromFormat('H:i', $shift['end']); + + $hour_shifts[] = [ + 'start' => $shift_start, + 'end' => $shift_end + ]; + } + + $params['obj'] = $obj; + $params['mode'] = 'update'; + $params['shift_entries'] = $hour_shifts; + + // response + return $this->render('shift-schedule/form.html.twig', $params); + } + + public function updateSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('shift_schedule.update', null, 'No access.'); + + // get object + $obj = $em->getRepository(ShiftSchedule::class)->find($id); + + // make sure this object exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + $this->setObject($obj, $em, $req); + + // validate + $errors = $validator->validate($obj); + + // initialize error list + $error_array = []; + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } + + // validated! save the entity + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + protected function setQueryFilters($datatable, QueryBuilder $query) { if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { @@ -190,7 +276,7 @@ class ShiftScheduleController extends Controller $name = $req->request->get('name'); // times - $format = 'g:i A'; + $format = 'h:i A'; $start_time = DateTime::createFromFormat($format, $req->request->get('start_time')); $end_time = DateTime::createFromFormat($format, $req->request->get('end_time')); @@ -201,9 +287,16 @@ class ShiftScheduleController extends Controller $shifts = []; for ($i = 0; $i < count($shift_start); $i++) { + // convert to DateTime so that we can reformat to 00-23 format + $s_start = DateTime::createFromFormat($format, $shift_start[$i]); + $e_start = DateTime::createFromFormat($format, $shift_end[$i]); + + $start = $s_start->format('H:00'); + $end = $e_start->format('H:00'); + $shifts[] = [ - 'start' => $shift_start[$i], - 'end' => $shift_end[$i] + 'start' => $start, + 'end' => $end ]; } diff --git a/templates/shift-schedule/form.html.twig b/templates/shift-schedule/form.html.twig index d39432c6..34629fd4 100644 --- a/templates/shift-schedule/form.html.twig +++ b/templates/shift-schedule/form.html.twig @@ -79,27 +79,30 @@