Guest User

Untitled

a guest
Jan 13th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @file NotificationHandler.inc.php
  5. *
  6. * Copyright (c) 2000-2011 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('classes.handler.Handler');
  16. import('classes.notification.Notification');
  17.  
  18. class NotificationHandler extends Handler {
  19.  
  20. /**
  21. * Display help table of contents.
  22. */
  23. function index() {
  24. $this->validate();
  25. $this->setupTemplate();
  26. $templateMgr =& TemplateManager::getManager();
  27.  
  28. $user = Request::getUser();
  29. if(isset($user)) {
  30. $userId = $user->getId();
  31. $templateMgr->assign('isUserLoggedIn', true);
  32. } else {
  33. $userId = 0;
  34. $templateMgr->assign('emailUrl', PKPRequest::url(NotificationHandler::getContextDepthArray(), 'notification', 'subscribeMailList'));
  35. $templateMgr->assign('isUserLoggedIn', false);
  36. }
  37.  
  38. $rangeInfo =& Handler::getRangeInfo('notifications');
  39. $notificationDao =& DAORegistry::getDAO('NotificationDAO');
  40. $notifications = $notificationDao->getNotificationsByUserId($userId, NOTIFICATION_LEVEL_NORMAL, $rangeInfo);
  41.  
  42. $templateMgr->assign('notifications', $notifications);
  43. $templateMgr->assign('unread', $notificationDao->getUnreadNotificationCount($userId));
  44. $templateMgr->assign('read', $notificationDao->getReadNotificationCount($userId));
  45. $templateMgr->assign('url', PKPRequest::url(NotificationHandler::getContextDepthArray(), 'notification', 'settings'));
  46. $templateMgr->display('notification/index.tpl');
  47. }
  48.  
  49. /**
  50. * Delete a notification
  51. */
  52. function delete($args) {
  53. $this->validate();
  54.  
  55. $notificationId = array_shift($args);
  56. if (array_shift($args) == 'ajax') {
  57. $isAjax = true;
  58. } else $isAjax = false;
  59.  
  60. $user = Request::getUser();
  61. if(isset($user)) {
  62. $userId = $user->getId();
  63. $notificationDao =& DAORegistry::getDAO('NotificationDAO');
  64. $notifications = $notificationDao->deleteNotificationById($notificationId, $userId);
  65. }
  66.  
  67. if (!$isAjax) PKPRequest::redirect(NotificationHandler::getContextDepthArray(), 'notification');
  68. }
  69.  
  70. /**
  71. * View and modify notification settings
  72. */
  73. function settings() {
  74. $this->validate();
  75. $this->setupTemplate();
  76.  
  77.  
  78. $user = Request::getUser();
  79. if(isset($user)) {
  80. import('classes.notification.form.NotificationSettingsForm');
  81. $notificationSettingsForm = new NotificationSettingsForm();
  82. $notificationSettingsForm->display();
  83. } else PKPRequest::redirect(NotificationHandler::getContextDepthArray(), 'notification');
  84. }
  85.  
  86. /**
  87. * Save user notification settings
  88. */
  89. function saveSettings() {
  90. $this->validate();
  91. $this->setupTemplate(true);
  92.  
  93. import('classes.notification.form.NotificationSettingsForm');
  94.  
  95. $notificationSettingsForm = new NotificationSettingsForm();
  96. $notificationSettingsForm->readInputData();
  97.  
  98. if ($notificationSettingsForm->validate()) {
  99. $notificationSettingsForm->execute();
  100. PKPRequest::redirect(NotificationHandler::getContextDepthArray(), 'notification', 'settings');
  101. } else {
  102. $notificationSettingsForm->display();
  103. }
  104. }
  105.  
  106. /**
  107. * Fetch the existing or create a new URL for the user's RSS feed
  108. */
  109. function getNotificationFeedUrl($args) {
  110. $user = Request::getUser();
  111. if(isset($user)) {
  112. $userId = $user->getId();
  113. } else $userId = 0;
  114.  
  115. $notificationSettingsDao =& DAORegistry::getDAO('NotificationSettingsDAO');
  116. $feedType = array_shift($args);
  117.  
  118. $token = $notificationSettingsDao->getRSSTokenByUserId($userId);
  119.  
  120. if ($token) {
  121. PKPRequest::redirect(NotificationHandler::getContextDepthArray(), 'notification', 'notificationFeed', array($feedType, $token));
  122. } else {
  123. $token = $notificationSettingsDao->insertNewRSSToken($userId);
  124. PKPRequest::redirect(NotificationHandler::getContextDepthArray(), 'notification', 'notificationFeed', array($feedType, $token));
  125. }
  126. }
  127.  
  128. /**
  129. * Fetch the actual RSS feed
  130. */
  131. function notificationFeed($args) {
  132. if(isset($args[0]) && isset($args[1])) {
  133. $type = $args[0];
  134. $token = $args[1];
  135. } else return false;
  136.  
  137. $this->setupTemplate(true);
  138.  
  139. $application = PKPApplication::getApplication();
  140. $appName = $application->getNameKey();
  141.  
  142. $site =& Request::getSite();
  143. $siteTitle = $site->getLocalizedTitle();
  144.  
  145. $notificationDao =& DAORegistry::getDAO('NotificationDAO');
  146. $notificationSettingsDao =& DAORegistry::getDAO('NotificationSettingsDAO');
  147.  
  148. $userId = $notificationSettingsDao->getUserIdByRSSToken($token);
  149. $notifications = $notificationDao->getNotificationsByUserId($userId);
  150.  
  151. // Make sure the feed type is specified and valid
  152. $typeMap = array(
  153. 'rss' => 'rss.tpl',
  154. 'rss2' => 'rss2.tpl',
  155. 'atom' => 'atom.tpl'
  156. );
  157. $mimeTypeMap = array(
  158. 'rss' => 'application/rdf+xml',
  159. 'rss2' => 'application/rss+xml',
  160. 'atom' => 'application/atom+xml'
  161. );
  162. if (!isset($typeMap[$type])) return false;
  163.  
  164. $versionDao =& DAORegistry::getDAO('VersionDAO');
  165. $version = $versionDao->getCurrentVersion();
  166.  
  167. $templateMgr =& TemplateManager::getManager();
  168. $templateMgr->assign('version', $version->getVersionString());
  169. $templateMgr->assign('selfUrl', Request::getCompleteUrl());
  170. $templateMgr->assign('locale', Locale::getPrimaryLocale());
  171. $templateMgr->assign('appName', $appName);
  172. $templateMgr->assign('siteTitle', $siteTitle);
  173. $templateMgr->assign_by_ref('notifications', $notifications->toArray());
  174.  
  175. $templateMgr->display(Core::getBaseDir() . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR .
  176. 'pkp' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'notification' . DIRECTORY_SEPARATOR . $typeMap[$type], $mimeTypeMap[$type]);
  177.  
  178. return true;
  179. }
  180.  
  181. /**
  182. * Display the public notification email subscription form
  183. */
  184. function subscribeMailList() {
  185. $this->setupTemplate();
  186.  
  187. $user = Request::getUser();
  188.  
  189. if(!isset($user)) {
  190. import('lib.pkp.classes.notification.form.NotificationMailingListForm');
  191. $notificationMailingListForm = new NotificationMailingListForm();
  192. $notificationMailingListForm->display();
  193. } else PKPRequest::redirect(NotificationHandler::getContextDepthArray(), 'notification');
  194. }
  195.  
  196. /**
  197. * Save the public notification email subscription form
  198. */
  199. function saveSubscribeMailList() {
  200. $this->validate();
  201. $this->setupTemplate(true);
  202.  
  203. import('lib.pkp.classes.notification.form.NotificationMailingListForm');
  204.  
  205. $notificationMailingListForm = new NotificationMailingListForm();
  206. $notificationMailingListForm->readInputData();
  207.  
  208. if ($notificationMailingListForm->validate()) {
  209. $notificationMailingListForm->execute();
  210. PKPRequest::redirect(null, 'notification', 'mailListSubscribed', array('success'));
  211. } else {
  212. $notificationMailingListForm->display();
  213. }
  214. }
  215.  
  216. /**
  217. * Display a success or error message if the user was subscribed
  218. */
  219. function mailListSubscribed($args) {
  220. $this->setupTemplate();
  221. $status = array_shift($args);
  222. $templateMgr =& TemplateManager::getManager();
  223.  
  224. if ($status = 'success') {
  225. $templateMgr->assign('status', 'subscribeSuccess');
  226. } else {
  227. $templateMgr->assign('status', 'subscribeError');
  228. }
  229.  
  230. $templateMgr->display('notification/maillistSubscribed.tpl');
  231. }
  232.  
  233. /**
  234. * Confirm the subscription (accessed via emailed link)
  235. */
  236. function confirmMailListSubscription($args) {
  237. $this->setupTemplate();
  238. $keyHash = array_shift($args);
  239. $email = array_shift($args);
  240.  
  241. $templateMgr =& TemplateManager::getManager();
  242. $templateMgr->assign('confirm', true);
  243.  
  244. $notificationSettingsDao =& DAORegistry::getDAO('NotificationSettingsDAO');
  245. $settingId = $notificationSettingsDao->getMailListSettingId($email);
  246.  
  247. $accessKeyDao =& DAORegistry::getDAO('AccessKeyDAO');
  248. $accessKey = $accessKeyDao->getAccessKeyByKeyHash('MailListContext', $settingId, $keyHash);
  249.  
  250. if($accessKey) {
  251. $notificationSettingsDao->confirmMailListSubscription($settingId);
  252. $templateMgr->assign('status', 'confirmSuccess');
  253. } else {
  254. $templateMgr->assign('status', 'confirmError');
  255. }
  256.  
  257. $templateMgr->display('notification/maillistSubscribed.tpl');
  258. }
  259.  
  260. /**
  261. * Save the maillist unsubscribe form
  262. */
  263. function unsubscribeMailList() {
  264. $this->setupTemplate();
  265. $templateMgr =& TemplateManager::getManager();
  266.  
  267. $userEmail = Request::getUserVar('email');
  268. $userPassword = Request::getUserVar('password');
  269.  
  270. if($userEmail != '' && $userPassword != '') {
  271. $notificationSettingsDao =& DAORegistry::getDAO('NotificationSettingsDAO');
  272. if($notificationSettingsDao->unsubscribeGuest($userEmail, $userPassword)) {
  273. $templateMgr->assign('success', "notification.unsubscribeSuccess");
  274. $templateMgr->display('notification/maillistSettings.tpl');
  275. } else {
  276. $templateMgr->assign('error', "notification.unsubscribeError");
  277. $templateMgr->display('notification/maillistSettings.tpl');
  278. }
  279. } else if($userEmail != '' && $userPassword == '') {
  280. $notificationSettingsDao =& DAORegistry::getDAO('NotificationSettingsDAO');
  281. if($newPassword = $notificationSettingsDao->resetPassword($userEmail)) {
  282. Notification::sendMailingListEmail($userEmail, $newPassword, 'NOTIFICATION_MAILLIST_PASSWORD');
  283. $templateMgr->assign('success', "notification.reminderSent");
  284. $templateMgr->display('notification/maillistSettings.tpl');
  285. } else {
  286. $templateMgr->assign('error', "notification.reminderError");
  287. $templateMgr->display('notification/maillistSettings.tpl');
  288. }
  289. } else {
  290. $templateMgr->assign('remove', true);
  291. $templateMgr->display('notification/maillistSettings.tpl');
  292. }
  293. }
  294.  
  295. /**
  296. * Return an array with null values * the context depth
  297. */
  298. function getContextDepthArray() {
  299. $contextDepthArray = array();
  300.  
  301. $application = PKPApplication::getApplication();
  302. $contextDepth = $application->getContextDepth();
  303.  
  304. for ($i=0; $i < $contextDepth; $i++) {
  305. array_push($contextDepthArray, null);
  306. }
  307.  
  308. return $contextDepthArray;
  309. }
  310. }
  311.  
  312. ?>
Add Comment
Please, Sign In to add comment