N°6619 - Attachment : Make contact_id an AttributeExternalKey instead of AttributeExternalField (#532)

This commit is contained in:
Anne-Catherine
2024-02-05 14:07:51 +01:00
committed by GitHub
parent 5308b49cd2
commit e830c90314
2 changed files with 27 additions and 10 deletions

View File

@@ -93,12 +93,12 @@
<target_class>User</target_class>
<allow_target_creation>false</allow_target_creation>
</field>
<field id="contact_id" xsi:type="AttributeExternalField">
<extkey_attcode>user_id</extkey_attcode>
<target_attcode>contactid</target_attcode>
<dependencies>
<attribute id="user_id"/>
</dependencies>
<field id="contact_id" xsi:type="AttributeExternalKey">
<sql>contact_id</sql>
<is_null_allowed>true</is_null_allowed>
<on_target_delete>DEL_MANUAL</on_target_delete>
<target_class>Person</target_class>
<allow_target_creation>false</allow_target_creation>
</field>
</fields>
<methods>
@@ -110,8 +110,14 @@
public function DBInsertNoReload()
{
$this->SetCurrentDateIfNull('creation_date');
$this->SetIfNull('user_id', CMDBChange::GetCurrentUserId());
$iUserId = CMDBChange::GetCurrentUserId();
if(utils::IsNotNullOrEmptyString($iUserId)){
$this->SetIfNull('user_id', $iUserId);
// Get Contact from user
$oUser = MetaModel::GetObject('User', $iUserId);
$this->SetIfNull('contact_id', $oUser->Get('contactid'));
}
return parent::DBInsertNoReload();
}
]]></code>

View File

@@ -194,14 +194,25 @@ SQL;
$oContainer = MetaModel::GetObject($oAttachment->Get('item_class'), $oAttachment->Get('item_id'), false /* must be found */, true /* allow all data */);
if ($oContainer)
{
if ($oContainer) {
$oAttachment->SetItem($oContainer, true /*updateonchange*/);
$iUpdated++;
}
}
SetupLog::Info("Initializing attachment/item_org_id - $iUpdated records have been adjusted");
if (MetaModel::GetAttributeDef('Attachment', 'contact_id') instanceof AttributeExternalKey) {
SetupLog::Info("Upgrading itop-attachment from '$sPreviousVersion' to '$sCurrentVersion'. Starting with 3.2.0, contact_id will be added into the DB...");
$sUserTableName = MetaModel::DBGetTable('User');
$sUserFieldContactId = MetaModel::GetAttributeDef('User', 'contactid')->Get('sql');
$sAttachmentFieldUserId = MetaModel::GetAttributeDef('Attachment', 'user_id')->Get('sql');
$sAttachmentFieldContactId = MetaModel::GetAttributeDef('Attachment', 'contact_id')->Get('sql');
$sAddContactId = "UPDATE `$sTableName` att, `$sUserTableName` us SET att.`$sAttachmentFieldContactId` = us.`$sUserFieldContactId` WHERE att.`$sAttachmentFieldUserId` = us.id AND att.`$sAttachmentFieldContactId` = 0";
CMDBSource::Query($sAddContactId);
$iNbProcessed = CMDBSource::AffectedRows();
SetupLog::Info("| | ".$iNbProcessed." attachment processed.");
}
}
}
}