Refactoring. Moved week_menu config option to plugin config.
[timetracker.git] / WEB-INF / lib / ttWeekViewHelper.class.php
index 08d219f..3486481 100644 (file)
@@ -109,7 +109,7 @@ class ttWeekViewHelper {
   // "cl:546,bl:0,pr:23456,ts:27464,cf_1:7623"
   // The above means client 546, not billable, project 23456, task 27464, custom field option id 7623.
   //
-  // Daily comments are implemented as alternate rows following week durations.
+  // Daily comments are implemented as alternate rows following week durations (when enabled).
   // For example: row_0 - new entry durations, row_1 - new entry daily comments,
   //              row_2 - existing entry durations, row_3 - existing entry comments, etc.
   //
@@ -167,6 +167,7 @@ class ttWeekViewHelper {
     global $i18n;
 
     $dataArray = array();
+    $includeNotes = $user->isOptionEnabled('week_notes');
 
     // Construct the first row for a brand new entry.
     $dataArray[] = array('row_id' => null,'label' => $i18n->get('form.week.new_entry').':'); // Insert row.
@@ -175,7 +176,7 @@ class ttWeekViewHelper {
       $control_id = '0_'. $dayHeaders[$i];
       $dataArray[0][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'duration' => null);
     }
-    if ($user->isPluginEnabled('wvns')) {
+    if ($includeNotes) {
       // Construct the second row for daily comments for a brand new entry.
       $dataArray[] = array('row_id' => null,'label' => $i18n->get('label.notes').':'); // Insert row.
       // Insert empty cells with proper control ids.
@@ -209,7 +210,7 @@ class ttWeekViewHelper {
           $dataArray[$pos][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'duration' => null);
         }
         // Insert row for comments.
-        if ($user->isPluginEnabled('wvns')) {
+        if ($includeNotes) {
           $dataArray[] = array('row_id' => $row_id.'_notes','label' => $i18n->get('label.notes').':');
           $pos++;
           // Insert empty cells with proper control ids.
@@ -223,7 +224,7 @@ class ttWeekViewHelper {
       // Insert actual cell data from $record (one cell only).
       $dataArray[$pos][$day_header] = array('control_id' => $pos.'_'. $day_header, 'tt_log_id' => $record['id'],'duration' => $record['duration']);
       // Insert existing comment from $record into the comment cell.
-      if ($user->isPluginEnabled('wvns')) {
+      if ($includeNotes) {
         $pos++;
         $dataArray[$pos][$day_header] = array('control_id' => $pos.'_'. $day_header, 'tt_log_id' => $record['id'],'note' => $record['comment']);
       }
@@ -273,12 +274,15 @@ class ttWeekViewHelper {
       $control_id = '0_'. $dayHeaders[$i];
       $dataArray[0][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'duration' => null);
     }
-    // Construct the second row for daily comments for a brand new entry.
-    $dataArray[] = array('row_id' => null,'label' => $i18n->get('label.notes').':'); // Insert row.
-    // Insert empty cells with proper control ids.
-    for ($i = 0; $i < 7; $i++) {
-      $control_id = '1_'. $dayHeaders[$i];
-      $dataArray[1][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'note' => null);
+    $includeNotes = $user->isOptionEnabled('week_notes');
+    if ($includeNotes) {
+      // Construct the second row for daily comments for a brand new entry.
+      $dataArray[] = array('row_id' => null,'label' => $i18n->get('label.notes').':'); // Insert row.
+      // Insert empty cells with proper control ids.
+      for ($i = 0; $i < 7; $i++) {
+        $control_id = '1_'. $dayHeaders[$i];
+        $dataArray[1][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'note' => null);
+      }
     }
 
     // Iterate through records and build an "empty" $dataArray.
@@ -297,14 +301,16 @@ class ttWeekViewHelper {
           $dataArray[$pos][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'duration' => null);
         }
         // Insert row for comments.
-        $dataArray[] = array('row_id' => $row_id.'_notes','label' => $i18n->get('label.notes').':');
-        $pos++;
-        // Insert empty cells with proper control ids.
-        for ($i = 0; $i < 7; $i++) {
-          $control_id = $pos.'_'. $dayHeaders[$i];
-          $dataArray[$pos][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'note' => null);
+        if ($includeNotes) {
+          $dataArray[] = array('row_id' => $row_id.'_notes','label' => $i18n->get('label.notes').':');
+          $pos++;
+          // Insert empty cells with proper control ids.
+          for ($i = 0; $i < 7; $i++) {
+            $control_id = $pos.'_'. $dayHeaders[$i];
+            $dataArray[$pos][$dayHeaders[$i]] = array('control_id' => $control_id, 'tt_log_id' => null,'note' => null);
+          }
+          $pos--;
         }
-        $pos--;
       }
     }
 
@@ -350,7 +356,7 @@ class ttWeekViewHelper {
     }
     // Convert minutes to hh:mm for display.
     foreach($dayHeaders as $dayHeader) {
-      $dayTotals[$dayHeader] = ttTimeHelper::toAbsDuration($dayTotals[$dayHeader]);
+      $dayTotals[$dayHeader] = ttTimeHelper::minutesToDuration($dayTotals[$dayHeader]);
     }
     return $dayTotals;
   }
@@ -575,8 +581,14 @@ class ttWeekViewHelper {
     }
 
     // We do have start time.
-    // Quick test if new duration is less then already existing.
     $newMinutes = ttTimeHelper::toMinutes($new_duration);
+    if ($newMinutes < 0) {
+      // Negative durations are not supported when start time is defined.
+      $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
+      return false;
+    }
+
+    // Quick test if new duration is less than already existing.
     $oldMinutes = ttTimeHelper::toMinutes($oldDuration);
     if ($newMinutes < $oldMinutes)
       return true; // Safe to modify.