You are here

function comment_notify_user in Comment Notify 6

Same name and namespace in other branches
  1. 5.2 comment_notify.module \comment_notify_user()
  2. 5 comment_notify.module \comment_notify_user()

Implementation of hook_user().

File

./comment_notify.module, line 274
This module provides comment follow-up e-mail notification for anonymous and registered users.

Code

function comment_notify_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'form':
      if ($category == 'account' && user_access('subscribe to comments', $user)) {
        $form = array();
        $form['comment_notify_settings'] = array(
          '#type' => 'fieldset',
          '#title' => t('Comment follow-up notification settings'),
          '#weight' => 4,
          '#collapsible' => TRUE,
        );

        // Only show the node followup UI if the user has permission to create nodes.
        $nodes = FALSE;
        foreach (node_get_types() as $type) {
          if (node_access('create', $type->type)) {
            $nodes = TRUE;
            break;
          }
        }
        if (user_access('administer nodes') || $nodes) {
          $form['comment_notify_settings']['node_notify_mailalert'] = array(
            '#type' => 'checkbox',
            '#title' => t('Receive content follow-up notification e-mails'),
            '#default_value' => isset($edit['node_notify_mailalert']) ? $edit['node_notify_mailalert'] : variable_get('node_notify_default_mailalert', FALSE),
            '#description' => t('Check this box to receive an e-mail notification for follow-ups on your content. You can not disable notifications for individual threads.'),
          );
        }
        else {
          $form['comment_notify_settings']['node_notify_mailalert'] = array(
            '#type' => 'hidden',
            '#value' => COMMENT_NOTIFY_DISABLED,
          );
        }
        $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
        $available_options += _comment_notify_options();
        $form['comment_notify_settings']['comment_notify_mailalert'] = array(
          '#type' => 'select',
          '#title' => t('Receive comment follow-up notification e-mails'),
          '#default_value' => isset($edit['comment_notify_mailalert']) ? $edit['comment_notify_mailalert'] : variable_get('comment_notify_default_registered_mailalert', COMMENT_NOTIFY_DISABLED),
          '#options' => $available_options,
          '#description' => t("Check this box to receive e-mail notification for follow-up comments to comments you posted. You can later disable this on a post-by-post basis... so if you leave this to YES, you can still disable follow-up notifications for comments you don't want follow-up mails anymore - i.e. for very popular posts."),
        );
        return $form;
      }
      break;
    case 'submit':

      // Save the values of node_notify_mailalert and comment_notify_mailalert
      // to {comment_notify_user_settings}.
      if (isset($edit['node_notify_mailalert']) && isset($edit['comment_notify_mailalert'])) {
        db_query('UPDATE {comment_notify_user_settings} SET node_notify = %d, comment_notify = %d WHERE uid = %d', $edit['node_notify_mailalert'], $edit['comment_notify_mailalert'], $user->uid);
        if (!db_affected_rows()) {
          @db_query('INSERT INTO {comment_notify_user_settings} (uid, node_notify, comment_notify) VALUES (%d, %d, %d)', $user->uid, $edit['node_notify_mailalert'], $edit['comment_notify_mailalert']);
        }
      }

      // Unset them from $user so they don't also get saved into {users}.data.
      unset($edit['node_notify_mailalert']);
      unset($edit['comment_notify_mailalert']);
      break;
    case 'load':
      $user_settings = db_fetch_array(db_query('SELECT node_notify AS node_notify_mailalert, comment_notify AS comment_notify_mailalert FROM {comment_notify_user_settings} WHERE uid = %d', $user->uid));
      if (!empty($user_settings)) {
        foreach ($user_settings as $property => $value) {
          $user->{$property} = $value;
        }
      }
      else {

        // If they have no preference, use the defaults.
        $user->node_notify_mailalert = variable_get('node_notify_default_mailalert', FALSE);
        $user->comment_notify_mailalert = variable_get('comment_notify_default_registered_mailalert', COMMENT_NOTIFY_NODE);
      }
      break;
    case 'delete':
      db_query('DELETE FROM {comment_notify_user_settings} WHERE uid = %d', $user->uid);
      break;
      break;
  }
}