/src/lib/util/PageUtil.php

https://github.com/ThiloWitt/core · PHP · 267 lines · 106 code · 28 blank · 133 comment · 24 complexity · a79b8cb782a81a5b07e48ef4558d93a8 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Util
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. /**
  15. * Zikula page variables functions.
  16. *
  17. * A <em>page variable</em> is an entity identified by a name that stores a value for the currently
  18. * rendered page. They are used to set for example the title of the page, the stylesheets used etc.
  19. * from the module.
  20. *
  21. * Page variables can be <em>single valued</em> or <em>multi valued</em>. In the first case, only
  22. * one single value can be set; each new setting will overwrite the old one. The title is an example
  23. * for a single values page variable (each page can have exactly one title). Multi valued variables
  24. * can contain more than one value, and new values can be added to the variable. An example of a multi
  25. * valued variable is stylesheet (a page can use more than one style sheet).
  26. *
  27. * Zikula offers a set of API functions to manipulate page variables.
  28. *
  29. * A module can register a new page variable by providing its metadata using the pnPageRegisterVar
  30. * function.
  31. *
  32. * Zikula doesn't impose any restriction on the page variable's name except for duplicate
  33. * and reserved names. As of this writing, the list of reserved names consists of
  34. * <ul>
  35. * <li>title</li>
  36. * <li>stylesheet</li>
  37. * <li>javascript</li>
  38. * <li>jsgettext</li>
  39. * <li>body</li>
  40. * <li>header</li>
  41. * <li>footer</li>
  42. * </ul>
  43. *
  44. * In addition, if your system is operating in legacy compatibility mode, then
  45. * the variable 'rawtext' is reserved, and maps to 'header'. (When not operating in
  46. * legacy compatibility mode, 'rawtext' is not reserved and will not be rendered
  47. * to the page output by the page variable output filter.)
  48. */
  49. class PageUtil
  50. {
  51. /**
  52. * Register Var.
  53. *
  54. * Registers a new page variable.
  55. * Zikula doesn't impose any restriction on the page variable's name except for duplicate
  56. * and reserved names. As of this writing, the list of reserved names consists of
  57. * <ul>
  58. * <li>title</li>
  59. * <li>stylesheet</li>
  60. * <li>javascript</li>
  61. * <li>jsgettext</li>
  62. * <li>body</li>
  63. * <li>header</li>
  64. * <li>footer</li>
  65. * </ul>
  66. *
  67. * In addition, if your system is operating in legacy compatibility mode, then
  68. * the variable 'rawtext' is reserved, and maps to 'header'. (When not operating in
  69. * legacy compatibility mode, 'rawtext' is not reserved and will not be rendered
  70. * to the page output by the page variable output filter.)
  71. *
  72. * @param string $varname The name of the new page variable.
  73. * @param boolean $multivalue To define a single or a multi valued variable.
  74. * @param string $default To set the default value. This value is assigned to the variable at registration time.
  75. *
  76. * @return boolean success or not
  77. */
  78. public static function registerVar($varname, $multivalue = false, $default = null)
  79. {
  80. global $_pageVars;
  81. // check for $_pageVars sanity
  82. if (!isset($_pageVars)) {
  83. $_pageVars = array();
  84. } elseif (!is_array($_pageVars)) {
  85. return false;
  86. }
  87. // if already registered, stop
  88. if (isset($_pageVars[$varname])) {
  89. return false;
  90. }
  91. // define the page variable and it's default value
  92. $_pageVars[$varname] = compact('multivalue', 'default');
  93. // always make the default value the contents (even if it's null - that will be filtered away)
  94. self::resetVar($varname);
  95. return true;
  96. }
  97. /**
  98. * Reset Var.
  99. *
  100. * Resets the pge variable back to its default value.
  101. * All values assigned by addVar() or setVar()
  102. * will get lost.
  103. *
  104. * @param string $varname The name of the page variable.
  105. *
  106. * @return boolean true On success, false of the page variable is not registered.
  107. */
  108. public static function resetVar($varname)
  109. {
  110. global $_pageVars;
  111. // check for $_pageVars sanity
  112. if (!isset($_pageVars)) {
  113. $_pageVars = array();
  114. } elseif (!is_array($_pageVars)) {
  115. return false;
  116. }
  117. if (!isset($_pageVars[$varname])) {
  118. return false;
  119. }
  120. if ($_pageVars[$varname]['multivalue']) {
  121. if (empty($_pageVars[$varname]['default'])) {
  122. $_pageVars[$varname]['contents'] = array();
  123. } else {
  124. $_pageVars[$varname]['contents'] = array($_pageVars[$varname]['default']);
  125. }
  126. } else {
  127. if (empty($_pageVars[$varname]['default'])) {
  128. $_pageVars[$varname]['contents'] = null;
  129. } else {
  130. $_pageVars[$varname]['contents'] = $_pageVars[$varname]['default'];
  131. }
  132. }
  133. return true;
  134. }
  135. /**
  136. * GetVar.
  137. *
  138. * Returns the value(s) of a page variable. In the case of
  139. * a mulit valued variable, this is an array containing all assigned
  140. * values.
  141. *
  142. * @param string $varname The name of the page variable.
  143. * @param mixed $default Default return value.
  144. *
  145. * @return mixed Contents of the variable
  146. */
  147. public static function getVar($varname, $default = null)
  148. {
  149. global $_pageVars;
  150. // check for $_pageVars sanity
  151. if (!isset($_pageVars)) {
  152. $_pageVars = array();
  153. } elseif (!is_array($_pageVars)) {
  154. return false;
  155. }
  156. if (isset($_pageVars[$varname]) && isset($_pageVars[$varname]['contents'])) {
  157. return $_pageVars[$varname]['contents'];
  158. } elseif (isset($_pageVars[$varname]['default'])) {
  159. return $_pageVars[$varname]['default'];
  160. }
  161. return $default;
  162. }
  163. /**
  164. * Set var.
  165. *
  166. * Sets the page variable to a new value. In the case of
  167. * a multi valued page variable, all previously added values
  168. * will get lost. If you want to add a value to a multi valued
  169. * page variable, use PageUtil::addVar.
  170. *
  171. * @param string $varname The name of the page variable.
  172. * @param mixed $value The new value.
  173. *
  174. * @see PageUtil::addVar
  175. * @return boolean true On success, false of the page variable is not registered.
  176. *
  177. */
  178. public static function setVar($varname, $value)
  179. {
  180. global $_pageVars;
  181. // check for $_pageVars sanity
  182. if (!isset($_pageVars)) {
  183. $_pageVars = array();
  184. } elseif (!is_array($_pageVars)) {
  185. return false;
  186. }
  187. if (!isset($_pageVars[$varname])) {
  188. return false;
  189. }
  190. if ($_pageVars[$varname]['multivalue']) {
  191. $_pageVars[$varname]['contents'] = array($value);
  192. } else {
  193. $_pageVars[$varname]['contents'] = $value;
  194. }
  195. return true;
  196. }
  197. /**
  198. * Add var.
  199. *
  200. * Adds a new vaule to a page variable. In the case of a single
  201. * page variable, this functions acts exactly like PageUtil::setVar.
  202. *
  203. * @param string $varname The name of the page variable.
  204. * @param mixed $value The new value.
  205. *
  206. * @see PageUtil::setVar
  207. * @return boolean true On success, false of the page variable is not registered.
  208. */
  209. public static function addVar($varname, $value)
  210. {
  211. global $_pageVars;
  212. // check for $_pageVars sanity
  213. if (!isset($_pageVars)) {
  214. $_pageVars = array();
  215. } elseif (!is_array($_pageVars)) {
  216. return false;
  217. }
  218. if (!isset($_pageVars[$varname])) {
  219. return false;
  220. }
  221. if (is_array($value)) {
  222. $value = array_unique($value);
  223. }
  224. $event = new Zikula_Event('pageutil.addvar_filter', $varname, array(), $value);
  225. $value = EventUtil::getManager()->notify($event)->getData();
  226. if ($_pageVars[$varname]['multivalue']) {
  227. if (is_array($value)) {
  228. $_pageVars[$varname]['contents'] = array_merge($_pageVars[$varname]['contents'], $value);
  229. } else {
  230. $_pageVars[$varname]['contents'][] = $value;
  231. }
  232. // make values unique
  233. $_pageVars[$varname]['contents'] = array_unique($_pageVars[$varname]['contents']);
  234. } else {
  235. $_pageVars[$varname]['contents'] = $value;
  236. }
  237. return true;
  238. }
  239. }