+ $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;
+
+ // Handle durations and comments in separate blocks of code.
+ if (!$user->isPluginEnabled('wvns') || (0 == $rowNumber % 2)) {
+ // Handle durations row here.
+
+ // Obtain 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 (false === ttTimeHelper::postedDurationToMinutes($postedDuration)) {
+ $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
+ $result = false; break; // Break out. Stop any further processing.
+ } else {
+ $minutes = ttTimeHelper::postedDurationToMinutes($postedDuration);
+ $postedDuration = ttTimeHelper::minutesToDuration($minutes);
+ }
+ }
+ // Do not process if value has not changed.
+ if ($postedDuration == $existingDuration)
+ continue;
+ // Posted value is different.
+ if ($existingDuration == null) {
+ // Skip inserting 0 duration values.
+ if (0 == ttTimeHelper::toMinutes($postedDuration))
+ continue;
+ // Insert a new record.
+ $fields = array();
+ $fields['row_id'] = $dataArray[$rowNumber]['row_id'];
+ if (!$fields['row_id']) {
+ // Special handling for row 0, a new entry. Need to construct new row_id.
+ $record = array();
+ $record['client_id'] = $cl_client;
+ $record['billable'] = $cl_billable ? '1' : '0';
+ $record['project_id'] = $cl_project;
+ $record['task_id'] = $cl_task;
+ $record['cf_1_value'] = $cl_cf_1;
+ $fields['row_id'] = ttWeekViewHelper::makeRowIdentifier($record).'_0';
+ // Note: no need to check for a possible conflict with an already existing row
+ // because we are doing an insert that does not affect already existing data.
+
+ if ($user->isPluginEnabled('wvn')) {
+ $fields['note'] = $request->getParameter('note');
+ }
+ }
+ $fields['day_header'] = $dayHeader;
+ $fields['start_date'] = $startDate->toString(DB_DATEFORMAT); // To be able to determine date for the entry using $dayHeader.
+ $fields['duration'] = $postedDuration;
+ $fields['browser_today'] = $request->getParameter('browser_today', null);
+ if ($user->isPluginEnabled('wvns')) {
+ // Take note value from the control below duration.
+ $noteRowNumber = $rowNumber + 1;
+ $note_control_id = $noteRowNumber.'_'.$dayHeader;
+ $fields['note'] = $request->getParameter($note_control_id);
+ }
+ $result = ttWeekViewHelper::insertDurationFromWeekView($fields, $custom_fields, $err);
+ } elseif ($postedDuration == null || 0 == ttTimeHelper::toMinutes($postedDuration)) {
+ // Delete an already existing record here.
+ $result = ttTimeHelper::delete($dataArray[$rowNumber][$dayHeader]['tt_log_id'], $user->getUser());
+ } else {
+ $fields = array();
+ $fields['tt_log_id'] = $dataArray[$rowNumber][$dayHeader]['tt_log_id'];
+ $fields['duration'] = $postedDuration;
+ $result = ttWeekViewHelper::modifyDurationFromWeekView($fields, $err);
+ }
+ if (!$result) break; // Break out of the loop in case of first error.
+
+ } else if ($user->isPluginEnabled('wvns')) {
+ // Handle commments row here.
+
+ // Obtain existing and posted comments.
+ $postedComment = $request->getParameter($control_id);
+ $existingComment = $dataArray[$rowNumber][$dayHeader]['note'];
+ // If posted value is not null, check it.
+ if ($postedComment && !ttValidString($postedComment, true)) {
+ $err->add($i18n->get('error.field'), $i18n->get('label.note'));
+ $result = false; break; // Break out. Stop any further processing.
+ }
+ // Do not process if value has not changed.
+ if ($postedComment == $existingComment)
+ continue;
+
+ // Posted value is different.
+ // TODO: handle new entries separately in the durations block above.
+
+ // Here, only update the comment on an already existing record.
+ $fields = array();
+ $fields['tt_log_id'] = $dataArray[$rowNumber][$dayHeader]['tt_log_id'];
+ if ($fields['tt_log_id']) {
+ $fields['comment'] = $postedComment;
+ $result = ttWeekViewHelper::modifyCommentFromWeekView($fields);
+ }
+ 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++;