You are here

public function HeartbeatEventSubscriber::flag_entity_flagged in Heartbeat 8

This method is called whenever the flag.entity_flagged event is dispatched.

Parameters

GetResponseEvent $event:

Throws

\Drupal\Core\Entity\EntityStorageException

File

src/EventSubscriber/HeartbeatEventSubscriber.php, line 79

Class

HeartbeatEventSubscriber
Class HeartbeatEventSubscriber.

Namespace

Drupal\heartbeat\EventSubscriber

Code

public function flag_entity_flagged(Event $event) {
  $friendStatus = NOT_FRIEND;
  $flagging = $event
    ->getFlagging();
  if ($flagging instanceof Flagging) {
    $flagId = $flagging
      ->getFlagId();
    if ($flagId === 'friendship') {
      $entity = $this->flagService
        ->getFlagById($flagging
        ->getFlagId());
      $user = $flagging
        ->getOwner();
      if ($entity
        ->id() && $user
        ->isAuthenticated()) {
        $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
        $tokenService = \Drupal::service('token');
        foreach ($heartbeatTypeService
          ->getTypes() as $type) {
          $heartbeatTypeEntity = $heartbeatTypeService
            ->load($type);
          if ($heartbeatTypeEntity
            ->getMainEntity() === "flagging") {
            $arguments = json_decode($heartbeatTypeEntity
              ->getArguments());
            $user2 = User::load($flagging
              ->getFlaggableId());
            $targetUserFriendships = $this->flagService
              ->getFlagFlaggings($entity, $user2);
            foreach ($targetUserFriendships as $friendship) {
              if ($friendship
                ->getFlaggableId() === $user
                ->id()) {
                $friendStatus = FRIEND;
                break;
              }
            }
            $friendStatus = $friendStatus == FRIEND ? FRIEND : PENDING;
            foreach ($arguments as $key => $argument) {
              $variables[$key] = $argument;
            }
            Heartbeat::updateFriendship($user
              ->id(), $user2
              ->id(), time(), $friendStatus);
            $preparsedMessageString = strtr($heartbeatTypeEntity
              ->getMessage(), $variables);
            $entitiesObj = new \stdClass();
            $entitiesObj->type = 'user';
            $entitiesObj->entities = [
              $user,
              $user2,
            ];
            $entities = array(
              'flagging' => $entity,
              'user' => $entitiesObj,
            );
            $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity
              ->getEntityTypeId(), null);
            $heartbeatActivity = Heartbeat::create([
              'type' => $heartbeatTypeEntity
                ->id(),
              'uid' => $user
                ->id(),
              'nid' => $entity
                ->id(),
              'name' => 'Dev Test',
            ]);
            $heartbeatActivity
              ->setMessage($heartbeatMessage);
            $heartbeatActivity
              ->save();
          }
        }
      }
      $friendships = Database::getConnection()
        ->select("heartbeat_friendship", "hf")
        ->fields('hf', array(
        'status',
        'uid',
        'uid_target',
      ))
        ->execute();
      $friendData = $friendships
        ->fetchAll();
      $friendConfig = \Drupal::configFactory()
        ->getEditable('heartbeat_friendship.settings');
      $friendConfig
        ->set('data', json_encode($friendData))
        ->save();
    }

    //      if ($flagging->getFlaggableType() === 'heartbeat') {
    //        $oppositeFlag = null;
    //        if ($flagging->getFlagId() === 'heartbeat_like') {
    //          $oppositeFlag = $this->flagService->getFlagById('heartbeat_unlike');
    //        } elseif ($flagging->getFlagId() === 'heartbeat_unlike') {
    //          $oppositeFlag = $this->flagService->getFlagById('heartbeat_unlike');
    //        }
    //        if ($oppositeFlag) {
    //          $entity = $flagging->getFlaggable();
    //          $user = \Drupal::currentUser()->getAccount();
    //          $oppositeFlagging = $this->flagService->getFlagging($oppositeFlag, $entity, $user);
    //          if ($oppositeFlagging) {
    //            $this->flagService->unflag($oppositeFlag, $entity, $user);
    //          }
    //        }
    //      }
  }
}