diff --git a/application/dashboard.class.inc.php b/application/dashboard.class.inc.php index a4e501468..6a748fc06 100644 --- a/application/dashboard.class.inc.php +++ b/application/dashboard.class.inc.php @@ -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; diff --git a/application/dashboardlayout.class.inc.php b/application/dashboardlayout.class.inc.php index a77c65f26..607c88795 100644 --- a/application/dashboardlayout.class.inc.php +++ b/application/dashboardlayout.class.inc.php @@ -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 diff --git a/lib/composer/autoload_classmap.php b/lib/composer/autoload_classmap.php index d9f068fdd..b8ec6b067 100644 --- a/lib/composer/autoload_classmap.php +++ b/lib/composer/autoload_classmap.php @@ -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', diff --git a/lib/composer/autoload_static.php b/lib/composer/autoload_static.php index 6ce49ef93..5f6d058e2 100644 --- a/lib/composer/autoload_static.php +++ b/lib/composer/autoload_static.php @@ -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', diff --git a/sources/Application/Dashboard/Layout/DashboardLayoutGrid.php b/sources/Application/Dashboard/Layout/DashboardLayoutGrid.php new file mode 100644 index 000000000..65b08b5b8 --- /dev/null +++ b/sources/Application/Dashboard/Layout/DashboardLayoutGrid.php @@ -0,0 +1,52 @@ +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]; + } +} diff --git a/sources/Controller/Base/Layout/DashboardController.php b/sources/Controller/Base/Layout/DashboardController.php index 8d3aca104..93797f882 100644 --- a/sources/Controller/Base/Layout/DashboardController.php +++ b/sources/Controller/Base/Layout/DashboardController.php @@ -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) {