--- signup.module.orig 2007-08-14 03:17:54.000000000 -0400 +++ signup.module 2007-09-18 17:34:49.000000000 -0400 @@ -1,5 +1,5 @@ e.event_start", $curtime); - $reminder_common_sql = array( - 'primary' => '{signup} s', - 'fields' => array('n.title', 'n.nid', 's.reminder_email', 's.forwarding_email'), - 'where' => array('s.send_reminder = 1'), - 'joins' => array('INNER JOIN {node} n ON n.nid = s.nid'), - ); - - $sql = _signup_build_query($reminder_common_sql, $reminder_sql); - $result = db_query($sql); - - // 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); - - // 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; - } + // 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' => signup_format_date($event), - '%username' => $signup->name, - '%useremail' => $mail_address, - '%info' => $signup_info, + $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); - } -} - -/** - * Helper function that handles auto-closing events during cron. - * - * Invokes the method for the installed event/date backend module to get the - * right query fragments, and builds a query to find all events where signups - * should be closed (e.g. events that already started, etc). - * - * @see signup_cron() - * @see signup_autoclose_sql() - * @see _signup_build_query() - */ -function _signup_cron_autoclose() { - if (function_exists('signup_autoclose_sql')) { - $autoclose_sql = signup_autoclose_sql(); - } - if (empty($autoclose_sql)) { - // The backend doesn't support auto-closing events, so bail out now. - return; - } - - $autoclose_common_sql = array( - 'primary' => '{signup} s', - 'fields' => array('s.nid'), - 'where' => array('s.status = 1'), - ); + $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)); + } - $sql = _signup_build_query($autoclose_common_sql, $autoclose_sql); - $result = db_query($sql); - - // 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); + // 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)); } - watchdog('signup', t('Signups closed for %event by cron.', array('%event' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid)); } } /** - * Private query builder helper function. - * - * @param $common_sql - * Nested array of shared query fragments that are common to all date-based - * backends. The supported keys are: - * 'primary': the query's primary table and its alias (required). - * 'fields': array of fields to SELECT. - * 'joins': array of JOIN statements for other tables. - * 'where': array of WHERE clauses. - * 'group_by': array of GROUP BY fields. - * - * @param $backend_sql - * Similar nested array provided by the date-based backend include file, - * except that 'primary' is not allowed. - * - * @return - * Complete SQL statement based on the given query fragments. - */ -function _signup_build_query($common_sql, $backend_sql) { - - $fields = array_merge( - (!empty($common_sql['fields']) ? $common_sql['fields'] : array()), - (!empty($backend_sql['fields']) ? $backend_sql['fields'] : array()) - ); - $joins = array_merge( - (!empty($common_sql['joins']) ? $common_sql['joins'] : array()), - (!empty($backend_sql['joins']) ? $backend_sql['joins'] : array()) - ); - $where = array_merge( - (!empty($common_sql['where']) ? $common_sql['where'] : array()), - (!empty($backend_sql['where']) ? $backend_sql['where'] : array()) - ); - $group_by = array_merge( - (!empty($common_sql['group_by']) ? $common_sql['group_by'] : array()), - (!empty($backend_sql['group_by']) ? $backend_sql['group_by'] : array()) - ); - - $sql = 'SELECT '. implode(', ', $fields); - $sql .= ' FROM '. $common_sql['primary'] .' '; - if (!empty($joins)) { - $sql .= implode(' ', $joins); - } - if (!empty($where)) { - $sql .= ' WHERE '. implode(' AND ', $where); - } - if (!empty($group_by)) { - $sql .= ' GROUP BY '. implode(', ', $group_by); - } - return $sql; -} - -/** * Implementation of hook_help(). * @ingroup signup_core */ @@ -249,7 +147,7 @@ // If we're still here, consider the URL for help on various menu tabs. if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'signup-broadcast') { $node = node_load(arg(1)); - return '

'. t('This page allows you to send an email message to every user who signed up for this @node_type.', array('@node_type' => $node->type)) .'

'; + return '

' . t('This page allows you to send an email message to every user who signed up for this @node_type.', array('@node_type' => $node->type)) .'

'; } } @@ -279,9 +177,11 @@ 'callback' => 'signup_admin_page', 'title' => t('Signup administration'), ); + } else { // !$may_cache: dynamic menu items - _signup_initialize_event_backend(); + // 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( @@ -299,7 +199,7 @@ $email_own = user_access('email users signed up for own content') && ($user->uid == $node->uid); $email_all = user_access('email all signed up users'); $items[] = array( - 'path' => 'node/'. arg(1) .'/signups', + 'path' => 'node/' . arg(1) . '/signups', 'title' => t('Signups'), 'callback' => 'signup_node_admin_page', 'callback arguments' => array($node), @@ -308,7 +208,7 @@ 'weight' => 20, ); $items[] = array( - 'path' => 'node/'. arg(1) .'/signup-broadcast', + 'path' => 'node/' . arg(1) . '/signup-broadcast', 'title' => t('Signup broadcast'), 'callback' => 'drupal_get_form', 'callback arguments' => array('signup_broadcast_form', $node), @@ -321,24 +221,6 @@ return $items; } -function _signup_initialize_event_backend() { - define('SIGNUP_PATH', drupal_get_path('module', 'signup')); - if (defined('EVENT_API') && EVENT_API == '5.2') { - include(SIGNUP_PATH .'/signup_event_5.x-2.inc'); - } - else if (module_exists('event')) { - include(SIGNUP_PATH .'/signup_event_5.x-1.inc'); - } - else if (module_exists('date')) { - // include(SIGNUP_PATH .'/signup_date.inc'); - // Until date.module support exists, fall back to the no event case. - include(SIGNUP_PATH .'/signup_event_none.inc'); - } - else { - include(SIGNUP_PATH .'/signup_event_none.inc'); - } -} - /** * Implementation of hook_perm(). * @ingroup signup_core @@ -355,6 +237,36 @@ } /** + * Returns TRUE if user has access to this node. Checks the role-specific permissions for the node. + */ +function _signup_access(&$node, $account = NULL) { + // All signups with an empty access setting are available to all roles. + if (!user_access('sign up for content', $account)) + return FALSE; + + if (!$node->signup_access) + return TRUE; + + if (is_null($account)) { + global $user; + $account = $user; + } + + // Admin can always sign up. + if ($account->uid == 1) + return TRUE; + + // Otherwise, check roles + static $roles = array(); + if (!isset($roles[$account->uid])) { + $roles[$account->uid] = array_keys($account->roles); + $roles[$account->uid][] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID; + } + + return array_intersect($node->signup_access, $roles[$account->uid]); +} + +/** * Implementation of hook_user(). * @ingroup signup_core */ @@ -377,7 +289,7 @@ * @ingroup signup_core */ function signup_form_alter($form_id, &$form) { - switch ($form_id) { + switch($form_id) { case 'node_type_form': signup_alter_node_type_form($form_id, $form); break; @@ -396,9 +308,22 @@ $form['workflow']['signup_form'] = array( '#type' => 'checkbox', '#title' => t('Allow signups by default'), - '#default_value' => variable_get('signup_form_'. $type, FALSE) == 1, + '#default_value' => variable_get('signup_form_' . $type, FALSE) == 1, '#description' => t('If selected, users will be allowed to signup for this node type by default. Users with %admin_all_signups permission will be able to toggle this setting on a per-node basis.', array('%admin_all_signups' => t('administer all signups'))), ); + // Set default type access to default general access if nothing was specified for this content type yet. + $default_signup_access = variable_get('signup_access_' . $type, NULL); + if (is_null($default_signup_access)) { + $defaults = db_fetch_array(db_query("SELECT access from {signup} WHERE nid = 0")); + $default_signup_access = explode(',', $defaults['access']); + } + $form['workflow']['signup_access'] = array( + '#type' => 'checkboxes', + '#title' => t('Access'), + '#default_value' => $default_signup_access, + '#options' => signup_handler_filter_role(), + '#description' => t('Only the checked roles will be allowed to signup for this node type by default. If no roles are checked, access will not be restricted. Users with %admin_all_signups permission will be able to toggle this setting on a per-node basis.', array('%admin_all_signups' => t('administer all signups'))), + ); } /** @@ -416,7 +341,10 @@ } $signup_enabled = variable_get('signup_form_'. $form['type']['#value'], 0); - + + // Assign type to node (necessary for lookup in _signup_admin_form($node)) + $node->type = $form['type']['#value']; + // If the current user has global signup administration permissions, // or if this node-type is signup-enabled and the user has permission // to administer signups for their own content, add a fieldset for @@ -440,7 +368,7 @@ $radio_options[1] = t('Enabled'); if ($has_signups) { $radio_options[0] = t('Disabled, but save existing signup information'); - $radio_options[2] = t('Disabled, and remove all signup information') .' ('. t('This can not be undone, use with extreme caution!') .')'; + $radio_options[2] = t('Disabled, and remove all signup information') . ' ('. t('This can not be undone, use with extreme caution!') .')'; } else { $radio_options[0] = t('Disabled'); @@ -529,13 +457,15 @@ * @defgroup signup_nodeapi Functions for nodeapi integration */ -/** + /** * hook_nodeapi implementation * * @ingroup signup_nodeapi - */ +*/ function signup_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { global $form_values; + if (is_array($form_values['signup_access'])) + $form_values['signup_access'] = implode(',', array_keys(array_filter($form_values['signup_access']))); switch ($op) { case 'insert': if (isset($form_values['signup_enabled'])) { @@ -549,6 +479,7 @@ $form_values['signup_reminder_days_before'], $form_values['signup_reminder_email'], $form_values['signup_close_signup_limit'], + $form_values['signup_access'], ); } } @@ -568,10 +499,11 @@ $defaults['reminder_days_before'], $defaults['reminder_email'], $defaults['close_signup_limit'], + $defaults['access'], ); } if (isset($values)) { - db_query("INSERT INTO {signup} (nid, forwarding_email, send_confirmation, confirmation_email, send_reminder, reminder_days_before, reminder_email, close_signup_limit) VALUES (%d, '%s', %d, '%s', %d, %d, '%s', %d)", $values); + db_query("INSERT INTO {signup} (nid, forwarding_email, send_confirmation, confirmation_email, send_reminder, reminder_days_before, reminder_email, close_signup_limit, access) VALUES (%d, '%s', %d, '%s', %d, %d, '%s', %d, '%s')", $values); } break; @@ -586,7 +518,7 @@ // we might have to change the status, too. $cur_limit = db_result(db_query("SELECT close_signup_limit FROM {signup} WHERE nid = %d", $node->nid)); $limit_changed = $cur_limit != $node->signup_close_signup_limit; - db_query("UPDATE {signup} SET forwarding_email = '%s', send_confirmation = %d, confirmation_email = '%s', send_reminder = %d, reminder_days_before = %d, reminder_email = '%s', close_signup_limit = %d WHERE nid = %d", + db_query("UPDATE {signup} SET forwarding_email = '%s', send_confirmation = %d, confirmation_email = '%s', send_reminder = %d, reminder_days_before = %d, reminder_email = '%s', close_signup_limit = %d, access = '%s' WHERE nid = %d", $node->signup_forwarding_email, $node->signup_send_confirmation, $node->signup_confirmation_email, @@ -594,18 +526,20 @@ $node->signup_reminder_days_before, $node->signup_reminder_email, $node->signup_close_signup_limit, + $node->signup_access, $node->nid ); } else { - db_query("INSERT INTO {signup} (nid, forwarding_email, send_confirmation, confirmation_email, send_reminder, reminder_days_before, reminder_email, close_signup_limit) VALUES (%d, '%s', %d, '%s', %d, %d, '%s', %d)", $node->nid, + db_query("INSERT INTO {signup} (nid, forwarding_email, send_confirmation, confirmation_email, send_reminder, reminder_days_before, reminder_email, close_signup_limit, access) VALUES (%d, '%s', %d, '%s', %d, %d, '%s', %d, '%s')", $node->nid, $node->signup_forwarding_email, $node->signup_send_confirmation, $node->signup_confirmation_email, $node->signup_send_reminder, $node->signup_reminder_days_before, $node->signup_reminder_email, - $node->signup_close_signup_limit + $node->signup_close_signup_limit, + $node->signup_access ); } if (_signup_event_completed($node) && $node->signup_status) { @@ -648,7 +582,7 @@ // Load signup data for both new nodes w/ enabled node types, // and any existing nodes that are already signup enabled. - if ((!$node->nid && variable_get('signup_form_'. $node->type, 0)) || ($node->nid && db_num_rows($result))) { + if ((!$node->nid && variable_get('signup_form_' . $node->type, 0)) || ($node->nid && db_num_rows($result))) { $signup = db_fetch_object($result); $node->signup = 1; $node->signup_forwarding_email = $signup->forwarding_email; @@ -658,6 +592,7 @@ $node->signup_reminder_days_before = $signup->reminder_days_before; $node->signup_reminder_email = $signup->reminder_email; $node->signup_close_signup_limit = $signup->close_signup_limit; + $node->signup_access = explode(',', $signup->access); $node->signup_status = $signup->status; if ($node->nid) { $node->signup_total = db_result(db_query("SELECT COUNT(*) FROM {signup_log} WHERE nid = %d", $node->nid)); @@ -680,16 +615,14 @@ // The node has been closed for signups, and the user has // signup permissions. Let them know it's closed. if (!$node->signup_status) { - if (user_access('sign up for content')) { - $output = '

'. t('Signups closed for this event') .'

'; - // If they're logged in and already signed up, show their current - // signup info and give them the option to cancel. - if ($user->uid) { - $result = db_query("SELECT signup_time, form_data FROM {signup_log} WHERE uid = %d AND nid = %d", $user->uid, $node->nid); - if (db_num_rows($result)) { - $signup_info = db_fetch_object($result); - $output .= _signup_print_current_signup($node, $signup_info); - } + if (_signup_access($node)) { + $output = '

' . t('Signups closed for this event') . '

'; + // If they're already signed up, show their current signup + // info and give them the option to cancel. + $result = db_query("SELECT signup_time, form_data FROM {signup_log} WHERE uid = %d AND nid = %d", $user->uid, $node->nid); + if (db_num_rows($result)) { + $signup_info = db_fetch_object($result); + $output .= _signup_print_current_signup($node, $signup_info); } } } @@ -702,7 +635,7 @@ '!login' => l(t('login'), 'user/login', array(), drupal_get_destination()), '!register' => l(t('register'), 'user/register', array(), drupal_get_destination()), ); - if (user_access('sign up for content')) { + if (_signup_access($node)) { $needs_signup_form = TRUE; $anon_signup_form['signup_anon_mail'] = array( '#type' => 'textfield', @@ -730,7 +663,7 @@ if ($needs_signup_form) { // User isn't signed up, so check to make sure they have signup // permissions, and if so, print the themed signup form. - if (user_access('sign up for content')) { + if (_signup_access($node)) { $output = drupal_get_form('signup_form', $node, $anon_signup_form); } } @@ -795,7 +728,6 @@ */ function signup_form($node, $anon_signup_form = NULL) { global $user; - include(SIGNUP_PATH .'/signup.theme'); $form['nid'] = array('#type' => 'value', '#value' => $node->nid); $form['uid'] = array('#type' => 'value', '#value' => $user->uid); @@ -875,6 +807,22 @@ 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'), @@ -884,25 +832,37 @@ array('data' => t('Operations')), ); - if (function_exists('signup_admin_form_header') && is_array($extra_header = signup_admin_form_header())) { - array_unshift($header, $extra_header); + // 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); } - list($sql, $sql_count) = 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, 0, $sql_count); + $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 (function_exists('signup_admin_form_extra')) { - $row['start'] = signup_admin_form_extra($signup_event); + 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) : '', + ); } - // 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. @@ -936,63 +896,6 @@ return $form; } -function signup_admin_form_sql() { - $admin_common_sql = array( - 'primary' => '{signup} s', - 'fields' => array( - 'n.nid', - 'n.title', - 's.status AS signup_status', - 'COUNT(s_l.nid) AS signup_total', - 's.close_signup_limit AS signup_close_signup_limit', - ), - 'group_by' => array( - 'n.nid', - 'n.title', - 'signup_status', - 'signup_close_signup_limit', - ), - 'joins' => array( - 'INNER JOIN {node} n ON n.nid = s.nid', - 'LEFT JOIN {signup_log} s_l ON s.nid = s_l.nid', - ), - ); - - $type = $_SESSION['signup_status_filter']; - if ($type == 'open') { - $filter_status = 1; - } - else if ($type == 'closed') { - $filter_status = 0; - } - if (isset($filter_status)) { - $admin_common_sql['where'] = array("s.status = $filter_status"); - } - - // Get the right query elements from the currently installed backend - if (function_exists('signup_admin_sql')) { - $admin_sql = signup_admin_sql(); - } - else { - $admin_sql = array(); - } - - // Build the main query. - $sql = _signup_build_query($admin_common_sql, $admin_sql); - - // Construct the proper pager query using just the WHERE clauses (if any). - $where = array_merge( - (!empty($admin_common_sql['where']) ? $admin_common_sql['where'] : array()), - (!empty($admin_sql['where']) ? $admin_sql['where'] : array()) - ); - $sql_count = "SELECT COUNT(nid) FROM {signup} s"; - if (!empty($where)) { - $sql_count .= ' WHERE '. implode(' AND ', $where); - } - - return array(db_rewrite_sql($sql), db_rewrite_sql($sql_count, 's')); -} - function theme_signup_admin_form($form) { if (!isset($form['nids'])) { $type = $_SESSION['signup_status_filter']; @@ -1087,7 +990,7 @@ function signup_open_signup($nid, $cron = 'no') { db_query("UPDATE {signup} SET status = 1 WHERE nid = %d", $nid); if ($cron == 'no') { - $node = node_load(array('nid' => $nid)); + $node = node_load(array('nid'=>$nid)); foreach (module_implements('signup_open') as $module) { $function = $module .'_signup_open'; $function($node); @@ -1114,7 +1017,7 @@ '#description' => t('New signup-enabled nodes will start with these settings.'), '#collapsible' => true, ); - $form['node_defaults']['_signup_admin_form'] = _signup_admin_form($node); + $form['node_defaults']['_signup_admin_form'] = _signup_admin_form(); $form['adv_settings'] = array( '#type' => 'fieldset', @@ -1143,16 +1046,18 @@ * @param $form_values The constructed form values array of the submitted form. */ function signup_settings_page_submit($form_id, $form_values) { + $form_values['signup_access'] = implode(',', array_keys(array_filter($form_values['signup_access']))); $op = isset($form_values['op']) ? $form_values['op'] : ''; if ($op == t('Save configuration') && db_num_rows(db_query('SELECT nid FROM {signup} WHERE nid = 0'))) { - db_query("UPDATE {signup} SET forwarding_email = '%s', send_confirmation = %d, confirmation_email = '%s', send_reminder = %d, reminder_days_before = %d, reminder_email = '%s', close_signup_limit = %d WHERE nid = 0", + db_query("UPDATE {signup} SET forwarding_email = '%s', send_confirmation = %d, confirmation_email = '%s', send_reminder = %d, reminder_days_before = %d, reminder_email = '%s', close_signup_limit = %d, access = '%s' WHERE nid = 0", $form_values['signup_forwarding_email'], $form_values['signup_send_confirmation'], $form_values['signup_confirmation_email'], $form_values['signup_send_reminder'], $form_values['signup_reminder_days_before'], $form_values['signup_reminder_email'], - $form_values['signup_close_signup_limit'] + $form_values['signup_close_signup_limit'], + $form_values['signup_access'] ); } else { @@ -1171,6 +1076,7 @@ 'signup_reminder_days_before', 'signup_reminder_email', 'signup_close_signup_limit', + 'signup_access' ); foreach ($settings as $setting) { unset($form_values[$setting]); @@ -1192,9 +1098,13 @@ // We don't want to return anything for anon users. if ($uid != 0) { - $sql = signup_list_user_signups_sql(); + $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'; + // Pull all open signup nodes for this user. - $result = db_query(db_rewrite_sql($sql), $uid); + $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); while ($node = db_fetch_array($result)) { $titles[$node['nid']] = l($node['title'], 'node/'. $node['nid']); } @@ -1252,7 +1162,7 @@ // If we made it this far, we're going to need the full $user object. $user = user_load(array('uid' => $signup_form['uid'])); - if (user_access('sign up for content') && $node->signup_status) { + if (_signup_access($node) && $node->signup_status) { // Grab the current time once, since we need it in a few places. $curtime = time(); @@ -1262,19 +1172,26 @@ // Figure out if confirmation or reminder emails will be sent and // inform the user. - $confirmation_email = $node->signup_send_confirmation ? ' '. t('You will receive a confirmation email shortly which contains further event information.') : ''; - $reminder_email = $node->signup_send_reminder ? ' '. t('You will receive a reminder email !number !days before the event.', array('!number' => $node->signup_reminder_days_before, '!days' => format_plural($node->signup_reminder_days_before, 'day', 'days'))) : ''; + $confirmation_email = $node->signup_send_confirmation ? ' ' . t('You will receive a confirmation email shortly which contains further event information.') : ''; + $reminder_email = $node->signup_send_reminder ? ' ' . t('You will receive a reminder email !number !days before the event.', array('!number' => $node->signup_reminder_days_before, '!days' => format_plural($node->signup_reminder_days_before, 'day', 'days'))) : ''; // 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. - $starttime = signup_format_date($node); + $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]'); $signup_data_array = array(); if (isset($signup_form['signup_form_data'])) { $signup_form_data = signup_build_signup_data($signup_form['signup_form_data'], 'email'); - $signup_data = t('SIGNUP INFORMATION') ."\n\r\n\r". $signup_form_data; + $signup_data = t('SIGNUP INFORMATION') . "\n\r\n\r" . $signup_form_data; } // Determine if this is an anon signup or not, and get the // appropriate email address to use. @@ -1302,12 +1219,12 @@ // If a forwarding email is to be sent, compose the mail message, // translate the string substitutions, and send it. if ($node->signup_forwarding_email) { - $header = array('From' => t('New Event Signup') ."<$from>"); + $header = array('From' => t('New Event Signup') . "<$from>"); $subject = t('Signup confirmation for event: !title', array('!title' => $node->title)); $message = t('The following information was submitted as a signup for !title', array('!title' => $node->title)) . - "\n\r". t('Date/Time: !time', array('!time' => $starttime)) .":\n\r\n\r". + "\n\r". t('Date/Time: !time', array('!time'=>$starttime)) .":\n\r\n\r". "\n\r". t('Username:') . $user->name . - "\n\r". t('Email:') . $user_mail ."\n\r\n\r". $signup_data; + "\n\r". t('Email:') . $user_mail . "\n\r\n\r" . $signup_data; drupal_mail('signup_forwarding_mail', $node->signup_forwarding_email, $subject, $message, $from, $header); } @@ -1334,7 +1251,6 @@ drupal_not_found(); return; } - include(SIGNUP_PATH .'/signup.theme'); drupal_set_title(t('Signups for @user', array('@user' => $user->name))); $titles = signup_list_user_signups($user->uid); foreach ($titles as $nid => $title) { @@ -1474,6 +1390,7 @@ drupal_set_message(t('Signups closed for !title.', array('!title' => l($node->title, "node/$node->nid")))); } } + } /** @@ -1525,7 +1442,7 @@ drupal_set_message(t('Total signups for !title now below limit, signups re-opened.', array('!title' => $node_link))); } } - else if ($type == 'limit') { + else if($type == 'limit') { drupal_set_message(t('Signup limit updated for !title.', array('!title' => $node_link))); } } @@ -1551,7 +1468,7 @@ $args = $args[0]; $form_id = array_shift($args); if (strpos($form_id, 'signup_user_cancel_form') !== FALSE) { - if ($form_id == 'signup_user_cancel_form_'. $args[0]) { + if ($form_id == 'signup_user_cancel_form_' . $args[0]) { array_shift($args); // Get rid of the extra uid arg. $forms[$form_id] = array( 'callback' => 'signup_user_cancel_form', @@ -1615,11 +1532,11 @@ * by the settings page and the node edit page. * @ingroup signup_internal */ -function _signup_admin_form($node) { +function _signup_admin_form($node = null) { $signup_token_description = t('Supported string substitutions: %event, %time, %username, %useremail, %info (user signup information).'); // Load the default admin form data for new nodes. - if (!$node || !$node->signup) { + if(!$node || !$node->signup) { $result = db_fetch_object(db_query("SELECT * FROM {signup} WHERE nid = 0")); $node->signup_forwarding_email = $result->forwarding_email; $node->signup_send_confirmation = $result->send_confirmation; @@ -1628,8 +1545,11 @@ $node->signup_reminder_days_before = $result->reminder_days_before; $node->signup_reminder_email = $result->reminder_email; $node->signup_close_signup_limit = $result->close_signup_limit; + + $type_access = variable_get('signup_access_' . $node->type, NULL); + $node->signup_access = (!is_null($type_access) ? $type_access : explode(',', $result->access)); } - + $form['signup_forwarding_email'] = array( '#type' => 'textfield', '#title' => t('Send signups to'), @@ -1684,6 +1604,14 @@ '#size' => 4, '#maxlength' => 8, '#description' => t('Maximum number of users who can sign up before signups are automatically closed. If set to 0, there is no limit.'), ); + + $form['signup_access'] = array( + '#type' => 'checkboxes', + '#title' => t('Access'), + '#default_value' => $node->signup_access, + '#options' => signup_handler_filter_role(), + '#description' => t('Only the checked roles will be allowed to signup for this node. If no roles are checked, access will not be restricted.'), + ); $form['signup'] = array('#type' => 'hidden', '#value' => 1); return $form; @@ -1710,7 +1638,7 @@ signup_build_signup_data($value, 'table'); } else { - $rows[] = array($key .':', check_plain($value)); + $rows[] = array($key . ':', check_plain($value)); } } return $rows; @@ -1723,7 +1651,7 @@ $output .= '
'. signup_build_signup_data($value) .'
'; } else { - $output .= '
'. $key .': '. check_plain($value) .'
'; + $output .= '
'. $key . ': ' . check_plain($value) . '
'; } } return $output; @@ -1761,13 +1689,6 @@ return $form; } - $tokens = array('%event', '%username', '%useremail'); - if (function_exists('signup_extra_tokens')) { - $tokens = array_merge($tokens, signup_extra_tokens()); - } - sort($tokens); - $token_text = t('Supported string substitutions: %tokens.', array('%tokens' => implode(', ', $tokens))); - $form['subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), @@ -1777,7 +1698,7 @@ '#type' => 'textarea', '#title' => t('Message body'), '#required' => true, - '#description' => t('Body of the email message you wish to send to all users who have signed up for this @node_type.', array('@node_type' => $node->type)) .' '. $token_text, + '#description' => t('Body of the email message you wish to send to all users who have signed up for this @node_type.', array('@node_type' => $node->type)) .' '. t('Supported string substitutions: %event, %time, %username, %useremail.'), '#rows' => 10, ); $form['send'] = array( @@ -1830,22 +1751,20 @@ * @param $form_values */ function signup_broadcast_form_submit($form_id, $form_values) { - $addresses = signup_get_email_addresses($form_values['nid']); + $addresses = signup_get_email_addresses($form_values[nid]); if (is_array($addresses)) { $from = $form_values['from']; $subject = $form_values['subject']; - $event = node_load($form_values['nid']); + $event = node_load($form_values[nid]); foreach ($addresses as $signup) { $mail_address = $signup->anon_mail ? $signup->anon_mail : $signup->mail; $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->uid ? $signup->name : variable_get('anonymous', t('Anonymous')), '%useremail' => $mail_address, // '%info' => $signup_info, ); - - $trans['%time'] = signup_format_date($event); - $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)); @@ -1853,3 +1772,34 @@ } drupal_set_message(t('Message sent to all users who have signed up')); } + + +/* + * Create a list of roles. + */ +function signup_handler_filter_role() { + $rids = array(); + $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name"); + while ($obj = db_fetch_object($result)) { + $rids[$obj->rid] = $obj->name; + } + return $rids; +} +/** + * 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; +}