Index: signup.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signup/signup.module,v
retrieving revision 1.109
diff -u -p -r1.109 signup.module
--- signup.module	21 Jun 2007 15:18:29 -0000	1.109
+++ signup.module	6 Jul 2007 12:59:12 -0000
@@ -1,6 +1,23 @@
 <?php
 // $Id: signup.module,v 1.109 2007/06/21 15:18:29 dww Exp $
 
+define('SIGNUP_PATH', drupal_get_path('module', 'signup'));
+if (defined(EVENT_API) && EVENT_API == '5.2') {
+  define('SIGNUP_EVENT', 'EVENT_5.2');
+  include(SIGNUP_PATH .'/signup_event_5.2.inc');
+  include(SIGNUP_PATH .'/signup_5.2.theme');
+}
+else if (module_exists('event')) {
+  define('SIGNUP_EVENT', 'EVENT_5.1');
+  include(SIGNUP_PATH .'/signup_event_5.1.inc');
+  include(SIGNUP_PATH .'/signup_5.1.theme');
+}
+else if (module_exists('date')) {
+  define('SIGNUP_EVENT', 'DATE');
+  include(SIGNUP_PATH .'/signup_date.inc');
+  include(SIGNUP_PATH .'/signup_date.theme');
+}
+
 /**
  * @defgroup signup_core Core drupal hooks
  */
@@ -39,90 +56,13 @@ function signup_block($op = 'list', $del
   }
 }
 
-function signup_init() {
-  if (module_exists('views')) {
+if (module_exists('views')) {
+  function signup_init() {
     require './'. drupal_get_path('module', 'signup') .'/signup_views.inc';
   }
 }
 
 /**
- * Implementation of hook_cron().
- * @ingroup signup_core
- */
-function signup_cron() {
-  // Only run this function if the event module is enabled.
-  if (module_exists('event')) {
-    // We must manually include this here as the event module doesn't
-    // include timezone support on all page requests.
-    include_once(drupal_get_path('module', 'event') .'/event_timezones.inc');
-
-    // Get the current time, and pull all of the nodes for which the
-    // current time + the reminder_days_before time is greater than
-    // the event's start date.  These are the events for which a
-    // reminder email needs to be sent.
-    $curtime = time();
-    $result = db_query("SELECT n.title, n.nid, e.event_start, e.timezone, s.reminder_email, s.forwarding_email FROM {signup} s
-      INNER JOIN {node} n ON n.nid = s.nid INNER JOIN {event} e ON e.nid = s.nid WHERE
-      s.send_reminder = 1 AND (%d + ((s.reminder_days_before) * 86400)) > e.event_start", $curtime);
-
-    // Grab each event, construct the email header and subject, and query
-    // the signup log to pull all users who are signed up for this event.
-    $from = variable_get('site_mail', 'noadmin@noadmin.com');
-    while ($event = db_fetch_object($result)) {
-      $subject = t('Event reminder: !event', array('!event' => $event->title));
-      $signups = db_query("SELECT u.name, u.mail, s_l.anon_mail, s_l.form_data FROM {signup_log} s_l INNER JOIN {users} u ON u.uid = s_l.uid WHERE s_l.nid = %d", $event->nid);
-
-      // Get timezone offset.
-      $offset = event_get_offset($event->timezone, $event->event_start);
-
-      // Loop through the users, composing their customized message
-      // and sending the email.
-      while ($signup = db_fetch_object($signups)) {
-        $mail_address = $signup->anon_mail ? $signup->anon_mail : $signup->mail;
-        $signup_data_array = unserialize($signup->form_data);
-        if (!empty($signup_data_array)) {
-          $signup_data = signup_build_signup_data($signup_data_array, 'email');
-          $signup_info = t('SIGNUP INFORMATION') ."\n\r". $signup_data;
-        }
-
-        $trans = array(
-          '%event' => $event->title,
-          '%time' => _event_date(variable_get('signup_date_string', 'D, M jS, g:i A'), $event->event_start, $offset),
-          '%username' => $signup->name,
-          '%useremail' => $mail_address,
-          '%info' => $signup_info,
-        );
-        $message = strtr($event->reminder_email, $trans);
-        drupal_mail('signup_reminder_mail', $mail_address, $subject, $message, $from);
-        watchdog('signup', t('Reminder for %event sent to %useremail.', array('%event' => $event->title, '%useremail' => $mail_address)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $event->nid));
-      }
-
-      // Reminders for this event are all sent, so mark it in the
-      // database so they're not sent again.
-      db_query("UPDATE {signup} SET send_reminder = 0 WHERE nid = %d", $event->nid);
-    }
-
-    // Calculate the closing time for the event, which is the current
-    // time + the number of hours before the event start when closing is
-    // preferred.  Query the database for all signup events which have a
-    // start time less than this.
-    $closing_time = $curtime + (variable_get('signup_close_early', 1) * 3600);
-    $result = db_query("SELECT s.nid FROM {signup} s INNER JOIN {event} e ON e.nid = s.nid WHERE s.status = 1 AND e.event_start < %d", $closing_time);
-
-    // Loop through the results, calling the event closing function.
-    while ($signup = db_fetch_object($result)) {
-      signup_close_signup($signup->nid, $cron = 'yes');
-      $node = node_load($signup->nid);
-      foreach (module_implements('signup_close') as $module) {
-        $function = $module .'_signup_close';
-        $function($node);
-      }
-      watchdog('signup', t('Signups closed for %event by cron.', array('%event' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
-    }
-  }
-}
-
-/**
  * Implementation of hook_help().
  * @ingroup signup_core
  */
@@ -180,9 +120,6 @@ function signup_menu($may_cache) {
   
   }
   else {  // !$may_cache: dynamic menu items
-    // Include other php code that we need when not serving cached pages:
-    include_once(drupal_get_path('module', 'signup') .'/signup.theme');
-
     // User signup schedule callback
     $items[] = array(
       'path' => 'user/'. arg(1) .'/signups',
@@ -754,62 +691,22 @@ function signup_filter_status_form_submi
 function signup_admin_form() {
   // Figure out if the current user has permission to use signup broadcast.
   $access_broadcast = user_access('email all signed up users');
-  
-  // Add optional SQL to the query if the event module is enabled.
-  $has_event = module_exists('event');
-  $event_select = $has_event ? ', e.event_start, e.timezone' : '';
-  $event_join = $has_event ? ' LEFT JOIN {event} e ON e.nid = n.nid' : '';
-
-  $type = $_SESSION['signup_status_filter'];
-  if ($type == 'open') {
-    $where = ' WHERE s.status = 1';
-  }
-  else if ($type == 'closed') {
-    $where = ' WHERE s.status = 0';
-  }
-  else {
-    $where = '';
-  }
-
-  $header = array(
-    array('data' => t('Title'), 'field' => 'n.title', 'sort' => 'asc'),
-    array('data' => t('Signups'), 'field' => 'signup_total'),
-    array('data' => t('Limit'), 'field' => 'signup_close_signup_limit'),
-    array('data' => t('Status'), 'field' => 'signup_status'),
-    array('data' => t('Operations')),
-  );
 
-  // If event.module enabled, add event start time to table
-  if ($has_event) {
-    $header = array_merge(array(array('data' => t('Start'), 'field' => 'e.event_start')), $header);
-  }
+  $header = signup_admin_form_header();
+  $sql = signup_admin_form_sql();
 
   $form['header']['#value'] = $header;
 
-  // Construct SQL to get all the info for the requested signup nodes.
-  $sql = "SELECT n.title, n.nid, s.status AS signup_status$event_select,
-          COUNT(s_l.nid) AS signup_total,
-          s.close_signup_limit AS signup_close_signup_limit
-          FROM {signup} s INNER JOIN {node} n ON n.nid = s.nid
-          LEFT JOIN {signup_log} s_l ON s.nid = s_l.nid $event_join";
-  $sql .= $where;
-  $sql .= " GROUP BY n.nid, n.title, s.close_signup_limit, s.status$event_select";
   $sql .= tablesort_sql($header);
   $result = pager_query($sql, 25);
 
   // Loop through the signup nodes, and generate our form elements
   while ($signup_event = db_fetch_object($result)) {
     $row = array();
-    if ($has_event) {
-      // Must include this here as event module doesn't include timezone
-      // support on all page requests.
-      include_once(drupal_get_path('module', 'event') .'/event_timezones.inc');
-      $offset = $signup_event->event_start ? event_get_offset($signup_event->timezone, $signup_event->event_start) : '';
-      $row['start'] = array(
-        '#type' => 'markup',
-        '#value' => $signup_event->event_start ?_event_date(variable_get('signup_date_string', 'D, M jS, g:i A'), $signup_event->event_start, $offset) : '',
-      );
+    if (function_exists('signup_admin_form_extra')) {
+      $row['start'] = signup_admin_form_extra($signup_event);
     }
+
     // Instead of duplicating the logic from the node/N/signups admin
     // form, we just call that form builder here and lift the elements
     // we need directly from that.
@@ -1042,13 +939,9 @@ function signup_list_user_signups($uid) 
 
   // We don't want to return anything for anon users.
   if ($uid != 0) {
-    $has_event = module_exists('event');
-    $event_join = $has_event ? ' LEFT JOIN {event} e ON e.nid = n.nid' : '';
-    $event_where = $has_event ? ' AND (e.event_start >= '. time() .' OR e.event_start IS NULL)' : '';
-    $order_by = $has_event ? 'e.event_start' : 'n.title';
-
+    $sql = signup_list_user_signups_sql();
     // Pull all open signup nodes for this user.
-    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n INNER JOIN {signup_log} s_l ON n.nid = s_l.nid $event_join WHERE s_l.uid = '%s' $event_where ORDER BY $order_by"), $uid);
+    $result = db_query(db_rewrite_sql($sql), $uid);
     while ($node = db_fetch_array($result)) {
       $titles[$node['nid']] = l($node['title'], 'node/'. $node['nid']);
     }
@@ -1122,16 +1015,9 @@ function signup_sign_up_user($signup_for
     // Insert the user into the signup_log.
     db_query("INSERT INTO {signup_log} (uid, nid, anon_mail, signup_time, form_data) VALUES (%d, %d, '%s', %d, '%s')", $signup_form['uid'], $signup_form['nid'], $signup_form['signup_anon_mail'], $curtime, $signup_form_data);
 
-    // Must include this here as event module doesn't include timezone
-    // support on all page requests.
-    if (module_exists('event')) {
-      include_once(drupal_get_path('module', 'event') .'/event_timezones.inc');
-    }
-
     // Format the start time, and compose the user's signup data for
     // later use in the emails.
-    $offset = $node->event_start ? event_get_offset($node->timezone, $node->event_start) : '';
-    $starttime = $node->event_start ? _event_date(variable_get('signup_date_string', 'D, M jS, g:i A'), $node->event_start, $offset) : t('[Untimed]');
+    $starttime = signup_format_date($node->event_start);
     $signup_data_array = array();
     if (isset($signup_form['signup_form_data'])) {
       $signup_form_data = signup_build_signup_data($signup_form['signup_form_data'], 'email');
@@ -1622,7 +1508,7 @@ function signup_broadcast_form($node) {
   }
 
   $tokens = array('%event', '%username', '%useremail');
-  if (module_exists('event')) {
+  if (SIGNUP_EVENT == 'EVENT_5.1' || SIGNUP_EVENT == 'EVENT_5.2') {
     $tokens[] = '%time';
   }
   sort($tokens);
@@ -1703,35 +1589,13 @@ function signup_broadcast_form_submit($f
         '%useremail' => $mail_address,
 //        '%info' => $signup_info,
       );
-      if (module_exists('event')) {
-        $trans['%time'] = _event_date(variable_get('signup_date_string', 'D, M jS, g:i A'), $event->event_start, $offset);
-      }
-      else {
-        $trans['%time'] = t('[Untimed]');
-      }
+
+      $trans['%time'] = signup_format_date($event->event_start);
+
       $message = strtr($form_values['message'], $trans);
       drupal_mail('signup_broadcast_mail', $mail_address, $subject, $message, $from);
       watchdog('signup', t('Broadcast email for %event sent to %email.', array('%event' => $event->title, '%email' => $mail_address)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $event->nid));
     }
   }
   drupal_set_message(t('Message sent to all users who have signed up'));
-}
-
-/**
- * Returns true if the given node is event-enabled, and the start time
- * has already passed the "Close x hours before" setting.
- */
-function _signup_event_completed($node) {
-  if (module_exists('event')) {
-    if (is_numeric($node)) {
-      $node = node_load($node);
-    }
-    if (isset($node->event_start)) {
-      $closing_time = time() + (variable_get('signup_close_early', 1) * 3600);
-      if ($node->event_start < $closing_time) {
-        return true;
-      }
-    }
-  }
-  return false;
-}
+}
\ No newline at end of file
