Compare commits

...

2 Commits

Author SHA1 Message Date
jf-cbd
d0a1e8bef7 Renaming + formatting 2025-09-15 11:34:22 +02:00
jf-cbd
d3add36637 N°8123 - Improve on mention data parsing 2025-09-15 11:21:51 +02:00

View File

@@ -3094,7 +3094,6 @@ TXT
* 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
*
@@ -3109,21 +3108,24 @@ TXT
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;
}
}