PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/OX/Admin/UI/Hooks.php

https://bitbucket.org/blackriver/openx
PHP | 253 lines | 91 code | 33 blank | 129 comment | 5 complexity | a1389a101d0ef55d08fb69b96580740f MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id$
  25. */
  26. require_once MAX_PATH . '/lib/OX/Admin/UI/Event/EventDispatcher.php';
  27. require_once MAX_PATH . '/lib/OX/Admin/UI/Event/EventContext.php';
  28. /**
  29. * A manager for UI related hooks.
  30. *
  31. * Allows registration of event listeners in the form of {@link http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback callback}
  32. * and when informed of an event it will pass the event to registered listeners.
  33. *
  34. * Listeners can be registered via provided methods. Plugins, whishing to listen
  35. * to ui events should implement 'registerUiListeners' plugin hook and
  36. * register apprioriate listeners when it is called.
  37. *
  38. * For performance reasons, plugin 'registerUiListeners' is called once per request
  39. * and only when ui event is being invoked.
  40. *
  41. *
  42. */
  43. class OX_Admin_UI_Hooks
  44. {
  45. private static $initialized = false;
  46. /*
  47. * UI hook. Invoked before OA_Admin_UI::showHeader() starts processing.
  48. * Note:
  49. * - at this point it should be safe to output any additional headers
  50. * if needed.
  51. * - do not output content here! (use after pageHeader or beforePageContent hooks to
  52. * render any content after layout and before actual page content.)
  53. *
  54. * @param string $menuSectionId section id of page being rendered
  55. * @param array $pageData array of page related parameters eg. clientid, campaignid
  56. * @param OA_Admin_UI_Model_PageHeaderModel optional $headerModel
  57. */
  58. public static function beforePageHeader($menuSectionId, $pageData, $oHeaderModel = null)
  59. {
  60. self::init();
  61. $oContext = new OX_Admin_UI_Event_EventContext(array(
  62. 'pageId' => $menuSectionId,
  63. 'pageData' => $pageData,
  64. 'headerModel' => $oHeaderModel,
  65. ));
  66. self::getDispatcher()->triggerEvent('beforePageHeader', $oContext);
  67. }
  68. /**
  69. * Registers callback to be invoked when 'beforePageHeader' event occurs.
  70. *
  71. * Callback will be passed an OX_Admin_UI_Event_EventContext object with the following
  72. * data:
  73. * 'pageId' - string menu section id of page being rendered
  74. * 'pageData' => menu links parameter values (eg. clientid, campaignid etc.),
  75. *
  76. * @param PHP callback $callback
  77. */
  78. public static function registerBeforePageHeaderListener($callback)
  79. {
  80. self::getDispatcher()->register('beforePageHeader', $callback);
  81. }
  82. /*
  83. * UI hook. Invoked right after OA_Admin_UI::showHeader() ends processing.
  84. * Any data displayed here will precede page content (including content rendered
  85. * via beforePageContent hook.
  86. *
  87. * @param string $menuSectionId section id of page being rendered
  88. */
  89. public static function afterPageHeader($menuSectionId)
  90. {
  91. self::init();
  92. $oContext = new OX_Admin_UI_Event_EventContext(array(
  93. 'pageId' => $menuSectionId,
  94. 'pageData' => $pageData,
  95. ));
  96. self::getDispatcher()->triggerEvent('afterPageHeader', $oContext);
  97. }
  98. /**
  99. * Registers callback to be invoked when 'afterPageHeader' event occurs.
  100. *
  101. * Callback will be passed an OX_Admin_UI_Event_EventContext object with the following
  102. * data:
  103. * 'pageId' - string menu section id of page being rendered
  104. * 'pageData' => menu links parameter values (eg. clientid, campaignid etc.),
  105. *
  106. * @param PHP callback $callback
  107. */
  108. public static function registerAfterPageHeaderListener($callback)
  109. {
  110. self::getDispatcher()->register('afterPageHeader', $callback);
  111. }
  112. /**
  113. * Template hook. Should be invoked only from smarty template at the top of the top-most page
  114. * content template, before page content.
  115. *
  116. * By page content, we mean anything which is not rendered from layout built by UI->showHeader();
  117. *
  118. * @param string $pageId
  119. * @param array $pageData
  120. * @param OA_Admin_Template $oTpl
  121. * @return string HTML content to be injected before page content
  122. */
  123. public static function beforePageContent($pageId, $pageData, &$oTpl)
  124. {
  125. self::init();
  126. $result = '';
  127. $oContext = new OX_Admin_UI_Event_EventContext(array(
  128. 'pageId' => $pageId,
  129. 'pageData' => $pageData,
  130. 'oTpl' => $oTpl
  131. ));
  132. $aStrings = self::getDispatcher()->triggerEvent('beforePageContent', $oContext);
  133. if (!empty($aStrings)) {
  134. $result = join('\n', $aStrings);
  135. }
  136. return $result;
  137. }
  138. /**
  139. * Registers callback to be invoked when 'beforePageContent' event occurs.
  140. *
  141. * Callback will be passed an OX_Admin_UI_Event_EventContext object with the following
  142. * data:
  143. * 'pageId' - string menu section id of page being rendered
  144. * 'pageData' => menu links parameter values (eg. clientid, campaignid etc.),
  145. * 'oTpl' => instance of OA_Admin_Template being used to render page content
  146. *
  147. * @param PHP callback $callback
  148. */
  149. public static function registerBeforePageContentListener($callback)
  150. {
  151. self::getDispatcher()->register('beforePageContent', $callback);
  152. }
  153. /**
  154. * Template hook. Should be invoked only from smarty template at the top of the top-most page
  155. * content template, before page content.
  156. *
  157. * By page content, we mean anything which is not rendered from layout built by UI->showHeader();
  158. *
  159. * @param string $pageId
  160. * @param array $pageData
  161. * @param OA_Admin_Template $oTpl
  162. * @return string HTML content to be injected before page content
  163. */
  164. public static function afterPageContent($pageId, $pageData, &$oTpl)
  165. {
  166. self::init();
  167. $oContext = new OX_Admin_UI_Event_EventContext(array(
  168. 'pageId' => $pageId,
  169. 'pageData' => $pageData,
  170. 'oTpl' => $oTpl
  171. ));
  172. $aStrings = self::getDispatcher()->triggerEvent('afterPageContent', $oContext);
  173. if (!empty($aStrings)) {
  174. $result = join('\n', $aStrings);
  175. }
  176. return $result;
  177. }
  178. /**
  179. * Registers callback to be invoked when 'beforePageContent' event occurs.
  180. *
  181. * Callback will be passed an OX_Admin_UI_Event_EventContext object with the following
  182. * data:
  183. * 'pageId' - string menu section id of page being rendered
  184. * 'pageData' => menu links parameter values (eg. clientid, campaignid etc.),
  185. * 'oTpl' => instance of OA_Admin_Template being used to render page content
  186. *
  187. * @param PHP callback $callback
  188. */
  189. public static function registerAfterPageContentListener($callback)
  190. {
  191. self::getDispatcher()->register('afterPageContent', $callback);
  192. }
  193. /**
  194. * Retrieves an instance of event dispatcher
  195. *
  196. * @return OX_Admin_UI_Event_EventDispatcher
  197. */
  198. private static function getDispatcher()
  199. {
  200. $oDispatcher = OX_Admin_UI_Event_EventDispatcher::getInstance();
  201. return $oDispatcher;
  202. }
  203. //hack function
  204. private static function init()
  205. {
  206. if (self::$initialized) {
  207. return;
  208. }
  209. //register UI listeners from plugins
  210. $aPlugins = OX_Component::getListOfRegisteredComponentsForHook('registerUiListeners');
  211. foreach ($aPlugins as $i => $id) {
  212. if ($obj = OX_Component::factoryByComponentIdentifier($id)) {
  213. if(is_callable(array($obj, 'registerUiListeners'))) {
  214. $obj->registerUiListeners();
  215. }
  216. }
  217. }
  218. self::$initialized = true;
  219. }
  220. }