Open Journal Systems  3.3.0
BrowseBySectionPlugin.inc.php
1 <?php
2 
13 import('lib.pkp.classes.plugins.GenericPlugin');
14 
15 define('BROWSEBYSECTION_DEFAULT_PER_PAGE', 30);
16 define('BROWSEBYSECTION_NMI_TYPE', 'BROWSEBYSECTION_NMI_');
17 
19 
23  public function register($category, $path, $mainContextId = NULL) {
24  $success = parent::register($category, $path);
25  if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return $success;
26  if ($success && $this->getEnabled()) {
27  HookRegistry::register('LoadHandler', array($this, 'loadPageHandler'));
28  HookRegistry::register('sectiondao::getAdditionalFieldNames', array($this, 'addSectionDAOFieldNames'));
29  HookRegistry::register('sectiondao::getLocaleFieldNames', array($this, 'addSectionDAOLocaleFieldNames'));
30  HookRegistry::register('Templates::Manager::Sections::SectionForm::AdditionalMetadata', array($this, 'addSectionFormFields'));
31  HookRegistry::register('sectionform::initdata', array($this, 'initDataSectionFormFields'));
32  HookRegistry::register('sectionform::readuservars', array($this, 'readSectionFormFields'));
33  HookRegistry::register('sectionform::execute', array($this, 'executeSectionFormFields'));
34  HookRegistry::register('NavigationMenus::itemTypes', array($this, 'addMenuItemTypes'));
35  HookRegistry::register('NavigationMenus::displaySettings', array($this, 'setMenuItemDisplayDetails'));
36  HookRegistry::register('SitemapHandler::createJournalSitemap', array($this, 'addSitemapURLs'));
38  }
39  return $success;
40  }
41 
45  public function getDisplayName() {
46  return __('plugins.generic.browseBySection.name');
47  }
48 
52  public function getDescription() {
53  return __('plugins.generic.browseBySection.description');
54  }
55 
67  public function loadPageHandler($hookName, $args) {
68  $page = $args[0];
69 
70  if ($this->getEnabled() && $page === 'section') {
71  $this->import('pages/BrowseBySectionHandler');
72  define('HANDLER_CLASS', 'BrowseBySectionHandler');
73  return true;
74  }
75 
76  return false;
77  }
78 
88  public function addSectionDAOFieldNames($hookName, $args) {
89  $fields =& $args[1];
90  $fields[] = 'browseByEnabled';
91  $fields[] = 'browseByPath';
92  $fields[] = 'browseByPerPage';
93  }
94 
104  public function addSectionDAOLocaleFieldNames($hookName, $args) {
105  $fields =& $args[1];
106  $fields[] = 'browseByDescription';
107  }
108 
123  public function addSectionFormFields($hookName, $args) {
124  $smarty =& $args[1];
125  $output =& $args[2];
126  $output .= $smarty->fetch($this->getTemplateResource('controllers/grids/settings/section/form/sectionFormAdditionalFields.tpl'));
127 
128  return false;
129  }
130 
139  public function initDataSectionFormFields($hookName, $args) {
140  $sectionForm = $args[0];
141  $request = Application::get()->getRequest();
142  $context = $request->getContext();
143  $contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
144 
145  $sectionDao = DAORegistry::getDAO('SectionDAO');
146  $section = $sectionDao->getById($sectionForm->getSectionId(), $contextId);
147 
148  if ($section) {
149  $sectionForm->setData('browseByEnabled', $section->getData('browseByEnabled'));
150  $sectionForm->setData('browseByPath', $section->getData('browseByPath'));
151  $sectionForm->setData('browseByPerPage', $section->getData('browseByPerPage'));
152  $sectionForm->setData('browseByDescription', $section->getData('browseByDescription'));
153  }
154  }
155 
165  public function readSectionFormFields($hookName, $args) {
166  $sectionForm =& $args[0];
167  $request = Application::get()->getRequest();
168 
169  $sectionForm->setData('browseByEnabled', $request->getUserVar('browseByEnabled'));
170  $sectionForm->setData('browseByPath', $request->getUserVar('browseByPath'));
171  $sectionForm->setData('browseByPerPage', $request->getUserVar('browseByPerPage'));
172  $sectionForm->setData('browseByDescription', $request->getUserVar('browseByDescription', null));
173  }
174 
183  public function executeSectionFormFields($hookName, $args) {
184  $sectionDao = DAORegistry::getDAO('SectionDAO');
185  $sectionForm = $args[0];
186  $section = $sectionDao->getById($sectionForm->getSectionId(), Application::getRequest()->getContext()->getId());
187 
188  $section->setData('browseByEnabled', $sectionForm->getData('browseByEnabled'));
189  $section->setData('browseByDescription', $sectionForm->getData('browseByDescription'));
190 
191  // Force a valid browseByPath
192  $browseByPath = $sectionForm->getData('browseByPath') ? $sectionForm->getData('browseByPath') : '';
193  if (empty($browseByPath)) {
194  $browseByPath = strtolower($section->getTitle(PKPLocale::getPrimaryLocale()));
195  }
196  $section->setData('browseByPath', preg_replace('/[^A-Za-z0-9-_]/', '', str_replace(' ', '-', $browseByPath)));
197 
198  // Force a valid browseByPerPage
199  $browseByPerPage = $sectionForm->getData('browseByPerPage') ? $sectionForm->getData('browseByPerPage') : '';
200  if (!ctype_digit((string) $browseByPerPage)) {
201  $browseByPerPage = null;
202  }
203  $section->setData('browseByPerPage', $browseByPerPage);
204 
205  $sectionDao->updateObject($section);
206  }
207 
216  public function addMenuItemTypes($hookName, $args) {
217  $types =& $args[0];
218  $request = Application::get()->getRequest();
219  $context = $request->getContext();
220  $contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
221 
222  $sectionDao = DAORegistry::getDAO('SectionDAO');
223  $sections = $sectionDao->getByContextId($contextId);
224 
225  while ($section = $sections->next()) {
226  if ($section->getData('browseByEnabled')) {
227  $types[BROWSEBYSECTION_NMI_TYPE . $section->getId()] = array(
228  'title' => __('plugins.generic.browseBySection.navMenuItem', array('name' => $section->getLocalizedTitle())),
229  'description' => __('plugins.generic.browseBySection.navMenuItem.description'),
230  );
231  }
232  }
233  }
234 
243  public function setMenuItemDisplayDetails($hookName, $args) {
244  $navigationMenuItem =& $args[0];
245  $typePrefixLength = strlen(BROWSEBYSECTION_NMI_TYPE);
246 
247  if (substr($navigationMenuItem->getType(), 0, $typePrefixLength) === BROWSEBYSECTION_NMI_TYPE) {
248  $request = Application::get()->getRequest();
249  $context = $request->getContext();
250  $contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
251  $sectionId = substr($navigationMenuItem->getType(), $typePrefixLength);
252  $sectionDao = DAORegistry::getDAO('SectionDAO');
253  $section = $sectionDao->getById($sectionId, $contextId);
254  if (!$section->getData('browseByEnabled')) {
255  $navigationMenuItem->setIsDisplayed(false);
256  } else {
257  $sectionPath = $section->getData('browseByPath') ? $section->getData('browseByPath') : $sectionId;
258  $dispatcher = $request->getDispatcher();
259  $navigationMenuItem->setUrl($dispatcher->url(
260  $request,
261  ROUTE_PAGE,
262  null,
263  'section',
264  'view',
265  htmlspecialchars($sectionPath)
266  ));
267  }
268  }
269  }
270 
278  function addSitemapURLs($hookName, $args) {
279  $doc = $args[0];
280  $rootNode = $doc->documentElement;
281 
283  $context = $request->getContext();
284  if ($context) {
285  $sectionDao = DAORegistry::getDAO('SectionDAO');
286  $sections = $sectionDao->getByContextId($context->getId());
287  while ($section = $sections->next()) {
288  if ($section->getData('browseByEnabled')) {
289  $sectionPath = $section->getData('browseByPath') ? $section->getData('browseByPath') : $section->getId();
290  // Create and append sitemap XML "url" element
291  $url = $doc->createElement('url');
292  $url->appendChild($doc->createElement('loc', htmlspecialchars($request->url($context->getPath(), 'section', 'view', $sectionPath), ENT_COMPAT, 'UTF-8')));
293  $rootNode->appendChild($url);
294  }
295  }
296  }
297  return false;
298  }
299 }
300 
BrowseBySectionPlugin\executeSectionFormFields
executeSectionFormFields($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:183
BrowseBySectionPlugin\addSectionDAOLocaleFieldNames
addSectionDAOLocaleFieldNames($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:104
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKPLocale\getPrimaryLocale
static getPrimaryLocale()
Definition: PKPLocale.inc.php:200
BrowseBySectionPlugin\loadPageHandler
loadPageHandler($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:67
BrowseBySectionPlugin\getDisplayName
getDisplayName()
Definition: BrowseBySectionPlugin.inc.php:45
BrowseBySectionPlugin\addSectionFormFields
addSectionFormFields($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:123
Plugin\getEnabled
getEnabled()
Definition: Plugin.inc.php:868
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
BrowseBySectionPlugin\addMenuItemTypes
addMenuItemTypes($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:216
BrowseBySectionPlugin\initDataSectionFormFields
initDataSectionFormFields($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:139
BrowseBySectionPlugin\addSitemapURLs
addSitemapURLs($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:278
BrowseBySectionPlugin\readSectionFormFields
readSectionFormFields($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:165
BrowseBySectionPlugin\addSectionDAOFieldNames
addSectionDAOFieldNames($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:88
Plugin\_registerTemplateResource
_registerTemplateResource($inCore=false)
Definition: Plugin.inc.php:376
Plugin\getTemplateResource
getTemplateResource($template=null, $inCore=false)
Definition: Plugin.inc.php:349
Plugin\$request
$request
Definition: Plugin.inc.php:68
BrowseBySectionPlugin\getDescription
getDescription()
Definition: BrowseBySectionPlugin.inc.php:52
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
PKPApplication\getRequest
getRequest()
Definition: PKPApplication.inc.php:270
BrowseBySectionPlugin
Allow visitors to browse journal content by section.
Definition: BrowseBySectionPlugin.inc.php:18
GenericPlugin
Abstract class for generic plugins.
Definition: GenericPlugin.inc.php:18
BrowseBySectionPlugin\setMenuItemDisplayDetails
setMenuItemDisplayDetails($hookName, $args)
Definition: BrowseBySectionPlugin.inc.php:243