applicationcontext.class.inc.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Class ApplicationContext
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once("../application/utils.inc.php");
  25. /**
  26. * Helper class to store and manipulate the parameters that make the application's context
  27. *
  28. * Usage:
  29. * 1) Build the application's context by constructing the object
  30. * (the object will read some of the page's parameters)
  31. *
  32. * 2) Add these parameters to hyperlinks or to forms using the helper, functions
  33. * GetForLink(), GetForForm() or GetAsHash()
  34. */
  35. class ApplicationContext
  36. {
  37. protected $aNames;
  38. protected $aValues;
  39. protected static $aDefaultValues; // Cache shared among all instances
  40. public function __construct()
  41. {
  42. $this->aNames = array(
  43. 'org_id', 'menu'
  44. );
  45. $this->ReadContext();
  46. }
  47. /**
  48. * Read the context directly in the PHP parameters (either POST or GET)
  49. * return nothing
  50. */
  51. protected function ReadContext()
  52. {
  53. if (empty(self::$aDefaultValues))
  54. {
  55. self::$aDefaultValues = array();
  56. $aContext = utils::ReadParam('c', array());
  57. foreach($this->aNames as $sName)
  58. {
  59. $sValue = isset($aContext[$sName]) ? $aContext[$sName] : '';
  60. // TO DO: check if some of the context parameters are mandatory (or have default values)
  61. if (!empty($sValue))
  62. {
  63. self::$aDefaultValues[$sName] = $sValue;
  64. }
  65. }
  66. }
  67. $this->aValues = self::$aDefaultValues;
  68. }
  69. /**
  70. * Returns the current value for the given parameter
  71. * @param string $sParamName Name of the parameter to read
  72. * @return mixed The value for this parameter
  73. */
  74. public function GetCurrentValue($sParamName, $defaultValue = '')
  75. {
  76. if (isset($this->aValues[$sParamName]))
  77. {
  78. return $this->aValues[$sParamName];
  79. }
  80. return $defaultValue;
  81. }
  82. /**
  83. * Returns the context as string with the format name1=value1&name2=value2....
  84. * return string The context as a string to be appended to an href property
  85. */
  86. public function GetForLink()
  87. {
  88. $aParams = array();
  89. foreach($this->aValues as $sName => $sValue)
  90. {
  91. $aParams[] = "c[$sName]".'='.urlencode($sValue);
  92. }
  93. return implode("&", $aParams);
  94. }
  95. /**
  96. * Returns the context as sequence of input tags to be inserted inside a <form> tag
  97. * return string The context as a sequence of <input type="hidden" /> tags
  98. */
  99. public function GetForForm()
  100. {
  101. $sContext = "";
  102. foreach($this->aValues as $sName => $sValue)
  103. {
  104. $sContext .= "<input type=\"hidden\" name=\"c[$sName]\" value=\"$sValue\" />\n";
  105. }
  106. return $sContext;
  107. }
  108. /**
  109. * Returns the context as a hash array 'parameter_name' => value
  110. * return array The context information
  111. */
  112. public function GetAsHash()
  113. {
  114. $aReturn = array();
  115. foreach($this->aValues as $sName => $sValue)
  116. {
  117. $aReturn["c[$sName]"] = $sValue;
  118. }
  119. return $aReturn;
  120. }
  121. /**
  122. * Returns an array of the context parameters NAMEs
  123. * @return array The list of context parameters
  124. */
  125. public function GetNames()
  126. {
  127. return $this->aNames;
  128. }
  129. /**
  130. * Removes the specified parameter from the context, for example when the same parameter
  131. * is already a search parameter
  132. * @param string $sParamName Name of the parameter to remove
  133. * @return none
  134. */
  135. public function Reset($sParamName)
  136. {
  137. if (isset($this->aValues[$sParamName]))
  138. {
  139. unset($this->aValues[$sParamName]);
  140. }
  141. }
  142. }
  143. ?>