From 2aae6cd744502146e55fc2764685adfef38cf9ee Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 14 Jan 2021 13:56:51 +0100 Subject: [PATCH] Add UIBlocks to twig (Use sub-blocks for alerts and fields) --- .../view/SelectUpdateFile.html.twig | 27 +++++++++++++++---- .../UI/Component/Alert/UIAlertNode.php | 25 ++++++++++++----- .../UI/Component/Field/UIFieldNode.php | 11 ++++---- .../UI/Component/Input/UIInputNode.php | 18 ++----------- .../UI/Base/Component/Alert/Alert.php | 13 ++++----- .../UI/Base/Component/Input/InputFactory.php | 15 ----------- .../base/components/alert/layout.html.twig | 12 +++++---- 7 files changed, 62 insertions(+), 59 deletions(-) diff --git a/datamodels/2.x/itop-core-update/view/SelectUpdateFile.html.twig b/datamodels/2.x/itop-core-update/view/SelectUpdateFile.html.twig index af14290a4..429ecce76 100644 --- a/datamodels/2.x/itop-core-update/view/SelectUpdateFile.html.twig +++ b/datamodels/2.x/itop-core-update/view/SelectUpdateFile.html.twig @@ -13,26 +13,42 @@ {% UIContentBlock {'container_class':'display_block display-files'} %} {% UIFieldSet {'legend':'iTopUpdate:UI:Status'|dict_s} %} -{% UIAlert ForInformation {'title':'', 'id':'header-requirements'} %} +{% UIAlert ForInformation {'title':'', 'id':'header-requirements', 'is_collapsible':false, 'is_closable':false} %} +{% UIHtml %} {{ 'iTopUpdate:UI:CanCoreUpdate:Loading'|dict_s }} +{% EndUIHtml %} {% EndUIAlert %} {% set sVersion = 'UI:iTopVersion:Long'|dict_format(constant('ITOP_APPLICATION'), constant('ITOP_VERSION'), constant('ITOP_REVISION'), constant('ITOP_BUILD_DATE')) %} {% UIField Small {'label':'iTopUpdate:UI:CurrentVersion'|dict_s} %} +{% UIHtml %} {{ sVersion }} +{% EndUIHtml %} {% EndUIField %} + {% UIField Small {'label':'iTopUpdate:UI:DiskFreeSpace'|dict_s} %} +{% UIHtml %} {{ sDiskFreeSpace }} +{% EndUIHtml %} {% EndUIField %} + {% UIField Small {'label':'iTopUpdate:UI:ItopDiskSpace'|dict_s, 'value_id':'itop-disk-space'} %} +{% UIHtml %} +{% EndUIHtml %} {% EndUIField %} + {% UIField Small {'label':'iTopUpdate:UI:DBDiskSpace'|dict_s, 'value_id':'db-disk-space'} %} +{% UIHtml %} +{% EndUIHtml %} {% EndUIField %} + {% UIField Small {'label':'iTopUpdate:UI:FileUploadMaxSize'|dict_s} %} +{% UIHtml %} {{ sFileUploadMaxSize }} +{% EndUIHtml %} {% EndUIField %} {% EndUIFieldSet %} @@ -57,7 +73,7 @@ {% EndUIHtml %} {% UIContentBlock {'container_class':'upload_container'} %} -{% UIInput WithLabel {'type':'file', 'id':'file', 'name':'file'} %} +{% UIInput Standard {'type':'file', 'id':'file', 'name':'file'} %} {% EndUIContentBlock %} {% UIHtml %} @@ -72,9 +88,10 @@ {% UIInput Standard {'type':'checkbox', 'id':'doFilesArchive', 'name':'doFilesArchive', 'value':'1', 'checked':true} %} {% EndUIField %} - {# #} - {#

#} - {# #} +{% UIHtml %} +

+{% EndUIHtml %} + {% EndUIForm %} {% EndUIFieldSet %} diff --git a/sources/application/TwigBase/UI/Component/Alert/UIAlertNode.php b/sources/application/TwigBase/UI/Component/Alert/UIAlertNode.php index f433f2dbe..57f24ff53 100644 --- a/sources/application/TwigBase/UI/Component/Alert/UIAlertNode.php +++ b/sources/application/TwigBase/UI/Component/Alert/UIAlertNode.php @@ -30,11 +30,8 @@ class UIAlertNode extends Node ->subcompile($oParams) ->raw(";\n") ->write("\$sTitle = \$aParams['title'] ?? '';\n") - ->write("\$sId = \$aParams['id'] ?? null;\n") - ->write("ob_start();\n") - ->subcompile($this->getNode('body')) - ->write("\$sContent = ob_get_contents();\n") - ->write("ob_end_clean();\n"); + ->write("\$sContent = \$aParams['content'] ?? '';\n") + ->write("\$sId = \$aParams['id'] ?? null;\n"); $sType = $this->getAttribute('type'); switch ($sType) { @@ -47,7 +44,17 @@ class UIAlertNode extends Node case 'WithBrandingPrimaryColor': case 'WithBrandingSecondaryColor': $compiler - ->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Alert\\AlertFactory::Make{$sType}(\$sTitle, \$sContent, \$sId);\n"); + ->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Alert\\AlertFactory::Make{$sType}(\$sTitle, \$sContent, \$sId);\n") + ->write("if (!\$aParams['is_collapsible'] ?? true) {\n") + ->indent() + ->write("\${$sBlockVar}->SetIsCollapsible(false);\n") + ->outdent() + ->write("}\n") + ->write("if (!\$aParams['is_closable'] ?? true) {\n") + ->indent() + ->write("\${$sBlockVar}->SetIsClosable(false);\n") + ->outdent() + ->write("}\n"); break; // TODO 3.0 add other Factory methods @@ -55,7 +62,11 @@ class UIAlertNode extends Node throw new SyntaxError(sprintf('%s: Bad type "%s" for %s at line %d', $this->getTemplateName(), $sType, $this->tag, $this->lineno), $this->lineno, $this->getSourceContext()); } - $compiler->write(UIBlockHelper::AddToParentBlock($sBlockVar)); + $compiler + ->write(UIBlockHelper::AddToParentBlock($sBlockVar)) + ->write(UIBlockHelper::PushParentBlock($sBlockVar)) + ->subcompile($this->getNode('body')) + ->write(UIBlockHelper::PopParentBlock()); } } diff --git a/sources/application/TwigBase/UI/Component/Field/UIFieldNode.php b/sources/application/TwigBase/UI/Component/Field/UIFieldNode.php index cef5ecd90..5126a987f 100644 --- a/sources/application/TwigBase/UI/Component/Field/UIFieldNode.php +++ b/sources/application/TwigBase/UI/Component/Field/UIFieldNode.php @@ -36,11 +36,8 @@ class UIFieldNode extends Node case 'Large': $compiler ->write("\$sLabel = \$aParams['label'] ?? '';\n") + ->write("\$sValue = \$aParams['value'] ?? '';\n") ->write("\$sValueId = \$aParams['value_id'] ?? null;\n") - ->write("ob_start();\n") - ->subcompile($this->getNode('body')) - ->write("\$sValue = ob_get_contents();\n") - ->write("ob_end_clean();\n") ->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Field\\FieldFactory::Make{$sType}(\$sLabel, \$sValue);\n") ->write("\${$sBlockVar}->SetValueId(\$sValueId);\n"); break; @@ -50,7 +47,11 @@ class UIFieldNode extends Node throw new SyntaxError(sprintf('%s: Bad type "%s" for %s at line %d', $this->getTemplateName(), $sType, $this->tag, $this->lineno), $this->lineno, $this->getSourceContext()); } - $compiler->write(UIBlockHelper::AddToParentBlock($sBlockVar)); + $compiler + ->write(UIBlockHelper::AddToParentBlock($sBlockVar)) + ->write(UIBlockHelper::PushParentBlock($sBlockVar)) + ->subcompile($this->getNode('body')) + ->write(UIBlockHelper::PopParentBlock()); } } diff --git a/sources/application/TwigBase/UI/Component/Input/UIInputNode.php b/sources/application/TwigBase/UI/Component/Input/UIInputNode.php index b785f27cf..33a2e1400 100644 --- a/sources/application/TwigBase/UI/Component/Input/UIInputNode.php +++ b/sources/application/TwigBase/UI/Component/Input/UIInputNode.php @@ -37,7 +37,7 @@ class UIInputNode extends Node ->write("\$sName = \$aParams['name'] ?? '';\n") ->write("\$sValue = \$aParams['value'] ?? '';\n") ->write("\$sId = \$aParams['id'] ?? null;\n") - ->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::Make{$sFactoryType}(\$sName, \$sValue, \$sId);\n"); + ->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::MakeForHidden(\$sName, \$sValue, \$sId);\n"); break; case 'Standard': @@ -46,7 +46,7 @@ class UIInputNode extends Node ->write("\$sName = \$aParams['name'] ?? '';\n") ->write("\$sValue = \$aParams['value'] ?? '';\n") ->write("\$sId = \$aParams['id'] ?? null;\n") - ->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::Make{$sFactoryType}(\$sType, \$sName, \$sValue, \$sId);\n") + ->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::MakeStandard(\$sType, \$sName, \$sValue, \$sId);\n") ->write("if (\$aParams['checked'] ?? false) {\n") ->indent() ->write("\${$sBlockVar}->SetChecked(true);\n") @@ -54,20 +54,6 @@ class UIInputNode extends Node ->write("}\n"); break; - case 'WithLabel': - $compiler - ->write("\$sLabel = \$aParams['label'] ?? '';\n") - ->write("\$sType = \$aParams['type'] ?? '';\n") - ->write("\$sName = \$aParams['name'] ?? '';\n") - ->write("\$sValue = \$aParams['value'] ?? '';\n") - ->write("\$sId = \$aParams['id'] ?? null;\n") - ->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::Make{$sFactoryType}(\$sType, \$sLabel, \$sName, \$sValue, \$sId);\n") - ->write("if (\$aParams['checked'] ?? false) {\n") - ->indent() - ->write("\${$sBlockVar}->GetInput()->SetChecked(true);\n") - ->outdent() - ->write("}\n"); - break; // TODO 3.0 add other Factory methods default: diff --git a/sources/application/UI/Base/Component/Alert/Alert.php b/sources/application/UI/Base/Component/Alert/Alert.php index c61963498..568afc735 100644 --- a/sources/application/UI/Base/Component/Alert/Alert.php +++ b/sources/application/UI/Base/Component/Alert/Alert.php @@ -20,7 +20,8 @@ namespace Combodo\iTop\Application\UI\Base\Component\Alert; -use Combodo\iTop\Application\UI\Base\UIBlock; +use Combodo\iTop\Application\UI\Base\Component\Html\Html; +use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock; /** * Class Alert @@ -29,7 +30,7 @@ use Combodo\iTop\Application\UI\Base\UIBlock; * @package Combodo\iTop\Application\UI\Base\Component\Alert * @since 3.0.0 */ -class Alert extends UIBlock +class Alert extends UIContentBlock { // Overloaded constants public const BLOCK_CODE = 'ibo-alert'; @@ -86,8 +87,6 @@ class Alert extends UIBlock /** @var string $sTitle */ protected $sTitle; - /** @var string $sContent The raw HTML content, must be already sanitized */ - protected $sContent; /** @var string $sColor */ protected $sColor; /** @var bool Whether the alert can be closed or not */ @@ -111,13 +110,15 @@ class Alert extends UIBlock */ public function __construct(string $sTitle = '', string $sContent = '', string $sColor = self::DEFAULT_COLOR, ?string $sId = null) { + parent::__construct($sId); $this->sTitle = $sTitle; - $this->sContent = $sContent; $this->sColor = $sColor; $this->bIsClosable = static::DEFAULT_IS_CLOSABLE; $this->bIsCollapsible = static::DEFAULT_IS_COLLAPSIBLE; $this->bIsOpenedByDefault = static::DEFAULT_IS_OPENED_BY_DEFAULT; - parent::__construct($sId); + if (!empty($sContent)) { + $this->AddSubBlock(new Html($sContent)); + } } /** diff --git a/sources/application/UI/Base/Component/Input/InputFactory.php b/sources/application/UI/Base/Component/Input/InputFactory.php index 573623aae..9ef0e70be 100644 --- a/sources/application/UI/Base/Component/Input/InputFactory.php +++ b/sources/application/UI/Base/Component/Input/InputFactory.php @@ -37,21 +37,6 @@ class InputFactory return $oInput; } - public static function MakeWithLabel(string $sType, string $sLabel, string $sName, string $sValue, ?string $sId = null): InputWithLabel - { - $oInput = new Input($sId); - - $oInput->SetType($sType) - ->SetName($sName) - ->SetValue($sValue); - - if (is_null($sId)) { - $sId = $oInput->GetId(); - } - - return new InputWithLabel($sLabel, $oInput, $sId); - } - /** * @see Field component that is better adapter when dealing with a standard iTop form * diff --git a/templates/base/components/alert/layout.html.twig b/templates/base/components/alert/layout.html.twig index 31bc0f980..66ac54110 100644 --- a/templates/base/components/alert/layout.html.twig +++ b/templates/base/components/alert/layout.html.twig @@ -14,11 +14,13 @@ {% endif %} {% if oUIBlock.GetTitle() is not empty %} -
{{ oUIBlock.GetTitle() }}
+
{{ oUIBlock.GetTitle() }}
{% endif %} - {% if oUIBlock.GetContent() is not empty %} -
{{ oUIBlock.GetContent()|raw }}
+ {% if oUIBlock.GetSubBlocks() is not empty %} +
+ {% for oSubBlock in oUIBlock.GetSubBlocks() %} + {{ render_block(oSubBlock, {aPage: aPage}) }} + {% endfor %} +
{% endif %} \ No newline at end of file