return $approvers;
}
- // submitTimesheet marks a timesheet as submitted and also sends an email
- // to a selected approver.
- static function submitTimesheet($fields) {
+ // 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;
+ }
+
+ // markSubmitted marks a timesheet as submitted.
+ static function markSubmitted($fields) {
global $user;
$mdb2 = getConnection();
$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.
$timesheet_id = $fields['timesheet_id'];
$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.
+
+ $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());
- // TODO: send email to approver here...
- // $approver_id = $fields['approver_id'];
+ 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) {
+ 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;
}
- // 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();
$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.
- static function disapproveTimesheet($fields) {
+ // markDisapproved marks a timesheet as not approved.
+ static function markDisapproved($fields) {
global $user;
$mdb2 = getConnection();
$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).
" 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
if ($options['show_cost'] && $user->isPluginEnabled('ex')) return false;
// Parts for client and project.
- if ($options['client_id']) $client_part = ' and client_id = '.(int)$options['client_id'];
- if ($options['project_id']) $project_part = ' and project_id = '.(int)$options['project_id'];
- // TODO: test and fix the above for NULL client and project ids...
+ if ($options['client_id']) $client_part = ' and (client_id is null or client_id = '.(int)$options['client_id'].')';
+ if ($options['project_id']) $project_part = ' and (project_id is null or project_id = '.(int)$options['project_id'].')';
// Determine start and end dates.
$dateFormat = $user->getDateFormat();