applicationcontext.class.inc.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. require_once("../application/utils.inc.php");
  3. /**
  4. * Helper class to store and manipulate the parameters that make the application's context
  5. *
  6. * Usage:
  7. * 1) Build the application's context by constructing the object
  8. * (the object will read some of the page's parameters)
  9. *
  10. * 2) Add these parameters to hyperlinks or to forms using the helper, functions
  11. * GetForLink(), GetForForm() or GetAsHash()
  12. */
  13. class ApplicationContext
  14. {
  15. protected $aNames;
  16. protected $aValues;
  17. protected static $aDefaultValues; // Cache shared among all instances
  18. public function __construct()
  19. {
  20. $this->aNames = array(
  21. 'org_id', 'menu'
  22. );
  23. $this->ReadContext();
  24. }
  25. /**
  26. * Read the context directly in the PHP parameters (either POST or GET)
  27. * return nothing
  28. */
  29. protected function ReadContext()
  30. {
  31. if (empty(self::$aDefaultValues))
  32. {
  33. self::$aDefaultValues = array();
  34. foreach($this->aNames as $sName)
  35. {
  36. $sValue = utils::ReadParam($sName, '');
  37. // TO DO: check if some of the context parameters are mandatory (or have default values)
  38. if (!empty($sValue))
  39. {
  40. self::$aDefaultValues[$sName] = $sValue;
  41. }
  42. }
  43. }
  44. $this->aValues = self::$aDefaultValues;
  45. }
  46. /**
  47. * Returns the context as string with the format name1=value1&name2=value2....
  48. * return string The context as a string to be appended to an href property
  49. */
  50. public function GetForLink()
  51. {
  52. $aParams = array();
  53. foreach($this->aValues as $sName => $sValue)
  54. {
  55. $aParams[] = $sName.'='.urlencode($sValue);
  56. }
  57. return implode("&", $aParams);
  58. }
  59. /**
  60. * Returns the context as sequence of input tags to be inserted inside a <form> tag
  61. * return string The context as a sequence of <input type="hidden" /> tags
  62. */
  63. public function GetForForm()
  64. {
  65. $sContext = "";
  66. foreach($this->aValues as $sName => $sValue)
  67. {
  68. $sContext .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  69. }
  70. return $sContext;
  71. }
  72. /**
  73. * Returns the context as a hash array 'parameter_name' => value
  74. * return array The context information
  75. */
  76. public function GetAsHash()
  77. {
  78. return $this->aValues;
  79. }
  80. /**
  81. * Removes the specified parameter from the context, for example when the same parameter
  82. * is already a search parameter
  83. * @param string $sParamName Name of the parameter to remove
  84. * @return none
  85. */
  86. public function Reset($sParamName)
  87. {
  88. if (isset($this->aValues[$sParamName]))
  89. {
  90. unset($this->aValues[$sParamName]);
  91. }
  92. }
  93. }
  94. ?>