N°2783 - Add support for custom zlists (#389)

This commit is contained in:
Molkobain
2023-02-13 16:13:51 +01:00
committed by GitHub
parent 825e402dd6
commit 4203382920
4 changed files with 97 additions and 19 deletions

View File

@@ -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 .= <<<PHP
/**
* Custom zlists
*/
PHP;
foreach ($this->aCustomListsCodes as $sCustomListCode) {
// Note: HEREDOC used to ease finding of \MetaModel::RegisterZList() method usages
$this->sMainPHPCode .= <<<PHP
MetaModel::RegisterZList('$sCustomListCode', ['description' => '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

View File

@@ -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");
}
/**

View File

@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0">
<classes>
<class id="ClassWithCustomZlist">
<presentation/>
</class>
<class id="ClassWithAttributeLinkedSetEditModeNone">
<fields>
<field id="status" xsi:type="AttributeLinkedSet">
@@ -75,4 +78,7 @@
</methods>
</class>
</classes>
<meta>
<presentation/>
</meta>
</itop_design>

View File

@@ -1,6 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1">
<classes>
<class id="ClassWithCustomZlist">
<presentation>
<custom_presentations>
<custom_presentation id="my-custom-list" _delta="define">
<items>
<item id="foo">
<rank>10</rank>
</item>
</items>
</custom_presentation>
</custom_presentations>
</presentation>
</class>
<class id="ClassWithAttributeLinkedSetEditModeNone">
<fields>
<field id="status" xsi:type="AttributeLinkedSet">
@@ -138,4 +151,13 @@
]]></code>
</listener>
</event_listeners>
<meta>
<presentation>
<custom_presentations>
<custom_presentation id="my-custom-list">
<description><![CDATA[Some description of the zlist]]></description>
</custom_presentation>
</custom_presentations>
</presentation>
</meta>
</itop_design>