first commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/StaticPageGridCellProvider.php
|
||||
*
|
||||
* Copyright (c) 2014-2020 Simon Fraser University
|
||||
* Copyright (c) 2000-2020 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class StaticPageGridCellProvider
|
||||
*
|
||||
* @ingroup controllers_grid_staticPages
|
||||
*
|
||||
* @brief Class for a cell provider to display information about static pages
|
||||
*/
|
||||
|
||||
namespace APP\plugins\generic\staticPages\controllers\grid;
|
||||
|
||||
use PKP\controllers\grid\GridCellProvider;
|
||||
use PKP\controllers\grid\GridColumn;
|
||||
use PKP\controllers\grid\GridHandler;
|
||||
use PKP\core\PKPApplication;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\RedirectAction;
|
||||
|
||||
class StaticPageGridCellProvider extends GridCellProvider
|
||||
{
|
||||
//
|
||||
// Template methods from GridCellProvider
|
||||
//
|
||||
/**
|
||||
* Get cell actions associated with this row/column combination
|
||||
*
|
||||
* @param \PKP\controllers\grid\GridRow $row
|
||||
* @param GridColumn $column
|
||||
* @param int $position GRID_ACTION_POSITION_...
|
||||
*
|
||||
* @return array an array of LinkAction instances
|
||||
*/
|
||||
public function getCellActions($request, $row, $column, $position = GridHandler::GRID_ACTION_POSITION_DEFAULT)
|
||||
{
|
||||
$staticPage = $row->getData();
|
||||
|
||||
switch ($column->getId()) {
|
||||
case 'path':
|
||||
$dispatcher = $request->getDispatcher();
|
||||
return [new LinkAction(
|
||||
'details',
|
||||
new RedirectAction(
|
||||
$dispatcher->url($request, PKPApplication::ROUTE_PAGE, null) . '/' . $staticPage->getPath(),
|
||||
'staticPage'
|
||||
),
|
||||
htmlspecialchars($staticPage->getPath())
|
||||
)];
|
||||
default:
|
||||
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)
|
||||
{
|
||||
$staticPage = $row->getData();
|
||||
|
||||
switch ($column->getId()) {
|
||||
case 'path':
|
||||
// The action has the label
|
||||
return ['label' => ''];
|
||||
case 'title':
|
||||
return ['label' => $staticPage->getLocalizedTitle()];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/StaticPageGridHandler.php
|
||||
*
|
||||
* Copyright (c) 2014-2020 Simon Fraser University
|
||||
* Copyright (c) 2003-2020 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class StaticPageGridHandler
|
||||
*
|
||||
* @ingroup controllers_grid_staticPages
|
||||
*
|
||||
* @brief Handle static pages grid requests.
|
||||
*/
|
||||
|
||||
namespace APP\plugins\generic\staticPages\controllers\grid;
|
||||
|
||||
use APP\plugins\generic\staticPages\classes\StaticPagesDAO;
|
||||
use APP\plugins\generic\staticPages\controllers\grid\form\StaticPageForm;
|
||||
use APP\plugins\generic\staticPages\StaticPagesPlugin;
|
||||
use PKP\controllers\grid\GridColumn;
|
||||
use PKP\controllers\grid\GridHandler;
|
||||
use PKP\core\JSONMessage;
|
||||
use PKP\core\PKPRequest;
|
||||
use PKP\db\DAO;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\form\Form;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxModal;
|
||||
use PKP\security\authorization\ContextAccessPolicy;
|
||||
use PKP\security\Role;
|
||||
|
||||
class StaticPageGridHandler extends GridHandler
|
||||
{
|
||||
/** @var StaticPagesPlugin The static pages plugin */
|
||||
public $plugin;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(StaticPagesPlugin $plugin)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->addRoleAssignment(
|
||||
[Role::ROLE_ID_MANAGER],
|
||||
['index', 'fetchGrid', 'fetchRow', 'addStaticPage', 'editStaticPage', 'updateStaticPage', 'delete']
|
||||
);
|
||||
$this->plugin = $plugin;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Overridden template methods
|
||||
//
|
||||
/**
|
||||
* @copydoc PKPHandler::authorize()
|
||||
*/
|
||||
public function authorize($request, &$args, $roleAssignments)
|
||||
{
|
||||
$this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
|
||||
return parent::authorize($request, $args, $roleAssignments);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridHandler::initialize()
|
||||
*
|
||||
* @param null|mixed $args
|
||||
*/
|
||||
public function initialize($request, $args = null)
|
||||
{
|
||||
parent::initialize($request, $args);
|
||||
$context = $request->getContext();
|
||||
|
||||
// Set the grid details.
|
||||
$this->setTitle('plugins.generic.staticPages.staticPages');
|
||||
$this->setEmptyRowText('plugins.generic.staticPages.noneCreated');
|
||||
|
||||
// Get the pages and add the data to the grid
|
||||
/** @var StaticPagesDAO */
|
||||
$staticPagesDao = DAORegistry::getDAO('StaticPagesDAO');
|
||||
$this->setGridDataElements($staticPagesDao->getByContextId($context->getId()));
|
||||
|
||||
// Add grid-level actions
|
||||
$router = $request->getRouter();
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'addStaticPage',
|
||||
new AjaxModal(
|
||||
$router->url($request, null, null, 'addStaticPage'),
|
||||
__('plugins.generic.staticPages.addStaticPage'),
|
||||
'modal_add_item'
|
||||
),
|
||||
__('plugins.generic.staticPages.addStaticPage'),
|
||||
'add_item'
|
||||
)
|
||||
);
|
||||
|
||||
// Columns
|
||||
$cellProvider = new StaticPageGridCellProvider();
|
||||
$this->addColumn(new GridColumn(
|
||||
'title',
|
||||
'plugins.generic.staticPages.pageTitle',
|
||||
null,
|
||||
'controllers/grid/gridCell.tpl', // Default null not supported in OMP 1.1
|
||||
$cellProvider
|
||||
));
|
||||
$this->addColumn(new GridColumn(
|
||||
'path',
|
||||
'plugins.generic.staticPages.path',
|
||||
null,
|
||||
'controllers/grid/gridCell.tpl', // Default null not supported in OMP 1.1
|
||||
$cellProvider
|
||||
));
|
||||
}
|
||||
|
||||
//
|
||||
// Overridden methods from GridHandler
|
||||
//
|
||||
/**
|
||||
* @copydoc GridHandler::getRowInstance()
|
||||
*/
|
||||
public function getRowInstance()
|
||||
{
|
||||
return new StaticPageGridRow();
|
||||
}
|
||||
|
||||
//
|
||||
// Public Grid Actions
|
||||
//
|
||||
/**
|
||||
* Display the grid's containing page.
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
* @return JSONMessage
|
||||
*/
|
||||
public function index($args, $request)
|
||||
{
|
||||
$form = new Form($this->plugin->getTemplateResource('staticPages.tpl'));
|
||||
return new JSONMessage(true, $form->fetch($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* An action to add a new custom static page
|
||||
*
|
||||
* @param array $args Arguments to the request
|
||||
* @param PKPRequest $request Request object
|
||||
*/
|
||||
public function addStaticPage($args, $request)
|
||||
{
|
||||
// Calling editStaticPage with an empty ID will add
|
||||
// a new static page.
|
||||
return $this->editStaticPage($args, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* An action to edit a static page
|
||||
*
|
||||
* @param array $args Arguments to the request
|
||||
* @param PKPRequest $request Request object
|
||||
*
|
||||
* @return JSONMessage Serialized JSON object
|
||||
*/
|
||||
public function editStaticPage($args, $request)
|
||||
{
|
||||
$staticPageId = $request->getUserVar('staticPageId');
|
||||
$context = $request->getContext();
|
||||
$this->setupTemplate($request);
|
||||
|
||||
// Create and present the edit form
|
||||
$staticPageForm = new StaticPageForm($this->plugin, $context->getId(), $staticPageId);
|
||||
$staticPageForm->initData();
|
||||
return new JSONMessage(true, $staticPageForm->fetch($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a custom block
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage Serialized JSON object
|
||||
*/
|
||||
public function updateStaticPage($args, $request)
|
||||
{
|
||||
$staticPageId = $request->getUserVar('staticPageId');
|
||||
$context = $request->getContext();
|
||||
$this->setupTemplate($request);
|
||||
|
||||
// Create and populate the form
|
||||
$staticPageForm = new StaticPageForm($this->plugin, $context->getId(), $staticPageId);
|
||||
$staticPageForm->readInputData();
|
||||
|
||||
// Check the results
|
||||
if ($staticPageForm->validate()) {
|
||||
// Save the results
|
||||
$staticPageForm->execute();
|
||||
return DAO::getDataChangedEvent();
|
||||
}
|
||||
// Present any errors
|
||||
return new JSONMessage(true, $staticPageForm->fetch($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a static page
|
||||
*
|
||||
* @param array $args
|
||||
* @param PKPRequest $request
|
||||
*
|
||||
* @return JSONMessage Serialized JSON object
|
||||
*/
|
||||
public function delete($args, $request)
|
||||
{
|
||||
if (!$request->checkCSRF()) return new JSONMessage(false);
|
||||
|
||||
$staticPageId = $request->getUserVar('staticPageId');
|
||||
$context = $request->getContext();
|
||||
|
||||
// Delete the static page
|
||||
/** @var StaticPagesDAO */
|
||||
$staticPagesDao = DAORegistry::getDAO('StaticPagesDAO');
|
||||
$staticPage = $staticPagesDao->getById($staticPageId, $context->getId());
|
||||
$staticPagesDao->deleteObject($staticPage);
|
||||
|
||||
return DAO::getDataChangedEvent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/StaticPageGridRow.php
|
||||
*
|
||||
* Copyright (c) 2014-2020 Simon Fraser University
|
||||
* Copyright (c) 2003-2020 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class StaticPageGridRow
|
||||
*
|
||||
* @ingroup controllers_grid_staticPages
|
||||
*
|
||||
* @brief Handle custom blocks grid row requests.
|
||||
*/
|
||||
|
||||
namespace APP\plugins\generic\staticPages\controllers\grid;
|
||||
|
||||
use PKP\controllers\grid\GridRow;
|
||||
use PKP\linkAction\LinkAction;
|
||||
use PKP\linkAction\request\AjaxModal;
|
||||
use PKP\linkAction\request\RemoteActionConfirmationModal;
|
||||
|
||||
class StaticPageGridRow extends GridRow
|
||||
{
|
||||
//
|
||||
// Overridden template methods
|
||||
//
|
||||
/**
|
||||
* @copydoc GridRow::initialize()
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function initialize($request, $template = null)
|
||||
{
|
||||
parent::initialize($request, $template);
|
||||
|
||||
$staticPageId = $this->getId();
|
||||
if (!empty($staticPageId)) {
|
||||
$router = $request->getRouter();
|
||||
|
||||
// Create the "edit static page" action
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'editStaticPage',
|
||||
new AjaxModal(
|
||||
$router->url($request, null, null, 'editStaticPage', null, ['staticPageId' => $staticPageId]),
|
||||
__('grid.action.edit'),
|
||||
'modal_edit',
|
||||
true
|
||||
),
|
||||
__('grid.action.edit'),
|
||||
'edit'
|
||||
)
|
||||
);
|
||||
|
||||
// Create the "delete static page" action
|
||||
$this->addAction(
|
||||
new LinkAction(
|
||||
'delete',
|
||||
new RemoteActionConfirmationModal(
|
||||
$request->getSession(),
|
||||
__('common.confirmDelete'),
|
||||
__('grid.action.delete'),
|
||||
$router->url($request, null, null, 'delete', null, ['staticPageId' => $staticPageId]),
|
||||
'modal_delete'
|
||||
),
|
||||
__('grid.action.delete'),
|
||||
'delete'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/form/StaticPageForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2020 Simon Fraser University
|
||||
* Copyright (c) 2003-2020 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class StaticPageForm
|
||||
*
|
||||
* @ingroup controllers_grid_staticPages
|
||||
*
|
||||
* @brief Form for press managers to create and modify sidebar blocks
|
||||
*/
|
||||
|
||||
namespace APP\plugins\generic\staticPages\controllers\grid\form;
|
||||
|
||||
use APP\plugins\generic\staticPages\classes\StaticPagesDAO;
|
||||
use APP\plugins\generic\staticPages\StaticPagesPlugin;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\db\DAORegistry;
|
||||
|
||||
class StaticPageForm extends \PKP\form\Form
|
||||
{
|
||||
/** @var int Context (press / journal) ID */
|
||||
public $contextId;
|
||||
|
||||
/** @var string Static page name */
|
||||
public $staticPageId;
|
||||
|
||||
/** @var StaticPagesPlugin Static pages plugin */
|
||||
public $plugin;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param StaticPagesPlugin $staticPagesPlugin The static page plugin
|
||||
* @param int $contextId Context ID
|
||||
* @param int $staticPageId Static page ID (if any)
|
||||
*/
|
||||
public function __construct($staticPagesPlugin, $contextId, $staticPageId = null)
|
||||
{
|
||||
parent::__construct($staticPagesPlugin->getTemplateResource('editStaticPageForm.tpl'));
|
||||
|
||||
$this->contextId = $contextId;
|
||||
$this->staticPageId = $staticPageId;
|
||||
$this->plugin = $staticPagesPlugin;
|
||||
|
||||
// Add form checks
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'title', 'required', 'plugins.generic.staticPages.nameRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorRegExp($this, 'path', 'required', 'plugins.generic.staticPages.pathRegEx', '/^[a-zA-Z0-9\/._-]+$/'));
|
||||
$form = $this;
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'path', 'required', 'plugins.generic.staticPages.duplicatePath', function ($path) use ($form) {
|
||||
/** @var StaticPagesDAO */
|
||||
$staticPagesDao = DAORegistry::getDAO('StaticPagesDAO');
|
||||
$page = $staticPagesDao->getByPath($form->contextId, $path);
|
||||
return !$page || $page->getId() == $form->staticPageId;
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current group group.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
$templateMgr = TemplateManager::getManager();
|
||||
if ($this->staticPageId) {
|
||||
/** @var StaticPagesDAO */
|
||||
$staticPagesDao = DAORegistry::getDAO('StaticPagesDAO');
|
||||
$staticPage = $staticPagesDao->getById($this->staticPageId, $this->contextId);
|
||||
$this->setData('path', $staticPage->getPath());
|
||||
$this->setData('title', $staticPage->getTitle(null)); // Localized
|
||||
$this->setData('content', $staticPage->getContent(null)); // Localized
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['path', 'title', 'content']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::fetch
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function fetch($request, $template = null, $display = false)
|
||||
{
|
||||
$templateMgr = TemplateManager::getManager();
|
||||
$templateMgr->assign([
|
||||
'staticPageId' => $this->staticPageId,
|
||||
'pluginJavaScriptURL' => $this->plugin->getJavaScriptURL($request),
|
||||
]);
|
||||
|
||||
if ($context = $request->getContext()) {
|
||||
$templateMgr->assign('allowedVariables', [
|
||||
'contactName' => __('plugins.generic.tinymce.variables.principalContactName', ['value' => $context->getData('contactName')]),
|
||||
'contactEmail' => __('plugins.generic.tinymce.variables.principalContactEmail', ['value' => $context->getData('contactEmail')]),
|
||||
'supportName' => __('plugins.generic.tinymce.variables.supportContactName', ['value' => $context->getData('supportName')]),
|
||||
'supportPhone' => __('plugins.generic.tinymce.variables.supportContactPhone', ['value' => $context->getData('supportPhone')]),
|
||||
'supportEmail' => __('plugins.generic.tinymce.variables.supportContactEmail', ['value' => $context->getData('supportEmail')]),
|
||||
]);
|
||||
}
|
||||
|
||||
return parent::fetch($request, $template, $display);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save form values into the database
|
||||
*/
|
||||
public function execute(...$functionParams)
|
||||
{
|
||||
parent::execute(...$functionParams);
|
||||
/** @var StaticPagesDAO */
|
||||
$staticPagesDao = DAORegistry::getDAO('StaticPagesDAO');
|
||||
if ($this->staticPageId) {
|
||||
// Load and update an existing page
|
||||
$staticPage = $staticPagesDao->getById($this->staticPageId, $this->contextId);
|
||||
} else {
|
||||
// Create a new static page
|
||||
$staticPage = $staticPagesDao->newDataObject();
|
||||
$staticPage->setContextId($this->contextId);
|
||||
}
|
||||
|
||||
$staticPage->setPath($this->getData('path'));
|
||||
$staticPage->setTitle($this->getData('title'), null); // Localized
|
||||
$staticPage->setContent($this->getData('content'), null); // Localized
|
||||
|
||||
if ($this->staticPageId) {
|
||||
$staticPagesDao->updateObject($staticPage);
|
||||
} else {
|
||||
$staticPagesDao->insertObject($staticPage);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user