| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file signup_restrict_by_role
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_form_alter
|
| 10 |
*/
|
| 11 |
function signup_restrict_by_role_form_alter(&$form, &$form_state, $form_id) {
|
| 12 |
if($form_id == 'event_node_form') {
|
| 13 |
// Retrieve the list of roles from the database
|
| 14 |
$roles = user_roles();
|
| 15 |
|
| 16 |
$nid = $form['nid']['#value'];
|
| 17 |
$default_options = array();
|
| 18 |
|
| 19 |
if ($nid) {
|
| 20 |
$result = db_query("SELECT rid FROM {signup_restrict_by_role} WHERE nid = %d", $nid);
|
| 21 |
while ($row = db_fetch_array($result)) {
|
| 22 |
array_push($default_options, $row['rid']);
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
foreach ($roles as $key => $value) {
|
| 27 |
$roles_options[$key] = $value;
|
| 28 |
}
|
| 29 |
|
| 30 |
// Add the form element to toggle if signups are allowed.
|
| 31 |
$form['signup']['signup_restrict'] = array(
|
| 32 |
'#type' => 'fieldset',
|
| 33 |
'#title' => t('Restrict access to certain Roles'),
|
| 34 |
'#collapsible' => TRUE,
|
| 35 |
'#collapsed' => FALSE,
|
| 36 |
'#tree' => TRUE,
|
| 37 |
);
|
| 38 |
|
| 39 |
$form['signup']['signup_restrict']['options'] = array(
|
| 40 |
'#type' => 'checkboxes',
|
| 41 |
'#options' => $roles_options,
|
| 42 |
'#default_value' => $default_options,
|
| 43 |
);
|
| 44 |
}
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Implementation of hook_nodeapi
|
| 49 |
*/
|
| 50 |
function signup_restrict_by_role_nodeapi(&$node, $op, $teaser=NULL, $page=NULL) {
|
| 51 |
global $user;
|
| 52 |
|
| 53 |
if ($node->type == 'event' && $node->signup) {
|
| 54 |
switch($op) {
|
| 55 |
case 'load':
|
| 56 |
// Check if signups should be enabled for the current user
|
| 57 |
$matches = implode(', ', array_keys($user->roles));
|
| 58 |
$result = db_result(db_query("SELECT COUNT(rid) FROM {signup_restrict_by_role} WHERE nid = %d AND rid IN (%s)", $node->nid, $matches));
|
| 59 |
if ($result == 0) {
|
| 60 |
// Hrm. Are there any settings for this node? If not, default to signups enabled
|
| 61 |
$result = db_result(db_query("SELECT COUNT(rid) FROM {signup_restrict_by_role} WHERE nid = %d", $node->nid));
|
| 62 |
if($result > 0) {
|
| 63 |
// This is a bit hacky, is there a better way to tell we're being loaded for editing?
|
| 64 |
if (arg(2) != 'edit') {
|
| 65 |
$node->signup = FALSE;
|
| 66 |
}
|
| 67 |
}
|
| 68 |
}
|
| 69 |
break;
|
| 70 |
|
| 71 |
case 'insert':
|
| 72 |
case 'update':
|
| 73 |
db_query("DELETE FROM {signup_restrict_by_role} WHERE nid = %d", $node->nid);
|
| 74 |
foreach ($node->signup_restrict[options] as $key => $value) {
|
| 75 |
if ($value) {
|
| 76 |
db_query("INSERT INTO {signup_restrict_by_role} (nid, rid) VALUES (%d, %d)", $node->nid, $key);
|
| 77 |
}
|
| 78 |
}
|
| 79 |
break;
|
| 80 |
}
|
| 81 |
}
|
| 82 |
}
|