Add update of shift schedule. #534

This commit is contained in:
Korina Cordero 2021-01-28 10:39:27 +00:00
parent 316ab545e5
commit ba5d7d364d
2 changed files with 119 additions and 23 deletions

View file

@ -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
];
}

View file

@ -79,27 +79,30 @@
<div class="form-group m-form__group row">
<button type="button" class="btn btn-primary" id="btn-add-shift-entry">Add Shift Entry</button>
</div>
<div class="form-group m-form__group row">
<div class="col-lg-3">
<div class="input-group timepicker">
<input id="timepicker_start_entry" type="text" name="shift_start_time[]" class="form-control m-input tp-start-shift-entry" readonly placeholder="Select time" type="text" value="{{ obj.getStartTime.format('g:i A') }}" />
<span class="input-group-addon">
<i class="la la-clock-o"></i>
</span>
<!-- loop through the existing shift entries -->
{% for shift_entry in shift_entries %}
<div class="form-group m-form__group row">
<div class="col-lg-3">
<div class="input-group timepicker">
<input id="timepicker_start_entry" type="text" name="shift_start_time[]" class="form-control m-input tp-start-shift-entry" readonly placeholder="Select time" type="text" value="{{ shift_entry.start.format('g:i A') }}" />
<span class="input-group-addon">
<i class="la la-clock-o"></i>
</span>
</div>
</div>
<div class="col-lg-3">
<div class="input-group timepicker">
<input id="timepicker_end_entry" type="text" name="shift_end_time[]" class="form-control m-input tp-end-shift-entry" readonly placeholder="Select time" type="text" value="{{ shift_entry.end.format('g:i A') }}" />
<span class="input-group-addon">
<i class="la la-clock-o"></i>
</span>
</div>
</div>
<div class="col-lg-1">
<button class="btn btn-danger btn-shift-entry-remove">X</button>
</div>
</div>
<div class="col-lg-3">
<div class="input-group timepicker">
<input id="timepicker_end_entry" type="text" name="shift_end_time[]" class="form-control m-input tp-end-shift-entry" readonly placeholder="Select time" type="text" value="{{ obj.getEndTime.format('g:i A') }}" />
<span class="input-group-addon">
<i class="la la-clock-o"></i>
</span>
</div>
</div>
<div class="col-lg-1">
<button class="btn btn-danger btn-shift-entry-remove">X</button>
</div>
</div>
{% endfor %}
</div>
</div>
</div>