\n"; print_r($oXml); echo "\n"; } if (isset($oXml->title)) { $sTitle = (string)$oXml->title; } if (isset($oXml->type)) { $sType = (string)$oXml->type; } else { $sType = 'table'; } if (!isset($oXml->sql)) { throw new Exception('Missing tag "sql" in sqlblock'); } $sQuery = (string)$oXml->sql; $aColumns = array(); if (isset($oXml->column)) { foreach ($oXml->column AS $oColumnData) { if (!isset($oColumnData->name)) { throw new Exception("Missing tag 'name' in sqlblock/column"); } $sName = (string) $oColumnData->name; if (strlen($sName) == 0) { throw new Exception("Empty tag 'name' in sqlblock/column"); } $aColumns[$sName] = array(); if (isset($oColumnData->label)) { $sLabel = (string)$oColumnData->label; if (strlen($sLabel) > 0) { $aColumns[$sName]['label'] = Dict::S($sLabel); } } if (isset($oColumnData->drilldown)) { $sDrillDown = (string)$oColumnData->drilldown; if (strlen($sDrillDown) > 0) { $aColumns[$sName]['drilldown'] = $sDrillDown; } } } } $aParams = array(); if (isset($oXml->parameter)) { foreach ($oXml->parameter AS $oParamData) { if (!isset($oParamData->name)) { throw new Exception("Missing tag 'name' for parameter in sqlblock/column"); } $sName = (string) $oParamData->name; if (strlen($sName) == 0) { throw new Exception("Empty tag 'name' for parameter in sqlblock/column"); } if (!isset($oParamData->mapping)) { throw new Exception("Missing tag 'mapping' for parameter in sqlblock/column"); } $sMapping = (string) $oParamData->mapping; if (strlen($sMapping) == 0) { throw new Exception("Empty tag 'mapping' for parameter in sqlblock/column"); } if (isset($oParamData->type)) { $sParamType = $oParamData->type; } else { $sParamType = 'context'; } $aParams[$sName] = array('mapping' => $sMapping, 'type' => $sParamType); } } return new SqlBlock($sQuery, $aColumns, $sTitle, $sType, $aParams); } /** * Applies the defined parameters into the SQL query * @return string the SQL query to execute */ public function BuildQuery() { $oAppContext = new ApplicationContext(); $sQuery = $this->m_sQuery; $sQuery = str_replace('$DB_PREFIX$', MetaModel::GetConfig()->GetDBSubname(), $sQuery); // put the tables DB prefix (if any) foreach($this->m_aParams as $sName => $aParam) { if ($aParam['type'] == 'context') { $sSearchPattern = '/\$CONDITION\('.$sName.',([^\)]+)\)\$/'; $value = $oAppContext->GetCurrentValue($aParam['mapping']); if (empty($value)) { $sSQLExpr = '(1)'; } else { // Special case for managing the hierarchy of organizations if (($aParam['mapping'] == 'org_id') && ( MetaModel::IsValidClass('Organization'))) { $sHierarchicalKeyCode = MetaModel::IsHierarchicalClass('Organization'); if ($sHierarchicalKeyCode != false) { // organizations are in hierarchy... gather all the orgs below the given one... $sOQL = "SELECT Organization AS node JOIN Organization AS root ON node.$sHierarchicalKeyCode BELOW root.id WHERE root.id = :value"; $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL), array(), array('value' => $value)); $aOrgIds = array(); while($oOrg = $oSet->Fetch()) { $aOrgIds[]= $oOrg->GetKey(); } $sSQLExpr = '($1 IN('.implode(',', $aOrgIds).'))'; } else { $sSQLExpr = '($1 = '.CMDBSource::Quote($value).')'; } } else { $sSQLExpr = '($1 = '.CMDBSource::Quote($value).')'; } } $sQuery = preg_replace($sSearchPattern, $sSQLExpr, $sQuery); } } return $sQuery; } public function RenderContent(WebPage $oPage, $aExtraParams = array()) { if (empty($aExtraParams['currentId'])) { $sId = 'sqlblock_'.$oPage->GetUniqueId(); // Works only if the page is not an Ajax one ! } else { $sId = $aExtraParams['currentId']; } // $oPage->add($this->GetRenderContent($oPage, $aExtraParams, $sId)); $sQuery = $this->BuildQuery(); $res = CMDBSource::Query($sQuery); $aQueryCols = CMDBSource::GetColumns($res); // Prepare column definitions (check + give default values) // foreach($this->m_aColumns as $sName => $aColumnData) { if (!in_array($sName, $aQueryCols)) { throw new Exception("Unknown column name '$sName' in sqlblock column"); } if (!isset($aColumnData['label'])) { $this->m_aColumns[$sName]['label'] = $sName; } if (isset($aColumnData['drilldown']) && !empty($aColumnData['drilldown'])) { // Check if the OQL is valid try { $this->m_aColumns[$sName]['filter'] = DBObjectSearch::FromOQL($aColumnData['drilldown']); } catch(OQLException $e) { unset($aColumnData['drilldown']); } } } if (strlen($this->m_sTitle) > 0) { $oPage->add("