applicationcontext.class.inc.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. foreach($this->aNames as $sName)
  57. {
  58. $sValue = utils::ReadParam($sName, '');
  59. // TO DO: check if some of the context parameters are mandatory (or have default values)
  60. if (!empty($sValue))
  61. {
  62. self::$aDefaultValues[$sName] = $sValue;
  63. }
  64. }
  65. }
  66. $this->aValues = self::$aDefaultValues;
  67. }
  68. /**
  69. * Returns the context as string with the format name1=value1&name2=value2....
  70. * return string The context as a string to be appended to an href property
  71. */
  72. public function GetForLink()
  73. {
  74. $aParams = array();
  75. foreach($this->aValues as $sName => $sValue)
  76. {
  77. $aParams[] = $sName.'='.urlencode($sValue);
  78. }
  79. return implode("&", $aParams);
  80. }
  81. /**
  82. * Returns the context as sequence of input tags to be inserted inside a <form> tag
  83. * return string The context as a sequence of <input type="hidden" /> tags
  84. */
  85. public function GetForForm()
  86. {
  87. $sContext = "";
  88. foreach($this->aValues as $sName => $sValue)
  89. {
  90. $sContext .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  91. }
  92. return $sContext;
  93. }
  94. /**
  95. * Returns the context as a hash array 'parameter_name' => value
  96. * return array The context information
  97. */
  98. public function GetAsHash()
  99. {
  100. return $this->aValues;
  101. }
  102. /**
  103. * Returns an array of the context parameters NAMEs
  104. * @return array The list of context parameters
  105. */
  106. public function GetNames()
  107. {
  108. return $this->aNames;
  109. }
  110. /**
  111. * Removes the specified parameter from the context, for example when the same parameter
  112. * is already a search parameter
  113. * @param string $sParamName Name of the parameter to remove
  114. * @return none
  115. */
  116. public function Reset($sParamName)
  117. {
  118. if (isset($this->aValues[$sParamName]))
  119. {
  120. unset($this->aValues[$sParamName]);
  121. }
  122. }
  123. }
  124. ?>