/classes/plugins/BlockPlugin.inc.php

https://github.com/ojsde/pkp-lib · PHP · 181 lines · 69 code · 20 blank · 92 comment · 7 complexity · e4929c487950f22d908460ffc79714e8 MD5 · raw file

  1. <?php
  2. /**
  3. * @file classes/plugins/BlockPlugin.inc.php
  4. *
  5. * Copyright (c) 2003-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class BlockPlugin
  9. * @ingroup plugins
  10. *
  11. * @brief Abstract class for block plugins
  12. */
  13. define('BLOCK_CONTEXT_LEFT_SIDEBAR', 0x00000001);
  14. define('BLOCK_CONTEXT_RIGHT_SIDEBAR', 0x00000002);
  15. define('BLOCK_CONTEXT_HOMEPAGE', 0x00000003);
  16. import('lib.pkp.classes.plugins.LazyLoadPlugin');
  17. class BlockPlugin extends LazyLoadPlugin {
  18. /**
  19. * Constructor
  20. */
  21. function BlockPlugin() {
  22. parent::LazyLoadPlugin();
  23. }
  24. /*
  25. * Override public methods from PKPPlugin
  26. */
  27. /**
  28. * @see PKPPlugin::register()
  29. */
  30. function register($category, $path) {
  31. $success = parent::register($category, $path);
  32. if ($success && $this->getEnabled()) {
  33. $contextMap =& $this->getContextMap();
  34. $blockContext = $this->getBlockContext();
  35. if (isset($contextMap[$blockContext])) {
  36. $hookName = $contextMap[$blockContext];
  37. HookRegistry::register($hookName, array(&$this, 'callback'));
  38. }
  39. }
  40. return $success;
  41. }
  42. /**
  43. * Override protected methods from PKPPlugin
  44. */
  45. /**
  46. * @see PKPPlugin::getSeq()
  47. *
  48. * NB: In the case of block plugins, higher numbers move
  49. * plugins down the page compared to other blocks.
  50. */
  51. function getSeq() {
  52. return $this->getContextSpecificSetting($this->getSettingMainContext(), 'seq');
  53. }
  54. /*
  55. * Block Plugin specific methods
  56. */
  57. /**
  58. * Set the sequence information for this plugin.
  59. *
  60. * NB: In the case of block plugins, higher numbers move
  61. * plugins down the page compared to other blocks.
  62. *
  63. * @param $seq int
  64. */
  65. function setSeq($seq) {
  66. return $this->updateContextSpecificSetting($this->getSettingMainContext(), 'seq', $seq, 'int');
  67. }
  68. /**
  69. * Get the block context (e.g. BLOCK_CONTEXT_...) for this block.
  70. *
  71. * @return int
  72. */
  73. function getBlockContext() {
  74. return $this->getContextSpecificSetting($this->getSettingMainContext(), 'context');
  75. }
  76. /**
  77. * Set the block context (e.g. BLOCK_CONTEXT_...) for this block.
  78. *
  79. * @param $context int
  80. */
  81. function setBlockContext($context) {
  82. return $this->updateContextSpecificSetting($this->getSettingMainContext(), 'context', $context, 'int');
  83. }
  84. /**
  85. * Get the supported contexts (e.g. BLOCK_CONTEXT_...) for this block.
  86. *
  87. * @return array
  88. */
  89. function getSupportedContexts() {
  90. // Will return left and right process as this is the
  91. // most frequent use case.
  92. return array(BLOCK_CONTEXT_LEFT_SIDEBAR, BLOCK_CONTEXT_RIGHT_SIDEBAR);
  93. }
  94. /**
  95. * Get an associative array linking block context to hook name.
  96. *
  97. * @return array
  98. */
  99. function &getContextMap() {
  100. static $contextMap = array(
  101. BLOCK_CONTEXT_LEFT_SIDEBAR => 'Templates::Common::LeftSidebar',
  102. BLOCK_CONTEXT_RIGHT_SIDEBAR => 'Templates::Common::RightSidebar',
  103. );
  104. $homepageHook = $this->_getContextSpecificHomepageHook();
  105. if ($homepageHook) $contextMap[BLOCK_CONTEXT_HOMEPAGE] = $homepageHook;
  106. HookRegistry::call('BlockPlugin::getContextMap', array(&$this, &$contextMap));
  107. return $contextMap;
  108. }
  109. /**
  110. * Get the filename of the template block. (Default behavior may
  111. * be overridden through some combination of this function and the
  112. * getContents function.)
  113. * Returning null from this function results in an empty display.
  114. *
  115. * @return string
  116. */
  117. function getBlockTemplateFilename() {
  118. return 'block.tpl';
  119. }
  120. /**
  121. * Get the HTML contents for this block.
  122. *
  123. * @param $templateMgr object
  124. * @param $request PKPRequest (Optional for legacy plugins)
  125. * @return string
  126. */
  127. function getContents(&$templateMgr, $request = null) {
  128. $blockTemplateFilename = $this->getBlockTemplateFilename();
  129. if ($blockTemplateFilename === null) return '';
  130. return $templateMgr->fetch($this->getTemplatePath() . $blockTemplateFilename);
  131. }
  132. /**
  133. * Callback that renders the block.
  134. *
  135. * @param $hookName string
  136. * @param $args array
  137. * @return string
  138. */
  139. function callback($hookName, $args) {
  140. $params =& $args[0];
  141. $smarty =& $args[1];
  142. $output =& $args[2];
  143. $output .= $this->getContents($smarty, $this->getRequest());
  144. return false;
  145. }
  146. /*
  147. * Private helper methods
  148. */
  149. /**
  150. * The application specific context home page hook name.
  151. *
  152. * @return string
  153. */
  154. function _getContextSpecificHomepageHook() {
  155. $application =& PKPApplication::getApplication();
  156. if ($application->getContextDepth() == 0) return null;
  157. $contextList = $application->getContextList();
  158. return 'Templates::Index::'.array_shift($contextList);
  159. }
  160. }
  161. ?>