first commit
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/notifications/NotificationsGridCellProvider.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class NotificationsGridCellProvider
|
||||
*
|
||||
* @ingroup controllers_grid_notifications
|
||||
*
|
||||
* @brief Class for a cell provider that can retrieve labels from notifications
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\notifications;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\facades\Repo;
|
||||
use APP\notification\Notification;
|
||||
use APP\notification\NotificationManager;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\controllers\grid\GridCellProvider;
|
||||
use PKP\controllers\grid\GridColumn;
|
||||
use PKP\controllers\grid\GridHandler;
|
||||
use PKP\core\PKPString;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxAction;
|
||||
use PKP\payment\QueuedPaymentDAO;
|
||||
use PKP\query\QueryDAO;
|
||||
use PKP\submission\reviewAssignment\ReviewAssignmentDAO;
|
||||
use PKP\submission\reviewRound\ReviewRoundDAO;
|
||||
|
||||
class NotificationsGridCellProvider extends GridCellProvider
|
||||
{
|
||||
/**
|
||||
* Get cell actions associated with this row/column combination
|
||||
*
|
||||
* @param \PKP\controllers\grid\GridRow $row
|
||||
* @param GridColumn $column
|
||||
*
|
||||
* @return array an array of LinkAction instances
|
||||
*/
|
||||
public function getCellActions($request, $row, $column, $position = GridHandler::GRID_ACTION_POSITION_DEFAULT)
|
||||
{
|
||||
assert($column->getId() == 'task');
|
||||
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
|
||||
$notification = $row->getData();
|
||||
$contextDao = Application::getContextDAO();
|
||||
$context = $contextDao->getById($notification->getContextId());
|
||||
|
||||
$notificationMgr = new NotificationManager();
|
||||
$router = $request->getRouter();
|
||||
|
||||
$templateMgr->assign([
|
||||
'notificationMgr' => $notificationMgr,
|
||||
'notification' => $notification,
|
||||
'context' => $context,
|
||||
'notificationObjectTitle' => $this->_getTitle($notification),
|
||||
'message' => PKPString::stripUnsafeHtml($notificationMgr->getNotificationMessage($request, $notification)),
|
||||
]);
|
||||
|
||||
// See if we're working in a multi-context environment
|
||||
$user = $request->getUser();
|
||||
$contextDao = Application::getContextDAO();
|
||||
$contexts = $contextDao->getAvailable($user ? $user->getId() : null)->toArray();
|
||||
$templateMgr->assign('isMultiContext', count($contexts) > 1);
|
||||
|
||||
return [new LinkAction(
|
||||
'details',
|
||||
new AjaxAction($router->url(
|
||||
$request,
|
||||
null,
|
||||
null,
|
||||
'markRead',
|
||||
null,
|
||||
['redirect' => 1, 'selectedElements' => [$notification->getId()]]
|
||||
)),
|
||||
$templateMgr->fetch('controllers/grid/tasks/task.tpl')
|
||||
)];
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Template methods from GridCellProvider
|
||||
//
|
||||
/**
|
||||
* Extracts variables for a given column from a data element
|
||||
* so that they may be assigned to template before rendering.
|
||||
*
|
||||
* @param \PKP\controllers\grid\GridRow $row
|
||||
* @param GridColumn $column
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateVarsFromRowColumn($row, $column)
|
||||
{
|
||||
assert($column->getId() == 'task');
|
||||
|
||||
// The action has the label.
|
||||
return ['label' => ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the title for a notification.
|
||||
*
|
||||
* @param Notification $notification
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function _getTitle($notification)
|
||||
{
|
||||
switch ($notification->getAssocType()) {
|
||||
case Application::ASSOC_TYPE_QUEUED_PAYMENT:
|
||||
$contextDao = Application::getContextDAO();
|
||||
$paymentManager = Application::getPaymentManager($contextDao->getById($notification->getContextId()));
|
||||
$queuedPaymentDao = DAORegistry::getDAO('QueuedPaymentDAO'); /** @var QueuedPaymentDAO $queuedPaymentDao */
|
||||
$queuedPayment = $queuedPaymentDao->getById($notification->getAssocId());
|
||||
if ($queuedPayment) {
|
||||
switch ($queuedPayment->getType()) {
|
||||
case \PKP\payment\PaymentManager::PAYMENT_TYPE_PUBLICATION: // FIXME: This is OJS-only; move out of pkp-lib
|
||||
return Repo::submission()->get($queuedPayment->getAssocId())->getLocalizedTitle();
|
||||
}
|
||||
}
|
||||
assert(false);
|
||||
return '—';
|
||||
case Application::ASSOC_TYPE_ANNOUNCEMENT:
|
||||
$announcementId = $notification->getAssocId();
|
||||
$announcement = Repo::announcement()->get($announcementId);
|
||||
if ($announcement) {
|
||||
return $announcement->getLocalizedTitle();
|
||||
}
|
||||
return null;
|
||||
case Application::ASSOC_TYPE_SUBMISSION:
|
||||
$submissionId = $notification->getAssocId();
|
||||
break;
|
||||
case Application::ASSOC_TYPE_SUBMISSION_FILE:
|
||||
$fileId = $notification->getAssocId();
|
||||
break;
|
||||
case Application::ASSOC_TYPE_REVIEW_ASSIGNMENT:
|
||||
$reviewAssignmentDao = DAORegistry::getDAO('ReviewAssignmentDAO'); /** @var ReviewAssignmentDAO $reviewAssignmentDao */
|
||||
$reviewAssignment = $reviewAssignmentDao->getById($notification->getAssocId());
|
||||
assert($reviewAssignment instanceof \PKP\submission\reviewAssignment\ReviewAssignment);
|
||||
$submissionId = $reviewAssignment->getSubmissionId();
|
||||
break;
|
||||
case Application::ASSOC_TYPE_REVIEW_ROUND:
|
||||
$reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO'); /** @var ReviewRoundDAO $reviewRoundDao */
|
||||
$reviewRound = $reviewRoundDao->getById($notification->getAssocId());
|
||||
assert($reviewRound instanceof \PKP\submission\reviewRound\ReviewRound);
|
||||
$submissionId = $reviewRound->getSubmissionId();
|
||||
break;
|
||||
case Application::ASSOC_TYPE_QUERY:
|
||||
$queryDao = DAORegistry::getDAO('QueryDAO'); /** @var QueryDAO $queryDao */
|
||||
$query = $queryDao->getById($notification->getAssocId());
|
||||
assert($query instanceof \PKP\query\Query);
|
||||
switch ($query->getAssocType()) {
|
||||
case Application::ASSOC_TYPE_SUBMISSION:
|
||||
$submissionId = $query->getAssocId();
|
||||
break;
|
||||
case Application::ASSOC_TYPE_REPRESENTATION:
|
||||
$representationDao = Application::getRepresentationDAO();
|
||||
$representation = $representationDao->getById($query->getAssocId());
|
||||
$publication = Repo::publication()->get($representation->getData('publicationId'));
|
||||
$submissionId = $publication->getData('submissionId');
|
||||
break;
|
||||
default: assert(false);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return '—';
|
||||
}
|
||||
|
||||
if (!isset($submissionId) && isset($fileId)) {
|
||||
assert(is_numeric($fileId));
|
||||
$submissionFile = Repo::submissionFile()->get($fileId);
|
||||
assert($submissionFile instanceof \PKP\submissionFile\SubmissionFile);
|
||||
$submissionId = $submissionFile->getData('submissionId');
|
||||
}
|
||||
assert(is_numeric($submissionId));
|
||||
$submission = Repo::submission()->get($submissionId);
|
||||
assert($submission instanceof \APP\submission\Submission);
|
||||
|
||||
return $submission->getLocalizedTitle();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/notifications/NotificationsGridHandler.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class NotificationsGridHandler
|
||||
*
|
||||
* @ingroup controllers_grid_notifications
|
||||
*
|
||||
* @brief Handle the display of notifications for a given user
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\notifications;
|
||||
|
||||
use APP\notification\Notification;
|
||||
use APP\notification\NotificationManager;
|
||||
use PKP\controllers\grid\feature\PagingFeature;
|
||||
use PKP\controllers\grid\feature\selectableItems\SelectableItemsFeature;
|
||||
use PKP\controllers\grid\GridColumn;
|
||||
use PKP\controllers\grid\GridHandler;
|
||||
use PKP\core\Core;
|
||||
use PKP\core\JSONMessage;
|
||||
use PKP\core\PKPRequest;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\NullAction;
|
||||
use PKP\notification\NotificationDAO;
|
||||
|
||||
class NotificationsGridHandler extends GridHandler
|
||||
{
|
||||
/** @var array $_selectedNotificationIds Set of selected IDs */
|
||||
public $_selectedNotificationIds;
|
||||
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::initialize()
|
||||
*
|
||||
* @param null|mixed $args
|
||||
*/
|
||||
public function initialize($request, $args = null)
|
||||
{
|
||||
parent::initialize($request, $args);
|
||||
|
||||
$this->_selectedNotificationIds = (array) $request->getUserVar('selectedNotificationIds');
|
||||
|
||||
$cellProvider = new NotificationsGridCellProvider();
|
||||
$this->addColumn(
|
||||
new GridColumn(
|
||||
'task',
|
||||
$this->getNotificationsColumnTitle(),
|
||||
null,
|
||||
null,
|
||||
$cellProvider,
|
||||
['anyhtml' => true,
|
||||
'alignment' => GridColumn::COLUMN_ALIGNMENT_LEFT]
|
||||
)
|
||||
);
|
||||
|
||||
// Set the no items row text
|
||||
$this->setEmptyRowText('grid.noItems');
|
||||
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'markNew',
|
||||
new NullAction(),
|
||||
__('grid.action.markNew'),
|
||||
'edit' // FIXME: Icon
|
||||
),
|
||||
GridHandler::GRID_ACTION_POSITION_BELOW
|
||||
);
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'markRead',
|
||||
new NullAction(),
|
||||
__('grid.action.markRead'),
|
||||
'edit' // FIXME: Icon
|
||||
),
|
||||
GridHandler::GRID_ACTION_POSITION_BELOW
|
||||
);
|
||||
|
||||
$router = $request->getRouter();
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'deleteNotification',
|
||||
new NullAction(),
|
||||
__('grid.action.delete'),
|
||||
'delete'
|
||||
),
|
||||
GridHandler::GRID_ACTION_POSITION_BELOW
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Overridden methods from GridHandler
|
||||
//
|
||||
/**
|
||||
* @see GridHandler::getJSHandler()
|
||||
*/
|
||||
public function getJSHandler()
|
||||
{
|
||||
return '$.pkp.controllers.grid.notifications.NotificationsGridHandler';
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GridHandler::setUrls()
|
||||
*/
|
||||
public function setUrls($request, $extraUrls = [])
|
||||
{
|
||||
$router = $request->getRouter();
|
||||
parent::setUrls(
|
||||
$request,
|
||||
array_merge(
|
||||
$extraUrls,
|
||||
[
|
||||
'markNewUrl' => $router->url($request, null, null, 'markNew', null, $this->getRequestArgs()),
|
||||
'markReadUrl' => $router->url($request, null, null, 'markRead', null, $this->getRequestArgs()),
|
||||
'deleteUrl' => $router->url($request, null, null, 'deleteNotifications', null, $this->getRequestArgs()),
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of "publish data changed" events.
|
||||
* Used to update the site context switcher upon create/delete.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPublishChangeEvents()
|
||||
{
|
||||
return ['updateUnreadNotificationsCount'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::initFeatures()
|
||||
*/
|
||||
public function initFeatures($request, $args)
|
||||
{
|
||||
return [new SelectableItemsFeature(), new PagingFeature()];
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::getSelectName()
|
||||
*/
|
||||
public function getSelectName()
|
||||
{
|
||||
return 'selectedNotifications';
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::isDataElementSelected()
|
||||
*/
|
||||
public function isDataElementSelected($gridDataElement)
|
||||
{
|
||||
return in_array($gridDataElement->getId(), $this->_selectedNotificationIds);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Protected methods.
|
||||
//
|
||||
/**
|
||||
* Get the notifications column title.
|
||||
*
|
||||
* @return string Locale key.
|
||||
*/
|
||||
protected function getNotificationsColumnTitle()
|
||||
{
|
||||
return 'common.tasks';
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Public methods
|
||||
//
|
||||
/**
|
||||
* Mark notifications unread
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function markNew($args, $request)
|
||||
{
|
||||
if (!$request->checkCSRF()) {
|
||||
return new JSONMessage(false);
|
||||
}
|
||||
$notificationDao = DAORegistry::getDAO('NotificationDAO'); /** @var NotificationDAO $notificationDao */
|
||||
$user = $request->getUser();
|
||||
|
||||
$selectedElements = (array) $request->getUserVar('selectedElements');
|
||||
foreach ($selectedElements as $notificationId) {
|
||||
if ($notificationDao->getById($notificationId, $user->getId())) {
|
||||
$notificationDao->setDateRead($notificationId, null);
|
||||
}
|
||||
}
|
||||
|
||||
$json = \PKP\db\DAO::getDataChangedEvent(null, null, $selectedElements);
|
||||
$json->setGlobalEvent('update:unread-tasks-count', ['count' => $this->getUnreadNotificationsCount($user)]);
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark notifications unread
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function markRead($args, $request)
|
||||
{
|
||||
if (!$request->checkCSRF()) {
|
||||
return new JSONMessage(false);
|
||||
}
|
||||
$notificationDao = DAORegistry::getDAO('NotificationDAO'); /** @var NotificationDAO $notificationDao */
|
||||
$user = $request->getUser();
|
||||
|
||||
$selectedElements = (array) $request->getUserVar('selectedElements');
|
||||
foreach ($selectedElements as $notificationId) {
|
||||
if ($notification = $notificationDao->getById($notificationId, $user->getId())) {
|
||||
$notificationDao->setDateRead($notificationId, Core::getCurrentDate());
|
||||
}
|
||||
}
|
||||
if ($request->getUserVar('redirect')) {
|
||||
// In this case, the user has clicked on a notification
|
||||
// and wants to view it. Mark it read first and redirect
|
||||
$notificationMgr = new NotificationManager();
|
||||
return $request->redirectUrlJson($notificationMgr->getNotificationUrl($request, $notification));
|
||||
} else {
|
||||
// The notification has been marked read explicitly.
|
||||
// Update its status in the grid.
|
||||
$json = \PKP\db\DAO::getDataChangedEvent(null, null, $selectedElements);
|
||||
$json->setGlobalEvent('update:unread-tasks-count', ['count' => $this->getUnreadNotificationsCount($user)]);
|
||||
return $json;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete notifications
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function deleteNotifications($args, $request)
|
||||
{
|
||||
if (!$request->checkCSRF()) {
|
||||
return new JSONMessage(false);
|
||||
}
|
||||
$notificationDao = DAORegistry::getDAO('NotificationDAO'); /** @var NotificationDAO $notificationDao */
|
||||
$user = $request->getUser();
|
||||
|
||||
$selectedElements = (array) $request->getUserVar('selectedElements');
|
||||
foreach ($selectedElements as $notificationId) {
|
||||
if ($notification = $notificationDao->getById($notificationId, $user->getId())) {
|
||||
$notificationDao->deleteObject($notification);
|
||||
}
|
||||
}
|
||||
$json = \PKP\db\DAO::getDataChangedEvent(null, null, $selectedElements);
|
||||
$json->setGlobalEvent('update:unread-tasks-count', ['count' => $this->getUnreadNotificationsCount($user)]);
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unread notifications count
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getUnreadNotificationsCount($user)
|
||||
{
|
||||
$notificationDao = DAORegistry::getDAO('NotificationDAO'); /** @var NotificationDAO $notificationDao */
|
||||
return (int) $notificationDao->getNotificationCount(false, $user->getId(), null, Notification::NOTIFICATION_LEVEL_TASK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/notifications/TaskNotificationsGridHandler.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class TaskNotificationsGridHandler
|
||||
*
|
||||
* @ingroup controllers_grid_notifications
|
||||
*
|
||||
* @brief Handle the display of task notifications for a given user
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\notifications;
|
||||
|
||||
use APP\notification\Notification;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\notification\NotificationDAO;
|
||||
|
||||
class TaskNotificationsGridHandler extends NotificationsGridHandler
|
||||
{
|
||||
/**
|
||||
* @copydoc GridHandler::initialize()
|
||||
*
|
||||
* @param null|mixed $args
|
||||
*/
|
||||
public function initialize($request, $args = null)
|
||||
{
|
||||
parent::initialize($request, $args);
|
||||
|
||||
// Basic grid configuration.
|
||||
$this->setTitle('common.tasks');
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GridHandler::loadData()
|
||||
*
|
||||
* @return array Grid data.
|
||||
*/
|
||||
protected function loadData($request, $filter)
|
||||
{
|
||||
$user = $request->getUser();
|
||||
|
||||
// Get all level task notifications.
|
||||
$notificationDao = DAORegistry::getDAO('NotificationDAO'); /** @var NotificationDAO $notificationDao */
|
||||
$notifications = $notificationDao->getByUserId($user->getId(), Notification::NOTIFICATION_LEVEL_TASK)->toArray();
|
||||
|
||||
// Checkbox selection requires the array keys match the notification id
|
||||
$notificationsForRow = [];
|
||||
foreach ($notifications as $notification) {
|
||||
$notificationsForRow[$notification->getId()] = $notification;
|
||||
}
|
||||
return $notificationsForRow;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user