|
|
| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @file NotificationHandler.inc.php |
| 5 |
* |
| 6 |
* Copyright (c) 2000-2008 John Willinsky |
| 7 |
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING. |
| 8 |
* |
| 9 |
* @class NotificationHandler |
| 10 |
* @ingroup pages_help |
| 11 |
* |
| 12 |
* @brief Handle requests for viewing notifications. |
| 13 |
*/ |
| 14 |
|
| 15 |
import('core.PKPHandler'); |
| 16 |
import('notification.Notification'); |
| 17 |
|
| 18 |
class NotificationHandler extends PKPHandler { |
| 19 |
|
| 20 |
/** |
| 21 |
* Display help table of contents. |
| 22 |
*/ |
| 23 |
function index() { |
| 24 |
parent::validate(); |
| 25 |
parent::setupTemplate(); |
| 26 |
$templateMgr =& TemplateManager::getManager(); |
| 27 |
|
| 28 |
$user = Request::getUser(); |
| 29 |
if(isset($user)) { |
| 30 |
$userId = $user->getUserId(); |
| 31 |
$templateMgr->assign('isUserLoggedIn', true); |
| 32 |
} else { |
| 33 |
$userId = 0; |
| 34 |
$templateMgr->assign('emailUrl', Request::url(null, 'notification', 'subscribeMailList')); |
| 35 |
$templateMgr->assign('isUserLoggedIn', false); |
| 36 |
} |
| 37 |
|
| 38 |
$notificationDao = &DAORegistry::getDAO('NotificationDAO'); |
| 39 |
$notifications = $notificationDao->getNotificationsByUserId($userId); |
| 40 |
|
| 41 |
$templateMgr->assign('notifications', $notifications); |
| 42 |
$templateMgr->assign('unread', $notificationDao->getUnreadNotificationCount($userId)); |
| 43 |
$templateMgr->assign('read', $notificationDao->getReadNotificationCount($userId)); |
| 44 |
$templateMgr->assign('url', Request::url(null, 'notification', 'settings')); |
| 45 |
$templateMgr->display('notification/index.tpl'); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Delete a notification |
| 50 |
*/ |
| 51 |
function delete($args) { |
| 52 |
parent::validate(); |
| 53 |
|
| 54 |
$user = Request::getUser(); |
| 55 |
if(isset($user)) { |
| 56 |
$userId = $user->getUserId(); |
| 57 |
$notificationDao = &DAORegistry::getDAO('NotificationDAO'); |
| 58 |
$notifications = $notificationDao->deleteNotificationById($args[0], $userId); |
| 59 |
} |
| 60 |
|
| 61 |
Request::redirect(null, 'notification'); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Delete a notification |
| 66 |
*/ |
| 67 |
function deleteAjax($args) { |
| 68 |
parent::validate(); |
| 69 |
|
| 70 |
$user = Request::getUser(); |
| 71 |
if(isset($user)) { |
| 72 |
$userId = $user->getUserId(); |
| 73 |
$notificationDao = &DAORegistry::getDAO('NotificationDAO'); |
| 74 |
$notifications = $notificationDao->deleteNotificationById($args[0], $userId); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* View and modify notification settings |
| 80 |
*/ |
| 81 |
function settings() { |
| 82 |
parent::validate(); |
| 83 |
|
| 84 |
$user = Request::getUser(); |
| 85 |
if(isset($user)) { |
| 86 |
$userId = $user->getUserId(); |
| 87 |
|
| 88 |
$notificationSettingsDao = &DAORegistry::getDAO('NotificationSettingsDAO'); |
| 89 |
$notificationSettings = $notificationSettingsDao->getNotificationSettings($userId); |
| 90 |
$emailSettings = $notificationSettingsDao->getNotificationEmailSettings($userId); |
| 91 |
|
| 92 |
$templateMgr = &TemplateManager::getManager(); |
| 93 |
$templateMgr->assign('notificationSettings', $notificationSettings); |
| 94 |
$templateMgr->assign('emailSettings', $emailSettings); |
| 95 |
$templateMgr->assign('titleVar', Locale::translate('common.title')); |
| 96 |
$templateMgr->assign('userVar', Locale::translate('common.user')); |
| 97 |
$templateMgr->display('notification/settings.tpl'); |
| 98 |
} else Request::redirect(null, 'notification'); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Save user notification settings |
| 103 |
*/ |
| 104 |
function saveSettings() { |
| 105 |
parent::validate(); |
| 106 |
|
| 107 |
$settings = array(); |
| 108 |
|
| 109 |
$user = Request::getUser(); |
| 110 |
if(isset($user)) { |
| 111 |
$userId = $user->getUserId(); |
| 112 |
$notificationSettingsDao = &DAORegistry::getDAO('NotificationSettingsDAO'); |
| 113 |
|
| 114 |
// Notification settings |
| 115 |
$settings = array(); |
| 116 |
if(!Request::getUserVar('notificationArticleSubmitted')) $settings[] = NOTIFICATION_TYPE_ARTICLE_SUBMITTED; |
| 117 |
if(!Request::getUserVar('notificationMetadataModified')) $settings[] = NOTIFICATION_TYPE_METADATA_MODIFIED; |
| 118 |
if(!Request::getUserVar('notificationSuppFileAdded')) $settings[] = NOTIFICATION_TYPE_SUPP_FILE_ADDED; |
| 119 |
if(!Request::getUserVar('notificationSuppFileModified')) $settings[] = NOTIFICATION_TYPE_SUPP_FILE_MODIFIED; |
| 120 |
if(!Request::getUserVar('notificationGalleyAdded')) $settings[] = NOTIFICATION_TYPE_GALLEY_ADDED; |
| 121 |
if(!Request::getUserVar('notificationGalleyModified')) $settings[] = NOTIFICATION_TYPE_GALLEY_MODIFIED; |
| 122 |
if(!Request::getUserVar('notificationSubmissionComment')) $settings[] = NOTIFICATION_TYPE_SUBMISSION_COMMENT; |
| 123 |
if(!Request::getUserVar('notificationLayoutComment')) $settings[] = NOTIFICATION_TYPE_LAYOUT_COMMENT; |
| 124 |
if(!Request::getUserVar('notificationCopyeditComment')) $settings[] = NOTIFICATION_TYPE_COPYEDIT_COMMENT; |
| 125 |
if(!Request::getUserVar('notificationProofreadComment')) $settings[] = NOTIFICATION_TYPE_PROOFREAD_COMMENT; |
| 126 |
if(!Request::getUserVar('notificationReviewerComment')) $settings[] = NOTIFICATION_TYPE_REVIEWER_COMMENT; |
| 127 |
if(!Request::getUserVar('notificationReviewerFormComment')) $settings[] = NOTIFICATION_TYPE_REVIEWER_FORM_COMMENT; |
| 128 |
if(!Request::getUserVar('notificationEditorDecisionComment')) $settings[] = NOTIFICATION_TYPE_EDITOR_DECISION_COMMENT; |
| 129 |
if(!Request::getUserVar('notificationPublishedIssue')) $settings[] = NOTIFICATION_TYPE_PUBLISHED_ISSUE; |
| 130 |
if(!Request::getUserVar('notificationUserComment')) $settings[] = NOTIFICATION_TYPE_USER_COMMENT; |
| 131 |
|
| 132 |
// Email settings |
| 133 |
$emailSettings = array(); |
| 134 |
if(Request::getUserVar('emailNotificationArticleSubmitted')) $emailSettings[] = NOTIFICATION_TYPE_ARTICLE_SUBMITTED; |
| 135 |
if(Request::getUserVar('emailNotificationMetadataModified')) $emailSettings[] = NOTIFICATION_TYPE_METADATA_MODIFIED; |
| 136 |
if(Request::getUserVar('emailNotificationSuppFileAdded')) $emailSettings[] = NOTIFICATION_TYPE_SUPP_FILE_ADDED; |
| 137 |
if(Request::getUserVar('emailNotificationSuppFileModified')) $emailSettings[] = NOTIFICATION_TYPE_SUPP_FILE_MODIFIED; |
| 138 |
if(Request::getUserVar('emailNotificationGalleyAdded')) $emailSettings[] = NOTIFICATION_TYPE_GALLEY_ADDED; |
| 139 |
if(Request::getUserVar('emailNotificationGalleyModified')) $emailSettings[] = NOTIFICATION_TYPE_GALLEY_MODIFIED; |
| 140 |
if(Request::getUserVar('emailNotificationSubmissionComment')) $emailSettings[] = NOTIFICATION_TYPE_SUBMISSION_COMMENT; |
| 141 |
if(Request::getUserVar('emailNotificationLayoutComment')) $emailSettings[] = NOTIFICATION_TYPE_LAYOUT_COMMENT; |
| 142 |
if(Request::getUserVar('emailNotificationCopyeditComment')) $emailSettings[] = NOTIFICATION_TYPE_COPYEDIT_COMMENT; |
| 143 |
if(Request::getUserVar('emailNotificationProofreadComment')) $emailSettings[] = NOTIFICATION_TYPE_PROOFREAD_COMMENT; |
| 144 |
if(Request::getUserVar('emailNotificationReviewerComment')) $emailSettings[] = NOTIFICATION_TYPE_REVIEWER_COMMENT; |
| 145 |
if(Request::getUserVar('emailNotificationReviewerFormComment')) $emailSettings[] = NOTIFICATION_TYPE_REVIEWER_FORM_COMMENT; |
| 146 |
if(Request::getUserVar('emailNotificationEditorDecisionComment')) $emailSettings[] = NOTIFICATION_TYPE_EDITOR_DECISION_COMMENT; |
| 147 |
if(Request::getUserVar('emailNotificationPublishedIssue')) $emailSettings[] = NOTIFICATION_TYPE_PUBLISHED_ISSUE; |
| 148 |
if(Request::getUserVar('emailNotificationUserComment')) $emailSettings[] = NOTIFICATION_TYPE_USER_COMMENT; |
| 149 |
|
| 150 |
$notificationSettingsDao->updateNotificationSettings($settings, $userId); |
| 151 |
$notificationSettingsDao->updateNotificationEmailSettings($emailSettings, $userId); |
| 152 |
Request::redirect(null, 'notification'); |
| 153 |
} else Request::redirect(null, 'notification'); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Fetch the existing or create a new URL for the user's RSS feed |
| 158 |
*/ |
| 159 |
function getNotificationFeedUrl($args) { |
| 160 |
$user = Request::getUser(); |
| 161 |
if(isset($user)) { |
| 162 |
$userId = $user->getUserId(); |
| 163 |
$notificationSettingsDao = &DAORegistry::getDAO('NotificationSettingsDAO'); |
| 164 |
$feedType = $args[0]; |
| 165 |
|
| 166 |
$token = $notificationSettingsDao->getRSSTokenByUserId($userId); |
| 167 |
|
| 168 |
if ($token) { |
| 169 |
Request::redirect(null, 'notification', 'notificationFeed', array($feedType, $token)); |
| 170 |
} else { |
| 171 |
$token = $notificationSettingsDao->insertNewRSSToken($userId); |
| 172 |
Request::redirect(null, 'notification', 'notificationFeed', $feedType, $token); |
| 173 |
} |
| 174 |
} else Request::redirect(null, 'notification'); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Fetch the actual RSS feed |
| 179 |
*/ |
| 180 |
function notificationFeed($args) { |
| 181 |
if(isset($args[0]) && isset($args[1])) { |
| 182 |
$type = $args[0]; |
| 183 |
$token = $args[1]; |
| 184 |
} else return false; |
| 185 |
|
| 186 |
$application = PKPApplication::getApplication(); |
| 187 |
$appName = $application->getNameKey(); |
| 188 |
|
| 189 |
$site =& Request::getSite(); |
| 190 |
$siteTitle = $site->getSiteTitle(); |
| 191 |
|
| 192 |
$notificationDao = &DAORegistry::getDAO('NotificationDAO'); |
| 193 |
$notificationSettingsDao = &DAORegistry::getDAO('NotificationSettingsDAO'); |
| 194 |
|
| 195 |
$userId = $notificationSettingsDao->getUserIdByRSSToken($token); |
| 196 |
$notifications = $notificationDao->getNotificationsByUserId($userId); |
| 197 |
|
| 198 |
// Make sure the feed type is specified and valid |
| 199 |
$typeMap = array( |
| 200 |
'rss' => 'rss.tpl', |
| 201 |
'rss2' => 'rss2.tpl', |
| 202 |
'atom' => 'atom.tpl' |
| 203 |
); |
| 204 |
$mimeTypeMap = array( |
| 205 |
'rss' => 'application/rdf+xml', |
| 206 |
'rss2' => 'application/rss+xml', |
| 207 |
'atom' => 'application/atom+xml' |
| 208 |
); |
| 209 |
if (!isset($typeMap[$type])) return false; |
| 210 |
|
| 211 |
$versionDao =& DAORegistry::getDAO('VersionDAO'); |
| 212 |
$version = $versionDao->getCurrentVersion(); |
| 213 |
|
| 214 |
$templateMgr = &TemplateManager::getManager(); |
| 215 |
$templateMgr->assign('version', $version->getVersionString()); |
| 216 |
$templateMgr->assign('selfUrl', Request::getCompleteUrl()); |
| 217 |
$templateMgr->assign('locale', Locale::getPrimaryLocale()); |
| 218 |
$templateMgr->assign('appName', $appName); |
| 219 |
$templateMgr->assign('siteTitle', $siteTitle); |
| 220 |
$templateMgr->assign_by_ref('notifications', $notifications->toArray()); |
| 221 |
|
| 222 |
$templateMgr->display(Core::getBaseDir() . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . |
| 223 |
'pkp' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'notification' . DIRECTORY_SEPARATOR . $typeMap[$type], $mimeTypeMap[$type]); |
| 224 |
|
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
/** |
| 230 |
* Display the public notification email subscription form |
| 231 |
*/ |
| 232 |
function subscribeMailList() { |
| 233 |
$templateMgr = &TemplateManager::getManager(); |
| 234 |
$templateMgr->assign('new', true); |
| 235 |
|
| 236 |
$user = Request::getUser(); |
| 237 |
// FIXME: Need to abstract subscription enabled check |
| 238 |
// import('payment.PaymentManager'); |
| 239 |
// $subscriptionEnabled = PaymentManager::isConfigured(); |
| 240 |
|
| 241 |
if(!isset($user)) { |
| 242 |
// $templateMgr->assign('subscriptionEnabled', $subscriptionEnabled); |
| 243 |
|
| 244 |
if($userEmail = Request::getUserVar('email')) { |
| 245 |
$notificationSettingsDao = &DAORegistry::getDAO('NotificationSettingsDAO'); |
| 246 |
if($password = $notificationSettingsDao->subscribeGuest($userEmail)) { |
| 247 |
NotificationHandler::sendMailingListEmail($userEmail, $password, 'NOTIFICATION_MAILLIST_WELCOME'); |
| 248 |
$templateMgr->assign('success', "notification.subscribeSuccess"); |
| 249 |
$templateMgr->display('notification/maillist.tpl'); |
| 250 |
} else { |
| 251 |
$templateMgr->assign('error', "notification.subscribeError"); |
| 252 |
$templateMgr->display('notification/maillist.tpl'); |
| 253 |
|
| 254 |
} |
| 255 |
} else { |
| 256 |
$templateMgr->display('notification/maillist.tpl'); |
| 257 |
} |
| 258 |
} else Request::redirect(null, 'notification'); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Display the public notification email subscription form |
| 263 |
*/ |
| 264 |
function unsubscribeMailList() { |
| 265 |
$templateMgr = &TemplateManager::getManager(); |
| 266 |
$templateMgr->assign('remove', true); |
| 267 |
|
| 268 |
$user = Request::getUser(); |
| 269 |
if(!isset($user)) { |
| 270 |
$userEmail = Request::getUserVar('email'); |
| 271 |
$userPassword = Request::getUserVar('password'); |
| 272 |
|
| 273 |
if($userEmail != '' && $userPassword != '') { |
| 274 |
$notificationSettingsDao = &DAORegistry::getDAO('NotificationSettingsDAO'); |
| 275 |
if($notificationSettingsDao->unsubscribeGuest($userEmail, $userPassword)) { |
| 276 |
$templateMgr->assign('success', "notification.unsubscribeSuccess"); |
| 277 |
$templateMgr->display('notification/maillist.tpl'); |
| 278 |
} else { |
| 279 |
$templateMgr->assign('error', "notification.unsubscribeError"); |
| 280 |
$templateMgr->display('notification/maillist.tpl'); |
| 281 |
} |
| 282 |
} else if($userEmail != '' && $userPassword == '') { |
| 283 |
$notificationSettingsDao = &DAORegistry::getDAO('NotificationSettingsDAO'); |
| 284 |
if($newPassword = $notificationSettingsDao->resetPassword($userEmail)) { |
| 285 |
NotificationHandler::sendMailingListEmail($userEmail, $userPassword, 'NOTIFICATION_MAILLIST_PASSWORD'); |
| 286 |
$templateMgr->assign('success', "notification.reminderSent"); |
| 287 |
$templateMgr->display('notification/maillist.tpl'); |
| 288 |
} else { |
| 289 |
$templateMgr->assign('error', "notification.reminderError"); |
| 290 |
$templateMgr->display('notification/maillist.tpl'); |
| 291 |
} |
| 292 |
} else { |
| 293 |
$templateMgr->assign('remove', true); |
| 294 |
$templateMgr->display('notification/maillist.tpl'); |
| 295 |
} |
| 296 |
} else Request::redirect(null, 'notification'); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Send an email to a mailing list user regarding signup or a lost password |
| 301 |
*/ |
| 302 |
function sendMailingListEmail($email, $password, $template) { |
| 303 |
import('mail.MailTemplate'); |
| 304 |
$journal = Request::getJournal(); |
| 305 |
$site = Request::getSite(); |
| 306 |
|
| 307 |
$mail = new MailTemplate($template); |
| 308 |
$mail->setFrom($site->getSiteContactEmail(), $site->getSiteContactName()); |
| 309 |
$mail->assignParams(array( |
| 310 |
'password' => $password, |
| 311 |
'siteTitle' => $journal->getJournalTitle(), |
| 312 |
'unsubscribeLink' => Request::url(null, 'notification', 'unsubscribeMailList') |
| 313 |
)); |
| 314 |
$mail->addRecipient($email); |
| 315 |
$mail->send(); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
?> |