Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions application/utils.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3094,7 +3094,6 @@ public static function ToAcronym(string $sInput): string
* Note: Only works for backoffice URLs for now
*
* @param string $sText Text containing the mentioned objects to be found
* @param string $sFormat {@uses static::ENUM_TEXT_FORMAT_HTML, ...}
*
* @return array Array of object classes / IDs for the ones found in $sText
*
Expand All @@ -3109,21 +3108,24 @@ public static function ToAcronym(string $sInput): string
public static function GetMentionedObjectsFromText(string $sText): array
{
$aMentionedObjects = [];
$aMentionMatches = [];
$sText = html_entity_decode($sText);
$oDom = new \DOMDocument();
libxml_use_internal_errors(true); // to keep processing even in case of "invalid" HTML, cf. testGetMentionedObjectsFromText
$oDom->loadHTML($sText);

preg_match_all('/<a\s*([^>]*)data-object-class="([^"]*)"\s.*data-object-key="([^"]*)"/Ui', $sText, $aMentionMatches);
foreach ($aMentionMatches[0] as $iMatchIdx => $sCompleteMatch) {
$sMatchedClass = $aMentionMatches[2][$iMatchIdx];
$sMatchedId = $aMentionMatches[3][$iMatchIdx];
$oXpath = new \DOMXPath($oDom);
$oNodes = $oXpath->query('//a[@data-object-class and @data-object-key]');

foreach ($oNodes as $oObjNode) {
$sObjClass = $oObjNode->getAttribute('data-object-class');
$sObjId = $oObjNode->getAttribute('data-object-key');

// Prepare array for matched class if not already present
if (!array_key_exists($sMatchedClass, $aMentionedObjects)) {
$aMentionedObjects[$sMatchedClass] = array();
if (!array_key_exists($sObjClass, $aMentionedObjects)) {
$aMentionedObjects[$sObjClass] = [];
}
// Add matched ID if not already there
if (!in_array($sMatchedId, $aMentionedObjects[$sMatchedClass])) {
$aMentionedObjects[$sMatchedClass][] = $sMatchedId;
if (!in_array($sObjId, $aMentionedObjects[$sObjClass])) {
$aMentionedObjects[$sObjClass][] = $sObjId;
}
}

Expand Down