Read custom dashboards with grid format

This commit is contained in:
Eric Espie
2026-01-15 10:19:33 +01:00
parent 15c8f5903b
commit 50ffaa2b55
6 changed files with 106 additions and 6 deletions

View File

@@ -5,6 +5,7 @@
* @license http://opensource.org/licenses/AGPL-3.0
*/
use Combodo\iTop\Application\Dashboard\Layout\DashboardLayoutGrid;
use Combodo\iTop\Application\UI\Base\Component\Button\ButtonUIBlockFactory;
use Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableSettings;
use Combodo\iTop\Application\UI\Base\Component\PopoverMenu\PopoverMenu;
@@ -38,7 +39,7 @@ abstract class Dashboard
protected $sLayoutClass;
/** @var array $aWidgetsData */
protected $aWidgetsData;
/** @var \DOMNode|null $oDOMNode */
/** @var DesignElement|null $oDOMNode */
protected $oDOMNode;
/** @var string $sId */
protected $sId;
@@ -47,6 +48,9 @@ abstract class Dashboard
/** @var \ModelReflection $oMetaModel */
protected $oMetaModel;
/** @var array Array of dashlets with position */
protected array $aGridDashlets = [];
/**
* Dashboard constructor.
*
@@ -81,10 +85,15 @@ abstract class Dashboard
/**
* @param \DOMDocument $oDoc
*/
public function FromDOMDocument(DOMDocument $oDoc)
public function FromDOMDocument(DesignDocument $oDoc)
{
$this->oDOMNode = $oDoc->getElementsByTagName('dashboard')->item(0);
if ($this->oDOMNode->getElementsByTagName('cells')->count() === 0) {
$this->FromDOMDocumentV2($oDoc);
return;
}
if ($oLayoutNode = $this->oDOMNode->getElementsByTagName('layout')->item(0)) {
$this->sLayoutClass = $oLayoutNode->textContent;
} else {
@@ -150,6 +159,36 @@ abstract class Dashboard
}
}
/**
* @param \DOMDocument $oDoc
*/
public function FromDOMDocumentV2(DesignDocument $oDoc)
{
$this->oDOMNode = $oDoc->getElementsByTagName('dashboard')->item(0);
$this->sLayoutClass = DashboardLayoutGrid::class;
$this->sTitle = $this->oDOMNode->GetChildText('title', '');
$iRefresh = intval($this->oDOMNode->GetChildText('refresh', '0'));
$this->bAutoReload = $iRefresh > 0;
$this->iAutoReloadSec = $iRefresh;
$oDashletsNode = $this->oDOMNode->GetUniqueElement('pos_dashlets');
$oDashletList = $oDashletsNode->getElementsByTagName('pos_dashlet');
foreach ($oDashletList as $oPosDashletNode) {
$aGridDashlet = [];
$aGridDashlet['position_x'] = intval($oPosDashletNode->GetChildText('position_x', '0'));
$aGridDashlet['position_y'] = intval($oPosDashletNode->GetChildText('position_y', '0'));
$aGridDashlet['width'] = intval($oPosDashletNode->GetChildText('width', '2'));
$aGridDashlet['height'] = intval($oPosDashletNode->GetChildText('height', '1'));
$oDashletNode = $oPosDashletNode->GetUniqueElement('dashlet');
$aGridDashlet['dashlet'] = $this->InitDashletFromDOMNode($oDashletNode);
$sId = $oPosDashletNode->getAttribute('id');
$this->aGridDashlets[$sId] = $aGridDashlet;
}
}
/**
* @param \DOMElement $oDomNode
*
@@ -524,7 +563,7 @@ EOF
{
$aExtraParams['dashboard_div_id'] = utils::Sanitize($aExtraParams['dashboard_div_id'] ?? null, $this->GetId(), utils::ENUM_SANITIZATION_FILTER_ELEMENT_IDENTIFIER);
/** @var \DashboardLayoutMultiCol $oLayout */
/** @var \DashboardLayout $oLayout */
$oLayout = new $this->sLayoutClass();
foreach ($this->aCells as $iCellIdx => $aDashlets) {
@@ -534,7 +573,13 @@ EOF
}
}
$oDashboard = $oLayout->Render($oPage, $this->aCells, $bEditMode, $aExtraParams);
if (count($this->aCells) > 0) {
$aDashlets = $this->aCells;
} else {
$aDashlets = $this->aGridDashlets;
}
$oDashboard = $oLayout->Render($oPage, $aDashlets, $bEditMode, $aExtraParams);
$oPage->AddUiBlock($oDashboard);
$bFromDasboardPage = isset($aExtraParams['from_dashboard_page']) ? isset($aExtraParams['from_dashboard_page']) : false;

View File

@@ -30,7 +30,7 @@ use Combodo\iTop\Application\WebPage\WebPage;
*/
abstract class DashboardLayout
{
abstract public function Render($oPage, $aDashlets, $bEditMode = false);
abstract public function Render($oPage, $aDashlets, $bEditMode = false, array $aExtraParams = []);
/**
* @param int $iCellIdx

View File

@@ -133,6 +133,7 @@ return array(
'Combodo\\iTop\\Application\\Dashboard\\FormBlock\\DashboardFormBlock' => $baseDir . '/sources/Application/Dashboard/FormBlock/DashboardFormBlock.php',
'Combodo\\iTop\\Application\\Dashboard\\FormBlock\\DashletFormBlock' => $baseDir . '/sources/Application/Dashboard/FormBlock/DashletFormBlock.php',
'Combodo\\iTop\\Application\\Dashboard\\FormBlock\\DashletPropertiesFormBlock' => $baseDir . '/sources/Application/Dashboard/FormBlock/DashletPropertiesFormBlock.php',
'Combodo\\iTop\\Application\\Dashboard\\Layout\\DashboardLayoutGrid' => $baseDir . '/sources/Application/Dashboard/Layout/DashboardLayoutGrid.php',
'Combodo\\iTop\\Application\\EventRegister\\ApplicationEvents' => $baseDir . '/sources/Application/EventRegister/ApplicationEvents.php',
'Combodo\\iTop\\Application\\Helper\\CKEditorHelper' => $baseDir . '/sources/Application/Helper/CKEditorHelper.php',
'Combodo\\iTop\\Application\\Helper\\ExportHelper' => $baseDir . '/sources/Application/Helper/ExportHelper.php',

View File

@@ -519,6 +519,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
'Combodo\\iTop\\Application\\Dashboard\\FormBlock\\DashboardFormBlock' => __DIR__ . '/../..' . '/sources/Application/Dashboard/FormBlock/DashboardFormBlock.php',
'Combodo\\iTop\\Application\\Dashboard\\FormBlock\\DashletFormBlock' => __DIR__ . '/../..' . '/sources/Application/Dashboard/FormBlock/DashletFormBlock.php',
'Combodo\\iTop\\Application\\Dashboard\\FormBlock\\DashletPropertiesFormBlock' => __DIR__ . '/../..' . '/sources/Application/Dashboard/FormBlock/DashletPropertiesFormBlock.php',
'Combodo\\iTop\\Application\\Dashboard\\Layout\\DashboardLayoutGrid' => __DIR__ . '/../..' . '/sources/Application/Dashboard/Layout/DashboardLayoutGrid.php',
'Combodo\\iTop\\Application\\EventRegister\\ApplicationEvents' => __DIR__ . '/../..' . '/sources/Application/EventRegister/ApplicationEvents.php',
'Combodo\\iTop\\Application\\Helper\\CKEditorHelper' => __DIR__ . '/../..' . '/sources/Application/Helper/CKEditorHelper.php',
'Combodo\\iTop\\Application\\Helper\\ExportHelper' => __DIR__ . '/../..' . '/sources/Application/Helper/ExportHelper.php',

View File

@@ -0,0 +1,52 @@
<?php
/*
* @copyright Copyright (C) 2010-2026 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\Dashboard\Layout;
use Combodo\iTop\Application\UI\Base\Layout\Dashboard\DashboardGrid;
use Combodo\iTop\Application\UI\Base\Layout\Dashboard\DashboardLayout as DashboardLayoutUIBlock;
class DashboardLayoutGrid extends \DashboardLayout
{
public function Render($oPage, $aDashlets, $bEditMode = false, array $aExtraParams = [])
{
$oDashboardLayout = new DashboardLayoutUIBlock($aExtraParams['dashboard_div_id']);
$oDashboardGrid = new DashboardGrid();
$oDashboardLayout->SetGrid($oDashboardGrid);
foreach ($aDashlets as $sDashletId => $aPosDashlet) {
/** @var \Dashlet $oDashlet */
$oDashlet = $aPosDashlet['dashlet'];
if ($oDashlet::IsVisible()) {
$oDashlet->SetID($sDashletId);
$sDashletClass = get_class($oDashlet);
$aDashletDenormalizedProperties = $oDashlet->GetDenormalizedProperties();
$aDashletsInfo = $sDashletClass::GetInfo();
// Also set minimal height/width
$iPositionX = $aPosDashlet['position_x'];
$iPositionY = $aPosDashlet['position_y'];
$iWidth = max($aPosDashlet['width'], array_key_exists('min_width', $aDashletsInfo) ? $aDashletsInfo['min_width'] : 1);
$iHeight = max($aPosDashlet['height'], array_key_exists('min_height', $aDashletsInfo) ? $aDashletsInfo['min_height'] : 1);
$oDashboardGrid->AddDashlet($oDashlet->DoRender($oPage, $bEditMode, true /* bEnclosingDiv */, $aExtraParams), $sDashletId, $sDashletClass, $aDashletDenormalizedProperties, $iPositionX, $iPositionY, $iWidth, $iHeight);
}
}
return $oDashboardLayout;
}
/**
* @inheritDoc
*/
public function GetDashletCoordinates($iCellIdx)
{
$iColNumber = $iCellIdx % 12;
$iRowNumber = (int)floor($iCellIdx / 12);
return [$iColNumber, $iRowNumber];
}
}

View File

@@ -104,7 +104,8 @@ class DashboardController extends Controller
$oDashboard = new RuntimeDashboard($aValues['id']);
$oDomNode = $oDashboard->CreateEmptyDashboard();
XMLSerializer::GetInstance()->Serialize($aValues, $oDomNode, 'DashboardGrid', 'Dashboard');
$oDashboard->PersistDashboard($oDomNode->ownerDocument->saveXML());
$sXml = $oDomNode->ownerDocument->saveXML();
$oDashboard->PersistDashboard($sXml);
// }
// }
} catch (Exception $e) {