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> <target_class>User</target_class>
<allow_target_creation>false</allow_target_creation> <allow_target_creation>false</allow_target_creation>
</field> </field>
<field id="contact_id" xsi:type="AttributeExternalField"> <field id="contact_id" xsi:type="AttributeExternalKey">
<extkey_attcode>user_id</extkey_attcode> <sql>contact_id</sql>
<target_attcode>contactid</target_attcode> <is_null_allowed>true</is_null_allowed>
<dependencies> <on_target_delete>DEL_MANUAL</on_target_delete>
<attribute id="user_id"/> <target_class>Person</target_class>
</dependencies> <allow_target_creation>false</allow_target_creation>
</field> </field>
</fields> </fields>
<methods> <methods>
@@ -110,8 +110,14 @@
public function DBInsertNoReload() public function DBInsertNoReload()
{ {
$this->SetCurrentDateIfNull('creation_date'); $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(); return parent::DBInsertNoReload();
} }
]]></code> ]]></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 */); $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*/); $oAttachment->SetItem($oContainer, true /*updateonchange*/);
$iUpdated++; $iUpdated++;
} }
} }
SetupLog::Info("Initializing attachment/item_org_id - $iUpdated records have been adjusted"); 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.");
}
} }
} }
} }