applicationcontext.class.inc.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * Interface for directing end-users to the relevant application
  27. */
  28. interface iDBObjectURLMaker
  29. {
  30. public static function MakeObjectURL($sClass, $iId);
  31. }
  32. /**
  33. * Direct end-users to the standard iTop application: UI.php
  34. */
  35. class iTopStandardURLMaker implements iDBObjectURLMaker
  36. {
  37. public static function MakeObjectURL($sClass, $iId)
  38. {
  39. $sPage = DBObject::ComputeStandardUIPage($sClass);
  40. $sAbsoluteUrl = utils::GetAbsoluteUrlAppRoot();
  41. $sUrl = "{$sAbsoluteUrl}pages/$sPage?operation=details&class=$sClass&id=$iId";
  42. return $sUrl;
  43. }
  44. }
  45. /**
  46. * Direct end-users to the standard Portal application
  47. */
  48. class PortalURLMaker implements iDBObjectURLMaker
  49. {
  50. public static function MakeObjectURL($sClass, $iId)
  51. {
  52. $sAbsoluteUrl = utils::GetAbsoluteUrlAppRoot();
  53. $sUrl = "{$sAbsoluteUrl}portal/index.php?operation=details&class=$sClass&id=$iId";
  54. return $sUrl;
  55. }
  56. }
  57. /**
  58. * Helper class to store and manipulate the parameters that make the application's context
  59. *
  60. * Usage:
  61. * 1) Build the application's context by constructing the object
  62. * (the object will read some of the page's parameters)
  63. *
  64. * 2) Add these parameters to hyperlinks or to forms using the helper, functions
  65. * GetForLink(), GetForForm() or GetAsHash()
  66. */
  67. class ApplicationContext
  68. {
  69. protected $aNames;
  70. protected $aValues;
  71. protected static $aDefaultValues; // Cache shared among all instances
  72. public function __construct($bReadContext = true)
  73. {
  74. $this->aNames = array(
  75. 'org_id', 'menu'
  76. );
  77. if ($bReadContext)
  78. {
  79. $this->ReadContext();
  80. }
  81. }
  82. /**
  83. * Read the context directly in the PHP parameters (either POST or GET)
  84. * return nothing
  85. */
  86. protected function ReadContext()
  87. {
  88. if (!isset(self::$aDefaultValues))
  89. {
  90. self::$aDefaultValues = array();
  91. $aContext = utils::ReadParam('c', array());
  92. foreach($this->aNames as $sName)
  93. {
  94. $sValue = isset($aContext[$sName]) ? $aContext[$sName] : '';
  95. // TO DO: check if some of the context parameters are mandatory (or have default values)
  96. if (!empty($sValue))
  97. {
  98. self::$aDefaultValues[$sName] = $sValue;
  99. }
  100. // Hmm, there must be a better (more generic) way to handle the case below:
  101. // When there is only one possible (allowed) organization, the context must be
  102. // fixed to this org
  103. if ($sName == 'org_id')
  104. {
  105. if (MetaModel::IsValidClass('Organization'))
  106. {
  107. $oSearchFilter = new DBObjectSearch('Organization');
  108. $oSet = new CMDBObjectSet($oSearchFilter);
  109. $iCount = $oSet->Count();
  110. if ($iCount == 1)
  111. {
  112. // Only one possible value for org_id, set it in the context
  113. $oOrg = $oSet->Fetch();
  114. self::$aDefaultValues[$sName] = $oOrg->GetKey();
  115. }
  116. }
  117. }
  118. }
  119. }
  120. $this->aValues = self::$aDefaultValues;
  121. }
  122. /**
  123. * Returns the current value for the given parameter
  124. * @param string $sParamName Name of the parameter to read
  125. * @return mixed The value for this parameter
  126. */
  127. public function GetCurrentValue($sParamName, $defaultValue = '')
  128. {
  129. if (isset($this->aValues[$sParamName]))
  130. {
  131. return $this->aValues[$sParamName];
  132. }
  133. return $defaultValue;
  134. }
  135. /**
  136. * Returns the context as string with the format name1=value1&name2=value2....
  137. * return string The context as a string to be appended to an href property
  138. */
  139. public function GetForLink()
  140. {
  141. $aParams = array();
  142. foreach($this->aValues as $sName => $sValue)
  143. {
  144. $aParams[] = "c[$sName]".'='.urlencode($sValue);
  145. }
  146. return implode("&", $aParams);
  147. }
  148. /**
  149. * Returns the context as sequence of input tags to be inserted inside a <form> tag
  150. * return string The context as a sequence of <input type="hidden" /> tags
  151. */
  152. public function GetForForm()
  153. {
  154. $sContext = "";
  155. foreach($this->aValues as $sName => $sValue)
  156. {
  157. $sContext .= "<input type=\"hidden\" name=\"c[$sName]\" value=\"".htmlentities($sValue, ENT_QUOTES, 'UTF-8')."\" />\n";
  158. }
  159. return $sContext;
  160. }
  161. /**
  162. * Returns the context as a hash array 'parameter_name' => value
  163. * return array The context information
  164. */
  165. public function GetAsHash()
  166. {
  167. $aReturn = array();
  168. foreach($this->aValues as $sName => $sValue)
  169. {
  170. $aReturn["c[$sName]"] = $sValue;
  171. }
  172. return $aReturn;
  173. }
  174. /**
  175. * Returns an array of the context parameters NAMEs
  176. * @return array The list of context parameters
  177. */
  178. public function GetNames()
  179. {
  180. return $this->aNames;
  181. }
  182. /**
  183. * Removes the specified parameter from the context, for example when the same parameter
  184. * is already a search parameter
  185. * @param string $sParamName Name of the parameter to remove
  186. * @return none
  187. */
  188. public function Reset($sParamName)
  189. {
  190. if (isset($this->aValues[$sParamName]))
  191. {
  192. unset($this->aValues[$sParamName]);
  193. }
  194. }
  195. /**
  196. * Initializes the given object with the default values provided by the context
  197. */
  198. public function InitObjectFromContext(DBObject &$oObj)
  199. {
  200. $sClass = get_class($oObj);
  201. foreach($this->GetNames() as $key)
  202. {
  203. $aCallSpec = array($sClass, 'MapContextParam');
  204. if (is_callable($aCallSpec))
  205. {
  206. $sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter
  207. }
  208. if (MetaModel::IsValidAttCode($sClass, $sAttCode))
  209. {
  210. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  211. if ($oAttDef->IsWritable())
  212. {
  213. $value = $this->GetCurrentValue($key, null);
  214. if (!is_null($value))
  215. {
  216. $oObj->Set($sAttCode, $value);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. static $m_sUrlMakerClass = null;
  223. /**
  224. * Set the current application url provider
  225. * @param sClass string Class implementing iDBObjectURLMaker
  226. * @return void
  227. */
  228. public static function SetUrlMakerClass($sClass = 'iTopStandardURLMaker')
  229. {
  230. $sPrevious = self::GetUrlMakerClass();
  231. self::$m_sUrlMakerClass = $sClass;
  232. $_SESSION['UrlMakerClass'] = $sClass;
  233. return $sPrevious;
  234. }
  235. /**
  236. * Get the current application url provider
  237. * @return string the name of the class
  238. */
  239. public static function GetUrlMakerClass()
  240. {
  241. if (is_null(self::$m_sUrlMakerClass))
  242. {
  243. if (isset($_SESSION['UrlMakerClass']))
  244. {
  245. self::$m_sUrlMakerClass = $_SESSION['UrlMakerClass'];
  246. }
  247. else
  248. {
  249. self::$m_sUrlMakerClass = 'iTopStandardURLMaker';
  250. }
  251. }
  252. return self::$m_sUrlMakerClass;
  253. }
  254. /**
  255. * Get the current application url provider
  256. * @return string the name of the class
  257. */
  258. public static function MakeObjectUrl($sObjClass, $sObjKey, $sUrlMakerClass = null, $bWithNavigationContext = true)
  259. {
  260. $oAppContext = new ApplicationContext();
  261. if (is_null($sUrlMakerClass))
  262. {
  263. $sUrlMakerClass = self::GetUrlMakerClass();
  264. }
  265. $sUrl = call_user_func(array($sUrlMakerClass, 'MakeObjectUrl'), $sObjClass, $sObjKey);
  266. if (strlen($sUrl) > 0)
  267. {
  268. if ($bWithNavigationContext)
  269. {
  270. return $sUrl."&".$oAppContext->GetForLink();
  271. }
  272. else
  273. {
  274. return $sUrl;
  275. }
  276. }
  277. else
  278. {
  279. return '';
  280. }
  281. }
  282. protected static $m_aPluginProperties = null;
  283. /**
  284. * Load plugin properties for the current session
  285. * @return void
  286. */
  287. protected static function LoadPluginProperties()
  288. {
  289. if (isset($_SESSION['PluginProperties']))
  290. {
  291. self::$m_aPluginProperties = $_SESSION['PluginProperties'];
  292. }
  293. else
  294. {
  295. self::$m_aPluginProperties = array();
  296. }
  297. }
  298. /**
  299. * Set plugin properties
  300. * @param sPluginClass string Class implementing any plugin interface
  301. * @param sProperty string Name of the property
  302. * @param value scalar Value (numeric or string)
  303. * @return void
  304. */
  305. public static function SetPluginProperty($sPluginClass, $sProperty, $value)
  306. {
  307. if (is_null(self::$m_aPluginProperties)) self::LoadPluginProperties();
  308. self::$m_aPluginProperties[$sPluginClass][$sProperty] = $value;
  309. $_SESSION['PluginProperties'][$sPluginClass][$sProperty] = $value;
  310. }
  311. /**
  312. * Get plugin properties
  313. * @param sPluginClass string Class implementing any plugin interface
  314. * @return array of sProperty=>value pairs
  315. */
  316. public static function GetPluginProperties($sPluginClass)
  317. {
  318. if (is_null(self::$m_aPluginProperties)) self::LoadPluginProperties();
  319. if (array_key_exists($sPluginClass, self::$m_aPluginProperties))
  320. {
  321. return self::$m_aPluginProperties[$sPluginClass];
  322. }
  323. else
  324. {
  325. return array();
  326. }
  327. }
  328. }
  329. ?>