applicationcontext.class.inc.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. $oSearchFilter->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true);
  109. $oSet = new CMDBObjectSet($oSearchFilter);
  110. $iCount = $oSet->Count();
  111. if ($iCount == 1)
  112. {
  113. // Only one possible value for org_id, set it in the context
  114. $oOrg = $oSet->Fetch();
  115. self::$aDefaultValues[$sName] = $oOrg->GetKey();
  116. }
  117. }
  118. }
  119. }
  120. }
  121. $this->aValues = self::$aDefaultValues;
  122. }
  123. /**
  124. * Returns the current value for the given parameter
  125. * @param string $sParamName Name of the parameter to read
  126. * @return mixed The value for this parameter
  127. */
  128. public function GetCurrentValue($sParamName, $defaultValue = '')
  129. {
  130. if (isset($this->aValues[$sParamName]))
  131. {
  132. return $this->aValues[$sParamName];
  133. }
  134. return $defaultValue;
  135. }
  136. /**
  137. * Returns the context as string with the format name1=value1&name2=value2....
  138. * return string The context as a string to be appended to an href property
  139. */
  140. public function GetForLink()
  141. {
  142. $aParams = array();
  143. foreach($this->aValues as $sName => $sValue)
  144. {
  145. $aParams[] = "c[$sName]".'='.urlencode($sValue);
  146. }
  147. return implode("&", $aParams);
  148. }
  149. /**
  150. * Returns the context as sequence of input tags to be inserted inside a <form> tag
  151. * return string The context as a sequence of <input type="hidden" /> tags
  152. */
  153. public function GetForForm()
  154. {
  155. $sContext = "";
  156. foreach($this->aValues as $sName => $sValue)
  157. {
  158. $sContext .= "<input type=\"hidden\" name=\"c[$sName]\" value=\"".htmlentities($sValue, ENT_QUOTES, 'UTF-8')."\" />\n";
  159. }
  160. return $sContext;
  161. }
  162. /**
  163. * Returns the context as a hash array 'parameter_name' => value
  164. * return array The context information
  165. */
  166. public function GetAsHash()
  167. {
  168. $aReturn = array();
  169. foreach($this->aValues as $sName => $sValue)
  170. {
  171. $aReturn["c[$sName]"] = $sValue;
  172. }
  173. return $aReturn;
  174. }
  175. /**
  176. * Returns an array of the context parameters NAMEs
  177. * @return array The list of context parameters
  178. */
  179. public function GetNames()
  180. {
  181. return $this->aNames;
  182. }
  183. /**
  184. * Removes the specified parameter from the context, for example when the same parameter
  185. * is already a search parameter
  186. * @param string $sParamName Name of the parameter to remove
  187. * @return none
  188. */
  189. public function Reset($sParamName)
  190. {
  191. if (isset($this->aValues[$sParamName]))
  192. {
  193. unset($this->aValues[$sParamName]);
  194. }
  195. }
  196. /**
  197. * Initializes the given object with the default values provided by the context
  198. */
  199. public function InitObjectFromContext(DBObject &$oObj)
  200. {
  201. $sClass = get_class($oObj);
  202. foreach($this->GetNames() as $key)
  203. {
  204. $aCallSpec = array($sClass, 'MapContextParam');
  205. if (is_callable($aCallSpec))
  206. {
  207. $sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter
  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. }
  223. static $m_sUrlMakerClass = null;
  224. /**
  225. * Set the current application url provider
  226. * @param sClass string Class implementing iDBObjectURLMaker
  227. * @return void
  228. */
  229. public static function SetUrlMakerClass($sClass = 'iTopStandardURLMaker')
  230. {
  231. $sPrevious = self::GetUrlMakerClass();
  232. self::$m_sUrlMakerClass = $sClass;
  233. $_SESSION['UrlMakerClass'] = $sClass;
  234. return $sPrevious;
  235. }
  236. /**
  237. * Get the current application url provider
  238. * @return string the name of the class
  239. */
  240. public static function GetUrlMakerClass()
  241. {
  242. if (is_null(self::$m_sUrlMakerClass))
  243. {
  244. if (isset($_SESSION['UrlMakerClass']))
  245. {
  246. self::$m_sUrlMakerClass = $_SESSION['UrlMakerClass'];
  247. }
  248. else
  249. {
  250. self::$m_sUrlMakerClass = 'iTopStandardURLMaker';
  251. }
  252. }
  253. return self::$m_sUrlMakerClass;
  254. }
  255. /**
  256. * Get the current application url provider
  257. * @return string the name of the class
  258. */
  259. public static function MakeObjectUrl($sObjClass, $sObjKey, $sUrlMakerClass = null, $bWithNavigationContext = true)
  260. {
  261. $oAppContext = new ApplicationContext();
  262. if (is_null($sUrlMakerClass))
  263. {
  264. $sUrlMakerClass = self::GetUrlMakerClass();
  265. }
  266. $sUrl = call_user_func(array($sUrlMakerClass, 'MakeObjectUrl'), $sObjClass, $sObjKey);
  267. if (strlen($sUrl) > 0)
  268. {
  269. if ($bWithNavigationContext)
  270. {
  271. return $sUrl."&".$oAppContext->GetForLink();
  272. }
  273. else
  274. {
  275. return $sUrl;
  276. }
  277. }
  278. else
  279. {
  280. return '';
  281. }
  282. }
  283. protected static $m_aPluginProperties = null;
  284. /**
  285. * Load plugin properties for the current session
  286. * @return void
  287. */
  288. protected static function LoadPluginProperties()
  289. {
  290. if (isset($_SESSION['PluginProperties']))
  291. {
  292. self::$m_aPluginProperties = $_SESSION['PluginProperties'];
  293. }
  294. else
  295. {
  296. self::$m_aPluginProperties = array();
  297. }
  298. }
  299. /**
  300. * Set plugin properties
  301. * @param sPluginClass string Class implementing any plugin interface
  302. * @param sProperty string Name of the property
  303. * @param value scalar Value (numeric or string)
  304. * @return void
  305. */
  306. public static function SetPluginProperty($sPluginClass, $sProperty, $value)
  307. {
  308. if (is_null(self::$m_aPluginProperties)) self::LoadPluginProperties();
  309. self::$m_aPluginProperties[$sPluginClass][$sProperty] = $value;
  310. $_SESSION['PluginProperties'][$sPluginClass][$sProperty] = $value;
  311. }
  312. /**
  313. * Get plugin properties
  314. * @param sPluginClass string Class implementing any plugin interface
  315. * @return array of sProperty=>value pairs
  316. */
  317. public static function GetPluginProperties($sPluginClass)
  318. {
  319. if (is_null(self::$m_aPluginProperties)) self::LoadPluginProperties();
  320. if (array_key_exists($sPluginClass, self::$m_aPluginProperties))
  321. {
  322. return self::$m_aPluginProperties[$sPluginClass];
  323. }
  324. else
  325. {
  326. return array();
  327. }
  328. }
  329. }
  330. ?>