<?php
// $Id: notify.module,v 2.57.2.1 2006/04/20 18:57:21 robroy Exp $

/**
 * Implementation of hook_help().
 */
function notify_help($section) {
  switch ($section) {
    case 'admin/help#notify':
      $output = '<p>'. t('The notification module allows users to subscribe to periodic emails which include all new or revised content and/or comments much like the daily news letters sent by some websites.  Even if this feature is not configured for normal site users, it can be a useful feature for an administrator of a site to monitor content submissions and comment posts.') .'</p>';
      $output .= '<p>'. t('The administrator sets the frequency of the emails in the notify administration interface. They can also set how many email failures should occur before notify stops sending notifications.  Note that cron must be enabled for notifications to be sent out.') .'</p>';
      $output .= t('<p>You can</p><ul><li>set up your site to run tasks automatically at required intervals. For more information, see <a href="%admin-help-system">cron</a>.</li><li>administer notify <a href="%admin-settings-notify">administer &gt;&gt; settings &gt;&gt; notify</a>.</li></ul>', array('%admin-help-system' => url('admin/help/system'), '%admin-settings-notify' => url('admin/settings/notify')));
      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%notify">Notify page</a>.', array('%notify' => 'http://www.drupal.org/handbook/modules/notify/')) .'</p>';
      return $output;
    case 'admin/modules#description':
      return t('Enables notifications by email.');
  }
}

/**
 * Implementation of hook_settings().
 */
function notify_settings() {
  $period = array( 
    900         => format_interval(900), 
    1800        => format_interval(1800), 
    3600        => format_interval(3600), 
    10800       => format_interval(10800), 
    21600       => format_interval(21600), 
    32400       => format_interval(32400), 
    43200       => format_interval(43200), 
    86400       => format_interval(86400), 
    172800      => format_interval(172800), 
    259200      => format_interval(259200), 
    604800      => format_interval(604800), 
    1209600     => format_interval(1209600), 
    2419200     => format_interval(2419200), 
    1000000000  => 'Never' 
    );
    
  $form['notify_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('E-mail notification settings'),
    '#collapsible' => TRUE
    );
    
  $form['notify_settings']['notify_send'] = array( 
    '#type' => 'select', 
    '#title' => t('Send notifications every'), 
    '#default_value' => variable_get('notify_send', 86400), 
    '#options' => $period, 
    '#description' => t('Requires crontab.')
    );
    
  $form['notify_settings']['notify_attempts'] = array( 
    '#type' => 'select', 
    '#title' => t('Number of failed sends after which notifications are disabled'), 
    '#default_value' => variable_get('notify_attempts', 5), 
    '#options' => array(t('Disabled'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20) 
    );

  return $form;
}

/**
 * Implementation of hook_cron().
 */
function notify_cron() {
  if (time() - variable_get('notify_send_last', 0) > variable_get('notify_send', 86400)) {
    include_once drupal_get_path('module', 'notify') . '/notify.inc';
    _notify_send();
    variable_set('notify_send_last', time());
    cache_clear_all('variables');
  }
}

/**
 * Implementation of hook_user().
 */
function notify_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'delete':
      db_query('DELETE FROM {notify} WHERE uid = %d', $user->uid);
      break;
  }
}

/**
 * Implementation of hook_perm().
 */
function notify_perm() {
  return array('access notify', 'administer notify');
}

/**
 * Implementation of hook_menu().
 */
function notify_menu($may_cache) {
  global $user;

  $items = array();

  if ($may_cache) {
    $items[] = array('path' => "user/$user->uid/notify", 'title' => t('my notify settings'), 'callback' => 'notify_page', 'access' => user_access('access notify'), 'type' => MENU_LOCAL_TASK);
    $items[] = array('path' => "admin/user/notify", 'title' => t("notifications"), 'callback' => 'notify_admin', 'access' => user_access('administer notify'), 'type' => MENU_LOCAL_TASK);
  }
  else {
    if (arg(0) == 'user' && is_numeric(arg(1))) {
      $account = user_load(array('uid' => arg(1)));
      if ($user !== FALSE && $account->uid) {
        $items[] = array(
          'path' => 'user/'.$account->uid.'/notify', 
          'title' => t('notify settings'), 
          'callback' => 'notify_page', 
          'callback arguments' => array(arg(1)), 
          'access' => user_access('administer notify'), 
          'type' => MENU_LOCAL_TASK
          );    
      }
    }
  }

  return $items;
}

/**
 * Implementation of hook_page().
 */
function notify_page($uid = 0) {
  include_once drupal_get_path('module', 'notify') . '/notify.inc';
  return _notify_page($uid);
}

/**
 * Implementation of hook_admin().
 */
function notify_admin() {
  include_once drupal_get_path('module', 'notify') . '/notify.inc';

  return _notify_admin();
}

