first commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/announcements/AnnouncementTypeGridCellProvider.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 AnnouncementTypeGridCellProvider
|
||||
*
|
||||
* @ingroup controllers_grid_announcements
|
||||
*
|
||||
* @brief Cell provider for title column of an announcement type grid.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\announcements;
|
||||
|
||||
use PKP\controllers\grid\GridCellProvider;
|
||||
use PKP\controllers\grid\GridColumn;
|
||||
use PKP\controllers\grid\GridHandler;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxModal;
|
||||
|
||||
class AnnouncementTypeGridCellProvider extends GridCellProvider
|
||||
{
|
||||
/**
|
||||
* @copydoc GridCellProvider::getCellActions()
|
||||
*/
|
||||
public function getCellActions($request, $row, $column, $position = GridHandler::GRID_ACTION_POSITION_DEFAULT)
|
||||
{
|
||||
switch ($column->getId()) {
|
||||
case 'name':
|
||||
$announcementType = $row->getData();
|
||||
$router = $request->getRouter();
|
||||
$actionArgs = ['announcementTypeId' => $row->getId()];
|
||||
|
||||
return [new LinkAction(
|
||||
'edit',
|
||||
new AjaxModal(
|
||||
$router->url($request, null, null, 'editAnnouncementType', null, $actionArgs),
|
||||
__('grid.action.edit'),
|
||||
null,
|
||||
true
|
||||
),
|
||||
htmlspecialchars($announcementType->getLocalizedTypeName())
|
||||
)];
|
||||
}
|
||||
return parent::getCellActions($request, $row, $column, $position);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$announcementType = $row->getData();
|
||||
$columnId = $column->getId();
|
||||
assert($announcementType instanceof \PKP\announcement\AnnouncementType && !empty($columnId));
|
||||
|
||||
switch ($columnId) {
|
||||
case 'title':
|
||||
return ['label' => $announcementType->getLocalizedTypeName()];
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return parent::getTemplateVarsFromRowColumn($row, $column);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/announcements/AnnouncementTypeGridHandler.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 AnnouncementTypeGridHandler
|
||||
*
|
||||
* @ingroup controllers_grid_announcements
|
||||
*
|
||||
* @brief Handle announcement type grid requests.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\announcements;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\notification\NotificationManager;
|
||||
use PKP\announcement\AnnouncementTypeDAO;
|
||||
use PKP\controllers\grid\announcements\form\AnnouncementTypeForm;
|
||||
use PKP\controllers\grid\GridColumn;
|
||||
use PKP\controllers\grid\GridHandler;
|
||||
use PKP\core\JSONMessage;
|
||||
use PKP\core\PKPRequest;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxModal;
|
||||
use PKP\notification\PKPNotification;
|
||||
use PKP\security\authorization\ContextAccessPolicy;
|
||||
use PKP\security\authorization\PKPSiteAccessPolicy;
|
||||
use PKP\security\authorization\UserRolesRequiredPolicy;
|
||||
use PKP\security\Role;
|
||||
|
||||
class AnnouncementTypeGridHandler extends GridHandler
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->addRoleAssignment(
|
||||
[Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN],
|
||||
[
|
||||
'fetchGrid', 'fetchRow',
|
||||
'addAnnouncementType', 'editAnnouncementType',
|
||||
'updateAnnouncementType',
|
||||
'deleteAnnouncementType'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Overridden template methods
|
||||
//
|
||||
/**
|
||||
* @copydoc GridHandler::authorize()
|
||||
*/
|
||||
public function authorize($request, &$args, $roleAssignments)
|
||||
{
|
||||
$context = $request->getContext();
|
||||
|
||||
if ($context) {
|
||||
$this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
|
||||
} else {
|
||||
$this->addPolicy(new PKPSiteAccessPolicy($request, null, $roleAssignments));
|
||||
}
|
||||
|
||||
$announcementTypeId = $request->getUserVar('announcementTypeId');
|
||||
if ($announcementTypeId) {
|
||||
// Ensure announcement type is valid and for this context
|
||||
$announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO'); /** @var AnnouncementTypeDAO $announcementTypeDao */
|
||||
$announcementType = $announcementTypeDao->getById($announcementTypeId);
|
||||
if (!$announcementType || $announcementType->getContextId() != $context?->getId()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return parent::authorize($request, $args, $roleAssignments);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::initialize()
|
||||
*
|
||||
* @param null|mixed $args
|
||||
*/
|
||||
public function initialize($request, $args = null)
|
||||
{
|
||||
parent::initialize($request, $args);
|
||||
|
||||
// Basic grid configuration
|
||||
$this->setTitle('manager.announcementTypes');
|
||||
|
||||
// Set the no items row text
|
||||
$this->setEmptyRowText('manager.announcementTypes.noneCreated');
|
||||
|
||||
// Columns
|
||||
$announcementTypeCellProvider = new AnnouncementTypeGridCellProvider();
|
||||
$this->addColumn(
|
||||
new GridColumn(
|
||||
'name',
|
||||
'common.name',
|
||||
null,
|
||||
null,
|
||||
$announcementTypeCellProvider,
|
||||
['width' => 60]
|
||||
)
|
||||
);
|
||||
|
||||
// Add grid action.
|
||||
$router = $request->getRouter();
|
||||
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'addAnnouncementType',
|
||||
new AjaxModal(
|
||||
$router->url($request, null, null, 'addAnnouncementType', null, null),
|
||||
__('grid.action.addAnnouncementType'),
|
||||
'modal_add_item',
|
||||
true
|
||||
),
|
||||
__('grid.action.addAnnouncementType'),
|
||||
'add_item'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::loadData()
|
||||
*/
|
||||
protected function loadData($request, $filter)
|
||||
{
|
||||
$announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO'); /** @var AnnouncementTypeDAO $announcementTypeDao */
|
||||
return $announcementTypeDao->getByContextId($request->getContext()?->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::getRowInstance()
|
||||
*/
|
||||
protected function getRowInstance()
|
||||
{
|
||||
return new AnnouncementTypeGridRow();
|
||||
}
|
||||
|
||||
//
|
||||
// Public grid actions.
|
||||
//
|
||||
/**
|
||||
* Display form to add announcement type.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage
|
||||
*/
|
||||
public function addAnnouncementType($args, $request)
|
||||
{
|
||||
return $this->editAnnouncementType($args, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display form to edit an announcement type.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function editAnnouncementType($args, $request)
|
||||
{
|
||||
$announcementTypeId = (int)$request->getUserVar('announcementTypeId');
|
||||
$announcementTypeForm = new AnnouncementTypeForm($request->getContext()?->getId(), $announcementTypeId);
|
||||
$announcementTypeForm->initData();
|
||||
|
||||
return new JSONMessage(true, $announcementTypeForm->fetch($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Save an edited/inserted announcement type.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function updateAnnouncementType($args, $request)
|
||||
{
|
||||
// Identify the announcement type id.
|
||||
$announcementTypeId = $request->getUserVar('announcementTypeId');
|
||||
|
||||
// Form handling.
|
||||
$announcementTypeForm = new AnnouncementTypeForm($request->getContext()?->getId(), $announcementTypeId);
|
||||
$announcementTypeForm->readInputData();
|
||||
|
||||
if ($announcementTypeForm->validate()) {
|
||||
$announcementTypeForm->execute();
|
||||
|
||||
if ($announcementTypeId) {
|
||||
// Successful edit of an existing announcement type.
|
||||
$notificationLocaleKey = 'notification.editedAnnouncementType';
|
||||
} else {
|
||||
// Successful added a new announcement type.
|
||||
$notificationLocaleKey = 'notification.addedAnnouncementType';
|
||||
}
|
||||
|
||||
// Record the notification to user.
|
||||
$notificationManager = new NotificationManager();
|
||||
$user = $request->getUser();
|
||||
$notificationManager->createTrivialNotification($user->getId(), PKPNotification::NOTIFICATION_TYPE_SUCCESS, ['contents' => __($notificationLocaleKey)]);
|
||||
|
||||
// Prepare the grid row data.
|
||||
return \PKP\db\DAO::getDataChangedEvent($announcementTypeId);
|
||||
} else {
|
||||
return new JSONMessage(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an announcement type.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage JSON object
|
||||
*/
|
||||
public function deleteAnnouncementType($args, $request)
|
||||
{
|
||||
$announcementTypeId = (int) $request->getUserVar('announcementTypeId');
|
||||
|
||||
$announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO'); /** @var AnnouncementTypeDAO $announcementTypeDao */
|
||||
$announcementType = $announcementTypeDao->getById($announcementTypeId, $request->getContext()?->getId());
|
||||
if ($announcementType && $request->checkCSRF()) {
|
||||
$announcementTypeDao->deleteObject($announcementType);
|
||||
|
||||
// Create notification.
|
||||
$notificationManager = new NotificationManager();
|
||||
$user = $request->getUser();
|
||||
$notificationManager->createTrivialNotification($user->getId(), PKPNotification::NOTIFICATION_TYPE_SUCCESS, ['contents' => __('notification.removedAnnouncementType')]);
|
||||
|
||||
return \PKP\db\DAO::getDataChangedEvent();
|
||||
}
|
||||
|
||||
return new JSONMessage(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/announcements/AnnouncementTypeGridRow.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 AnnouncementTypeGridRow
|
||||
*
|
||||
* @ingroup controllers_grid_content_announcements
|
||||
*
|
||||
* @brief Announcement type grid row definition
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\announcements;
|
||||
|
||||
use PKP\controllers\grid\GridRow;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxModal;
|
||||
use PKP\linkAction\request\RemoteActionConfirmationModal;
|
||||
|
||||
class AnnouncementTypeGridRow extends GridRow
|
||||
{
|
||||
//
|
||||
// Overridden methods from GridRow
|
||||
//
|
||||
/**
|
||||
* @copydoc GridRow::initialize()
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function initialize($request, $template = null)
|
||||
{
|
||||
parent::initialize($request, $template);
|
||||
|
||||
// Is this a new row or an existing row?
|
||||
$element = $this->getData();
|
||||
assert($element instanceof \PKP\announcement\AnnouncementType);
|
||||
|
||||
$rowId = $this->getId();
|
||||
|
||||
if (!empty($rowId) && is_numeric($rowId)) {
|
||||
// Only add row actions if this is an existing row
|
||||
$router = $request->getRouter();
|
||||
$actionArgs = [
|
||||
'announcementTypeId' => $rowId
|
||||
];
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'edit',
|
||||
new AjaxModal(
|
||||
$router->url($request, null, null, 'editAnnouncementType', null, $actionArgs),
|
||||
__('grid.action.edit'),
|
||||
'modal_edit',
|
||||
true
|
||||
),
|
||||
__('grid.action.edit'),
|
||||
'edit'
|
||||
)
|
||||
);
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'remove',
|
||||
new RemoteActionConfirmationModal(
|
||||
$request->getSession(),
|
||||
__('common.confirmDelete'),
|
||||
__('common.remove'),
|
||||
$router->url($request, null, null, 'deleteAnnouncementType', null, $actionArgs),
|
||||
'modal_delete'
|
||||
),
|
||||
__('grid.action.remove'),
|
||||
'delete'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/announcements/form/AnnouncementTypeForm.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 AnnouncementTypeForm
|
||||
*
|
||||
* @ingroup controllers_grid_announcements_form
|
||||
*
|
||||
* @see AnnouncementType
|
||||
*
|
||||
* @brief Form for manager to create/edit announcement types.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\announcements\form;
|
||||
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\announcement\AnnouncementTypeDAO;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\form\Form;
|
||||
|
||||
class AnnouncementTypeForm extends Form
|
||||
{
|
||||
/** @var ?int Context ID or null for site announcement */
|
||||
public $contextId;
|
||||
|
||||
/** @var int The ID of the announcement type being edited */
|
||||
public $typeId;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param ?int $contextId Context ID or null for site announcement
|
||||
* @param int $typeId leave as default for new announcement type
|
||||
*/
|
||||
public function __construct($contextId, $typeId = null)
|
||||
{
|
||||
$this->typeId = isset($typeId) ? (int) $typeId : null;
|
||||
$this->contextId = $contextId;
|
||||
|
||||
parent::__construct('manager/announcement/announcementTypeForm.tpl');
|
||||
|
||||
// Type name is provided
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorLocale($this, 'name', 'required', 'manager.announcementTypes.form.typeNameRequired'));
|
||||
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of localized field names for this form
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getLocaleFieldNames()
|
||||
{
|
||||
$announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO'); /** @var AnnouncementTypeDAO $announcementTypeDao */
|
||||
return $announcementTypeDao->getLocaleFieldNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::fetch()
|
||||
*/
|
||||
public function fetch($request, $template = 'controllers/grid/announcements/form/announcementTypeForm.tpl', $display = false)
|
||||
{
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
$templateMgr->assign('typeId', $this->typeId);
|
||||
return parent::fetch($request, $template, $display);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current announcement type.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
if (isset($this->typeId)) {
|
||||
$announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO'); /** @var AnnouncementTypeDAO $announcementTypeDao */
|
||||
$announcementType = $announcementTypeDao->getById($this->typeId);
|
||||
|
||||
if ($announcementType != null) {
|
||||
$this->_data = [
|
||||
'name' => $announcementType->getName(null) // Localized
|
||||
];
|
||||
} else {
|
||||
$this->typeId = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute()
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO'); /** @var AnnouncementTypeDAO $announcementTypeDao */
|
||||
|
||||
if (isset($this->typeId)) {
|
||||
$announcementType = $announcementTypeDao->getById($this->typeId);
|
||||
}
|
||||
|
||||
if (!isset($announcementType)) {
|
||||
$announcementType = $announcementTypeDao->newDataObject();
|
||||
}
|
||||
|
||||
$announcementType->setContextId($this->contextId);
|
||||
$announcementType->setName($this->getData('name'), null); // Localized
|
||||
|
||||
// Update or insert announcement type
|
||||
if ($announcementType->getId() != null) {
|
||||
$announcementTypeDao->updateObject($announcementType);
|
||||
} else {
|
||||
$announcementTypeDao->insertObject($announcementType);
|
||||
}
|
||||
parent::execute(...$functionArgs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user