/** * Class ApplicationContext * * @copyright Copyright (C) 2010-2018 Combodo SARL * @license http://opensource.org/licenses/AGPL-3.0 */ require_once(APPROOT."/application/utils.inc.php"); /** * Interface for directing end-users to the relevant application */ interface iDBObjectURLMaker { /** * @param string $sClass * @param string $iId * * @return string */ public static function MakeObjectURL($sClass, $iId); } /** * Direct end-users to the standard iTop application: UI.php */ class iTopStandardURLMaker implements iDBObjectURLMaker { /** * @param string $sClass * @param string $iId * * @return string * @throws \Exception */ public static function MakeObjectURL($sClass, $iId) { $sPage = DBObject::ComputeStandardUIPage($sClass); $sAbsoluteUrl = utils::GetAbsoluteUrlAppRoot(); $sUrl = "{$sAbsoluteUrl}pages/$sPage?operation=details&class=$sClass&id=$iId"; return $sUrl; } } /** * Direct end-users to the standard Portal application */ class PortalURLMaker implements iDBObjectURLMaker { /** * @param string $sClass * @param string $iId * * @return string * @throws \Exception */ public static function MakeObjectURL($sClass, $iId) { $sAbsoluteUrl = utils::GetAbsoluteUrlAppRoot(); $sUrl = "{$sAbsoluteUrl}portal/index.php?operation=details&class=$sClass&id=$iId"; return $sUrl; } } /** * 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 { public static $m_sUrlMakerClass = null; protected static $m_aPluginProperties = null; protected static $aDefaultValues; // Cache shared among all instances protected $aNames; protected $aValues; /** * ApplicationContext constructor. * * @param bool $bReadContext * * @throws \Exception */ public function __construct($bReadContext = true) { $this->aNames = array( 'org_id', 'menu' ); if ($bReadContext) { $this->ReadContext(); } } /** * Read the context directly in the PHP parameters (either POST or GET) * return nothing * * @throws \Exception */ protected function ReadContext() { if (!isset(self::$aDefaultValues)) { self::$aDefaultValues = array(); $aContext = utils::ReadParam('c', array(), false, 'context_param'); foreach($this->aNames as $sName) { $sValue = isset($aContext[$sName]) ? $aContext[$sName] : ''; // TO DO: check if some of the context parameters are mandatory (or have default values) if (!empty($sValue)) { self::$aDefaultValues[$sName] = $sValue; } // Hmm, there must be a better (more generic) way to handle the case below: // When there is only one possible (allowed) organization, the context must be // fixed to this org unless there is only one organization in the system then // no filter is applied if ($sName == 'org_id') { if (MetaModel::IsValidClass('Organization')) { $oSearchFilter = new DBObjectSearch('Organization'); $oSet = new CMDBObjectSet($oSearchFilter); $iCount = $oSet->CountWithLimit(2); if ($iCount > 1) { $oSearchFilter->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true); $oSet = new CMDBObjectSet($oSearchFilter); $iCount = $oSet->CountWithLimit(2); if ($iCount == 1) { // Only one possible value for org_id, set it in the context $oOrg = $oSet->Fetch(); self::$aDefaultValues[$sName] = $oOrg->GetKey(); } } } } } } $this->aValues = self::$aDefaultValues; } /** * Returns the current value for the given parameter * * @param string $sParamName Name of the parameter to read * @param string $defaultValue * * @return mixed The value for this parameter */ public function GetCurrentValue($sParamName, $defaultValue = '') { if (isset($this->aValues[$sParamName])) { return $this->aValues[$sParamName]; } return $defaultValue; } /** * 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[] = "c[$sName]".'='.urlencode($sValue); } return implode("&", $aParams); } /** * @since 3.0.0 N°2534 - dashboard: bug with autorefresh that deactivates filtering on organisation * Returns the params as c[menu]:..., c[org_id]:.... * @return string The params */ public function GetForPostParams() { return json_encode( $this->aValues); } /** * Returns the context as sequence of input tags to be inserted inside a