comment_notify = comment_notify_variable_registry_get('default_registered_mailalert'); $users[0]->node_notify = comment_notify_variable_registry_get('node_notify_default_mailalert'); } else { $setting = db_select('comment_notify_user_settings', 'cnus') ->fields('cnus') ->condition('uid', $uid) ->execute() ->fetchObject(); if (!$setting) { return NULL; } else { $users[$uid] = $setting; } } } return $users[$uid]; } 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') ); } /** * Remove comment notification preferences for a user. * * @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. * * @param integer $uid * @return integer */ function comment_notify_get_user_comment_notify_preference($uid) { $setting = comment_notify_get_user_notification_setting($uid); if (!$setting) { $setting = comment_notify_get_default_notification_setting(); } return $setting->comment_notify; } /** * Get a user's default preference for node update notification. * * This is notification on nodes where the user is the author. * * @param integer $uid * @return integer */ function comment_notify_get_user_node_notify_preference($uid) { $setting = comment_notify_get_user_notification_setting($uid); if (!$setting) { $settings = comment_notify_get_default_notification_setting(); } return $setting->node_notify; } /** * Sets the notification preferences for a specific user. * * @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); if (!is_null($node_notification)) { $fields['node_notify'] = $node_notification; } if (!is_null($comment_notification)) { $fields['comment_notify'] = $comment_notification; } if (comment_notify_get_user_notification_setting($uid)) { $query = db_update('comment_notify_user_settings'); $query->condition('uid', $uid); } else { $query = db_insert('comment_notify_user_settings'); } return (bool)$query ->fields($fields) ->execute(); } /** * Add a notification against a comment. * * @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( 'cid' => $cid, 'notify' => $notify, 'notify_hash' => $notify_hash, )) ->execute(); } /** * Remove all the notifications linked with a comment * * @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 * * @param integer $cid * @param integer $notify * @return boolean */ function comment_notify_update_notification($cid, $notify) { return (bool)db_update('comment_notify') ->fields(array( 'notify' => $notify, )) ->condition('cid', $cid) ->execute(); } /** * Returns TRUE if the owner of this comment notification has already been notified. * * @param integer $cid * @return boolean */ function comment_notify_is_notification_already_sent($cid) { return (bool) db_select('comment_notify', 'cn') ->fields('cn', array('cid')) ->condition('cid', $cid) ->condition('notified', 1) ->execute() ->fetchField(); } /** * Get the unique identification hash for a comment notification record. * * @param integer $cid * @return string */ function comment_notify_get_notify_hash($cid) { return db_select('comment_notify', 'cn') ->fields('cn', array('notify_hash')) ->condition('cid', $cid) ->execute() ->fetchField(); } /** * Get the type of notification for a comment notification record. * * @param integer $cid * @return integer */ function comment_notify_get_notification_type($cid) { return db_select('comment_notify', 'cn') ->fields('cn', array('notify')) ->condition('cid', $cid) ->execute() ->fetchField(); } /** * Returns the thread which a comment belongs to. * * @param integer $cid * @return string * The thread identifier. */ function comment_notify_get_thread($cid) { return db_select('comment', 'c') ->fields('c', array('thread')) ->condition('cid', $cid) ->execute() ->fetchField(); } /** * Get a list of mails which need to be contacted for a node. * * @param integer $nid * @return QueryStatement */ function comment_notify_get_watchers($nid) { $query = db_select('comment', 'c'); $query->join('comment_notify', 'cn', 'c.cid = cn.cid'); $query->leftJoin('users', 'u', 'c.uid = u.uid'); $query ->condition('nid', $nid) ->condition('notify', COMMENT_NOTIFY_DISABLED, '<>') ->condition('c.status', COMMENT_PUBLISHED) ->condition( db_or() ->condition('u.uid', 0) ->condition('u.status', 1) ) ->fields('c', array('cid', 'nid', 'uid', 'name', 'thread')) ->fields('cn', array('notify', 'notify_hash')); $query->addField('c', 'mail', 'cmail'); $query->addField('u', 'init', 'uinit'); $query->addField('u', 'mail', 'umail'); return $query->execute(); } /** * Record that the owner of a comment notification request has already been notified. * * @param integer $cid * @return boolean */ function comment_notify_mark_comment_as_notified($cid) { return (bool)db_update('comment_notify') ->fields(array( 'notified' => 1, )) ->condition('cid', $cid) ->execute(); } /** * Unsubscribe all comment notification requests associated with an email. * * If the email belongs to a user, it will unsubscribe all of their comment notify records. * If it does not, then it will unsubscribe all anonymous users. * * @param string $mail * @return boolean */ function comment_notify_unsubscribe_by_email($mail) { $update_query = db_update('comment_notify'); $update_query->fields(array('notify' => 0)); $comment_query = db_select('comment', 'c'); $comment_query->fields('c', array('cid')); $uid = db_select('users', 'u') ->fields('u', array('uid')) ->condition('mail', $mail) ->execute() ->fetchField(); if ($uid) { $comment_query->condition('uid', $uid); } else { $comment_query->condition('mail', $mail); } $update_query->condition('cid', $comment_query, 'IN'); return (bool)$update_query->execute(); } /** * Unsubscribe all comment notification requests associated with a hash. * * This is used in the unsubscribe link. * * @param string $hash * @return boolean */ function comment_notify_unsubscribe_by_hash($hash) { return (bool)db_update('comment_notify') ->fields(array( 'notify' => 0, )) ->condition('notify_hash', $hash) ->execute(); } /** * Helper function to centralize variable management and defaults. * * All variables fall under the "comment_notify" psuedo namespace. This ensures * 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. * * @param string $name * @return mixed */ function comment_notify_variable_registry_get($name) { $variables = array(); $variables['author_subject'] = '[site:name] :: new comment for your post.'; $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; $variables['node_notify_default_mailalert'] = 0; $variables['comment_notify_default_registered_mailalert'] = COMMENT_NOTIFY_DISABLED; $variables['watcher_subject'] = '[site:name] :: new comment on [node:title]'; $variables['comment_notify_default_mailtext'] = DEFAULT_MAILTEXT; $variables['node_types'] = array('article' => 'article'); // 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. * * @param string $name * @param mixed $value * @return boolean */ function comment_notify_variable_registry_set($name, $value) { return variable_set("comment_notify_" . $name, $value); }