applicationcontext.class.inc.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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(APPROOT."/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. // Hmm, there must be a better (more generic) way to handle the case below:
  66. // When there is only one possible (allowed) organization, the context must be
  67. // fixed to this org
  68. if ($sName == 'org_id')
  69. {
  70. if (MetaModel::IsValidClass('Organization'))
  71. {
  72. $oSearchFilter = new DBObjectSearch('Organization');
  73. $oSet = new CMDBObjectSet($oSearchFilter);
  74. $iCount = $oSet->Count();
  75. if ($iCount == 1)
  76. {
  77. // Only one possible value for org_id, set it in the context
  78. $oOrg = $oSet->Fetch();
  79. self::$aDefaultValues[$sName] = $oOrg->GetKey();
  80. }
  81. }
  82. }
  83. }
  84. }
  85. $this->aValues = self::$aDefaultValues;
  86. }
  87. /**
  88. * Returns the current value for the given parameter
  89. * @param string $sParamName Name of the parameter to read
  90. * @return mixed The value for this parameter
  91. */
  92. public function GetCurrentValue($sParamName, $defaultValue = '')
  93. {
  94. if (isset($this->aValues[$sParamName]))
  95. {
  96. return $this->aValues[$sParamName];
  97. }
  98. return $defaultValue;
  99. }
  100. /**
  101. * Returns the context as string with the format name1=value1&name2=value2....
  102. * return string The context as a string to be appended to an href property
  103. */
  104. public function GetForLink()
  105. {
  106. $aParams = array();
  107. foreach($this->aValues as $sName => $sValue)
  108. {
  109. $aParams[] = "c[$sName]".'='.urlencode($sValue);
  110. }
  111. return implode("&", $aParams);
  112. }
  113. /**
  114. * Returns the context as sequence of input tags to be inserted inside a <form> tag
  115. * return string The context as a sequence of <input type="hidden" /> tags
  116. */
  117. public function GetForForm()
  118. {
  119. $sContext = "";
  120. foreach($this->aValues as $sName => $sValue)
  121. {
  122. $sContext .= "<input type=\"hidden\" name=\"c[$sName]\" value=\"$sValue\" />\n";
  123. }
  124. return $sContext;
  125. }
  126. /**
  127. * Returns the context as a hash array 'parameter_name' => value
  128. * return array The context information
  129. */
  130. public function GetAsHash()
  131. {
  132. $aReturn = array();
  133. foreach($this->aValues as $sName => $sValue)
  134. {
  135. $aReturn["c[$sName]"] = $sValue;
  136. }
  137. return $aReturn;
  138. }
  139. /**
  140. * Returns an array of the context parameters NAMEs
  141. * @return array The list of context parameters
  142. */
  143. public function GetNames()
  144. {
  145. return $this->aNames;
  146. }
  147. /**
  148. * Removes the specified parameter from the context, for example when the same parameter
  149. * is already a search parameter
  150. * @param string $sParamName Name of the parameter to remove
  151. * @return none
  152. */
  153. public function Reset($sParamName)
  154. {
  155. if (isset($this->aValues[$sParamName]))
  156. {
  157. unset($this->aValues[$sParamName]);
  158. }
  159. }
  160. }
  161. ?>