You are here

function notify_user_settings_form in Notify 7

Same name and namespace in other branches
  1. 5.2 notify.module \notify_user_settings_form()
  2. 5 notify.module \notify_user_settings_form()
  3. 6 notify.module \notify_user_settings_form()

Menu callback; show user notification options.

1 string reference to 'notify_user_settings_form'
notify_menu in ./notify.module
Implements hook_menu().

File

./notify.admin.inc, line 904
Administrative pages callbacks for the Notify module.

Code

function notify_user_settings_form($form, &$form_state, $arg) {
  global $user;
  if ($user->uid != $arg->uid && !user_access('administer notify')) {
    drupal_access_denied();
    return;
  }
  $account = user_load($arg->uid);
  if (!is_object($account)) {
    drupal_not_found();
    return;
  }
  $result = db_query('SELECT u.uid, u.name, u.mail, n.status, n.node, n.teasers, n.comment FROM {users} u LEFT JOIN {notify} n ON u.uid = n.uid WHERE u.uid = :uid', array(
    ':uid' => $account->uid,
  ));
  if (0 == $result
    ->rowCount()) {
    $notify = NULL;
  }
  else {
    $notify = $result
      ->fetchObject();
  }
  $form = array();
  if (!$notify->mail) {
    drupal_set_message(t('Your e-mail address must be specified on your <a href="@url">my account</a> page.', array(
      '@url' => url('user/' . $account->uid . '/edit'),
    )), 'error');
  }
  $form['notify_page_master'] = array(
    '#type' => 'fieldset',
    '#title' => t('Master switch'),
  );
  $periodsec = variable_get('notify_period', 86400);
  if (0 == $periodsec) {
    $cron = $periodsec = variable_get('cron_safe_threshold', DRUPAL_CRON_DEFAULT_THRESHOLD);
  }
  $sendhour = variable_get('notify_send_hour', 9);
  $pp = format_interval($periodsec);

  // If $periodsec, period depends on external cron, so we can't report it.
  if ($periodsec) {
    $ss = '<p>' . t('Notifications are sent every !period', array(
      '!period' => $pp,
    ));
    if ($periodsec >= 86400) {
      $ss .= ' ' . t("after !sendhour o'clock", array(
        '!sendhour' => $sendhour,
      ));
    }
    $ss .= '.</p>';
    $form['notify_page_master']['period'] = array(
      '#markup' => $ss,
    );
  }

  // If user existed before notify was enabled, these are not set in db.
  if (!isset($notify->status)) {
    $notify->status = 0;
    $notify->node = 0;
    $notify->teasers = 0;
    $notify->comment = 0;
  }
  if (module_exists('advanced_help')) {
    $output = theme('advanced_help_topic', array(
      'module' => 'notify',
      'topic' => 'users',
    ));
  }
  else {
    $output = '';
  }
  $form['notify_page_master']['status'] = array(
    '#type' => 'radios',
    '#title' => t('Notify status'),
    '#default_value' => $notify->status,
    '#options' => array(
      t('Disabled'),
      t('Enabled'),
    ),
    '#description' => $output . '&nbsp;' . t('The master switch overrides all other settings for Notify.  You can use it to suspend notifications without having to disturb any of your settings under “Detailed settings” and “Subscriptions”.'),
  );
  $form['notify_page_detailed'] = array(
    '#type' => 'fieldset',
    '#title' => t('Detailed settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => t('These settings will only be effective if the master switch is set to “Enabled”.'),
  );
  $form['notify_page_detailed']['node'] = array(
    '#type' => 'radios',
    '#title' => t('Notify new content'),
    '#default_value' => $notify->node,
    '#options' => array(
      t('Disabled'),
      t('Enabled'),
    ),
    '#description' => t('Include new posts in the notification mail.'),
  );
  $form['notify_page_detailed']['comment'] = array(
    '#type' => 'radios',
    '#access' => module_exists('comment'),
    '#title' => t('Notify new comments'),
    '#default_value' => $notify->comment,
    '#options' => array(
      t('Disabled'),
      t('Enabled'),
    ),
    '#description' => t('Include new comments in the notification mail.'),
  );
  $form['notify_page_detailed']['teasers'] = array(
    '#type' => 'radios',
    '#title' => t('How much to include?'),
    '#default_value' => $notify->teasers,
    '#options' => array(
      t('Title only'),
      t('Title + Teaser/Excerpt'),
      t('Title + Body'),
      t('Title + Body + Fields'),
    ),
    '#description' => t('Select the amount of each item to include in the notification e-mail.'),
  );
  $set = 'notify_page_nodetype';
  $form[$set] = array(
    '#type' => 'fieldset',
    '#title' => t('Subscriptions'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Tick the content types you want to subscribe to.'),
  );
  $alltypes = node_type_get_types();
  $enatypes = array();
  foreach (node_type_get_types() as $type => $object) {
    if (variable_get(NOTIFY_NODE_TYPE . $type, 0)) {
      $enatypes[] = array(
        $type,
        $object->name,
      );
    }
  }
  if (user_access('administer notify queue', $account) || empty($enatypes)) {
    $enatypes = array();
    foreach ($alltypes as $obj) {
      $enatypes[] = array(
        $obj->type,
        $obj->name,
      );
    }
  }
  $exists = _notify_user_has_subscriptions($account->uid);
  if ($exists) {

    // Custom subscriptions exists, use those.
    foreach ($enatypes as $type) {
      $field = db_query('SELECT uid FROM {notify_subscriptions} WHERE (uid = :uid) AND (type = :type)', array(
        ':uid' => $account->uid,
        ':type' => $type[0],
      ))
        ->fetchObject();
      $default = $field ? TRUE : FALSE;
      $form[$set][NOTIFY_NODE_TYPE . $type[0]] = array(
        '#type' => 'checkbox',
        '#title' => $type[1],
        '#return_value' => 1,
        '#default_value' => $default,
      );
    }
  }
  else {

    // No custom subscriptions, so inherit default.
    foreach ($enatypes as $type) {
      $form[$set][NOTIFY_NODE_TYPE . $type[0]] = array(
        '#type' => 'checkbox',
        '#title' => $type[1],
        '#return_value' => 1,
        '#default_value' => TRUE,
      );
    }
  }
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $account->uid,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save settings'),
  );
  return $form;
}