From a0dc68ab9ad30df181284859fb5f14e292789a06 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Thu, 4 Jul 2024 22:43:37 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B07565=20-=20Generate=20newsroom=20action?= =?UTF-8?q?=20for=20Person=20mention=20on=20installation/upgrade?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/utils.inc.php | 9 +++ .../itop-structure/module.itop-structure.php | 59 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/application/utils.inc.php b/application/utils.inc.php index 2665b5f37..9a7528fd1 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -2490,6 +2490,15 @@ SQL; $sData = @file_get_contents($sPath); if ($sData === false) { + IssueLog::Error(<< $sPath, + ]); throw new Exception("Failed to load the file from the URL '$sPath'."); } else diff --git a/datamodels/2.x/itop-structure/module.itop-structure.php b/datamodels/2.x/itop-structure/module.itop-structure.php index 66e8381f5..1960d4388 100644 --- a/datamodels/2.x/itop-structure/module.itop-structure.php +++ b/datamodels/2.x/itop-structure/module.itop-structure.php @@ -123,7 +123,7 @@ if (!class_exists('StructureInstaller')) SetupLog::Info("| | ".$iNbProcessed." triggers processed."); } - // Add default configuration, so Persons are notified if mentioned on any Caselog + // Add default configuration, so Persons are notified by email if mentioned on any log if (version_compare($sPreviousVersion, '3.0.0', '<')) { SetupLog::Info("Adding default triggers/action for Person objects mentions. All DM classes with at least 1 log attribute will be concerned..."); @@ -137,7 +137,7 @@ if (!class_exists('StructureInstaller')) foreach (MetaModel::EnumChildClasses($sRootClass, ENUM_CHILD_CLASSES_ALL, true) as $sClass) { $aLogAttCodes = MetaModel::GetAttributesList($sClass, ['AttributeCaseLog']); - // Skip class with log attribute + // Skip class with no log attribute if (count($aLogAttCodes) === 0) { continue; } @@ -202,6 +202,7 @@ if (!class_exists('StructureInstaller')) $oAction = MetaModel::NewObject('ActionEmail'); $oAction->Set('name', 'Notification to persons mentioned in logs'); $oAction->Set('status', 'enabled'); + $oAction->Set('language', 'EN US'); $oAction->Set('from', '$current_contact->email$'); $oAction->Set('to', 'SELECT Person WHERE id = :mentioned->id'); $oAction->Set('subject', 'You have been mentioned in "$this->friendlyname$"'); @@ -228,6 +229,60 @@ if (!class_exists('StructureInstaller')) SetupLog::Info("... default triggers/action successfully created for $iClassesWithLogCount classes."); } } + + // Add default configuration, so Persons are notified by newsroom if mentioned on any log + if (version_compare($sPreviousVersion, '3.2.0', '<')) { + SetupLog::Info("Adding default newsroom actions for Person objects mentions. All existing TriggerOnObjectMention for the Person class will be concerned..."); + + $sPersonClass = Person::class; + $iExistingTriggersCount = 0; + + // Start by creating the default action no matter what (even if there is no relevant trigger, it will be there for future use) + $oAction = MetaModel::NewObject(ActionNewsroom::class); + $oAction->Set('name', 'Notification to persons mentioned in logs'); + $oAction->Set('status', 'enabled'); + $oAction->Set('language', 'EN US'); + $oAction->Set('recipients', 'SELECT Person WHERE id = :mentioned->id'); + $oAction->Set('title', 'You have been mentioned in $this->friendlyname$'); + $oAction->Set('message', 'You have been mentioned by $current_contact->friendlyname$ in **$this->friendlyname$**'); + $oAction->DBWrite(); + + SetupLog::Info("|- Created newsroom action \"{$oAction->Get('name')}\"."); + + // Retrieve all triggers and find those with a mentioned_filter on the Person class + $oTriggersSearch = DBObjectSearch::FromOQL("SELECT " . TriggerOnObjectMention::class); + $oTriggersSearch->AllowAllData(); + + $oTriggersSet = new DBObjectSet($oTriggersSearch); + while ($oTrigger = $oTriggersSet->Fetch()) { + $oMentionedFilter = DBSearch::FromOQL($oTrigger->Get('mentioned_filter')); + $sMentionedClass = $oMentionedFilter->GetClass(); + + // If mentioned class is not a Person, ignore + if (is_a($sMentionedClass, $sPersonClass, true) === false) { + continue; + } + + // Link the trigger to the action + /** @var \ormLinkSet $oOrm */ + $oOrm = $oTrigger->Get('action_list'); + $oLink = new lnkTriggerAction(); + $oLink->Set('action_id', $oAction->GetKey()); + $oOrm->AddItem($oLink); + + $oTrigger->Set('action_list', $oOrm); + $oTrigger->DBUpdate(); + $iExistingTriggersCount++; + + SetupLog::Info("|- Linked newsroom action \"{$oAction->GetName()}\" to existing trigger \"{$oTrigger->GetName()}\"."); + } + + if ($iExistingTriggersCount === 0) { + SetupLog::Info("... no action created as there is no existing trigger on mention for the $sPersonClass class."); + } else { + SetupLog::Info("... default newsroom action successfully created and linked to $iExistingTriggersCount triggers on mention."); + } + } } } }