View source
<?php
function signup_block($op = 'list', $delta = 0) {
global $user;
switch ($op) {
case 'list':
$blocks[0]['info'] = t('List of users current signups.');
return $blocks;
case 'view':
if (user_access('access content')) {
switch ($delta) {
case 0:
$titles = signup_list_user_signups($user->uid);
if (count($titles)) {
$block['subject'] = t('Current signups');
$block['content'] = theme_item_list($titles) . l(t('View signup schedule'), "user/{$user->uid}/signups");
}
return $block;
}
}
break;
}
}
function signup_cron() {
if (module_exists('event')) {
require_once drupal_get_path('module', 'event') . '/event_timezones.inc';
$curtime = time();
$result = db_query("SELECT n.title, n.nid, e.event_start, e.timezone, s.reminder_email, s.forwarding_email FROM {signup} s\n INNER JOIN {node} n ON n.nid = s.nid INNER JOIN {event} e ON e.nid = s.nid WHERE\n s.send_reminder = 1 AND (%d + ((s.reminder_days_before) * 86400)) > e.event_start", $curtime);
$from = variable_get('site_mail', ini_get('sendmail_from'));
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);
$offset = event_get_offset($event->timezone, $event->event_start);
while ($signup = db_fetch_object($signups)) {
$mail_address = $signup->anon_mail ? $signup->anon_mail : $signup->mail;
$signup_data = unserialize($signup->form_data);
if (!empty($signup_data)) {
$signup_info = theme('signup_email_token_custom_data', $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));
}
db_query("UPDATE {signup} SET send_reminder = 0 WHERE nid = %d", $event->nid);
}
$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.completed = 0 AND e.event_start < %d", $closing_time);
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));
}
}
}
function signup_help($section) {
switch ($section) {
case 'admin/help#signup':
return '<p>' . t('Signup allows users to sign up (in other words, register) for content of any type. The most common use is for events, where users indicate they are planning to attend. This module includes options for sending a notification email to a selected email address upon a new user signup (good for notifying event coordinators, etc.) and a confirmation email to users who sign up. Each of these options are controlled per node. When used on event nodes (with event.module installed and regular cron runs), it can also send out reminder emails to all signups a configurable number of days before the start of the event (also controlled per node) and to automatically close event signups 1 hour before their start (general setting). Settings exist for resticting signups to selected roles and content types.') . '</p><p>' . t('To use signup, you must enable which content types should allow signups in administer->settings->content types, and you must also grant the %sign_up_for_content permission to any user role that should be able to sign up in administer->access control. Each signup-enabled node will now have a place for users to sign up.', array(
'%sign_up_for_content' => 'sign up for content',
)) . '</p><p>' . t('There are two ways for a user to have administrative access to the signup information about a given node: either the user has the %administer_signups_for_own_content permission and they are viewing a node they created, or the user has the global %administer_all_signups permission. Administrative access allows a user to view all the users who have signed up for the node, along with whatever information they included when they signed up. Signup administrators can also cancel other user\'s signups for the node, and can close signups on the node entirely (meaning no one else is allowed to sign up).', array(
'%administer_signups_for_own_content' => 'administer signups for own content',
'%administer all signups' => 'administer all signups',
)) . '</p><p>' . t('Default settings for notification email address, reminder emails and confirmation emails are located in administer->settings->signup. These will be the default values used for a signup node unless otherwise specified (to configure these options per node, visit \'edit\' for that node and make the adjustments in the \'Sign up settings\' section).') . '</p><p>' . t('Signups can be manually closed for any node at the %signup_overview page, or on the \'signups\' tab on each node.', array(
'%signup_overview' => t('Signup overview'),
)) . '</p><p>' . t('The user signup form is fully themable -- form fields may be added or deleted. For more details see the instructions in signup.theme, where a sample user form is included.') . '</p>';
}
}
function signup_menu($may_cache) {
global $user;
$items = array();
$access = user_access('administer all signups');
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/signup',
'description' => t('Configure settings for signups.'),
'access' => $access,
'callback' => 'drupal_get_form',
'callback arguments' => array(
'signup_settings_page',
),
'title' => user_access('access administration pages') ? t('Signup') : t('Signup settings'),
);
$items[] = array(
'path' => 'admin/content/signup',
'description' => t('View all signup-enabled posts, and open or close signups on them.'),
'access' => $access,
'callback' => 'signup_admin_page',
'title' => t('Signup administration'),
);
$items[] = array(
'path' => 'closesignup',
'access' => $access,
'type' => MENU_CALLBACK,
'callback' => 'signup_close_signup_admin',
);
$items[] = array(
'path' => 'opensignup',
'access' => $access,
'type' => MENU_CALLBACK,
'callback' => 'signup_open_signup_admin',
);
}
else {
require_once drupal_get_path('module', 'signup') . '/signup.theme';
$items[] = array(
'path' => 'user/' . arg(1) . '/signups',
'access' => $access || $user->uid == arg(1),
'type' => MENU_CALLBACK,
'callback' => 'signup_user_schedule',
'callback arguments' => array(
$uid => arg(1),
),
);
if (arg(0) == 'node' && is_numeric(arg(1)) && db_num_rows(db_query("SELECT nid FROM {signup} WHERE nid = %d", arg(1)))) {
$node = node_load(array(
'nid' => arg(1),
));
$access_own = user_access('administer signups for own content') && $user->uid == $node->uid;
$items[] = array(
'path' => 'node/' . arg(1) . '/signups',
'title' => t('Signups'),
'callback' => 'signup_user_signups_form',
'callback arguments' => array(
$node,
),
'access' => $access || $access_own,
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
}
}
return $items;
}
function signup_perm() {
return array(
'sign up for content',
'view all signups',
'administer all signups',
'administer signups for own content',
);
}
function signup_user($op, &$edit, &$user, $category = NULL) {
switch ($op) {
case 'view':
$signups = signup_list_user_signups($user->uid);
if (count($signups)) {
$output = '<h4>' . t('Current signups') . ' -- ' . l(t('view signup schedule'), "user/{$user->uid}/signups") . '</h4>' . theme_item_list($signups);
}
if (isset($output)) {
return array(
t('Signup information') => array(
array(
'value' => $output,
'class' => 'user',
),
),
);
}
break;
case 'delete':
$uids = array();
if (is_array($edit['accounts'])) {
$uids = $edit['accounts'];
}
else {
$uids[] = $edit['uid'];
}
foreach ($uids as $uid) {
$nids = db_query("SELECT nid FROM {signup_log} WHERE uid = %d", $uid);
while ($data = db_fetch_object($nids)) {
signup_cancel_signup($uid, $data->nid);
}
}
break;
}
}
function signup_form_alter($form_id, &$form) {
switch ($form_id) {
case 'node_type_form':
signup_alter_node_type_form($form_id, $form);
break;
case $form['type']['#value'] . '_node_form':
signup_alter_node_form($form_id, $form);
break;
}
}
function signup_alter_node_type_form($form_id, &$form) {
$type = $form['old_type']['#value'];
$form['workflow']['signup_form'] = array(
'#type' => 'checkbox',
'#title' => t('Allow signups by default'),
'#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'),
)),
);
}
function signup_alter_node_form($form_id, &$form) {
global $user;
if (!empty($form['nid']['#value'])) {
$node = node_load($form['nid']['#value']);
}
else {
$node = NULL;
}
$signup_enabled = variable_get('signup_form_' . $form['type']['#value'], 0);
if (user_access('administer all signups') || !empty($node) && $signup_enabled && $node->uid == $user->uid && user_access('administer signups for own content')) {
$form['signup'] = array(
'#type' => 'fieldset',
'#title' => t('Signup settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 30,
);
$has_signups = !empty($node) && db_result(db_query("SELECT COUNT(*) from {signup_log} WHERE nid = %d", $node->nid));
$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') . ' <strong>(' . t('This can not be undone, use with extreme caution!') . ')</strong>';
}
else {
$radio_options[0] = t('Disabled');
}
if (isset($node->signup)) {
$default_option = $node->signup;
}
else {
$default_option = $signup_enabled;
}
if ($default_option == 1) {
$hint = t('If enabled, you can control whether users may sign up by visiting the !signups tab and toggling if signups are %open or %closed for this @node_type.', array(
'!signups' => !empty($node) ? l(t('Signups'), 'node/' . $node->nid . '/signups') : theme('placeholder', t('Signups')),
'%open' => t('open'),
'%closed' => t('closed'),
'@node_type' => drupal_strtolower(node_get_types('name', $form['type']['#value'])),
));
}
else {
$hint = '';
}
$form['signup']['signup_enabled'] = array(
'#type' => 'radios',
'#options' => $radio_options,
'#default_value' => $default_option,
'#description' => $hint . '<div class="js-hide">' . t('If disabled, all of the other signup settings will be ignored.') . '</div>',
'#prefix' => '<div class="signup-allow-radios">',
'#suffix' => '</div>',
);
$settings_class = "signup-node-settings";
if ($default_option != 1) {
$settings_class .= " js-hide";
}
drupal_add_js(drupal_get_path('module', 'signup') . '/signup.js');
drupal_add_css(drupal_get_path('module', 'signup') . '/signup.css');
$form['signup']['node_settings'] = array(
'#prefix' => '<div class="' . $settings_class . '">',
'#suffix' => '</div>',
);
$form['signup']['node_settings']['settings'] = _signup_admin_form($node);
}
}
function signup_form_cancel_submit($form_id, $form_values) {
signup_cancel_signup($form_values['uid'], $form_values['nid'], $form_values['signup_anon_mail']);
}
function signup_form_submit($form_id, $form_values) {
signup_sign_up_user($form_values);
}
function signup_form_validate($form_id, $form_values) {
$anon_mail = $form_values['signup_anon_mail'] ? trim($form_values['signup_anon_mail']) : '';
if ($anon_mail) {
signup_validate_anon_email($form_values['nid'], $anon_mail, 'signup_anon_mail');
}
}
function signup_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
global $form_values;
switch ($op) {
case 'insert':
if (isset($form_values['signup_enabled'])) {
if ($form_values['signup_enabled'] == 1) {
$values = array(
$node->nid,
$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'],
);
}
}
elseif (variable_get('signup_form_' . $node->type, 0)) {
$defaults = db_fetch_array(db_query("SELECT * from {signup} WHERE nid = 0"));
$values = array(
$node->nid,
$defaults['forwarding_email'],
$defaults['send_confirmation'],
$defaults['confirmation_email'],
$defaults['send_reminder'],
$defaults['reminder_days_before'],
$defaults['reminder_email'],
);
}
if (isset($values)) {
db_query("INSERT INTO {signup} (nid, forwarding_email, send_confirmation, confirmation_email, send_reminder, reminder_days_before, reminder_email) VALUES (%d, '%s', %d, '%s', %d, %d, '%s')", $values);
}
break;
case 'update':
if (isset($form_values['signup_enabled'])) {
$has_signup_record = db_result(db_query('SELECT COUNT(*) FROM {signup} WHERE nid = %d', $node->nid));
switch ($form_values['signup_enabled']) {
case 1:
if ($has_signup_record) {
db_query("UPDATE {signup} SET forwarding_email = '%s', send_confirmation = %d, confirmation_email = '%s', send_reminder = %d, reminder_days_before = %d, reminder_email = '%s' WHERE nid = %d", $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->nid);
}
else {
db_query("INSERT INTO {signup} (nid, forwarding_email, send_confirmation, confirmation_email, send_reminder, reminder_days_before, reminder_email) VALUES (%d, '%s', %d, '%s', %d, %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);
}
break;
case 2:
db_query("DELETE FROM {signup_log} WHERE nid = %d", $node->nid);
case 0:
if ($has_signup_record) {
db_query("DELETE FROM {signup} WHERE nid = %d", $node->nid);
}
break;
}
}
break;
case 'delete':
db_query("DELETE FROM {signup} WHERE nid = %d", $node->nid);
db_query("DELETE FROM {signup_log} WHERE nid = %d", $node->nid);
break;
case 'load':
$result = db_query("SELECT * FROM {signup} WHERE nid = %d", $node->nid ? $node->nid : 0);
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;
$node->signup_send_confirmation = $signup->send_confirmation;
$node->signup_confirmation_email = $signup->confirmation_email;
$node->signup_send_reminder = $signup->send_reminder;
$node->signup_reminder_days_before = $signup->reminder_days_before;
$node->signup_reminder_email = $signup->reminder_email;
$node->signup_completed = $signup->completed;
}
else {
$node->signup = 0;
}
break;
case 'view':
$suppress = module_invoke_all('signup_suppress', $node);
if ($node->signup && $page && !in_array(TRUE, $suppress)) {
global $user;
$anon_signup_form = array();
if ($node->signup_completed) {
if (user_access('sign up for content')) {
$output = '<h3>' . t('Signups closed for this event') . '</h3>';
}
}
else {
if ($user->uid == 0) {
$login_array = array(
'!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')) {
$needs_signup_form = TRUE;
$anon_signup_form['signup_anon_mail'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#description' => t('An e-mail address is required for users who are not registered at this site. If you are a registered user at this site, please !login to sign up for this event.', $login_array),
'#size' => 40,
'#maxlength' => 255,
'#required' => TRUE,
);
}
else {
$needs_signup_form = FALSE;
$signup_roles = user_roles(FALSE, 'sign up for content');
if (!empty($signup_roles[DRUPAL_AUTHENTICATED_RID])) {
if (variable_get('user_register', 1) == 0) {
$anon_login_text = t('Please !login to sign up for this event.', $login_array);
}
else {
$anon_login_text = t('Please !login or !register to sign up for this event.', $login_array);
}
$output .= '<div class="signup_anonymous_login">' . $anon_login_text . '</div>';
}
}
}
else {
$result = db_query("SELECT signup_time, form_data FROM {signup_log} WHERE uid = %d AND nid = %d", $user->uid, $node->nid);
$needs_signup_form = db_num_rows($result) == 0;
}
if ($needs_signup_form) {
if (user_access('sign up for content')) {
$output = drupal_get_form('signup_form', $node, $anon_signup_form);
}
}
elseif ($user->uid !== 0 && isset($result)) {
$result = db_fetch_object($result);
$form_data = unserialize($result->form_data);
$output .= theme('signup_custom_data_table', $form_data);
$output .= drupal_get_form('signup_form_cancel', $node);
}
}
if (user_access('view all signups')) {
$registered_signups = db_query("SELECT u.uid, u.name, s.signup_time, s.form_data FROM {signup_log} s INNER JOIN {users} u ON u.uid = s.uid WHERE s.nid = %d AND u.uid <> 0", $node->nid);
$anon_signups = db_num_rows(db_query("SELECT anon_mail FROM {signup_log} WHERE nid = %d AND uid = 0", $node->nid));
$header = array(
array(
'data' => t('!users signed up', array(
'!users' => format_plural(db_num_rows($registered_signups) + $anon_signups, '1 individual', '@count individuals'),
)),
),
);
$rows = array();
while ($signed_up_user = db_fetch_object($registered_signups)) {
$rows[] = array(
theme('username', $signed_up_user),
);
}
if ($anon_signups) {
$rows[] = array(
t('!count anonymous', array(
'!count' => $anon_signups,
)),
);
}
$output .= theme('table', $header, $rows);
}
$node->signup_view = $output;
$node->content['signup'] = array(
'#value' => $output,
'#weight' => 10,
);
}
break;
}
}
function signup_form($node, $anon_signup_form = array()) {
global $user;
$form['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
$form['uid'] = array(
'#type' => 'value',
'#value' => $user->uid,
);
$form['collapse'] = array(
'#type' => 'fieldset',
'#title' => t('Sign up for @title', array(
'@title' => $node->title,
)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$signup_themed_form = theme('signup_user_form');
if (!empty($anon_signup_form)) {
$signup_themed_form = array_merge($signup_themed_form, $anon_signup_form);
}
$form['collapse']['signup_user_form'] = $signup_themed_form;
$form['collapse']['submit'] = array(
'#type' => 'submit',
'#value' => t('Sign up'),
);
return $form;
}
function signup_form_cancel($node) {
global $user;
$form['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
$form['uid'] = array(
'#type' => 'value',
'#value' => $user->uid,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Cancel signup'),
);
return $form;
}
function signup_admin_page() {
drupal_add_css(drupal_get_path('module', 'signup') . '/signup.css');
$output = drupal_get_form('signup_filter_status_form');
$type = $_SESSION['signup_status_filter'];
$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' : '';
if ($type == 'open') {
$where = ' WHERE s.completed = 0';
}
elseif ($type == 'closed') {
$where = ' WHERE s.completed = 1';
}
else {
$where = '';
}
$header = array(
array(
'data' => t('Event'),
'field' => 'n.title',
'sort' => 'asc',
),
array(
'data' => t('Signups'),
'field' => 'count',
),
array(
'data' => t('Operations'),
),
);
if ($has_event) {
$header = array_merge(array(
array(
'data' => t('Start'),
'field' => 'e.event_start',
),
), $header);
}
$sql = "SELECT n.nid, n.title, s.completed{$event_select},\n COUNT(s_l.nid) AS count\n FROM {signup} s INNER JOIN {node} n ON n.nid = s.nid\n LEFT JOIN {signup_log} s_l ON s.nid = s_l.nid {$event_join}";
$sql .= $where;
$sql .= " GROUP BY n.nid, n.title, s.completed{$event_select}";
$sql .= tablesort_sql($header);
$sql = db_rewrite_sql($sql);
$sql_count = "SELECT COUNT(s.nid) FROM {signup} s";
$sql_count .= $where;
$sql_count = db_rewrite_sql($sql_count, 's');
$result = pager_query($sql, 25, 0, $sql_count);
while ($signup_event = db_fetch_object($result)) {
$row = array();
if ($has_event) {
require_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[] = $signup_event->event_start ? _event_date(variable_get('signup_date_string', 'D, M jS, g:i A'), $signup_event->event_start, $offset) : '';
}
$row[] = l($signup_event->title, "node/{$signup_event->nid}");
$row[] = $signup_event->count;
$op_links = l(t('View Signups'), "node/{$signup_event->nid}/signups");
$op_links .= '<br />';
if ($signup_event->completed) {
if (!arg(2)) {
$op_links .= '<em>' . t('Closed: ') . '</em>';
}
$op_links .= l(t('Open Event'), "opensignup/{$signup_event->nid}/" . arg(2));
}
else {
if (!arg(2)) {
$op_links .= '<em>' . t('Open: ') . '</em>';
}
$op_links .= l(t('Close Event'), "closesignup/{$signup_event->nid}/" . arg(2));
}
$row[] = $op_links;
$rows[] = $row;
}
$output .= theme('table', $header, $rows, array(
'style' => 'width:100%',
));
$pager = theme('pager', NULL, 25, 0);
if (!empty($pager)) {
$output .= $pager;
}
return $output;
}
function signup_filter_status_form() {
$options = array(
'all' => t('All'),
'open' => t('Open'),
'closed' => t('Closed'),
);
if (empty($_SESSION['signup_status_filter'])) {
$_SESSION['signup_status_filter'] = 'all';
}
$form['filter'] = array(
'#type' => 'select',
'#title' => t('Filter by signup status'),
'#options' => $options,
'#default_value' => $_SESSION['signup_status_filter'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
);
$form['#redirect'] = FALSE;
return $form;
}
function theme_signup_filter_status_form($form) {
return '<div class="container-inline">' . drupal_render($form) . '</div>';
}
function signup_filter_status_form_submit($form_id, $form_values) {
$_SESSION['signup_status_filter'] = $form_values['filter'];
}
function signup_cancel_signup($uid, $nid, $anon_mail = NULL) {
if ($anon_mail) {
db_query("DELETE FROM {signup_log} WHERE anon_mail = '%s' AND nid = %d", $anon_mail, $nid);
}
else {
db_query('DELETE FROM {signup_log} WHERE uid = %d AND nid = %d', $uid, $nid);
}
$node = node_load($nid);
module_invoke_all('signup_cancel', $node, $uid);
drupal_set_message(t('Signup to !title cancelled.', array(
'!title' => l($node->title, "node/{$node->nid}"),
)));
}
function signup_close_signup($nid, $cron = 'no') {
db_query("UPDATE {signup} SET completed = 1 WHERE nid = %d", $nid);
if ($cron == 'no') {
$node = node_load($nid);
foreach (module_implements('signup_close') as $module) {
$function = $module . '_signup_close';
$function($node);
}
watchdog('signup', t('Signups closed for %title.', array(
'%title' => $node->title,
)), WATCHDOG_NOTICE, l(t('view'), 'node/' . $nid));
}
}
function signup_close_signup_admin($nid, $tab = NULL) {
signup_close_signup($nid, $cron = 'no');
drupal_goto("admin/content/signup/{$tab}");
}
function signup_open_signup($nid, $cron = 'no') {
db_query("UPDATE {signup} SET completed = 0 WHERE nid = %d", $nid);
if ($cron == 'no') {
$node = node_load(array(
'nid' => $nid,
));
foreach (module_implements('signup_open') as $module) {
$function = $module . '_signup_open';
$function($node);
}
watchdog('signup', t('Signups reopened for %title.', array(
'%title' => $node->title,
)), WATCHDOG_NOTICE, l(t('view'), 'node/' . $nid));
}
}
function signup_open_signup_admin($nid, $tab = NULL) {
signup_open_signup($nid, $cron = 'no');
drupal_goto("admin/content/signup/{$tab}");
}
function signup_settings_page() {
$form['signup_close_early'] = array(
'#title' => t('Close x hours before'),
'#type' => 'textfield',
'#default_value' => variable_get('signup_close_early', 1),
'#size' => 5,
'#maxlength' => 10,
'#description' => t('The number of hours before the event which signups will no longer be allowed. Use negative numbers to close signups after the event start (example: -12).'),
);
$form['node_defaults'] = array(
'#type' => 'fieldset',
'#title' => t('Default signup information'),
'#description' => t('New signup-enabled nodes will start with these settings.'),
'#collapsible' => TRUE,
);
$form['node_defaults']['_signup_admin_form'] = _signup_admin_form(NULL);
$form['#submit']['signup_settings_page_submit'] = array();
return system_settings_form($form);
}
function signup_settings_page_submit($form_id, $form_values) {
$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' 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']);
}
else {
require_once 'signup.install';
db_query("DELETE FROM {signup} WHERE nid = 0");
signup_insert_default_signup_info();
}
$settings = array(
'signup_forwarding_email',
'signup_send_confirmation',
'signup_confirmation_email',
'signup_send_reminder',
'signup_reminder_days_before',
'signup_reminder_email',
);
foreach ($settings as $setting) {
unset($form_values[$setting]);
}
unset($form_values['signup']);
system_settings_form_submit($form_id, $form_values);
}
function signup_list_user_signups($uid) {
$titles = array();
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';
$event_col = $has_event ? ', e.event_start' : '';
$result = db_query(db_rewrite_sql("SELECT n.nid, n.title{$event_col} 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']);
}
}
return $titles;
}
function signup_sign_up_user($signup_form) {
$node = node_load($signup_form['nid']);
if (!empty($signup_form['signup_anon_mail'])) {
$signup_form['uid'] = 0;
if (db_num_rows(db_query("SELECT anon_mail FROM {signup_log} WHERE anon_mail = '%s' AND nid = %d", $signup_form['signup_anon_mail'], $node->nid))) {
drupal_set_message(t('Anonymous user %email is already signed up for %title', array(
'%email' => $signup_form['signup_anon_mail'],
'%title' => $node->title,
), 'error'));
return FALSE;
}
}
else {
$query = db_query("SELECT sl.uid, u.name FROM {signup_log} sl INNER JOIN {users} u ON sl.uid = u.uid WHERE sl.uid = %d AND sl.nid = %d", $signup_form['uid'], $signup_form['nid']);
if (db_num_rows($query)) {
$user = db_fetch_object($query);
drupal_set_message(t('User !user is already signed up for %title', array(
'!user' => theme('username', $user),
'%title' => $node->title,
)), 'error');
return FALSE;
}
}
$user = user_load(array(
'uid' => $signup_form['uid'],
));
if (user_access('sign up for content') && !$node->signup_completed) {
$curtime = time();
$extra = module_invoke_all('signup_sign_up', $node, $user);
$signup_info = array();
if (!empty($signup_form['signup_form_data'])) {
$signup_info = $signup_form['signup_form_data'];
}
if (!empty($extra)) {
$signup_info = array_merge($signup_info, $extra);
}
$signup_form_data = serialize($signup_info);
$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'),
)) : '';
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);
if (module_exists('event')) {
require_once drupal_get_path('module', 'event') . '/event_timezones.inc';
}
$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 = '';
if (isset($signup_form['signup_form_data'])) {
$signup_data = theme('signup_email_token_custom_data', $signup_form['signup_form_data']);
}
$user_mail = $signup_form['signup_anon_mail'] ? $signup_form['signup_anon_mail'] : $user->mail;
$trans = array(
"%event" => $node->title,
"%time" => $starttime,
"%username" => $user->name,
"%useremail" => $user_mail,
"%info" => $signup_data,
);
$from = variable_get('site_mail', ini_get('sendmail_from'));
if ($node->signup_send_confirmation && $user_mail) {
$subject = t('Signup confirmation for event: !event', array(
'!event' => $node->title,
));
$message = strtr($node->signup_confirmation_email, $trans);
drupal_mail('signup_confirmation_mail', $user_mail, $subject, $message, $from);
}
if ($node->signup_forwarding_email) {
$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('Username:') . $user->name;
if (!empty($user->uid)) {
$message .= "\n\r" . t('Profile page:') . url('user/' . $user->uid, NULL, NULL, TRUE);
}
else {
$message .= "\n\r" . t('E-mail:') . $user_mail;
}
$message .= "\n\r\n\r" . $signup_data;
drupal_mail('signup_forwarding_mail', $node->signup_forwarding_email, $subject, $message, $from, $header);
}
drupal_set_message(t('Signup to !title confirmed.', array(
'!title' => l($node->title, "node/{$node->nid}"),
)) . $confirmation_email . $reminder_email);
}
else {
drupal_access_denied();
}
}
function signup_user_schedule($uid) {
$output = '';
$user = user_load(array(
'uid' => $uid,
));
if (!$user) {
drupal_not_found();
return;
}
drupal_set_title(t('Signups for @user', array(
'@user' => $user->name,
)));
$titles = signup_list_user_signups($user->uid);
foreach ($titles as $nid => $title) {
$node = node_load(array(
'nid' => $nid,
));
$output .= theme('signup_user_schedule', $node);
}
return $output;
}
function signup_user_signups_form($node) {
drupal_set_title(check_plain($node->title));
$ctrl_row = array();
if ($node->signup_completed) {
$ctrl_row[] = array(
t('Signups <strong>closed</strong> for this event'),
drupal_get_form('signup_open_signups_form', $node->nid),
);
}
else {
$ctrl_row[] = array(
t('Signups <strong>open</strong> for this event'),
drupal_get_form('signup_close_signups_form', $node->nid),
);
}
$output .= '<div class="signup-admin-row">';
$output .= theme('table', NULL, $ctrl_row);
$output .= '</div><br />';
$result = db_query("SELECT u.uid, u.name, s.anon_mail, s.signup_time, s.form_data FROM {signup_log} s INNER JOIN {users} u ON u.uid = s.uid WHERE s.nid =%d", $node->nid);
$header = array(
array(
'data' => t('!users signed up', array(
'!users' => format_plural(db_num_rows($result), '1 individual', '@count individuals'),
)),
'colspan' => 3,
),
);
$rows = array();
$offset = intval(variable_get('date_default_timezone', 0));
while ($signed_up_user = db_fetch_object($result)) {
$table_data = array();
$form_data = unserialize($signed_up_user->form_data);
$signup_form_data = theme('signup_custom_data', $form_data);
if ($signed_up_user->uid == 0) {
$username = check_plain($signed_up_user->anon_mail);
$id = $signed_up_user->anon_mail;
}
else {
$username = theme('username', $signed_up_user);
$id = $signed_up_user->uid;
}
$rows[] = array(
$username . '<br />' . gmdate(variable_get('signup_date_string', 'M jS, g:i A'), $signed_up_user->signup_time + $offset),
$signup_form_data,
drupal_get_form('signup_user_cancel_form_' . $id, $id, $node->nid, $signed_up_user->uid, $signed_up_user->anon_mail),
);
}
$output .= theme('table', $header, $rows);
return $output;
}
function signup_open_signups_form($nid) {
$form['nid'] = array(
'#type' => 'value',
'#value' => $nid,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Open Signups'),
);
return $form;
}
function signup_open_signups_form_submit($form_id, $form_values) {
signup_open_signup($form_values['nid']);
}
function signup_close_signups_form($nid) {
$form['nid'] = array(
'#type' => 'value',
'#value' => $nid,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Close Signups'),
);
return $form;
}
function signup_close_signups_form_submit($form_id, $form_values) {
signup_close_signup($form_values['nid']);
}
function signup_forms() {
$args = func_get_args();
$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]) {
array_shift($args);
$forms[$form_id] = array(
'callback' => 'signup_user_cancel_form',
'callback arguments' => $args,
);
return $forms;
}
}
}
function signup_user_cancel_form($nid, $uid, $anon_mail) {
$form['#base'] = 'signup_form_cancel';
$form['nid'] = array(
'#type' => 'value',
'#value' => $nid,
);
$form['uid'] = array(
'#type' => 'value',
'#value' => $uid,
);
$form['signup_anon_mail'] = array(
'#type' => 'value',
'#value' => $anon_mail,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Cancel signup'),
);
return $form;
}
function signup_validate_anon_email($nid, $anon_mail, $name = FALSE) {
if (!valid_email_address($anon_mail)) {
$message = t('Invalid email address entered for signup.');
}
elseif (db_num_rows(db_query("SELECT mail FROM {users} WHERE mail = '%s'", $anon_mail))) {
$message = t('The email address entered belongs to a registered user.');
}
elseif (db_num_rows(db_query("SELECT anon_mail FROM {signup_log} WHERE anon_mail = '%s' AND nid = %d", $anon_mail, $nid))) {
$message = t('The email address entered has already been used to sign up for this event.');
}
if (!isset($message)) {
return TRUE;
}
if ($name) {
form_set_error($name, $message);
}
else {
drupal_set_message($message, 'error');
}
return FALSE;
}
function _signup_admin_form($node = NULL) {
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;
$node->signup_confirmation_email = $result->confirmation_email;
$node->signup_send_reminder = $result->send_reminder;
$node->signup_reminder_days_before = $result->reminder_days_before;
$node->signup_reminder_email = $result->reminder_email;
}
$signup_token_description = t('Supported string substitutions: %event, %time, %username, %useremail, %info (user signup information).');
$form['signup_forwarding_email'] = array(
'#type' => 'textfield',
'#title' => t('Send signups to'),
'#default_value' => $node->signup_forwarding_email,
'#size' => 40,
'#maxlength' => 64,
'#description' => t('Email address where notification of new signups will be sent. Leave blank for no notifications.'),
);
$form['signup_send_confirmation'] = array(
'#type' => 'checkbox',
'#title' => t('Send confirmation'),
'#default_value' => $node->signup_send_confirmation,
);
$form['signup_confirmation_email'] = array(
'#type' => 'textarea',
'#title' => t('Confirmation email'),
'#default_value' => $node->signup_confirmation_email,
'#cols' => 40,
'#rows' => 6,
'#description' => t('Email sent to user upon signup.') . ' ' . $signup_token_description,
);
$form['signup_reminder'] = array(
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
);
$form['signup_reminder']['signup_send_reminder'] = array(
'#type' => 'checkbox',
'#title' => t('Send reminder'),
'#default_value' => $node->signup_send_reminder,
);
$options = array();
for ($i = 1; $i <= 60; $i++) {
$options[$i] = $i;
}
$form['signup_reminder']['signup_reminder_days_before'] = array(
'#type' => 'select',
'#default_value' => $node->signup_reminder_days_before,
'#options' => $options,
'#suffix' => t('day(s) before event'),
);
$form['signup_reminder_email'] = array(
'#type' => 'textarea',
'#title' => t('Reminder email'),
'#default_value' => $node->signup_reminder_email,
'#cols' => 40,
'#rows' => 6,
'#description' => t('Email sent to user as an event reminder.') . ' ' . $signup_token_description,
);
$form['signup'] = array(
'#type' => 'hidden',
'#value' => 1,
);
return $form;
}
function signup_build_signup_data($data, $type = 'output') {
switch ($type) {
case 'email':
return theme('signup_custom_data_email', $data);
case 'table':
return theme('signup_custom_data_rows', $data);
default:
return theme('signup_custom_data', $data);
}
}
function theme_signup_email_token_custom_data($signup_data) {
return t('SIGNUP INFORMATION') . "\n\r" . theme('signup_custom_data_email', $signup_data);
}
function theme_signup_custom_data_email($data) {
$output = '';
foreach ($data as $key => $value) {
if (is_array($value)) {
$output .= "\n\r" . call_user_func(__FUNCTION__, $value) . "\n\r";
}
else {
$output .= $key . ': ' . $value . "\n\r";
}
}
return $output;
}
function theme_signup_custom_data_table($data) {
$output = '';
if (is_array($data)) {
$header = array(
array(
'data' => t('Your signup information'),
'colspan' => 2,
),
);
$rows = theme('signup_custom_data_rows', $data);
$output .= theme('table', $header, $rows);
}
return $output;
}
function theme_signup_custom_data_rows($data) {
$rows = array();
foreach ($data as $key => $value) {
if (is_array($value)) {
$rows += call_user_func(__FUNCTION__, $value);
}
else {
$rows[] = array(
$key . ':',
check_plain($value),
);
}
}
return $rows;
}
function theme_signup_custom_data($data) {
$output = '';
foreach ($data as $key => $value) {
$output .= '<div id="' . signup_id_safe($key) . '">';
if (is_array($value)) {
$output .= call_user_func(__FUNCTION__, $value);
}
else {
$output .= $key . ': ' . check_plain($value);
}
$output .= "</div>\n";
}
return $output;
}
function signup_id_safe($string) {
$string = drupal_strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
if (!ctype_lower($string[0])) {
$string = 'id' . $string;
}
return $string;
}