You are here

public static function Heartbeat::updateFriendship in Heartbeat 8

Updates the friendship status of these two users

_target

Parameters

$uid:

$unixtime:

$friendStatus:

Return value

\Drupal\Core\Database\StatementInterface|int|null

2 calls to Heartbeat::updateFriendship()
HeartbeatEventSubscriber::flag_entity_flagged in src/EventSubscriber/HeartbeatEventSubscriber.php
This method is called whenever the flag.entity_flagged event is dispatched.
HeartbeatEventSubscriber::flag_entity_unflagged in src/EventSubscriber/HeartbeatEventSubscriber.php
This method is called whenever the flag.entity_unflagged event is dispatched.

File

src/Entity/Heartbeat.php, line 875

Class

Heartbeat

Namespace

Drupal\heartbeat\Entity

Code

public static function updateFriendship($uid, $uid_target, $unixtime, $friendStatus) {

  //    $query = Database::getConnection()->upsert('heartbeat_friendship')
  //      ->fields(array(
  //        'uid' => $uid,
  //        'uid_target' => $uid_target,
  //        'created' => $unixtime,
  //        'status' => $friendStatus,
  //      ))
  //      ->key('uid_relation');
  //    return $query->execute();
  $update = Database::getConnection()
    ->update('heartbeat_friendship')
    ->fields([
    'status' => $friendStatus,
  ])
    ->condition('uid', $uid, '=')
    ->condition('uid_target', $uid_target, '=');
  if (!$update
    ->execute()) {
    $insert = Database::getConnection()
      ->insert('heartbeat_friendship')
      ->fields([
      'uid' => $uid,
      'uid_target' => $uid_target,
      'created' => $unixtime,
      'status' => $friendStatus,
    ]);
    if (!$insert
      ->execute()) {
      \Drupal::logger('Heartbeat')
        ->error('Unable to update friendship between %uid and %uid_target', array(
        '%uid' => $uid,
        '%uid_target' => $uid_target,
      ));
    }
  }
  if ($friendStatus === 1) {
    $update2 = Database::getConnection()
      ->update('heartbeat_friendship')
      ->fields([
      'status' => $friendStatus,
    ])
      ->condition('uid', $uid_target, '=')
      ->condition('uid_target', $uid, '=');
    if (!$update2
      ->execute()) {
      $insert2 = Database::getConnection()
        ->insert('heartbeat_friendship')
        ->fields([
        'uid' => $uid_target,
        'uid_target' => $uid,
        'created' => $unixtime,
        'status' => $friendStatus,
      ]);
      if (!$insert2
        ->execute()) {
        \Drupal::logger('Heartbeat')
          ->error('Unable to update friendship between %uid and %uid_target', array(
          '%uid' => $uid_target,
          '%uid_target' => $uid,
        ));
      }
    }
  }
  return null;
}