Odlično. Izvini što smaram... Problem je što nije normalizovano, neko pametan je odlučio da napravi polja sa imenima dana (a polja u HTML formi sadrže iste nazive).
schedules
Kôd:
+----+----------+----------+----------+--------+--------+---------+-----------+----------+--------+----------+
| id | staff_id | start | end | sunday | monday | tuesday | wednesday | thursday | friday | saturday |
+----+----------+----------+----------+--------+--------+---------+-----------+----------+--------+----------+
| 1 | 66 | 21:00:00 | 03:00:00 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
+----+----------+----------+----------+--------+--------+---------+-----------+----------+--------+----------+
shifts
Kôd:
+----+-------------+------+----------+----------+
| id | schedule_id | day | start | end |
+----+-------------+------+----------+----------+
| 3 | 1 | 6 | 21:00:00 | 00:00:00 |
| 4 | 1 | 0 | 00:00:00 | 03:00:00 |
+----+-------------+------+----------+----------+
day označava dan u sedmici (0(sun)..6(sat)), ali sad vidim da čitava struktura f***ed up, i da pored ovoga ima i fail cases. Ovo dole sam koristio prije SQL...
PHP kôd:
private function _shifts($schedule_id)
{
if (!$schedule_id)
die('No schedule selected');
$shift_span = WA_DT::shift_span($_POST['start'], $_POST['end']);
$shift_span_count = count($shift_span);
$_days = array(
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday'
);
# loop through weekdays
foreach ($_days as $k => $v)
{
# if day is checked
if ($_POST[$v] != false)
{
$today = $k;
$tomorrow = $today + 1;
if ($tomorrow > 6)
$tomorrow = --$tomorrow % 6;
# if shift is one day or two days
if ($shift_span_count == 1)
{
$shifts = new Shifts;
$shifts->fromArray(array(
'schedule_id' => $schedule_id,
'day' => $today,
'start' => $_POST['start'],
'end' => $_POST['end']
));
$shifts->save();
}
else
{
$shifts = new Shifts;
$shifts->fromArray(array(
'schedule_id' => $schedule_id,
'day' => $today,
'start' => sprintf('%02s:00:00', 24 - $shift_span[0]),
'end' => '00:00:00'
));
$shifts->save();
$shifts = new Shifts;
$shifts->fromArray(array(
'schedule_id' => $schedule_id,
'day' => $tomorrow,
'start' => '00:00:00',
'end' => sprintf('%02s:00:00', $shift_span[1])
));
$shifts->save();
}
}
}
}
Usput, mrzim Doctrine.