From 4203382920fc98f54d168689707359a5e61364ff Mon Sep 17 00:00:00 2001 From: Molkobain Date: Mon, 13 Feb 2023 16:13:51 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B02783=20-=20Add=20support=20for=20custom?= =?UTF-8?q?=20zlists=20(#389)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup/compiler.class.inc.php | 84 ++++++++++++++----- setup/itopdesignformat.class.inc.php | 4 + .../Convert-samples/3.1_to_3.0.expected.xml | 6 ++ .../Convert-samples/3.1_to_3.0.input.xml | 22 +++++ 4 files changed, 97 insertions(+), 19 deletions(-) diff --git a/setup/compiler.class.inc.php b/setup/compiler.class.inc.php index b73bdc357..4cabc169f 100644 --- a/setup/compiler.class.inc.php +++ b/setup/compiler.class.inc.php @@ -95,6 +95,11 @@ class MFCompiler protected $oFactory; protected $aRootClasses; + /** + * @var array $aCustomListsCodes Codes of the custom zlists + * @since 3.1.0 + */ + protected $aCustomListsCodes; protected $aLog; protected $sMainPHPCode; // Code that goes into core/main.php protected $aSnippets; @@ -652,7 +657,21 @@ EOF; $aWebservicesFiles[] = "MetaModel::IncludeModule(MODULESROOT.'/$sRelativeDir/$sRelFileName');"; } } // foreach module - + + // Register custom zlists + $this->sMainPHPCode .= <<aCustomListsCodes as $sCustomListCode) { + // Note: HEREDOC used to ease finding of \MetaModel::RegisterZList() method usages + $this->sMainPHPCode .= << 'Custom zlist $sCustomListCode', 'type' => 'attributes']); +PHP; + } + $this->sMainPHPCode .= "\n"; + // Compile the dictionaries -out of the modules // $sDictDir = $sTempTargetDir.'/dictionaries'; @@ -2192,32 +2211,40 @@ EOF; // ZLists // - $aListRef = array( + $oPresentation = $oClass->GetUniqueElement('presentation'); + $sZlists = ''; + + // - Standard zlists + $aListRef = [ 'details' => 'details', 'standard_search' => 'search', 'default_search' => 'default_search', 'list' => 'list', - ); - - $oPresentation = $oClass->GetUniqueElement('presentation'); - $sZlists = ''; - foreach ($aListRef as $sListCode => $sListTag) - { + ]; + foreach ($aListRef as $sListCode => $sListTag) { $oListNode = $oPresentation->GetOptionalElement($sListTag); - if ($oListNode) - { - $aAttributes = $oListNode->GetNodeAsArrayOfItems(); - if(!is_array($aAttributes)) - { - $aAttributes = array(); - } - $this->ArrayOfItemsToZList($aAttributes); - - $sZAttributes = var_export($aAttributes, true); - $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n"; + if ($oListNode) { + $sZlists .= $this->GeneratePhpCodeForZlist($sListCode, $oListNode); } } + // - Custom zlists + foreach ($oPresentation->GetNodes('custom_presentations/custom_presentation') as $oListNode) { + $sListCode = $oListNode->getAttribute('id'); + + // Cannot have a custom zlist with an ID that is among the reserved ones {@see $aListRef} + if (array_key_exists($sListCode, $aListRef)) { + throw new DOMFormatException('Custom zlist "'.$sListCode.'" cannot be compiled as it is using a reserved identifier ('.implode(', ', array_keys($aListRef)).')'); + } + + // Store custom zlist code for further registration + if (false === in_array($sListCode, $this->aCustomListsCodes)) { + $this->aCustomListsCodes[] = $sListCode; + } + + $sZlists .= "\n" . $this->GeneratePhpCodeForZlist($sListCode, $oListNode); + } + // Methods $oMethods = $oClass->GetUniqueElement('methods'); foreach($oMethods->getElementsByTagName('method') as $oMethod) @@ -3856,6 +3883,25 @@ EOF; return $sPHP; } + /** + * @param string $sListCode Code of the zlist, used in iTop code to retrieve a specific zlist + * @param \DOMNode $oListNode XML node to parse to retrieve the zlist attributes + * + * @return string PHP Code to declare a zlist + * @since 3.1.0 N°2783 + */ + private function GeneratePhpCodeForZlist(string $sListCode, DOMNode $oListNode): string + { + $aAttributes = $oListNode->GetNodeAsArrayOfItems(); + if(!is_array($aAttributes)) { + $aAttributes = array(); + } + $this->ArrayOfItemsToZList($aAttributes); + + $sZAttributes = var_export($aAttributes, true); + return " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n"; + } + /** * Write a file only if not exists * Also add some informations when write failure occurs diff --git a/setup/itopdesignformat.class.inc.php b/setup/itopdesignformat.class.inc.php index 9eeb4eb6d..4fe780457 100644 --- a/setup/itopdesignformat.class.inc.php +++ b/setup/itopdesignformat.class.inc.php @@ -1131,6 +1131,10 @@ class iTopDesignFormat // - remove display style $this->RemoveNodeFromXPath("/itop_design/classes//class/fields/field[@xsi:type='AttributeLinkedSet']/display_style"); $this->RemoveNodeFromXPath("/itop_design/classes//class/fields/field[@xsi:type='AttributeLinkedSetIndirect']/display_style"); + + // N°2783 Custom zlists + $this->RemoveNodeFromXPath("/itop_design/classes//class/presentation/custom_presentations"); + $this->RemoveNodeFromXPath("/itop_design/meta/presentation/custom_presentations"); } /** diff --git a/tests/php-unit-tests/unitary-tests/setup/iTopDesignFormat/Convert-samples/3.1_to_3.0.expected.xml b/tests/php-unit-tests/unitary-tests/setup/iTopDesignFormat/Convert-samples/3.1_to_3.0.expected.xml index 382302bd2..e89a5a501 100644 --- a/tests/php-unit-tests/unitary-tests/setup/iTopDesignFormat/Convert-samples/3.1_to_3.0.expected.xml +++ b/tests/php-unit-tests/unitary-tests/setup/iTopDesignFormat/Convert-samples/3.1_to_3.0.expected.xml @@ -1,6 +1,9 @@ + + + @@ -75,4 +78,7 @@ + + + diff --git a/tests/php-unit-tests/unitary-tests/setup/iTopDesignFormat/Convert-samples/3.1_to_3.0.input.xml b/tests/php-unit-tests/unitary-tests/setup/iTopDesignFormat/Convert-samples/3.1_to_3.0.input.xml index 8cae34fc4..face0a6db 100644 --- a/tests/php-unit-tests/unitary-tests/setup/iTopDesignFormat/Convert-samples/3.1_to_3.0.input.xml +++ b/tests/php-unit-tests/unitary-tests/setup/iTopDesignFormat/Convert-samples/3.1_to_3.0.input.xml @@ -1,6 +1,19 @@ + + + + + + + 10 + + + + + + @@ -138,4 +151,13 @@ ]]> + + + + + + + + +