Index: signup.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signup/signup.module,v
retrieving revision 1.120
diff -u -r1.120 signup.module
--- signup.module 16 Oct 2007 19:39:30 -0000 1.120
+++ signup.module 29 Oct 2007 23:22:48 -0000
@@ -302,6 +302,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_perm().
* @ingroup signup_core
*/
@@ -344,6 +374,19 @@
'#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('Signup 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'))),
+ );
}
/**
@@ -351,7 +394,6 @@
*/
function signup_alter_node_form($form_id, &$form) {
global $user;
-
// Load the node if it already exists.
if (isset($form['nid']['#value'])) {
$node = node_load($form['nid']['#value']);
@@ -429,6 +471,12 @@
'#prefix' => '
',
'#suffix' => '
',
);
+
+ // Set node type (needed to set default role-specific signup access).
+ if (!$node || !$node->type)
+ $node->type = $form['type']['#value'];
+
+ // Include the signup options to the form.
$form['signup']['node_settings']['settings'] = _signup_admin_form($node);
}
}
@@ -481,6 +529,8 @@
*/
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'])) {
@@ -494,6 +544,7 @@
$form_values['signup_reminder_days_before'],
$form_values['signup_reminder_email'],
$form_values['signup_close_signup_limit'],
+ $form_values['signup_access']
);
}
}
@@ -513,10 +564,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;
@@ -531,7 +583,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,
@@ -539,18 +591,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)", $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) {
@@ -603,6 +657,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));
@@ -625,7 +680,7 @@
// 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')) {
+ if (_signup_access($node)) {
$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.
@@ -647,7 +702,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',
@@ -672,7 +727,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);
}
}
@@ -1058,7 +1113,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(NULL);
$form['adv_settings'] = array(
'#type' => 'fieldset',
@@ -1088,15 +1143,17 @@
*/
function signup_settings_page_submit($form_id, $form_values) {
$op = isset($form_values['op']) ? $form_values['op'] : '';
+ $form_values['signup_access'] = implode(',', array_keys(array_filter($form_values['signup_access'])));
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 {
@@ -1115,6 +1172,7 @@
'signup_reminder_days_before',
'signup_reminder_email',
'signup_close_signup_limit',
+ 'signup_access'
);
foreach ($settings as $setting) {
unset($form_values[$setting]);
@@ -1196,7 +1254,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();
@@ -1551,6 +1609,8 @@
$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(
@@ -1607,6 +1667,13 @@
'#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;
@@ -1777,6 +1844,18 @@
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;
+}
+
/**
* @defgroup signup_views Views-integration hooks
*/