From bac0563d910ee1c055db5e2ad9ec4c6d08846f7f Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Tue, 5 Mar 2019 16:29:17 +0000 Subject: [PATCH] Added timesheet approval email notification. --- WEB-INF/lib/ttTimesheetHelper.class.php | 29 +++++++++++++++++-------- WEB-INF/resources/en.lang.php | 5 ++--- WEB-INF/templates/footer.tpl | 2 +- timesheet_view.php | 14 ++++++++---- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/WEB-INF/lib/ttTimesheetHelper.class.php b/WEB-INF/lib/ttTimesheetHelper.class.php index 9d4919dc..ca837fe7 100644 --- a/WEB-INF/lib/ttTimesheetHelper.class.php +++ b/WEB-INF/lib/ttTimesheetHelper.class.php @@ -321,6 +321,23 @@ 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'])); + + 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) { @@ -344,8 +361,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(); @@ -353,19 +370,13 @@ 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). " 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. diff --git a/WEB-INF/resources/en.lang.php b/WEB-INF/resources/en.lang.php index 9e2a364c..fdf72ae6 100644 --- a/WEB-INF/resources/en.lang.php +++ b/WEB-INF/resources/en.lang.php @@ -557,8 +557,7 @@ $i18n_key_words = array( 'form.timesheet_view.submit_subject' => 'Timesheet approval request', 'form.timesheet_view.submit_body' => "A new timesheet requires approval.

User: %s.", 'form.timesheet_view.approve_subject' => 'Timesheet approved', -'form.timesheet_view.approve_body' => "Dear User,\n\nYour timesheet %s was approved.", +'form.timesheet_view.approve_body' => "Your timesheet %s was approved.", 'form.timesheet_view.disapprove_subject' => 'Timesheet not approved', -'form.timesheet_view.disapprove_subject' => "Dear User,\n\nYour timesheet %s was not approved.\n\n%s", -'form.timesheet_view.success_message' => 'Notification sent by email.', +'form.timesheet_view.disapprove_subject' => "Your timesheet %s was not approved.", ); diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 06ab6141..1a85ccfc 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.52.4824 | Copyright © Anuko | +  Anuko Time Tracker 1.18.52.4825 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/timesheet_view.php b/timesheet_view.php index 3dc80253..f6e8aac7 100644 --- a/timesheet_view.php +++ b/timesheet_view.php @@ -92,7 +92,7 @@ if ($showApprove) { if ($request->isPost()) { if ($request->getParameter('btn_submit')) { $fields = array('timesheet_id' => $timesheet['id'], - 'approver_id' => $approver_id); // TODO: obtain (and check) approver id above during access checks. + 'approver_id' => $approver_id); if (!ttTimesheetHelper::markSubmitted($fields)) $err->add($i18n->get('error.db')); if ($err->no() && !ttTimesheetHelper::sendSubmitEmail($fields)) { @@ -107,13 +107,19 @@ if ($request->isPost()) { if ($request->getParameter('btn_approve')) { $fields = array('timesheet_id' => $timesheet['id'], + 'name' => $timesheet['name'], + 'user_id' => $timesheet['user_id'], 'comment' => $cl_comment); - if (ttTimesheetHelper::approveTimesheet($fields)) { + if (!ttTimesheetHelper::markApproved($fields)) + $err->add($i18n->get('error.db')); + if ($err->no() && !ttTimesheetHelper::sendApprovedEmail($fields)) { + $err->add($i18n->get('error.mail_send')); + } + if ($err->no()) { // Redirect to self. header('Location: timesheet_view.php?id='.$timesheet['id']); exit(); - } else - $err->add($i18n->get('error.db')); + } } if ($request->getParameter('btn_disapprove')) { -- 2.20.1