XML data from dashboard

This commit is contained in:
Eric Espie
2026-02-03 11:02:39 +01:00
parent b2041e3b63
commit 0b17e1f9b3
3 changed files with 60 additions and 17 deletions

View File

@@ -166,24 +166,11 @@ class DashboardController extends Controller
throw new SecurityException('Invalid dashboard file !');
}
if (!filter_var(appUserPreferences::GetPref('display_original_dashboard_'.$sDashboardId, false), FILTER_VALIDATE_BOOLEAN)) {
// Search for an eventual user defined dashboard
$oUDSearch = new DBObjectSearch('UserDashboard');
$oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
$oUDSearch->AddCondition('menu_code', $sDashboardId, '=');
$oUDSet = new DBObjectSet($oUDSearch);
if ($oUDSet->Count() > 0) {
// Assuming there is at most one couple {user, menu}!
$oUserDashboard = $oUDSet->Fetch();
$sDashboardDefinition = $oUserDashboard->Get('contents');
} else {
$sDashboardDefinition = @file_get_contents($sDashboardFileSanitized);
}
} else {
$sDashboardDefinition = @file_get_contents($sDashboardFileSanitized);
}
/** @var \Combodo\iTop\Application\Dashboard\Dashboard $oDashboard */
$oDashboard = RuntimeDashboard::GetDashboard($sDashboardFile, $sDashboardId);
$sDashboardDefinition = $oDashboard->ToXML();
if (!is_null($oDashboard)) {
$oPage->TrashUnexpectedOutput();
$oPage->SetContentType('text/xml');

View File

@@ -91,6 +91,20 @@ abstract class Dashboard
$this->FromDOMDocument($oDoc);
}
/**
* @param string $sXml
*
* @throws \Exception
*/
public function ToXML(): string
{
$oDomNode = $this->CreateEmptyDashboard();
$aModelData = $this->ToModelData();
$this->oXMLSerializer->Serialize($aModelData, $oDomNode, 'DashboardGrid', 'Dashboard');
return $oDomNode->ownerDocument->saveXML();
}
/**
* @param \DOMDocument $oDoc
*/
@@ -484,4 +498,33 @@ JS
$this->aGridDashlets[] = $aGridDashlet;
}
}
public function ToModelData(): array
{
$aModelData = [];
$aModelData['id'] = $this->sId;
$aModelData['layout'] = $this->sLayoutClass;
$aModelData['title'] = $this->sTitle;
$aModelData['refresh'] = $this->bAutoReload ? $this->iAutoReloadSec : 0;
foreach ($this->aGridDashlets as $aGridDashlet) {
$aPosDashlet = [];
$aPosDashlet['position_x'] = $aGridDashlet['position_x'] ?? 0;
$aPosDashlet['position_y'] = $aGridDashlet['position_y'] ?? 0;
$aPosDashlet['width'] = $aGridDashlet['width'] ?? 2;
$aPosDashlet['height'] = $aGridDashlet['height'] ?? 1;
/** @var Dashlet $oDashlet */
$oDashlet = $aGridDashlet['dashlet'];
$aDashlet = [];
$aDashlet['type'] = $oDashlet->GetDashletType();
$sId = $oDashlet->GetID();
$aDashlet['id'] = $sId;
$aDashlet['properties'] = $oDashlet->ToModelData();
$aPosDashlet['dashlet'] = $aDashlet;
$aModelData['pos_dashlets'][$sId] = $aPosDashlet;
}
return $aModelData;
}
}

View File

@@ -213,6 +213,19 @@ abstract class Dashlet
$this->OnUpdate();
}
/**
* Load a dashboard using Model Data
*
* @param array $aModelData
*
* @return void
* @since 3.3.0
*/
public function ToModelData(): array
{
return $this->aProperties;
}
/**
* @return array Rel. path to the app. root of the JS files required by the dashlet
* @since 3.0.0