applicationcontext.class.inc.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. public function __construct()
  18. {
  19. $this->aNames = array(
  20. 'org_id', 'menu'
  21. );
  22. $this->ReadContext();
  23. }
  24. /**
  25. * Read the context directly in the PHP parameters (either POST or GET)
  26. * return nothing
  27. */
  28. protected function ReadContext()
  29. {
  30. $this->aValues = array();
  31. foreach($this->aNames as $sName)
  32. {
  33. $sValue = utils::ReadParam($sName, '');
  34. // TO DO: check if some of the context parameters are mandatory (or have default values)
  35. if (!empty($sValue))
  36. {
  37. $this->aValues[$sName] = $sValue;
  38. }
  39. }
  40. }
  41. /**
  42. * Returns the context as string with the format name1=value1&name2=value2....
  43. * return string The context as a string to be appended to an href property
  44. */
  45. public function GetForLink()
  46. {
  47. $aParams = array();
  48. foreach($this->aValues as $sName => $sValue)
  49. {
  50. $aParams[] = $sName.'='.urlencode($sValue);
  51. }
  52. return implode("&", $aParams);
  53. }
  54. /**
  55. * Returns the context as sequence of input tags to be inserted inside a <form> tag
  56. * return string The context as a sequence of <input type="hidden" /> tags
  57. */
  58. public function GetForForm()
  59. {
  60. $sContext = "";
  61. foreach($this->aValues as $sName => $sValue)
  62. {
  63. $sContext .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  64. }
  65. return $sContext;
  66. }
  67. /**
  68. * Returns the context as a hash array 'parameter_name' => value
  69. * return array The context information
  70. */
  71. public function GetAsHash()
  72. {
  73. return $this->aValues;
  74. }
  75. }
  76. ?>