+// TODO: refactor the above.
+*/
+ // Obtain values. Perhaps, it's best to iterate throigh posted parameters one by one,
+ // see if anything changed, and apply one change at a time until we see an error.
+ // TODO: check for locked days just in case.
+ $result = true;
+ $rowNumber = 0;
+ // Iterate through existing rows.
+ foreach ($dataArray as $row) {
+ // Iterate through days.
+ foreach ($dayHeaders as $key => $dayHeader) {
+ // Do not process locked days.
+ if ($lockedDays[$key]) continue;
+ // Make control id for the cell.
+ $control_id = $rowNumber.'_'.$dayHeader;
+ // Optain existing and posted durations.
+ $postedDuration = $request->getParameter($control_id);
+ $existingDuration = $dataArray[$rowNumber][$dayHeader]['duration'];
+ // If posted value is not null, check and normalize it.
+ if ($postedDuration) {
+ if (ttTimeHelper::isValidDuration($postedDuration)) {
+ $postedDuration = ttTimeHelper::normalizeDuration($postedDuration, false); // No leading zero.
+ } else {
+ $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
+ $result = false; break; // Break out. Stop any further processing.
+ }
+ }
+ // Do not process if value has not changed.
+ if ($postedDuration == $existingDuration)
+ continue;
+ // Posted value is different.
+ if ($existingDuration == null) {
+ // Insert a new record here.
+ $fields = array();
+ $result = ttTimeHelper::insertDurationFromWeekView($fields, $err);
+ //$dataArray[$rowNumber]['row_id'],
+ //$dayHeader,
+ //$postedDuration,
+ //$startDate->toString(DB_DATEFORMAT));
+ } elseif ($postedDuration == null || 0 == ttTimeHelper::toMinutes($postedDuration)) {
+ // Delete an already existing record here.
+ $result = ttTimeHelper::delete($dataArray[$rowNumber][$dayHeader]['tt_log_id'], $user->getActiveUser());
+ } else {
+ $fields = array();
+ $result = ttTimeHelper::modifyDurationFromWeekView($fields, $err);
+ //$result = ttTimeHelper::modifyDurationFromWeekView($dataArray[$rowNumber][$dayHeader]['tt_log_id'], $postedDuration, $user->getActiveUser());
+ }
+ if (!$result) break; // Break out of the loop in case of first error.
+ }
+ if (!$result) break; // Break out of the loop in case of first error.
+ $rowNumber++;
+ }
+ if ($result) {
+ header('Location: week.php'); // Normal exit.
+ exit();
+ }
+ $err->add($i18n->getKey('error.db'));
+ /*
+ //
+ //
+ //