Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz committed Nov 22, 2024
1 parent 80c9bdc commit 97852bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
35 changes: 24 additions & 11 deletions inc/abstractcontainerinstance.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Itemtype><name>,
* ['forwardentityfrom' => <Itemtype>]
* );
*
* 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,
Expand All @@ -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());
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ function plugin_init_fields()
if (count($itemtypes) > 0) {
Plugin::registerClass(
'PluginFieldsContainer',
['addtabon' => $itemtypes],
['addtabon' => $itemtypes,
'forwardentityfrom' => true],
);
}

Expand Down Expand Up @@ -285,6 +286,7 @@ function plugin_fields_checkFiles()
}
}


function plugin_fields_exportBlockAsYaml($container_id = null)
{
/** @var DBmysql $DB */
Expand Down
3 changes: 1 addition & 2 deletions templates/container.class.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class %%CLASSNAME%% extends PluginFieldsAbstractContainerInstance
$obj = new self();
$table = $obj->getTable();
$migration = new PluginFieldsMigration(0);
// create Table
if (!$DB->tableExists($table)) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 97852bf

Please sign in to comment.