From a34baf840a025ac9bb13b754da199e45ca0be635 Mon Sep 17 00:00:00 2001 From: Stephen Abello Date: Tue, 3 Dec 2024 10:41:25 +0100 Subject: [PATCH] =?UTF-8?q?=20N=C2=B07995=20-=20Allow=20to=20redefine=20po?= =?UTF-8?q?rtal=20twig=20template=20for=20all=20bricks=20in=20a=20portal?= =?UTF-8?q?=20(#686)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * N°7995 - Allow to redefine portal twig template for all bricks in a portal * Apply modifications from code review * Fix variable name * Apply changes from code review --- .../portal/src/Brick/AbstractBrick.php | 34 +++++++++++++++++++ .../portal/src/Brick/BrickCollection.php | 15 +++++++- .../PortalXmlConfiguration/Basic.php | 9 +++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/datamodels/2.x/itop-portal-base/portal/src/Brick/AbstractBrick.php b/datamodels/2.x/itop-portal-base/portal/src/Brick/AbstractBrick.php index 599429ece..03561dec5 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/Brick/AbstractBrick.php +++ b/datamodels/2.x/itop-portal-base/portal/src/Brick/AbstractBrick.php @@ -659,4 +659,38 @@ abstract class AbstractBrick return $this; } + /** + * Load brick configuration that is not part of the brick definition but is part of the portal global properties. + * + * @param $aPortalProperties + * + * @return void + * @throws \DOMFormatException + * @since 3.2.1 + */ + public function LoadFromPortalProperties($aPortalProperties) + { + // Get the bricks templates + $aBricksTemplates = $aPortalProperties['templates']['bricks']; + $sClassFQCN = get_class($this); + + // Get the current brick templates + $aCurrentBricksTemplates = array_key_exists($sClassFQCN, $aBricksTemplates) ? $aBricksTemplates[$sClassFQCN] : []; + foreach($aCurrentBricksTemplates as $sTemplateKey => $sTemplate) { + // Clean the template id + $sTemplateId = str_ireplace($sClassFQCN.':', '', $sTemplateKey); + + // Call the set method for the template + $sSetTemplateMethodName = 'Set'.$sTemplateId.'TemplatePath'; + + if(method_exists($this, $sSetTemplateMethodName)) { + $this->{$sSetTemplateMethodName}($sTemplate); + } + else { + throw new DOMFormatException( + 'Template "'.$sTemplateId.'" is not a valid template for brick ' . $sClassFQCN, + null, null); + } + } + } } diff --git a/datamodels/2.x/itop-portal-base/portal/src/Brick/BrickCollection.php b/datamodels/2.x/itop-portal-base/portal/src/Brick/BrickCollection.php index 167b4fc11..5552fdeb4 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/Brick/BrickCollection.php +++ b/datamodels/2.x/itop-portal-base/portal/src/Brick/BrickCollection.php @@ -21,6 +21,7 @@ namespace Combodo\iTop\Portal\Brick; use DOMFormatException; use Exception; +use Symfony\Component\DependencyInjection\ContainerInterface; use UserRights; use ModuleDesign; use Combodo\iTop\Portal\Helper\ApplicationHelper; @@ -47,15 +48,21 @@ class BrickCollection private $aHomeOrdering; /** @var array $aNavigationMenuOrdering */ private $aNavigationMenuOrdering; + /** @var \array $aCombodoPortalInstanceConf + * @since 3.2.1 + */ + private $aCombodoPortalInstanceConf; /** * BrickCollection constructor. * * @param \ModuleDesign $oModuleDesign + * @param $aCombodoPortalInstanceConf * * @throws \Exception + * @since 3.2.1 Added $aCombodoPortalInstanceConf parameter */ - public function __construct(ModuleDesign $oModuleDesign) + public function __construct(ModuleDesign $oModuleDesign, $aCombodoPortalInstanceConf) { $this->oModuleDesign = $oModuleDesign; $this->aAllowedBricks = null; @@ -63,6 +70,7 @@ class BrickCollection $this->iDisplayedInNavigationMenu = 0; $this->aHomeOrdering = array(); $this->aNavigationMenuOrdering = array(); + $this->aCombodoPortalInstanceConf = $aCombodoPortalInstanceConf; $this->Load(); } @@ -196,6 +204,11 @@ class BrickCollection { /** @var \Combodo\iTop\Portal\Brick\PortalBrick $oBrick */ $oBrick = new $sBrickClass(); + + // Load the portal properties that are common to all bricks of this type + $oBrick->LoadFromPortalProperties($this->aCombodoPortalInstanceConf['properties']); + + // Load the brick specific properties from its XML definition $oBrick->LoadFromXml($oBrickNode); $aBricks[] = $oBrick; diff --git a/datamodels/2.x/itop-portal-base/portal/src/DependencyInjection/SilexCompatBootstrap/PortalXmlConfiguration/Basic.php b/datamodels/2.x/itop-portal-base/portal/src/DependencyInjection/SilexCompatBootstrap/PortalXmlConfiguration/Basic.php index d9f870eb7..fc6fb9432 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/DependencyInjection/SilexCompatBootstrap/PortalXmlConfiguration/Basic.php +++ b/datamodels/2.x/itop-portal-base/portal/src/DependencyInjection/SilexCompatBootstrap/PortalXmlConfiguration/Basic.php @@ -85,6 +85,7 @@ class Basic extends AbstractConfiguration 'templates' => array( 'layout' => 'itop-portal-base/portal/templates/layout.html.twig', 'home' => 'itop-portal-base/portal/templates/home/layout.html.twig', + 'bricks' => array(), ), 'urlmaker_class' => null, 'triggers_query' => null, @@ -185,6 +186,14 @@ class Basic extends AbstractConfiguration $aPortalConf['properties']['templates'][$sNodeId] = $oSubNode->GetText(null); break; default: + // Try to accept the value as a global brick template, brick id format is "FQCN:page" + [$sBrickFQCN, $sPage] = explode(':', $sNodeId); + if (utils::IsNotNullOrEmptyString($sBrickFQCN) && utils::IsNotNullOrEmptyString($sPage)) + { + $aPortalConf['properties']['templates']['bricks'][$sBrickFQCN][$sPage] = $oSubNode->GetText(null); + break; + } + throw new DOMFormatException( 'Value "'.$sNodeId.'" is not handled for template[@id]', null, null, $oSubNode