From 566ab32f690c6e281a85c79345af95dca9a97898 Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Tue, 5 Mar 2019 15:43:17 +0000 Subject: [PATCH] Implemented sending email to timesheet approver. --- WEB-INF/lib/ttTimesheetHelper.class.php | 65 +++++++++++++++++++++++-- WEB-INF/lib/ttUser.class.php | 5 ++ WEB-INF/resources/en.lang.php | 10 ++++ WEB-INF/templates/footer.tpl | 2 +- timesheet_view.php | 13 +++-- 5 files changed, 87 insertions(+), 8 deletions(-) diff --git a/WEB-INF/lib/ttTimesheetHelper.class.php b/WEB-INF/lib/ttTimesheetHelper.class.php index c2841c88..391e9327 100644 --- a/WEB-INF/lib/ttTimesheetHelper.class.php +++ b/WEB-INF/lib/ttTimesheetHelper.class.php @@ -264,6 +264,29 @@ class ttTimesheetHelper { return $approvers; } + // getApprover obtains approver properties such as name and email. + static function getApprover($user_id) { + global $user; + $mdb2 = getConnection(); + + $group_id = $user->getGroup(); + $org_id = $user->org_id; + + $rank = ttUserHelper::getUserRank($user->getUser()); + $sql = "select u.name, u.email". + " from tt_users u". + " left join tt_roles r on (r.id = u.role_id)". + " where u.id = $user_id and u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id". + " and (r.rank > $rank and r.rights like '%approve_timesheets%')"; + $res = $mdb2->query($sql); + if (!is_a($res, 'PEAR_Error')) { + if ($val = $res->fetchRow()) { + return $val; + } + } + return false; + } + // submitTimesheet marks a timesheet as submitted and also sends an email // to a selected approver. static function submitTimesheet($fields) { @@ -281,10 +304,46 @@ class ttTimesheetHelper { $sql = "update tt_timesheets set submit_status = 1". " where id = $timesheet_id 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; + return (!is_a($affected, 'PEAR_Error')); + } + + // sendSubmitEmail sends a notification to an approver about a timesheet submit. + static function sendSubmitEmail($fields) { + global $i18n; + global $user; + + // Send email to a selected approver. + if (!$fields['approver_id']) return true; // No approver, nothing to do. - // TODO: send email to approver here... - // $approver_id = $fields['approver_id']; + $approver = ttTimesheetHelper::getApprover($fields['approver_id']); + if (!$approver) return false; // Invalid approver id. + + $fields['to'] = $approver['email']; + $fields['subject'] = $i18n->get('form.timesheet_view.submit_subject'); + $fields['body'] = sprintf($i18n->get('form.timesheet_view.submit_body'), $user->getName()); + + 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) { + global $i18n; + global $user; + + // Send email. + import('mail.Mailer'); + $mailer = new Mailer(); + $mailer->setCharSet(CHARSET); + if ($html) + $mailer->setContentType('text/html'); + $mailer->setSender(SENDER); + $mailer->setReceiver($fields['to']); + if (!empty($user->bcc_email)) + $mailer->setReceiverBCC($user->bcc_email); + $mailer->setMailMode(MAIL_MODE); + if (!$mailer->send($fields['subject'], $fields['body'])) + return false; return true; } diff --git a/WEB-INF/lib/ttUser.class.php b/WEB-INF/lib/ttUser.class.php index 28eeebcf..4a2320b4 100644 --- a/WEB-INF/lib/ttUser.class.php +++ b/WEB-INF/lib/ttUser.class.php @@ -167,6 +167,11 @@ class ttUser { return ($this->behalfUser ? $this->behalfUser->id : $this->id); } + // getName returns user name on behalf of whom the current user is operating. + function getName() { + return ($this->behalfUser ? $this->behalfUser->name : $this->name); + } + // getQuotaPercent returns quota percent for active user. function getQuotaPercent() { return ($this->behalfUser ? $this->behalfUser->quota_percent : $this->quota_percent); diff --git a/WEB-INF/resources/en.lang.php b/WEB-INF/resources/en.lang.php index 0784ed5f..9e2a364c 100644 --- a/WEB-INF/resources/en.lang.php +++ b/WEB-INF/resources/en.lang.php @@ -551,4 +551,14 @@ $i18n_key_words = array( 'role.admin.label' => 'Administrator', 'role.admin.low_case_label' => 'administrator', 'role.admin.description' => 'Site adminsitrator.', + +// Timesheet View form. See example at https://timetracker.anuko.com/timesheet_view.php. +// TODO: improve wording, then fill in other translation files. +'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.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.', ); diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index b1be81d2..fe8ac9ff 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.52.4822 | Copyright © Anuko | +  Anuko Time Tracker 1.18.52.4823 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/timesheet_view.php b/timesheet_view.php index d0198c78..0a3cc720 100644 --- a/timesheet_view.php +++ b/timesheet_view.php @@ -51,6 +51,7 @@ if (!$timesheet) { if ($request->isPost()) { $cl_comment = trim($request->getParameter('comment')); + $approver_id = $request->getParameter('approver'); } $options = ttTimesheetHelper::getReportOptions($timesheet); @@ -76,7 +77,7 @@ if ($showSubmit) { 'name'=>'approver', 'style'=>'width: 200px;', 'data'=>$approvers, - 'datakeys'=>array('id','name'))); + 'datakeys'=>array('id','name','email'))); } $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit'))); } @@ -92,12 +93,16 @@ 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. - if (ttTimesheetHelper::submitTimesheet($fields)) { + if (!ttTimesheetHelper::submitTimesheet($fields)) + $err->add($i18n->get('error.db')); + if ($err->no() && !ttTimesheetHelper::sendSubmitEmail($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_approve')) { -- 2.20.1