From 97852bfc447ece2653877630837929c264466945 Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Fri, 22 Nov 2024 09:48:18 +0100 Subject: [PATCH] fix --- inc/abstractcontainerinstance.class.php | 35 +++++++++++++++++-------- setup.php | 4 ++- templates/container.class.tpl | 3 +-- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/inc/abstractcontainerinstance.class.php b/inc/abstractcontainerinstance.class.php index 3b15eab4..27faee4f 100644 --- a/inc/abstractcontainerinstance.class.php +++ b/inc/abstractcontainerinstance.class.php @@ -33,14 +33,26 @@ abstract class PluginFieldsAbstractContainerInstance extends CommonDBChild public static $itemtype = 'itemtype'; public static $items_id = 'items_id'; - + /** + * This function relies on the static property `static::$plugins_forward_entity`, + * which should be populated using the following method (from setup): + * + * Plugin::registerClass( + * PluginFields, + * ['forwardentityfrom' => ] + * ); + * + * However, the order in which plugins are loaded can affect the behavior. + * For example, if a container is defined on a `GenericObject` itemtype and + * the `fields` plugin initializes before the `genericobject` plugin, the + * `itemtype` for the container will not yet exist, leading to potential issues. + * + * Modification of this function to meet specific requirements. + */ public function addNeededInfoToInput($input) { - // is entity missing and forwarding on ? - if ($this->tryEntityForwarding() && !isset($input['entities_id'])) { - // Merge both arrays to ensure all the fields are defined for the following checks + if ($this->tryEntityForwarding()) { $completeinput = array_merge($this->fields, $input); - // Set the item to allow parent::prepareinputforadd to get the right item ... if ( $itemToGetEntity = static::getItemFromArray( static::$itemtype, @@ -51,12 +63,13 @@ public function addNeededInfoToInput($input) if ( ($itemToGetEntity instanceof CommonDBTM) ) { - $input['entities_id'] = $itemToGetEntity->getEntityID(); - $input['is_recursive'] = intval($itemToGetEntity->isRecursive()); - } else { - // No entity link : set default values - $input['entities_id'] = 0; - $input['is_recursive'] = 0; + if ($itemToGetEntity->isEntityAssign()) { + $input['entities_id'] = $itemToGetEntity->getEntityID(); + } + + if ($itemToGetEntity->maybeRecursive()) { + $input['is_recursive'] = intval($itemToGetEntity->isRecursive()); + } } } } diff --git a/setup.php b/setup.php index d47a31bd..eefb4be8 100644 --- a/setup.php +++ b/setup.php @@ -126,7 +126,8 @@ function plugin_init_fields() if (count($itemtypes) > 0) { Plugin::registerClass( 'PluginFieldsContainer', - ['addtabon' => $itemtypes], + ['addtabon' => $itemtypes, + 'forwardentityfrom' => true], ); } @@ -285,6 +286,7 @@ function plugin_fields_checkFiles() } } + function plugin_fields_exportBlockAsYaml($container_id = null) { /** @var DBmysql $DB */ diff --git a/templates/container.class.tpl b/templates/container.class.tpl index 29674dcf..4c566339 100644 --- a/templates/container.class.tpl +++ b/templates/container.class.tpl @@ -13,6 +13,7 @@ class %%CLASSNAME%% extends PluginFieldsAbstractContainerInstance $obj = new self(); $table = $obj->getTable(); + $migration = new PluginFieldsMigration(0); // create Table if (!$DB->tableExists($table)) { @@ -32,10 +33,8 @@ class %%CLASSNAME%% extends PluginFieldsAbstractContainerInstance $result = $DB->query("SHOW COLUMNS FROM `$table`"); if ($result && $DB->numrows($result) > 0) { $changed = false; - $migration = new PluginFieldsMigration(0); while ($data = $DB->fetchAssoc($result)) { if (str_starts_with($data['Field'], 'itemtype_') && $data['Null'] !== 'YES') { - Toolbox::logDebug($data); $migration->changeField($table, $data['Field'], $data['Field'], "varchar(100) DEFAULT NULL"); $changed = true; }