/** * Calculates the leave left for a given user */ public function calcAction() { $username = $this->_getParam('username'); $user = $this->userService->getUserByField('username', $username); $type = $this->_getParam('type', 'Annual'); $validTypes = array('Annual', 'Sick', 'Long Service'); if (!in_array($type, $validTypes)) { return; } if ($user == null) { $this->log->warn("Invalid username"); echo 0; return; } // Calculate how much is left $leave = $this->userService->getLeaveForUser($user); $accruedLeave = $this->userService->calculateLeave($user); $leaveApplications = $this->userService->getLeaveApplicationsForUser($user); $leaveTotal = 0; $leaveTotals = array(); foreach ($leaveApplications as $app) { if ($app->status == LeaveApplication::LEAVE_APPROVED) { $current = ifset($leaveTotals, $app->leavetype, 0); $current += $app->days; $leaveTotals[$app->leavetype] = $current; } } if ($type == 'Annual') { echo "About " . sprintf("%d", floor($leave->days + $accruedLeave - ifset($leaveTotals, "Annual", 0))) . " days of annual leave available, " . ifset($leaveTotals, "Annual", 0) . " taken"; } else { echo ifset($leaveTotals, $type, 0) . ' days of ' . $type . ' leave taken'; } }
/** * Create or update a user's account * * @param string $username * @param array $entry */ private function synchroniseUser($entry) { $mapping = ifset($this->config, 'mapping', array()); $username = ifset($entry, $mapping['username'], false); if (!$username) { throw new Exception("No username mapping found."); } // Cast back from the array representation $username = $username[0]; // get all the user's details and map the relevant attributes... which we'll skip! $user = $this->userService->getUserByField('username', $username); if ($user == null) { $params = array(); $params['username'] = $username; $params['password'] = md5(time() . rand(1, 10000)); // Fill in mapped params $email = ifset($entry, $mapping['email'], null); if ($email) { $email = $email[0]; } else { $email = $params['password'] . '@null.com'; } $params['email'] = $email; try { $user = $this->userService->createUser($params, false); } catch (Exception $e) { $this->log->err("Failed creating user for {$username}: " . $e->getMessage() . ': ' . current_url() . "\r\n" . print_r($params, true)); return null; } } za()->log(__CLASS__ . ':' . __LINE__ . " User is " . get_class($user)); return $user; }
/** * Save a contact against a given client object * * * @param array|Contact $params * @return Contact */ public function saveContact($params) { $contact = null; try { $this->dbService->beginTransaction(); // Get an existing one $contact = $this->dbService->saveObject($params, 'Contact'); // check for a user object to also update if ($contact->id) { $user = $this->userService->getUserByField('contactid', $contact->id); if ($user) { $params = array('firstname' => $contact->firstname, 'lastname' => $contact->lastname, 'email' => $contact->email); $this->userService->updateUser($user, $params); } } $this->trackerService->track('update-contact', $contact->id, null, null, print_r($params, true)); $this->dbService->commit(); } catch (Exception $e) { $this->dbService->rollback(); $this->log->err("Failed creating contact for params " . print_r($params, true) . ": " . $e->getMessage()); $contact = null; throw $e; } return $contact; }
/** * Get the detailed timesheet for this project. * */ public function detailedtimesheetAction() { $project = $this->projectService->getProject($this->_getParam('projectid')); $client = $this->clientService->getClient($this->_getParam('clientid')); $task = $this->projectService->getTask($this->_getParam('taskid')); $user = $this->userService->getUserByField('username', $this->_getParam('username')); if (!$project && !$client && !$task && !$user) { return; } if ($task) { $this->view->records = $this->projectService->getDetailedTimesheet(null, $task->id); } else { if ($project) { $start = null; $this->view->records = $this->projectService->getDetailedTimesheet(null, null, $project->id); } else { if ($client) { $start = null; $this->view->records = $this->projectService->getDetailedTimesheet(null, null, null, $client->id); } else { if ($user) { $this->view->records = $this->projectService->getDetailedTimesheet($user); } } } } $this->view->task = $task; $this->renderRawView('timesheet/ajax-timesheet-details.php'); }
/** * Notify the user (or users) about something * * @param string $subject * @param array|User $user * @param string $msg */ public function notifyUser($subject, $users, $msg, $from = null, $fromname = null, $html = false) { include_once 'Zend/Mail.php'; if (!is_array($users) && !$users instanceof ArrayAccess) { $users = array($users); } foreach ($users as $u) { if (!$u instanceof NovemberUser) { $this->log->debug("Getting user for " . var_export($u, true)); $u = $this->userService->getUserByField('username', $u); } if ($u == null) { $this->log->debug("Tried sending to non-existent user"); continue; } $mail = new Zend_Mail(); if ($from == null) { $mail->setFrom(za()->getConfig('from_email'), za()->getConfig('name')); } else { if (!$fromname) { $fromname = za()->getConfig('name'); } $mail->setFrom($from, $fromname); } $message = null; if ($msg instanceof TemplatedMessage) { $msg->model['user'] = $u; $message = $this->generateEmail($msg->template, $msg->model); } else { $message = $msg; } if ($html) { $mail->setBodyHtml($message); } else { $mail->setBodyText($message); } $mail->addTo($u->email, $u->username); $mail->setSubject($subject); try { $this->log->debug("Sending message '{$subject}' to " . $u->email); $mail->send(); } catch (Zend_Mail_Transport_Exception $e) { $this->log->debug(__CLASS__ . ':' . __LINE__ . " Failed sending mail: " . $e->getMessage()); throw $e; } } }
/** * Update all the watchers for a given item */ public function setwatchersAction() { $itemType = ucfirst($this->_getParam('attachedtotype')); $itemId = $this->_getParam('attachedtoid'); if ($itemType == null || $itemId == null) { $this->flash("Could not update subscribers, null request passed"); $this->redirect('error'); return; } // get all watchers and then remove $subscribers = $this->notificationService->deleteAllSubscribers($itemId, $itemType); foreach ($this->_getParam('watchusers') as $username) { $user = $this->userService->getUserByField('username', $username); $this->notificationService->createWatch($user, $itemId, $itemType); } $caller = $this->getCallingUrl(); if ($this->_getParam('_ajax')) { } else { if (mb_strlen($caller)) { $this->_redirect($caller); } } }
/** * Action to list the access a user has to certain elements * */ public function useraccessAction() { $username = $this->_getParam('username'); if ($username == null) { $this->flash("Must provide username!"); $this->redirect('admin'); return; } $this->view->access = $this->accessService->getAccessList($username); $this->view->user = $this->userService->getUserByField('username', $username); $this->view->modules = za()->getConfig('modules'); $this->view->modules[] = 'default'; $this->renderView('admin/user-access.php'); }
/** * 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; } }
/** * 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'); } } }
/** * View the expenses for a given user / client */ public function viewAction() { $view = new CompositeView(); $view->addScriptPath('extensions/expenses/views'); $report = $this->byId(null, 'ExpenseReport'); $client = null; $user = null; $expenses = array(); // we either have a fixed report, or we have a dynamic one if ($report) { if (mb_strlen($report->username)) { $expenses = $this->expenseService->getExpenses(array('userreportid=' => $report->id)); $user = $this->userService->getUserByField('username', $report->username); } else { $expenses = $this->expenseService->getExpenses(array('expensereportid=' => $report->id)); $client = $this->clientService->getClient($report->clientid); } $view->start = $report->from; $view->end = $report->to; } else { $client = $this->clientService->getClient($this->_getParam('clientid')); $user = $this->userService->getUserByField('username', $this->_getParam('username')); $view->user = $user; $start = $this->_getParam('start', $this->_getParam('start', $this->calculateDefaultStartDate())); $end = $this->_getParam('end', $this->_getParam('end', $this->calculateDefaultEndDate())); $expenses = $this->expenseService->getDynamicExpenseReport($start, $end, $user, $client); $view->start = $start; $view->end = $end; } $view->expenses = $expenses; $view->client = $client; $view->user = $user; $view->report = $report; $view->mode = $this->_getParam('pdf') ? 'pdf' : 'html'; $content = $view->render('expense/view.php'); if ($this->_getParam('pdf')) { ini_set('memory_limit', '32M'); include_once "dompdf/dompdf_config.inc.php"; include_once "dompdf/include/dompdf.cls.php"; include_once "dompdf/include/frame_tree.cls.php"; include_once "dompdf/include/stylesheet.cls.php"; include_once "dompdf/include/frame.cls.php"; include_once "dompdf/include/style.cls.php"; include_once "dompdf/include/attribute_translator.cls.php"; include_once "dompdf/include/frame_factory.cls.php"; include_once "dompdf/include/frame_decorator.cls.php"; include_once "dompdf/include/positioner.cls.php"; include_once "dompdf/include/block_positioner.cls.php"; include_once "dompdf/include/block_frame_decorator.cls.php"; include_once "dompdf/include/frame_reflower.cls.php"; include_once "dompdf/include/block_frame_reflower.cls.php"; include_once "dompdf/include/frame_reflower.cls.php"; include_once "dompdf/include/text_frame_reflower.cls.php"; include_once "dompdf/include/canvas_factory.cls.php"; include_once "dompdf/include/canvas.cls.php"; include_once "dompdf/include/abstract_renderer.cls.php"; include_once "dompdf/include/renderer.cls.php"; include_once "dompdf/include/cpdf_adapter.cls.php"; include_once "dompdf/include/font_metrics.cls.php"; include_once "dompdf/include/block_renderer.cls.php"; include_once "dompdf/include/text_renderer.cls.php"; include_once "dompdf/include/image_cache.cls.php"; include_once "dompdf/include/text_frame_decorator.cls.php"; include_once "dompdf/include/inline_positioner.cls.php"; include_once "dompdf/include/page_frame_reflower.cls.php"; include_once "dompdf/include/page_frame_decorator.cls.php"; include_once "dompdf/include/table_frame_decorator.cls.php"; include_once "dompdf/include/cellmap.cls.php"; include_once "dompdf/include/table_frame_reflower.cls.php"; include_once "dompdf/include/table_row_frame_decorator.cls.php"; include_once "dompdf/include/null_positioner.cls.php"; include_once "dompdf/include/table_row_frame_reflower.cls.php"; include_once "dompdf/include/table_cell_frame_decorator.cls.php"; include_once "dompdf/include/table_cell_positioner.cls.php"; include_once "dompdf/include/table_cell_frame_reflower.cls.php"; include_once "dompdf/include/table_row_group_frame_decorator.cls.php"; include_once "dompdf/include/table_row_group_frame_reflower.cls.php"; include_once "dompdf/include/table_cell_renderer.cls.php"; include_once "dompdf/include/inline_frame_decorator.cls.php"; include_once "dompdf/include/inline_frame_reflower.cls.php"; include_once "dompdf/include/image_frame_decorator.cls.php"; include_once "dompdf/include/image_frame_reflower.cls.php"; include_once "dompdf/include/inline_renderer.cls.php"; include_once "dompdf/include/image_renderer.cls.php"; include_once "dompdf/include/dompdf_exception.cls.php"; $dompdf = new DOMPDF(); // $dompdf->set_paper('letter', 'landscape'); $dompdf->load_html($content); $dompdf->render(); $name = "expenses-" . date('Y-m-d', strtotime($view->start)) . '-to-' . date('Y-m-d', strtotime($view->end)) . '.pdf'; $dompdf->stream($name); } else { echo $content; } }