baseline
[omp.pkp.sfu.ca.git] / pages / user / EmailHandler.inc.php
blobcdedc9df27df655becea19f5bd0b03d1d1410964
1 <?php
3 /**
4 * @file EmailHandler.inc.php
6 * Copyright (c) 2003-2008 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class EmailHandler
10 * @ingroup pages_user
12 * @brief Handle requests for user emails.
15 // $Id: EmailHandler.inc.php,v 1.9 2009/09/22 19:22:10 asmecher Exp $
17 import('pages.user.UserHandler');
19 class EmailHandler extends UserHandler {
20 /**
21 * Constructor
23 function EmailHandler() {
24 parent::UserHandler();
27 /**
28 * Determine whether the current user has access to the monograph in some form
29 * @param $monographId int
30 * @return boolean
32 function _monographAccessChecks($monographId, $userId) {
33 $monographDao =& DAORegistry::getDAO('MonographDAO');
34 $signoffDao =& DAORegistry::getDAO('SignoffDAO');
36 $monograph =& $monographDao->getMonograph($monographId);
38 // First, conditions where access is OK.
39 // 1. User is submitter
40 if ($monograph && $monograph->getUserId() == $userId) return true;
41 // 2. User is acquisitions editor of monograph or full editor
42 $editAssignmentDao =& DAORegistry::getDAO('EditAssignmentDAO');
43 $editAssignments =& $editAssignmentDao->getByMonographId($monographId);
44 while ($editAssignment =& $editAssignments->next()) {
45 if ($editAssignment->getEditorId() === $userId) return true;
47 if (Validation::isEditor($press->getId())) return true;
49 // 3. User is reviewer
50 $reviewAssignmentDao =& DAORegistry::getDAO('ReviewAssignmentDAO');
51 foreach ($reviewAssignmentDao->getByMonographId($monographId) as $reviewAssignment) {
52 if ($reviewAssignment->getReviewerId() === $userId) return true;
54 // 4. User is a designer
55 $designerAssignmentDao =& DAORegistry::getDAO('LayoutAssignmentDAO');
56 foreach ($designerAssignmentDao->getByMonographId($monographId) as $designAssignment) {
57 if ($designAssignment->getDesignerId() === $userId) return true;
59 // 5. User is copyeditor
60 $copyedSignoff =& $signoffDao->getBySymbolic('SIGNOFF_COPYEDITING_INITIAL', ASSOC_TYPE_MONOGRAPH, $monographId);
61 if ($copyedSignoff && $copyedSignoff->getUserId() === $userId) return true;
62 // 6. User is production editor
63 $productionSignoff =& $signoffDao->getBySymbolic('SIGNOFF_PRODUCTION', ASSOC_TYPE_MONOGRAPH, $monographId);
64 if ($productionSignoff && $productionSignoff->getUserId() === $userId) return true;
65 // 7. User is proofreader
66 $proofSignoff =& $signoffDao->getBySymbolic('SIGNOFF_PROOFREADING_PROOFREADER', ASSOC_TYPE_MONOGRAPH, $monographId);
67 if ($proofSignoff && $proofSignoff->getUserId() === $userId) return true;
68 // 8. User is indexer
69 $indexSignoff =& $signoffDao->getBySymbolic('SIGNOFF_INDEXING', ASSOC_TYPE_MONOGRAPH, $monographId);
70 if ($indexSignoff && $indexSignoff->getUserId() === $userId) return true;
72 // 9. User is director
73 if (Validation::isDirector($press->getId())) return true;
75 // Last, "deal-breakers" -- access is not allowed.
76 if (!$monograph || ($monograph && $monograph->getPressId() !== $press->getId())) return false;
78 return false;
81 function email($args) {
82 $this->validate();
84 $this->setupTemplate(true);
86 $templateMgr =& TemplateManager::getManager();
88 $userDao =& DAORegistry::getDAO('UserDAO');
90 $press =& Request::getPress();
91 $user =& Request::getUser();
93 // See if this is the Editor or Manager and an email template has been chosen
94 $template = Request::getUserVar('template');
95 if ( !$press || empty($template) || (
96 !Validation::isPressManager($press->getId()) &&
97 !Validation::isEditor($press->getId()) &&
98 !Validation::isAcquisitionsEditor($press->getId())
99 )) {
100 $template = null;
103 // Determine whether or not this account is subject to
104 // email sending restrictions.
105 $canSendUnlimitedEmails = Validation::isSiteAdmin();
106 $unlimitedEmailRoles = array(
107 ROLE_ID_PRESS_MANAGER
109 $roleDao =& DAORegistry::getDAO('RoleDAO');
110 if ($press) {
111 $roles =& $roleDao->getRolesByUserId($user->getId(), $press->getId());
112 foreach ($roles as $role) {
113 if (in_array($role->getRoleId(), $unlimitedEmailRoles)) $canSendUnlimitedEmails = true;
117 // Check when this user last sent an email, and if it's too
118 // recent, make them wait.
119 if (!$canSendUnlimitedEmails) {
120 $dateLastEmail = $user->getDateLastEmail();
121 if ($dateLastEmail && strtotime($dateLastEmail) + ((int) Config::getVar('email', 'time_between_emails')) > strtotime(Core::getCurrentDate())) {
122 $templateMgr->assign('pageTitle', 'email.compose');
123 $templateMgr->assign('message', 'email.compose.tooSoon');
124 $templateMgr->assign('backLink', 'javascript:history.back()');
125 $templateMgr->assign('backLinkLabel', 'email.compose');
126 return $templateMgr->display('common/message.tpl');
130 $email = null;
131 if ($monographId = Request::getUserVar('monographId')) {
132 $monographDao =& DAORegistry::getDAO('MonographDAO');
133 // This message is in reference to a monograph.
134 // Determine whether the current user has access
135 // to the monograph in some form, and if so, use an
136 // MonographMailTemplate.
137 $hasAccess = $this->_monographAccessChecks($monographId, $user->getId());
139 if ($hasAccess) {
140 import('mail.MonographMailTemplate');
141 $email = new MonographMailTemplate($monographDao->getMonograph($monographId, $template));
145 if ($email === null) {
146 import('mail.MailTemplate');
147 $email = new MailTemplate($template);
150 if (Request::getUserVar('send') && !$email->hasErrors()) {
151 $recipients = $email->getRecipients();
152 $ccs = $email->getCcs();
153 $bccs = $email->getBccs();
155 // Make sure there aren't too many recipients (to
156 // prevent use as a spam relay)
157 $recipientCount = 0;
158 if (is_array($recipients)) $recipientCount += count($recipients);
159 if (is_array($ccs)) $recipientCount += count($ccs);
160 if (is_array($bccs)) $recipientCount += count($bccs);
162 if (!$canSendUnlimitedEmails && $recipientCount > ((int) Config::getVar('email', 'max_recipients'))) {
163 $templateMgr->assign('pageTitle', 'email.compose');
164 $templateMgr->assign('message', 'email.compose.tooManyRecipients');
165 $templateMgr->assign('backLink', 'javascript:history.back()');
166 $templateMgr->assign('backLinkLabel', 'email.compose');
167 return $templateMgr->display('common/message.tpl');
169 $email->send();
170 $redirectUrl = Request::getUserVar('redirectUrl');
171 if (empty($redirectUrl)) $redirectUrl = Request::url(null, 'user');
172 $user->setDateLastEmail(Core::getCurrentDate());
173 $userDao->updateObject($user);
174 Request::redirectUrl($redirectUrl);
175 } else {
176 $email->displayEditForm(Request::url(null, null, 'email'), array('redirectUrl' => Request::getUserVar('redirectUrl'), 'monographId' => $monographId), null, array('disableSkipButton' => true, 'monographId' => $monographId));