Index: restricted_content.module =================================================================== --- restricted_content.module (revision 2571) +++ restricted_content.module (working copy) @@ -27,20 +27,46 @@ * Implementation of hook_form_alter(). */ function restricted_content_form_alter(&$form, $form_state, $form_id) { + if ($form_id == 'node_type_form') { - //restricted_content_node_form($form); + restricted_content_node_type_form($form); } elseif ($form['#id'] == 'node-form') { $default = unserialize(db_result(db_query("SELECT rids FROM {restricted_content} WHERE nid = %d", $form['nid']['#value']))); + + # Check if we've got any existing values set. + # If we don't, look up the defaults for the type. + if(!is_array($default) || count($default) == 0) { + $type = $form['type']['#value']; + $default = unserialize(db_result(db_query("SELECT rids FROM {restricted_content_defaults} WHERE type = '%s'", $form['type']['#value']))); + } + restricted_content_node_form($form, $default); - $form['#submit'][] = 'restricted_content_node_form_submit'; } } +function restricted_content_get_max_weight($form) { + # Find the max weight so we add our element at the bottom. + $maxweight = -999; + foreach($form as $element) { + if(!is_array($element)) { + continue; + } + if(isset($element['#weight']) && $element['#weight'] > $maxweight) { + $maxweight = $element['#weight']; + } + } + return $maxweight; +} + /** * Option elements to add to node forms. */ function restricted_content_node_form(&$form, $default) { + + # Find the max weight so we add our element at the bottom. + $maxweight = restricted_content_get_max_weight($form); + $form['restricted_content'] = array( '#type' => 'fieldset', '#title' => t('Restricted Access'), @@ -48,6 +74,7 @@ '#collapsed' => TRUE, '#tree' => TRUE, '#access' => restricted_content_form_access($form['uid']['#value']), + '#weight' => $maxweight + 1, ); $form['restricted_content']['rids'] = array( '#type' => 'checkboxes', @@ -59,6 +86,31 @@ $form['#submit'][] = 'restricted_content_node_form_submit'; } +function restricted_content_node_type_form(&$form) { + + # Find the max weight so we add our element at the bottom. + $maxweight = restricted_content_get_max_weight($form); + + $form['restricted_content'] = array( + '#type' => 'fieldset', + '#title' => t('Restricted Access'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#tree' => TRUE, + '#access' => restricted_content_form_access($form['uid']['#value']), + '#description' => 'Set the defaults for this content type. These can be overridden on the individual nodes.', + '#weight' => $maxweight + 1, + ); + $form['restricted_content']['rids'] = array( + '#type' => 'checkboxes', + '#title' => t('Restrict access to users with the following user roles'), + '#description' => t('If no roles are selected, the node will be viewable by all users.'), + '#options' => user_roles(), + '#default_value' => is_array($default) ? $default : array(), + ); + $form['#submit'][] = 'restricted_content_node_form_type_submit'; +} + function restricted_content_node_form_submit($form, $form_state) { $nid = $form_state['values']['nid']; $rids = array_keys(array_filter($form_state['values']['restricted_content']['rids'])); @@ -68,6 +120,15 @@ } } +function restricted_content_node_form_type_submit($form, $form_state) { + $type = $form_state['values']['type']; + $rids = array_keys(array_filter($form_state['values']['restricted_content']['rids'])); + db_query("DELETE FROM {restricted_content_defaults} WHERE type = '%s'", $type); + if ($rids) { + db_query("INSERT INTO {restricted_content_defaults} VALUES ('%s', '%s')", $type, serialize($rids)); + } +} + function restricted_content_form_access($uid) { global $user; return user_access('restrict content access') || ($uid == $user->uid && user_access('restrict own content access')); @@ -187,6 +248,9 @@ if (!isset($defaults[$name])) { watchdog('restricted_conte', 'Default variable for %variable not found.', array('%variable' => $name), WATCHDOG_WARNING); } - + return variable_get($name, isset($defaults[$name]) ? $defaults[$name] : NULL); } + +# variable_set('restricted_content_message',''); +# variable_set('restricted_content_message_anon', "This content is only available to registered users. Please sign up for an account if you'd like to see it."); Index: restricted_content.install =================================================================== --- restricted_content.install (revision 2571) +++ restricted_content.install (working copy) @@ -6,6 +6,7 @@ */ function restricted_content_install() { drupal_install_schema('restricted_content'); + drupal_install_schema('restricted_content_defaults'); } /** @@ -13,6 +14,7 @@ */ function restricted_content_uninstall() { drupal_uninstall_schema('restricted_content'); + drupal_uninstall_schema('restricted_content_defaults'); drupal_load('module', 'restricted_content'); $variables = restricted_content_variables(); @@ -41,5 +43,23 @@ ), 'primary key' => array('nid'), ); + + $schema['restricted_content_defaults'] = array( + 'fields' => array( + 'type' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + + ), + 'rids' => array( + 'type' => 'text', + 'size' => 'medium', + 'not null' => FALSE, + ), + ), + 'primary key' => array('type') #, 32), + ); return $schema; }