comment_notify.inc 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 *
 * Contains functions which utilize the database and other internal helpers.
 */

/**
 * Get the notification preferences for a specific user.
11
 *
12
 * @param integer $uid
13 14
 * @return mixed
 *  StdClass if found, else NULL
15 16 17 18 19 20 21 22 23
 */
function comment_notify_get_user_notification_setting($uid) {
  $users = &drupal_static(__FUNCTION__);
  if (!isset($users[$uid])) {
    if (is_null($uid)) {
      throw new Exception('Cannot get user preference, uid missing');
    }
    // Handle anonymous users with defaults.
    if ($uid == 0) {
24 25
      $users[0] = new stdClass();
      $users[0]->comment_notify = comment_notify_variable_registry_get('default_registered_mailalert');
26
      $users[0]->node_notify = comment_notify_variable_registry_get('node_notify_default_mailalert');
27 28
    }
    else {
29
      $setting = db_select('comment_notify_user_settings', 'cnus')
30 31
        ->fields('cnus')
        ->condition('uid', $uid)
32 33 34 35 36
        ->execute()
        ->fetchObject();

      if (!$setting) {
        return NULL;
37 38
      }
      else {
39
        $users[$uid] = $setting;
40
      }
41 42 43 44 45
    }
  }
  return $users[$uid];
}

46 47 48 49 50 51 52
function comment_notify_get_default_notification_setting() {
  return (object) array(
    'comment_notify' => comment_notify_variable_registry_get('default_registered_mailalert'),
    'node_notify' => comment_notify_variable_registry_get('node_notify_default_mailalert')
  );
}

53 54
/**
 * Remove comment notification preferences for a user.
55
 *
56 57 58 59 60 61 62 63 64 65 66
 * @param integer $uid
 * @return boolean
 */
function comment_notify_delete_user_notification_setting($uid) {
  return (bool)db_delete('comment_notify_user_settings')
    ->condition('uid', $uid)
    ->execute();
}

/**
 * Get a user's default preference for comment notification.
67
 *
68 69 70 71 72
 * @param integer $uid
 * @return integer
 */
function comment_notify_get_user_comment_notify_preference($uid) {
  $setting = comment_notify_get_user_notification_setting($uid);
73 74 75 76
  if (!$setting) {
    $setting = comment_notify_get_default_notification_setting();
  }
  return $setting->comment_notify;
77 78 79 80
}

/**
 * Get a user's default preference for node update notification.
81
 *
82
 * This is notification on nodes where the user is the author.
83
 *
84 85 86 87 88
 * @param integer $uid
 * @return integer
 */
function comment_notify_get_user_node_notify_preference($uid) {
  $setting = comment_notify_get_user_notification_setting($uid);
89
  if (!$setting) {
90
    $setting = comment_notify_get_default_notification_setting();
91 92
  }
  return $setting->node_notify;
93 94 95 96
}

/**
 * Sets the notification preferences for a specific user.
97
 *
98 99 100 101 102 103 104 105 106 107
 * @param integer $uid
 * @param integer $node_notification
 * @param integer $comment_notification
 * @return boolean
 */
function comment_notify_set_user_notification_setting($uid, $node_notification = NULL, $comment_notification = NULL) {
  if (!$uid) {
    throw new Exception('Cannot set user preference, uid missing');
  }
  $fields = array('uid' => $uid);
108

109 110 111 112
  if (!is_null($node_notification)) {
    $fields['node_notify'] = $node_notification;
  }
  if (!is_null($comment_notification)) {
113
    $fields['comment_notify'] = $comment_notification;
114 115 116 117
  }
  if (comment_notify_get_user_notification_setting($uid)) {
    $query = db_update('comment_notify_user_settings');
    $query->condition('uid', $uid);
118 119
  }
  else {
120 121 122 123 124 125 126 127 128
    $query = db_insert('comment_notify_user_settings');
  }
  return (bool)$query
    ->fields($fields)
    ->execute();
}

/**
 * Add a notification against a comment.
129
 *
130 131 132 133 134 135 136 137
 * @param integer $cid
 * @param integer $notify
 * @param string $notify_hash
 * @return boolean
 */
function comment_notify_add_notification($cid, $notify, $notify_hash) {
  return (bool)db_insert('comment_notify')
    ->fields(array(
138
      'cid' => $cid,
139
      'notify' => $notify === NULL ? 0 : $notify,
140
      'notify_hash' => $notify_hash,
141 142 143 144 145 146
    ))
    ->execute();
}

/**
 * Remove all the notifications linked with a comment
147
 *
148 149 150 151 152 153 154 155 156 157 158
 * @param integer $cid
 * @return boolean
 */
function comment_notify_remove_all_notifications($cid) {
  return (bool)db_delete('comment_notify')
    ->condition('cid', $cid)
    ->execute();
}

/**
 * Updated a notification with a different notification type
159
 *
160 161 162 163 164 165 166
 * @param integer $cid
 * @param integer $notify
 * @return boolean
 */
function comment_notify_update_notification($cid, $notify) {
  return (bool)db_update('comment_notify')
    ->fields(array(
167
      'notify' => $notify === NULL ? 0 : $notify,
168 169 170 171 172 173 174
    ))
    ->condition('cid', $cid)
    ->execute();
}

/**
 * Get the type of notification for a comment notification record.
175
 *
176 177 178 179
 * @param integer $cid
 * @return integer
 */
function comment_notify_get_notification_type($cid) {
180
  return db_select('comment_notify', 'cn')
181 182 183 184 185 186 187 188
    ->fields('cn', array('notify'))
    ->condition('cid', $cid)
    ->execute()
    ->fetchField();
}

/**
 * Get a list of mails which need to be contacted for a node.
189
 *
190 191 192 193
 * @param integer $nid
 * @return QueryStatement
 */
function comment_notify_get_watchers($nid) {
194 195 196 197 198 199
  $cids = db_query("SELECT c.cid FROM {comment} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users} u ON c.uid = u.uid WHERE c.nid = :nid AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", array(
    ':nid' => $nid,
    ':status' => COMMENT_PUBLISHED,
    ':notify' => COMMENT_NOTIFY_DISABLED,
  ))->fetchCol();
  return comment_load_multiple($cids);
200 201 202 203
}

/**
 * Record that the owner of a comment notification request has already been notified.
204
 *
205 206 207
 * @param integer $cid
 * @return boolean
 */
208 209 210 211 212
function comment_notify_mark_comment_as_notified($comment) {
  // First, mark the passed comment (an object, so passed by reference).
  $comment->notified = 1;

  // Next, store this fact in the DB as well.
213 214
  return (bool)db_update('comment_notify')
    ->fields(array(
215
      'notified' => 1,
216
    ))
217
    ->condition('cid', $comment->cid)
218 219 220 221 222 223
    ->execute();
}

/**
 * Unsubscribe all comment notification requests associated with an email.
 *
224
 * If the email belongs to a user, it will unsubscribe all of their Comment Notify records.
225
 * If it does not, then it will unsubscribe all anonymous users.
226
 *
227 228 229 230 231 232
 * @param string $mail
 * @return boolean
 */
function comment_notify_unsubscribe_by_email($mail) {
  $update_query = db_update('comment_notify');
  $update_query->fields(array('notify' => 0));
233

234 235
  $comment_query = db_select('comment', 'c');
  $comment_query->fields('c', array('cid'));
236

237 238 239 240 241 242
  $uid = db_select('users', 'u')
    ->fields('u', array('uid'))
    ->condition('mail', $mail)
    ->execute()
    ->fetchField();
  if ($uid) {
243 244 245
    $comment_query->condition('uid', $uid);
  }
  else {
246 247 248
    $comment_query->condition('mail', $mail);
  }
  $update_query->condition('cid', $comment_query, 'IN');
249

250 251 252 253
  return (bool)$update_query->execute();
}

/**
254
 * Unsubscribe comment notification requests associated with a hash.
255
 *
256
 * This is used in the unsubscribe link.
257
 *
258 259 260 261
 * @param string $hash
 * @return boolean
 */
function comment_notify_unsubscribe_by_hash($hash) {
262 263 264 265 266 267 268 269 270 271 272
  $notification = db_select('comment_notify')
      ->fields('comment_notify')
      ->condition('notify_hash', $hash)
      ->execute()->fetchAll();

  // If this notification is at the node level, delete all notifications for this node.
  if (COMMENT_NOTIFY_NODE == $notification[0]->notify) {
    // Get all this user's comments for this node.
    $result = db_query("SELECT c.cid
      FROM {comment} c, (
        SELECT oc.nid, oc.uid
273
        FROM {comment} AS oc, {comment_notify} AS ocn
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        WHERE oc.cid = ocn.cid
        AND ocn.notify_hash = :hash
      ) AS o
      WHERE o.nid = c.nid
      AND o.uid = c.uid", array(':hash' => $hash));

    $cids = $result->fetchCol();

    // Update all comment notifications to be disabled.
    return (bool)db_update('comment_notify')
      ->fields(array(
        'notify' => 0,
      ))
      ->condition('cid', $cids, 'IN')
      ->execute();
  }
  else {
   // Update this notification to be disabled.
   return (bool)db_update('comment_notify')
     ->fields(array(
       'notify' => 0,
     ))
296 297
    ->condition('notify_hash', $hash)
    ->execute();
298
  }
299
}
300

301 302
/**
 * Helper function to centralize variable management and defaults.
303
 *
304
 * All variables fall under the "comment_notify" psuedo namespace.  This ensures
305 306 307
 * consistancy, and eliminates some verbosity in the calling code.  In addition
 * by storing all of the variables in one place, we avoid repeating duplicate
 * defaults which are harder to maintain.
308
 *
309 310 311 312
 * @param string $name
 * @return mixed
 */
function comment_notify_variable_registry_get($name) {
313
  $variables = array();
greggles's avatar
(Issue  
greggles committed
314
  $variables['author_subject'] = t('[site:name] :: new comment for your post.');
315 316 317 318
  $variables['available_alerts'] = array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT);
  $variables['default_anon_mailalert'] = COMMENT_NOTIFY_NODE;
  $variables['node_notify_default_mailtext'] = AUTHOR_MAILTEXT;
  $variables['default_registered_mailalert'] = COMMENT_NOTIFY_DISABLED;
319
  $variables['node_notify_default_mailalert'] = 0;
320
  $variables['watcher_subject'] = '[site:name] :: new comment on [comment:node:title]';
321
  $variables['comment_notify_default_mailtext'] = DEFAULT_MAILTEXT;
322 323
  $variables['node_types'] = array('article' => 'article');

324 325 326 327 328 329 330
  // Errors
  $variables['error_anonymous_email_missing'] = 'If you want to subscribe to comments you must supply a valid e-mail address.';
  return variable_get("comment_notify_" . $name, $variables[$name]);
}

/**
 * Helper function to centralize setting variables.
331
 *
332 333 334 335 336 337 338
 * @param string $name
 * @param mixed $value
 * @return boolean
 */
function comment_notify_variable_registry_set($name, $value) {
  return variable_set("comment_notify_" . $name, $value);
}