Skip to content

Commit

Permalink
Merge pull request #42 from kaitlinnewson/37-3_4_0
Browse files Browse the repository at this point in the history
#37 fix errors with creating and deleting sections…
  • Loading branch information
kaitlinnewson authored Dec 4, 2024
2 parents 5d2a62f + eb5cc5b commit 5acbf86
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 80 deletions.
115 changes: 66 additions & 49 deletions BrowseBySectionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* @file plugins/generic/browseBySection/BrowseBySectionPlugin.inc.php
*
* Copyright (c) 2014-2017 Simon Fraser University
* Copyright (c) 2003-2017 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2003-2024 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class BrowseBySectionPlugin
Expand All @@ -15,30 +15,32 @@

namespace APP\plugins\generic\browseBySection;

use PKP\core\PKPApplication;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\config\Config;
use APP\core\Application;
use PKP\i18n\Locale;
use PKP\db\DAORegistry;
use APP\facades\Repo;
use APP\plugins\generic\browseBySection\pages\BrowseBySectionHandler;

define('BROWSEBYSECTION_DEFAULT_PER_PAGE', 30);
define('BROWSEBYSECTION_NMI_TYPE', 'BROWSEBYSECTION_NMI_');

class BrowseBySectionPlugin extends GenericPlugin {

class BrowseBySectionPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::register
*/
public function register($category, $path, $mainContextId = NULL) {
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return $success;
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) {
return $success;
}
if ($success && $this->getEnabled()) {
Hook::add('LoadHandler', [$this, 'loadPageHandler']);
Hook::add('Templates::Manager::Sections::SectionForm::AdditionalMetadata', [$this, 'addSectionFormFields']);
Hook::add('Schema::get::section', function($hookName, $args) {
Hook::add('Schema::get::section', function ($hookName, $args) {
$schema = &$args[0];

$schema->properties->browseByEnabled = (object)[
Expand Down Expand Up @@ -67,8 +69,8 @@ public function register($category, $path, $mainContextId = NULL) {

$schema->properties->browseByDescription = (object)[
'type' => 'string',
'apiSummary' => true,
'multilingual' => true,
'apiSummary' => true,
'multilingual' => true,
'validation' => ['nullable']
];
});
Expand All @@ -86,14 +88,16 @@ public function register($category, $path, $mainContextId = NULL) {
/**
* @copydoc PKPPlugin::getDisplayName
*/
public function getDisplayName() {
public function getDisplayName()
{
return __('plugins.generic.browseBySection.name');
}

/**
* @copydoc PKPPlugin::getDescription
*/
public function getDescription() {
public function getDescription()
{
return __('plugins.generic.browseBySection.description');
}

Expand All @@ -108,7 +112,8 @@ public function getDescription() {
* ]
* @return bool
*/
public function loadPageHandler($hookName, $args) {
public function loadPageHandler($hookName, $args)
{
$page = $args[0];
$handler =& $args[3];

Expand All @@ -134,7 +139,8 @@ public function loadPageHandler($hookName, $args) {
* ]
* @return bool
*/
public function addSectionFormFields($hookName, $args) {
public function addSectionFormFields($hookName, $args)
{
$smarty =& $args[1];
$output =& $args[2];
$output .= $smarty->fetch($this->getTemplateResource('controllers/grids/settings/section/form/sectionFormAdditionalFields.tpl'));
Expand All @@ -150,28 +156,30 @@ public function addSectionFormFields($hookName, $args) {
* @option SectionForm
* ]
*/
public function initDataSectionFormFields($hookName, $args) {
public function initDataSectionFormFields($hookName, $args)
{
$sectionForm = $args[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
$contextId = $context ? $context->getId() : PKPApplication::CONTEXT_ID_NONE;

$section = Repo::section()->get($sectionForm->getSectionId(), $contextId);
$section = $sectionForm->getSectionId() ? Repo::section()->get($sectionForm->getSectionId(), $contextId) : null;

if ($section) {
$sectionForm->setData('browseByEnabled', $section->getData('browseByEnabled'));
$sectionForm->setData('browseByPath', $section->getData('browseByPath'));
$sectionForm->setData('browseByPerPage', $section->getData('browseByPerPage'));
$sectionForm->setData('browseByDescription', $section->getData('browseByDescription'));
$orderTypes = [
'datePubDesc' => 'catalog.sortBy.datePublishedDesc',
'datePubAsc' => 'catalog.sortBy.datePublishedAsc',
'titleAsc' => 'catalog.sortBy.titleAsc',
'titleDesc' => 'catalog.sortBy.titleDesc',
];
$sectionForm->setData('orderTypes', $orderTypes);
$sectionForm->setData('browseByOrder', $section->getData('browseByOrder'));
}

$orderTypes = [
'datePubDesc' => 'catalog.sortBy.datePublishedDesc',
'datePubAsc' => 'catalog.sortBy.datePublishedAsc',
'titleAsc' => 'catalog.sortBy.titleAsc',
'titleDesc' => 'catalog.sortBy.titleDesc',
];
$sectionForm->setData('orderTypes', $orderTypes);
}

/**
Expand All @@ -183,7 +191,8 @@ public function initDataSectionFormFields($hookName, $args) {
* @option array User vars
* ]
*/
public function readSectionFormFields($hookName, $args) {
public function readSectionFormFields($hookName, $args)
{
$sectionForm =& $args[0];
$request = Application::get()->getRequest();

Expand All @@ -202,7 +211,8 @@ public function readSectionFormFields($hookName, $args) {
* @option SectionForm
* ]
*/
public function executeSectionFormFields($hookName, $args) {
public function executeSectionFormFields($hookName, $args)
{
$sectionForm = $args[0];
$request = Application::get()->getRequest();
$section = Repo::section()->get($sectionForm->getSectionId(), $request->getContext()->getId());
Expand All @@ -214,7 +224,7 @@ public function executeSectionFormFields($hookName, $args) {
// Force a valid browseByPath
$browseByPath = $sectionForm->getData('browseByPath') ? $sectionForm->getData('browseByPath') : '';
if (empty($browseByPath)) {
$browseByPath = strtolower($section->getTitle(Locale::getPrimaryLocale()));
$browseByPath = strtolower($section->getTitle($request->getContext()->getPrimaryLocale()));
}
$section->setData('browseByPath', preg_replace('/[^A-Za-z0-9-_]/', '', str_replace(' ', '-', $browseByPath)));

Expand All @@ -236,11 +246,12 @@ public function executeSectionFormFields($hookName, $args) {
* @option array Existing menu item types
* ]
*/
public function addMenuItemTypes($hookName, $args) {
public function addMenuItemTypes($hookName, $args)
{
$types =& $args[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
$contextId = $context ? $context->getId() : PKPApplication::CONTEXT_ID_NONE;

$sections = Repo::section()->getCollector()->filterByContextIds([$contextId])->getMany();

Expand All @@ -262,29 +273,33 @@ public function addMenuItemTypes($hookName, $args) {
* @option NavigationMenuItem
* ]
*/
public function setMenuItemDisplayDetails($hookName, $args) {
public function setMenuItemDisplayDetails($hookName, $args)
{
$navigationMenuItem =& $args[0];
$typePrefixLength = strlen(BROWSEBYSECTION_NMI_TYPE);

if (substr($navigationMenuItem->getType(), 0, $typePrefixLength) === BROWSEBYSECTION_NMI_TYPE) {
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
$contextId = $context ? $context->getId() : PKPApplication::CONTEXT_ID_NONE;
$sectionId = substr($navigationMenuItem->getType(), $typePrefixLength);
$section = Repo::section()->get($sectionId, $contextId);
if (!$section->getData('browseByEnabled')) {
$navigationMenuItem->setIsDisplayed(false);
} else {
$sectionPath = $section->getData('browseByPath') ? $section->getData('browseByPath') : $sectionId;
$dispatcher = $request->getDispatcher();
$navigationMenuItem->setUrl($dispatcher->url(
$request,
ROUTE_PAGE,
null,
'section',
'view',
htmlspecialchars($sectionPath)
));

if ($section) {
if (!$section->getData('browseByEnabled')) {
$navigationMenuItem->setIsDisplayed(false);
} else {
$sectionPath = $section->getData('browseByPath') ? $section->getData('browseByPath') : $sectionId;
$dispatcher = $request->getDispatcher();
$navigationMenuItem->setUrl($dispatcher->url(
$request,
PKPApplication::ROUTE_PAGE,
null,
'section',
'view',
htmlspecialchars($sectionPath)
));
}
}
}
}
Expand All @@ -296,14 +311,17 @@ public function setMenuItemDisplayDetails($hookName, $args) {
* @param $args array
* @return boolean
*/
function addSitemapURLs($hookName, $args) {
public function addSitemapURLs($hookName, $args)
{
$doc = $args[0];
$rootNode = $doc->documentElement;

$request = Application::getRequest();
$request = Application::get()->getRequest();
$context = $request->getContext();
if ($context) {
$sections = Repo::section()->getCollector()->filterByContextIds([$context->getId()])->getMany();
$sections = Repo::section()->getCollector()
->filterByContextIds([$context->getId()])
->getMany();
foreach ($sections as $section) {
if ($section->getData('browseByEnabled')) {
$sectionPath = $section->getData('browseByPath') ? $section->getData('browseByPath') : $section->getId();
Expand All @@ -317,4 +335,3 @@ function addSitemapURLs($hookName, $args) {
return false;
}
}

Loading

0 comments on commit 5acbf86

Please sign in to comment.