diff --git a/src/Controller/ShiftScheduleController.php b/src/Controller/ShiftScheduleController.php index b4172ce2..532089d8 100644 --- a/src/Controller/ShiftScheduleController.php +++ b/src/Controller/ShiftScheduleController.php @@ -2,4 +2,215 @@ namespace App\Controller; +use App\Entity\ShiftSchedule; +use Doctrine\ORM\Query; +use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Validator\Validator\ValidatorInterface; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; + +use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; + +use Catalyst\MenuBundle\Annotation\Menu; + +use DateTime; + +class ShiftScheduleController extends Controller +{ + /** + * @Menu(selected="shift_schedule_list") + */ + public function index() + { + $this->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) + { + $hour_shifts[] = [ + $shift['start'] . ' - ' . $shift['end'] + ]; + } + + $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'; + + // 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!' + ]); + } + + 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 = 'g: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++) + { + $shifts[] = [ + 'start' => $shift_start[$i], + 'end' => $shift_end[$i] + ]; + } + + $obj->setName($name) + ->setStartTime($start_time) + ->setEndTime($end_time) + ->setHourShifts($shifts); + } + +} diff --git a/src/Entity/ShiftSchedule.php b/src/Entity/ShiftSchedule.php index 7f73b2eb..1cdf7896 100644 --- a/src/Entity/ShiftSchedule.php +++ b/src/Entity/ShiftSchedule.php @@ -5,6 +5,8 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; +use DateTime; + /** * @ORM\Entity * @ORM\Table(name="shift_schedule") @@ -28,26 +30,19 @@ class ShiftSchedule // start time /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="time") * @Assert\NotBlank() */ protected $start_time; // end time /** - * @ORM\(Column(type="string", nullable=true) + * @ORM\Column(type="time") * @Assert\NotBlank() */ protected $end_time; - // number of hours in a shift - /** - * @ORM\(Column(type="integer") - * @Assert\NotBlank() - */ - protected $number_of_hours; - - // hours + // hour shifts /** * @ORM\Column(type="json") */ @@ -55,6 +50,8 @@ class ShiftSchedule public function __construct() { + $this->start_time = new DateTime(); + $this->end_time = new DateTime(); $this->hour_shifts = []; } @@ -79,7 +76,7 @@ class ShiftSchedule return $this->start_time; } - public function setStartTime($start_time) + public function setStartTime(DateTime $start_time) { $this->start_time = $start_time; return $this; @@ -90,30 +87,19 @@ class ShiftSchedule return $this->end_time; } - public function setEndTime($end_time) + public function setEndTime(DateTime $end_time) { $this->end_time = $end_time; return $this; } - public function getNumberOfHours() - { - return $this->number_of_hours; - } - - public function setNumberOfHours($number_of_hours) - { - $this->number_of_hours = $number_of_hours; - return $this; - } - public function addHourShift($id, $value) { $this->hour_shifts[$id] = $value; return $this; } - public function getHourShiftsiById($id) + public function getHourShiftsById($id) { // return null if we don't have it if (!isset($this->hour_shifts[$id])) @@ -126,4 +112,10 @@ class ShiftSchedule { return $this->hour_shifts; } + + public function setHourShifts(array $hour_shifts) + { + $this->hour_shifts = $hour_shifts; + return $this; + } } diff --git a/templates/shift-schedule/.form.html.twig.swo b/templates/shift-schedule/.form.html.twig.swo new file mode 100644 index 00000000..26da1a74 Binary files /dev/null and b/templates/shift-schedule/.form.html.twig.swo differ diff --git a/templates/shift-schedule/form.html.twig b/templates/shift-schedule/form.html.twig new file mode 100644 index 00000000..d39432c6 --- /dev/null +++ b/templates/shift-schedule/form.html.twig @@ -0,0 +1,251 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Shift Schedules

+
+
+
+ +
+ +
+
+
+
+
+
+ + + +

+ {% if mode == 'update' %} + Edit Shift Schedule + {{ obj.getName() }} + {% else %} + New Shift Schedule + {% endif %} +

+
+
+
+
+
+
+
+ + + +
+
+
+
+ +
+ + + + +
+ +
+
+ +
+ + + + +
+ +
+
+
+
+
+

+ Breakdown of Shift Schedule +

+
+
+ +
+
+
+
+ + + + +
+
+
+
+ + + + +
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+ + Back +
+
+
+
+ +
+ +{% endblock %} + + +{% block scripts %} + +{% endblock %} diff --git a/templates/shift-schedule/list.html.twig b/templates/shift-schedule/list.html.twig new file mode 100644 index 00000000..ecb8a05f --- /dev/null +++ b/templates/shift-schedule/list.html.twig @@ -0,0 +1,155 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Shift Schedules +

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %}