123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- require_once("../application/utils.inc.php");
- /**
- * Helper class to store and manipulate the parameters that make the application's context
- *
- * Usage:
- * 1) Build the application's context by constructing the object
- * (the object will read some of the page's parameters)
- *
- * 2) Add these parameters to hyperlinks or to forms using the helper, functions
- * GetForLink(), GetForForm() or GetAsHash()
- */
- class ApplicationContext
- {
- protected $aNames;
- protected $aValues;
- protected static $aDefaultValues; // Cache shared among all instances
-
- public function __construct()
- {
- $this->aNames = array(
- 'org_id', 'menu'
- );
- $this->ReadContext();
- }
-
- /**
- * Read the context directly in the PHP parameters (either POST or GET)
- * return nothing
- */
- protected function ReadContext()
- {
- if (empty(self::$aDefaultValues))
- {
- self::$aDefaultValues = array();
- foreach($this->aNames as $sName)
- {
- $sValue = utils::ReadParam($sName, '');
- // TO DO: check if some of the context parameters are mandatory (or have default values)
- if (!empty($sValue))
- {
- self::$aDefaultValues[$sName] = $sValue;
- }
- }
- }
- $this->aValues = self::$aDefaultValues;
- }
-
- /**
- * Returns the context as string with the format name1=value1&name2=value2....
- * return string The context as a string to be appended to an href property
- */
- public function GetForLink()
- {
- $aParams = array();
- foreach($this->aValues as $sName => $sValue)
- {
- $aParams[] = $sName.'='.urlencode($sValue);
- }
- return implode("&", $aParams);
- }
-
- /**
- * Returns the context as sequence of input tags to be inserted inside a <form> tag
- * return string The context as a sequence of <input type="hidden" /> tags
- */
- public function GetForForm()
- {
- $sContext = "";
- foreach($this->aValues as $sName => $sValue)
- {
- $sContext .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
- }
- return $sContext;
- }
- /**
- * Returns the context as a hash array 'parameter_name' => value
- * return array The context information
- */
- public function GetAsHash()
- {
- return $this->aValues;
- }
- /**
- * Removes the specified parameter from the context, for example when the same parameter
- * is already a search parameter
- * @param string $sParamName Name of the parameter to remove
- * @return none
- */
- public function Reset($sParamName)
- {
- if (isset($this->aValues[$sParamName]))
- {
- unset($this->aValues[$sParamName]);
- }
- }
- }
- ?>
|