type.'_node_form' == $form_id) { // Retrieve the list of roles from the database $roles = user_roles(); $nid = $form['nid']['#value']; $default_options = array(); if ($nid) { $result = db_query("SELECT rid FROM {signup_restrict_by_role} WHERE nid = %d", $nid); while ($row = db_fetch_array($result)) { array_push($default_options, $row['rid']); } } foreach ($roles as $key => $value) { $roles_options[$key] = $value; } // Add the form element to toggle if signups are allowed. $form['signup']['signup_restrict'] = array( '#type' => 'fieldset', '#title' => t('Restrict access to certain Roles'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, ); $form['signup']['signup_restrict']['options'] = array( '#type' => 'checkboxes', '#options' => $roles_options, '#default_value' => $default_options, ); } } /** * Implementation of hook_nodeapi */ function signup_restrict_by_role_nodeapi(&$node, $op, $teaser=NULL, $page=NULL) { global $user; switch($op) { case 'load': // Check if signups should be enabled for the current user $matches = implode(', ', array_keys($user->roles)); $result = db_result(db_query("SELECT COUNT(rid) FROM {signup_restrict_by_role} WHERE nid = %d AND rid IN (%s)", $node->nid, $matches)); if ($result == 0) { // Hrm. Are there any settings for this node? If not, default to signups enabled $result = db_result(db_query("SELECT COUNT(rid) FROM {signup_restrict_by_role} WHERE nid = %d", $node->nid)); if($result > 0) { // This is a bit hacky, is there a better way to tell we're being loaded for editing? if (arg(2) != 'edit' && $user->uid != 1) { $node->signup = FALSE; } } } break; case 'insert': case 'update': db_query("DELETE FROM {signup_restrict_by_role} WHERE nid = %d", $node->nid); if(!empty($node->signup_restrict['options'])) { foreach ($node->signup_restrict['options'] as $rid) { if ($rid) { db_query("INSERT INTO {signup_restrict_by_role} (nid, rid) VALUES (%d, %d)", $node->nid, $rid); } } } break; } }