denyAccessUnlessGranted('shift_schedule.list', null, 'No access.'); return $this->render('shift-schedule/list.html.twig'); } public function rows(Request $req) { $this->denyAccessUnlessGranted('shift_schedule.list', null, 'No access.'); // get query builder $qb = $this->getDoctrine() ->getRepository(ShiftSchedule::class) ->createQueryBuilder('q'); // get datatable params $datatable = $req->request->get('datatable'); // count total records $tquery = $qb->select('COUNT(q)'); $this->setQueryFilters($datatable, $tquery); $total = $tquery->getQuery() ->getSingleScalarResult(); // get current page number $page = $datatable['pagination']['page'] ?? 1; $perpage = $datatable['pagination']['perpage']; $offset = ($page - 1) * $perpage; // add metadata $meta = [ 'page' => $page, 'perpage' => $perpage, 'pages' => ceil($total / $perpage), 'total' => $total, 'sort' => 'asc', 'field' => 'id' ]; // build query $query = $qb->select('q'); $this->setQueryFilters($datatable, $query); // check if sorting is present, otherwise use default if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) { $order = $datatable['sort']['sort'] ?? 'asc'; $query->orderBy('q.' . $datatable['sort']['field'], $order); } else { $query->orderBy('q.id', 'asc'); } // get rows for this page $obj_rows = $query->setFirstResult($offset) ->setMaxResults($perpage) ->getQuery() ->getResult(); // process rows $rows = []; foreach ($obj_rows as $orow) { // add row data $shifts = $orow->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[] = [ $shift_start->format('g:i A') . ' - ' . $shift_end->format('g:i A') ]; } $row['id'] = $orow->getID(); $row['name'] = $orow->getName(); $row['start_time'] = $orow->getStartTime()->format('g:i A'); $row['end_time'] = $orow->getEndtime()->format('g:i A'); $row['hour_shifts'] = $hour_shifts; // add row metadata $row['meta'] = [ 'update_url' => '', 'delete_url' => '' ]; // add crud urls if ($this->isGranted('shift_schedule.update')) $row['meta']['update_url'] = $this->generateUrl('shift_schedule_update', ['id' => $row['id']]); if ($this->isGranted('shift_schedule.delete')) $row['meta']['delete_url'] = $this->generateUrl('shift_schedule_delete', ['id' => $row['id']]); $rows[] = $row; } // response return $this->json([ 'meta' => $meta, 'data' => $rows ]); } /** * @Menu(selected="shift_schedule_list") */ public function addForm() { $this->denyAccessUnlessGranted('shift_schedule.add', null, 'No access.'); $params = []; $params['obj'] = new ShiftSchedule(); $params['mode'] = 'create'; $params['shift_entries'] = []; // response return $this->render('shift-schedule/form.html.twig', $params); } public function addSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator) { $this->denyAccessUnlessGranted('shift_schedule.add', null, 'No access.'); $obj = new ShiftSchedule(); // set and save values $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->persist($obj); $em->flush(); // return successful response return $this->json([ 'success' => 'Changes have been saved!' ]); } /** * @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'])) { $query->where('q.name LIKE :filter') ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); } } protected function setObject(ShiftSchedule $obj, EntityManagerInterface $em, Request $req) { $name = $req->request->get('name'); // times $format = 'h:i A'; $start_time = DateTime::createFromFormat($format, $req->request->get('start_time')); $end_time = DateTime::createFromFormat($format, $req->request->get('end_time')); // get the shift entries $shift_start = $req->request->get('shift_start_time'); $shift_end = $req->request->get('shift_end_time'); $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' => $start, 'end' => $end ]; } $obj->setName($name) ->setStartTime($start_time) ->setEndTime($end_time) ->setHourShifts($shifts); } }