Merged some functions to help keeping things compact.
[timetracker.git] / WEB-INF / lib / ttTimesheetHelper.class.php
index 391e932..17119ed 100644 (file)
@@ -27,6 +27,7 @@
 // +----------------------------------------------------------------------+
 
 import('ttUserHelper');
+import('ttFileHelper');
 
 // Class ttTimesheetHelper is used to help with project related tasks.
 class ttTimesheetHelper {
@@ -74,9 +75,12 @@ class ttTimesheetHelper {
     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
     $end = $end_date->toString(DB_DATEFORMAT);
 
-    $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name, comment, start_date, end_date)".
+    $created_part = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
+
+    $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name, comment,".
+      " start_date, end_date, created, created_ip, created_by)".
       " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).", ".$mdb2->quote($name).
-      ", ".$mdb2->quote($comment).", ".$mdb2->quote($start).", ".$mdb2->quote($end).")";
+      ", ".$mdb2->quote($comment).", ".$mdb2->quote($start).", ".$mdb2->quote($end).$created_part.")";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
       return false;
@@ -111,10 +115,17 @@ class ttTimesheetHelper {
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
+    if ($user->isPluginEnabled('at')) {
+      $filePart = ', if(Sub1.entity_id is null, 0, 1) as has_files';
+      $fileJoin =  " left join (select distinct entity_id from tt_files".
+      " where entity_type = 'timesheet' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
+      " on (ts.id = Sub1.entity_id)";
+    }
+
     $result = array();
     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name,".
-      " ts.submit_status, ts.approve_status from tt_timesheets ts".
-      " left join tt_clients c on (c.id = ts.client_id)".
+      " ts.submit_status, ts.approve_status $filePart from tt_timesheets ts".
+      " left join tt_clients c on (c.id = ts.client_id) $fileJoin".
       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
       " order by ts.name";
     $res = $mdb2->query($sql);
@@ -137,10 +148,17 @@ class ttTimesheetHelper {
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
+    if ($user->isPluginEnabled('at')) {
+      $filePart = ', if(Sub1.entity_id is null, 0, 1) as has_files';
+      $fileJoin =  " left join (select distinct entity_id from tt_files".
+      " where entity_type = 'timesheet' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
+      " on (ts.id = Sub1.entity_id)";
+    }
+
     $result = array();
     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name,".
-      " ts.submit_status, ts.approve_status from tt_timesheets ts".
-      " left join tt_clients c on (c.id = ts.client_id)".
+      " ts.submit_status, ts.approve_status $filePart from tt_timesheets ts".
+      " left join tt_clients c on (c.id = ts.client_id) $fileJoin".
       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
       " order by ts.name";
     $res = $mdb2->query($sql);
@@ -181,6 +199,15 @@ class ttTimesheetHelper {
     global $user;
     $mdb2 = getConnection();
 
+    // Delete associated files.
+    if ($user->isPluginEnabled('at')) {
+      import('ttFileHelper');
+      global $err;
+      $fileHelper = new ttFileHelper($err);
+      if (!$fileHelper->deleteEntityFiles($timesheet_id, 'timesheet'))
+        return false;
+    }
+
     $user_id = $user->getUser();
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
@@ -191,8 +218,10 @@ class ttTimesheetHelper {
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error')) return false;
 
+    $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
+
     // Delete timesheet.
-    $sql = "update tt_timesheets set status = null".
+    $sql = "update tt_timesheets set status = null".$modified_part.
       " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
     $affected = $mdb2->exec($sql);
     return (!is_a($affected, 'PEAR_Error'));
@@ -212,7 +241,10 @@ class ttTimesheetHelper {
     $comment = $fields['comment'];
     $status = $fields['status']; // Timesheet status.
 
-    $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", comment = ".$mdb2->quote($comment).
+    $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
+
+    $sql = "update tt_timesheets set name = ".$mdb2->quote($name).
+      ", comment = ".$mdb2->quote($comment).$modified_part.
       ", status = ".$mdb2->quote($status).
       " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
     $affected = $mdb2->exec($sql);
@@ -287,9 +319,8 @@ class ttTimesheetHelper {
     return false;
   }
 
-  // submitTimesheet marks a timesheet as submitted and also sends an email
-  // to a selected approver.
-  static function submitTimesheet($fields) {
+  // markSubmitted marks a timesheet as submitted.
+  static function markSubmitted($fields) {
     global $user;
     $mdb2 = getConnection();
 
@@ -297,11 +328,10 @@ class ttTimesheetHelper {
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
-    // First, mark timesheet as submitted.
-    // Even if mail part below does not work, this will get us a functioning workflow
-    // without email notification.
+    $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
+
     $timesheet_id = $fields['timesheet_id'];
-    $sql = "update tt_timesheets set submit_status = 1".
+    $sql = "update tt_timesheets set submit_status = 1".$modified_part.
       " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
     $affected = $mdb2->exec($sql);
     return (!is_a($affected, 'PEAR_Error'));
@@ -325,6 +355,40 @@ class ttTimesheetHelper {
     return ttTimesheetHelper::sendEmail($fields);
   }
 
+  // sendApprovedEmail sends a notification to user about a timesheet approval.
+  static function sendApprovedEmail($fields) {
+    global $i18n;
+    global $user;
+
+    // Obtain user email.
+    $user_details = $user->getUserDetails($fields['user_id']);
+    $email = $user_details['email'];
+    if (!$email) return true; // No email to send to, nothing to do.
+
+    $fields['to'] = $email;
+    $fields['subject'] = $i18n->get('form.timesheet_view.approve_subject');
+    $fields['body'] = sprintf($i18n->get('form.timesheet_view.approve_body'), htmlspecialchars($fields['name']), htmlspecialchars($fields['comment']));
+
+    return ttTimesheetHelper::sendEmail($fields);
+  }
+
+  // sendDisapprovedEmail sends a notification to user about a timesheet disapproval.
+  static function sendDisapprovedEmail($fields) {
+    global $i18n;
+    global $user;
+
+    // Obtain user email.
+    $user_details = $user->getUserDetails($fields['user_id']);
+    $email = $user_details['email'];
+    if (!$email) return true; // No email to send to, nothing to do.
+
+    $fields['to'] = $email;
+    $fields['subject'] = $i18n->get('form.timesheet_view.disapprove_subject');
+    $fields['body'] = sprintf($i18n->get('form.timesheet_view.disapprove_body'), htmlspecialchars($fields['name']), htmlspecialchars($fields['comment']));
+
+    return ttTimesheetHelper::sendEmail($fields);
+  }
+
   // sendEmail is a generic finction that sends a timesheet related email.
   // TODO: perhaps make it even more generic for the entire application.
   static function sendEmail($fields, $html = true) {
@@ -348,8 +412,8 @@ class ttTimesheetHelper {
     return true;
   }
 
-  // approveTimesheet marks a timesheet as approved and sends an email to submitter.
-  static function approveTimesheet($fields) {
+  // markApproved marks a timesheet as approved.
+  static function markApproved($fields) {
     global $user;
     $mdb2 = getConnection();
 
@@ -357,23 +421,19 @@ class ttTimesheetHelper {
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
-    // First, mark timesheet as approved.
-    // Even if mail part below does not work, this will get us a functioning workflow
-    // without email notification.
     $timesheet_id = $fields['timesheet_id'];
     $comment = $fields['comment'];
 
-    $sql = "update tt_timesheets set approve_status = 1, approve_comment = ".$mdb2->quote($comment).
+    $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
+
+    $sql = "update tt_timesheets set approve_status = 1, approve_comment = ".$mdb2->quote($comment).$modified_part.
       " where id = $timesheet_id and submit_status = 1 and user_id = $user_id and group_id = $group_id and org_id = $org_id";
     $affected = $mdb2->exec($sql);
-    if (is_a($affected, 'PEAR_Error')) return false;
-
-    // TODO: send email to submitter here...
-    return true;
+    return (!is_a($affected, 'PEAR_Error'));
   }
 
-  // disapproveTimesheet marks a timesheet as approved and sends an email to submitter.
-  static function disapproveTimesheet($fields) {
+  // markDisapproved marks a timesheet as not approved.
+  static function markDisapproved($fields) {
     global $user;
     $mdb2 = getConnection();
 
@@ -381,19 +441,15 @@ class ttTimesheetHelper {
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
-    // First, mark timesheet as disapproved.
-    // Even if mail part below does not work, this will get us a functioning workflow
-    // without email notification.
     $timesheet_id = $fields['timesheet_id'];
     $comment = $fields['comment'];
 
-    $sql = "update tt_timesheets set approve_status = 0, approve_comment = ".$mdb2->quote($comment).
+    $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
+
+    $sql = "update tt_timesheets set approve_status = 0, approve_comment = ".$mdb2->quote($comment).$modified_part.
       " where id = $timesheet_id and submit_status = 1 and user_id = $user_id and group_id = $group_id and org_id = $org_id";
     $affected = $mdb2->exec($sql);
-    if (is_a($affected, 'PEAR_Error')) return false;
-
-    // TODO: send email to submitter here...
-    return true;
+    return (!is_a($affected, 'PEAR_Error'));
   }
 
   // The timesheetItemsExist determines whether tt_log records exist in the specified period