You are here

protected static function Heartbeat::handleMultipleEntities in Heartbeat 8

1 call to Heartbeat::handleMultipleEntities()
Heartbeat::buildMessage in src/Entity/Heartbeat.php
Builds a message template for a given HeartbeatType

File

src/Entity/Heartbeat.php, line 552

Class

Heartbeat

Namespace

Drupal\heartbeat\Entity

Code

protected static function handleMultipleEntities(Token $tokenService, $message, $entities) {
  $tokens = $tokenService
    ->scan($message);
  foreach ($tokens as $key => $token) {
    foreach ($token as $type) {
      if (substr_count($message, $type) > 1) {
        foreach ($entities as $entityKey => $entityValue) {
          if ($entityValue instanceof \stdClass && count($entityValue->entities) > 1) {
            if ($key == $entityValue->type) {
              $messageArray = explode($type, $message);
              $stringRebuild = array();
              $replacements = array();
              $i = 0;
              foreach ($entityValue->entities as $entity) {
                $stringRebuild[] = $tokenService
                  ->replace($message, array(
                  $key => $entity,
                ));
                foreach (self::getWordRepeats($stringRebuild[$i]) as $word => $num) {
                  if ($num > 1 && !strpos($messageArray[1], $word)) {
                    $replacements[] = $word;
                  }
                }
                $i++;
              }
              if (count($replacements) == 2) {
                $uid = $entityValue->entities[0]
                  ->id();
                $uid_target = $entityValue->entities[1]
                  ->id();
                $query = Database::getConnection()
                  ->query('
                    SELECT status
                    FROM heartbeat_friendship
                    WHERE uid = :uid AND uid_target = :uid_target', array(
                  ':uid' => $uid,
                  ':uid_target' => $uid_target,
                ));
                if ($query
                  ->fetchCol()[0] < 1) {
                  $messageArray[1] = ' has requested friendship with ';
                }
                $user1Link = Link::fromTextAndUrl($replacements[0], $entityValue->entities[0]
                  ->toUrl());
                $user2Link = Link::fromTextAndUrl($replacements[1], $entityValue->entities[1]
                  ->toUrl());
                $rebuiltMessage = $user1Link
                  ->toString() . $messageArray[1] . $user2Link
                  ->toString();
                return $rebuiltMessage;
              }
            }
          }
        }
      }
    }
  }
  return null;
}