You are here

function comment_notify_unsubscribe_by_hash in Comment Notify 8

Same name and namespace in other branches
  1. 7 comment_notify.inc \comment_notify_unsubscribe_by_hash()

Unsubscribe comment notification requests associated with a hash.

This is used in the unsubscribe link.

Parameters

string $hash: The hash that identified the comment.

Return value

bool Returns TRUE if the comment was unsubscribed correctly, FALSE otherwise.

1 call to comment_notify_unsubscribe_by_hash()
CommentNotifyController::disable in src/Controller/CommentNotifyController.php
Creates a page for disabling notifications.

File

./comment_notify.inc, line 198
Contains functions which utilize the database and other internal helpers.

Code

function comment_notify_unsubscribe_by_hash($hash) {
  $query = \Drupal::database()
    ->select('comment_notify', 'cn');
  $query
    ->join('comment_field_data', 'cf', 'cn.cid = cf.cid');
  $query
    ->condition('cn.notify_hash', $hash)
    ->condition('cn.notify', COMMENT_NOTIFY_DISABLED, '!=')
    ->fields('cn', [
    'cid',
    'notify',
    'notified',
  ])
    ->fields('cf', [
    'entity_id',
    'entity_type',
    'uid',
  ])
    ->execute()
    ->fetchObject();
  $notification = $query
    ->execute()
    ->fetchObject();
  if (empty($notification)) {
    return FALSE;
  }

  // If this notification is at the entity level and the commenter has a Drupal
  // account, delete all notifications for this entity.
  if (COMMENT_NOTIFY_ENTITY == $notification->notify && $notification->uid) {
    $result = \Drupal::database()
      ->query("SELECT cid FROM {comment_field_data} WHERE entity_id = :entity_id AND entity_type = :entity_type AND uid = :uid", [
      ':entity_id' => $notification->entity_id,
      ':entity_type' => $notification->entity_type,
      ':uid' => $notification->uid,
    ]);
    $cids = $result
      ->fetchCol();

    // Update all comment notifications to be disabled.
    return (bool) \Drupal::database()
      ->update('comment_notify')
      ->fields([
      'notify' => 0,
    ])
      ->condition('cid', $cids, 'IN')
      ->execute();
  }
  else {

    // Update this notification to be disabled.
    return (bool) \Drupal::database()
      ->update('comment_notify')
      ->fields([
      'notify' => 0,
    ])
      ->condition('notify_hash', $hash)
      ->execute();
  }
}