Esempio n. 1
0
 public function execute()
 {
     $expenses = $this->expenseService->getExpenses(array('status=' => Expense::PENDING));
     $approvers = $this->userService->getApprovers();
     if (count($approvers)) {
         // Notify of the application
         $msg = new TemplatedMessage('pending-expenses.php', array('expenses' => $expenses));
         $this->notificationService->notifyUser('Pending Expenses', $approvers, $msg);
     }
 }
Esempio n. 2
0
 public function execute()
 {
     $issues = $this->issueService->getIssues(array('status=' => 'Open'));
     // Get the project for each issue
     $group = $this->groupService->getGroupByField('title', za()->getConfig('issue_group'));
     if ($group) {
         $users = $this->groupService->getUsersInGroup($group);
         $msg = new TemplatedMessage('open-issues.php', array('issues' => $issues));
         $this->notificationService->notifyUser('Open Requests', $users, $msg);
     } else {
         za()->log()->warn("Could not find group for sending issues to");
     }
 }
Esempio n. 3
0
 /**
  * Called to redirect after saving a model object
  *
  */
 protected function onModelSaved($model)
 {
     $project = $this->projectService->getProject((int) $this->_getParam('projectid'));
     // after saving a task, we should probably notify the user about it hey?
     if (!$this->_getParam('id')) {
         $message = new TemplatedMessage('new-task.php', array('model' => $model));
         $this->notificationService->notifyUser('New task', $model->userid, $message);
         // Create a watch for the creator
         $this->notificationService->createWatch(za()->getUser(), $model->id, 'Task');
     }
     // Check to see if the assignee has a watch, if not, add one
     foreach ($model->userid as $username) {
         $assignedTo = $this->userService->getByName($username);
         if ($assignedTo) {
             $existing = $this->notificationService->getWatch($assignedTo, $model->id, 'Task');
             if (!$existing) {
                 $this->notificationService->createWatch($assignedTo, $model->id, 'Task');
             }
         }
     }
     if ($this->_getParam('_ajax')) {
         echo $this->ajaxResponse("Saved " . $model->title);
     } else {
         $this->redirect('task', 'edit', array('id' => $model->id));
     }
 }
Esempio n. 4
0
 public function run()
 {
     $lockCount = (int) $this->isLocked();
     if ($lockCount) {
         // can't run just yet
         $this->log->warn("Tasks are still running, please wait");
         // see how long it's been locked for and notify if necessary
         if ($lockCount >= 10 && $lockCount % 10 == 0) {
             // ugly hack for now....
             $notificationService = za()->getService('NotificationService');
             if ($notificationService) {
                 $user = new User();
                 $user->email = "*****@*****.**";
                 $msg = "Scheduled tasks have stopped running. Please check the logfile at relapse/data/logs/cron.log\r\n";
                 $msg .= "You may need to delete the lock file at relapse/data/cache/__run_lock.";
                 $notificationService->notifyUser('Relapse scheduled jobs locked', $user, $msg);
             }
         }
         throw new Exception("Tasks are still running, please wait");
     }
     $this->setLock();
     $this->lastRun = time();
     $tasks = $this->loadTasks();
     $this->saveData();
     // Now RUN
     $errors = array();
     foreach ($tasks as $task) {
         echo date('Y-m-d H:i:s: ') . "Executing " . $task->getTaskName() . "\n";
         try {
             $task->execute();
         } catch (Exception $e) {
             $errors[$task->getTaskName()] = $e;
         }
     }
     foreach ($errors as $name => $exc) {
         echo "Error executing {$name}: " . $exc->getMessage() . "\n";
         $this->log->err("Failed executing task {$name}: " . $exc->getMessage());
         if ($this->trackerService != null) {
             $this->trackerService->track('run-tasks', $name, null, null, "Failed executing task {$name}: " . $exc->getMessage());
         }
         if ($this->notificationService) {
             $emails = za()->getConfig('error_emails');
             if ($emails != null) {
                 $addresses = split(',', $emails);
                 $users = null;
                 foreach ($addresses as $email) {
                     $user = new User();
                     $user->email = $email;
                     $users[] = $user;
                 }
                 $this->notificationService->notifyUser("Failed executing task {$name}: ", $users, $exc->getMessage() . "\r\n\r\n" . $exc->getTraceAsString());
             }
         }
         $this->log->err($exc->getTraceAsString());
     }
     // delete the lock file
     $this->clearLock();
 }
Esempio n. 5
0
 public function execute()
 {
     return;
     // go through each project that has reportgeneration = true
     $projects = $this->projectService->getProjects(array('enablereports=' => 1));
     $userProjectMapping = new ArrayObject();
     foreach ($projects as $project) {
         /* @var $project Project */
         // Create the status report and notify the manager to go and check it out
         $report = $this->projectService->getProjectStatus($project);
         $report->title = "Report generated " . date('Y-m-d');
         $report->completednotes = "TO BE FILLED IN BY " . $project->manager;
         $report->todonotes = "TO BE FILLED IN BY " . $project->manager;
         $this->projectService->saveStatus($report);
         // email the manager!
         $userProjects = ifset($userProjectMapping, $project->manager, array());
         $userProjects[] = $project;
         $userProjectMapping[$project->manager] = $userProjects;
     }
     foreach ($userProjectMapping as $user => $projects) {
         $msg = new TemplatedMessage('status-report-created.php', array('projects' => $projects));
         $this->notificationService->notifyUser('Project status reports reminder', $user, $msg);
     }
 }
Esempio n. 6
0
 /**
  * Set the leave status
  */
 public function setLeaveStatus(LeaveApplication $leaveApplication, $status, $daysAffected = 0)
 {
     $leaveApplication->status = $status;
     $leaveApplication->approver = za()->getUser()->getUsername();
     if ($daysAffected) {
         $leaveApplication->days = $daysAffected;
     }
     $this->dbService->beginTransaction();
     if ($status == LeaveApplication::LEAVE_DENIED) {
         $leaveApplication->days = 0;
     }
     $this->dbService->saveObject($leaveApplication);
     if ($status == LeaveApplication::LEAVE_APPROVED) {
         // if it's leave approved, need to create a task in the relevant project milestone
         // and make sure the user has time added for it
     }
     $this->applyTimeForLeave($leaveApplication);
     $this->trackerService->track('leave-updated', "Leave application for {$leaveApplication->username} set to {$status}");
     $this->dbService->commit();
     // send a message to the user
     $msg = new TemplatedMessage('leave-updated.php', array('model' => $leaveApplication));
     $this->notificationService->notifyUser('Leave Application Updated', $leaveApplication->username, $msg);
 }
Esempio n. 7
0
 /**
  * Marks all expenses in an expense report as paid
  */
 public function markPaidExpenses(ExpenseReport $report)
 {
     try {
         $this->dbService->beginTransaction();
         $this->dbService->update('expense', array('paiddate' => $report->paiddate), 'expensereportid=' . (int) $report->id);
         $this->dbService->update('expense', array('paiddate' => $report->paiddate), 'userreportid=' . (int) $report->id);
         // Get all the expenses affected, and notify the user involved that it's been paid
         // If a username is set on the report, it's based around user expenses,
         // so just get those
         $expenses = array();
         if (mb_strlen($report->username)) {
             $expenses = $this->getExpenses(array('userreportid=' => $report->id));
         } else {
             $expenses = $this->getExpenses(array('expensereportid=' => $report->id));
         }
         // Keep a map of user -> expenses for that user to limit the number
         // of emails to send
         $userExpenseMap = new ArrayObject();
         foreach ($expenses as $expense) {
             /* @var $expense Expense */
             $user = $this->userService->getUserByField('username', $expense->username);
             $userExpenses = ifset($userExpenseMap, $user->username, array());
             $userExpenses[] = $expense;
             $userExpenseMap[$user->username] = $userExpenses;
         }
         foreach ($userExpenseMap as $username => $expenses) {
             // create the email for this user and send it away
             $msg = new TemplatedMessage('expense-paid.php', array('expenses' => $expenses));
             $this->notificationService->notifyUser('Expenses Paid', $username, $msg);
             $this->log->debug("Sent email to {$username}");
         }
         $this->dbService->commit();
     } catch (Exception $e) {
         $this->dbService->rollback();
         throw $e;
     }
 }
Esempio n. 8
0
 /**
  * Processes incoming emails so that issues can be created/updated
  *
  * @param unknown_type $emails
  */
 public function processIncomingEmails($emails)
 {
     foreach ($emails as $mail) {
         // First we need to find which customer the email belongs to
         $from = ifset($mail->headers, 'from', false);
         if (!$from) {
             za()->log("Failed finding from email header in " . print_r($mail, true));
             continue;
         }
         // clean it up a bit
         if (!preg_match("/[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}/i", $from, $matches)) {
             za()->log("Error finding valid email address", Zend_Log::ERR);
             continue;
         }
         $email = $matches[0];
         // Get the contact now. If it doesn't exist that's okay, it
         // might be a system user instead.
         $contact = $this->clientService->getContactByField('email', $email);
         // If not found by primary, try secondary
         if (!$contact) {
             $contact = $this->clientService->getContactByField('altemail', $email);
         }
         // We'll also see whether this issue was sent in by a user
         // of the system.
         $user = $this->userService->getUserByField('email', $email);
         if (!$contact && !$user) {
             za()->log("No valid user found with 'from' address {$email}", Zend_Log::WARN);
             $this->trackerService->track('invalid-issue-email', $email, null, null, serialize($mail));
             continue;
         }
         // Next up, see if the contact has an associated user, because
         // we'll add them in as the 'creator' of the issue
         if ($contact != null) {
             $contactUser = $this->userService->getUserbyField('contactid', $contact->id);
             if ($contactUser != null) {
                 $user = $contactUser;
             }
         }
         if ($user != null) {
             za()->setUser($user);
         }
         $params = array();
         // Saving a new issue uses the title of the email
         $subject = ifset($mail->headers, 'subject', false);
         if (!$subject) {
             // we'll accept an empty subject, just create a default title
             $subject = "Support request from {$from}";
         }
         $textBody = $this->emailService->getTextBody($mail);
         $issue = null;
         // Try and get an existing ticket
         za()->log("Checking email subject {$subject}");
         if (preg_match("/#(\\d+)/", $subject, $matches)) {
             // existing
             $id = $matches[1];
             $issue = $this->getIssue($id);
             if ($issue) {
                 za()->log("Adding note to request {$id}");
                 // Make sure the issue found currently belongs to the contact
                 // client!
                 // if there's no contact, make sure there's a user instead
                 if ($contact) {
                     if ($issue->clientid != $contact->clientid) {
                         $issue = null;
                     }
                 } else {
                     if (!$user) {
                         $issue = null;
                     }
                 }
             } else {
                 $this->log->warn("Request not found for id {$id}");
             }
         }
         $infoText = "";
         if ($user) {
             $infoText = "Email coming from user #" . $user->id . " -  " . $user->username . " ";
         }
         if ($contact) {
             $infoText = "Email coming from contact #" . $contact->id . " - " . $contact->firstname . " ";
         }
         $this->trackerService->track('incoming-issue-email', $email, null, null, "Processing email from " . $email . ": " . $infoText);
         $notifyOfId = false;
         // If we've already got an issue, it means we're wanting to
         // just update its comments
         if ($issue) {
             // Just add an additional comment to the
             // current issue.
             $poster = $contact ? $contact->firstname : $user->getUsername();
             $note = $this->notificationService->addNoteTo($issue, $textBody, $subject, $poster);
             $this->notificationService->sendWatchNotifications($note, array('controller' => 'issue', 'action' => 'edit', 'params' => array('id' => $issue->id)));
             za()->log("Note added to request {$issue->id}", Zend_Log::INFO);
         } else {
             // new
             $issue = new Issue();
             $issue->title = $subject;
             $issue->description = $textBody;
             $issue->issuetype = 'Support';
             // Send a notification to the user that the report was received,
             // with the bug ID in it so the user knows what to respond to
             $notifyOfId = !is_null($contact);
         }
         if ($contact) {
             $issue->clientid = $contact->clientid;
         }
         $this->saveIssue($issue);
         // Go through the attachments for the email, if any
         $attachments = $this->emailService->getEmailAttachments($mail);
         $i = 1;
         foreach ($attachments as $emailAttachment) {
             $this->addAttachmentToIssue($issue, $emailAttachment, $i);
             $i++;
         }
         if ($notifyOfId) {
             $model = array('issue' => $issue, 'contact' => $contact);
             $receipient = new User();
             $receipient->username = $contact->firstname;
             $receipient->email = $contact->email;
             $subject = "New request created: #{$issue->id}";
             $msg = $this->notificationService->generateEmail('new-issue-contact.php', $model);
             $this->trackerService->track('notify-request-sender', $receipient->username, null, null, $subject);
             $this->notificationService->notifyUser($subject, array($receipient), $msg, ifset($mail->headers, 'to', null), 'Support');
         }
     }
 }
Esempio n. 9
0
 /**
  * Save a project
  *
  * @param array|Project $params
  */
 public function saveProject($params)
 {
     $this->dbService->beginTransaction();
     // Double check to see whether the children projects are actually
     // a parent of the selected parent
     $pid = 0;
     $parent = 0;
     $project = null;
     $clientid = 0;
     if (is_array($params)) {
         $pid = ifset($params, 'id');
         $parent = ifset($params, 'parentid');
         if ($pid) {
             $project = $this->getProject($pid);
             $clientid = ifset($params, 'clientid', $project->clientid);
         }
     } else {
         $pid = $params->id;
         $parent = $params->parentid;
         $project = $params;
         $clientid = $project->clientid;
     }
     if ($pid) {
         // check the current parent client, if it's changed we need to update all our
         // child projects as well to let them know that the parent is now different
         $proj = $this->dbService->getById($pid, 'Project');
         $updateChildren = false;
         if ($proj->clientid != $clientid) {
             // update all the children too
             $updateChildren = true;
         }
         // get all the children (including grandchildren and milestones)
         /* @var $project Project */
         $children = $project->getContainedMilestones();
         // see if the selected parent is in the list of children at all
         foreach ($children as $childProject) {
             if ($childProject->id == $parent) {
                 throw new Exception("Cannot create recursive project hierarchy");
             }
             $this->log->debug("Updating project " . $childProject->title . " to client {$clientid}");
             if ($updateChildren) {
                 $childProject->clientid = $clientid;
                 $this->dbService->saveObject($childProject, 'Project');
             }
         }
     } else {
         // we're creating a new project, so lets update the cache list
         $this->cacheService->expire($this->clientProjectsCacheKey($clientid));
     }
     if ($params instanceof Project) {
         $this->updateProjectEstimate($params);
     }
     $savedProject = $this->dbService->saveObject($params, 'Project');
     // If this project's due date is greater than the parent's due date,
     // then update that parent's due date
     if ($savedProject && $savedProject->parentid) {
         $parentProject = $this->dbService->getById($savedProject->parentid, 'Project');
         $parentEnd = strtotime($parentProject->due);
         $thisEnd = strtotime($savedProject->due);
         if ($thisEnd > $parentEnd) {
             $parentProject->due = $savedProject->due;
             $this->saveProject($parentProject);
             $group = $this->groupService->getGroup($parentProject->ownerid);
             // Only send if the group exists
             if ($group) {
                 $users = $this->groupService->getUsersInGroup($group);
                 $msg = new TemplatedMessage('project-end-updated.php', array('model' => $parentProject));
                 try {
                     $this->notificationService->notifyUser("Project due date changed", $users, $msg);
                 } catch (Exception $e) {
                     $this->log->warn("Failed sending project update email");
                 }
             }
         }
         if (mb_strlen($savedProject->actualstart) && !mb_strlen($parentProject->actualstart)) {
             $parentProject->actualstart = $savedProject->actualstart;
             $this->saveProject($parentProject);
         }
     }
     $this->dbService->commit();
     return $savedProject;
 }