Index: content_access.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/content_access/content_access.module,v
retrieving revision 1.6
diff -u -r1.6 content_access.module
--- content_access.module	20 Aug 2007 09:39:08 -0000	1.6
+++ content_access.module	12 Jan 2008 01:47:17 -0000
@@ -62,41 +62,79 @@
  * Per node settings page
  */
 function content_access_page($nid) {
-  $roles = content_access_get_roles_and_author();
   $node = node_load($nid);
   drupal_set_title(check_plain($node->title));
 
+  foreach (content_access_get_ops() as $i) {
+    $defaults[$i] = content_access_per_node_setting($i, $node);
+  }
+  
+  $form = content_access_page_form($defaults, $node);
+
+  $form['node'] = array('#type' => 'value', '#value' => $node);
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Submit'),
+    '#weight' => 10,
+  );
+  return $form;
+}
+
+/**
+ * Builds per node setting page form without requiring a node. Used by
+ * content_access_action_set_node_permissions_form().
+ * 
+ * @param $defaults
+ *   Array of defaults for view/update/delete checkboxes.
+ * @param $node
+ *   Optional node for ACL.
+ */
+function content_access_page_form($defaults = array(), $node = false) {
+  
+  // Make sure defaults array is full
+  foreach (content_access_get_ops() as $op) {
+    if (!isset($defaults[$op])) $defaults[$op] = array();
+  }
+  
+  $roles = content_access_get_roles_and_author();
   $form['settings'] = array(
     '#type' => 'fieldset', 
     '#title' => t('Role access control settings'),
     '#collapsible' => TRUE,
   );
+  
+  if (!$node) {
+    $form['settings']['#description'] = t('Warning: No defaults are set; be sure to fill out all boxes appropriately.');
+  }
+  
   drupal_add_css(drupal_get_path('module', 'content_access') . '/content_access.css');
   $form['settings']['view'] = array('#type' => 'checkboxes',
     '#prefix' => '<div class="content_access-div">',
     '#suffix' => '</div>',
     '#options' => $roles,
     '#title' => t('View'),
-    '#default_value' => content_access_per_node_setting('view', $node),
+    '#default_value' => $defaults['view'],
   );
   $form['settings']['update'] = array('#type' => 'checkboxes',
     '#prefix' => '<div class="content_access-div">',
     '#suffix' => '</div>',
     '#options' => $roles,
     '#title' => t('Edit'),
-    '#default_value' => content_access_per_node_setting('update', $node),
+    '#default_value' => $defaults['update'],
   );
   $form['settings']['delete'] = array('#type' => 'checkboxes',
     '#prefix' => '<div class="content_access-div">',
     '#suffix' => '</div>',
     '#options' => $roles,
     '#title' => t('Delete'),
-    '#default_value' => content_access_per_node_setting('delete', $node),
+    '#default_value' => $defaults['delete'],
   );
   $form['settings']['clearer'] = array(
     '#value' => '<br clear="all" />',
   );
-  if (module_exists('acl')) {
+  if (module_exists('acl') && $node) {
+    // This is disabled when there is no node passed; we could probably
+    // make this compatible in that case too though.
     $form['acl'] = array(
       '#type' => 'fieldset', 
       '#title' => t('User access control lists'),
@@ -104,7 +142,7 @@
       '#collapsible' => TRUE,
       '#tree' => TRUE,
     );
-    foreach (array('view', 'update', 'delete') as $op) {
+    foreach (content_access_get_ops() as $op) {
       $acl_id = acl_get_id_by_name('content_access', $op .'_'. $node->nid);
       if (!$acl_id) { // create one
         $acl_id = acl_create_new_acl('content_access', $op .'_'. $node->nid);
@@ -114,32 +152,25 @@
       $form['acl'][$op]['#collapsed'] = !isset($_POST['acl'][$op]['add_button']) && !isset($_POST['acl'][$op]['delete_button']); 
     }
   }
-
-  $form['node'] = array('#type' => 'value', '#value' => $node);
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Submit'),
-    '#weight' => 10,
-  );
   return $form;
 }
 
 function content_access_page_submit($form_id, $form_values) {
   $node = $form_values['node'];
   $settings = array();
-  foreach (array('view', 'update', 'delete') as $op) {
-    //set the settings so that further calls will return this settings
-    unset($form_values[$op][0]);
-    $settings[$op] = array_filter($form_values[$op]);
-
+  content_access_parse_settings(content_access_get_ops(), $settings, $form_values);
+  
+  // Save ACL settings.
+  foreach (content_access_get_ops() as $op) {
     if (module_exists('acl') && isset($form_values['acl'][$op])) {
       acl_save_form($form_values['acl'][$op]);
     }
   }
-  //save new settings for later..
+  
+  // Save per-node settings.
   content_access_save_per_node_settings($node, $settings);
 
-  //apply new settings
+  // Apply new settings.
   node_access_acquire_grants($node);
   drupal_set_message('Your changes have been saved.');
 }
@@ -217,17 +248,40 @@
   return $form;
 }
 
-function content_access_admin_settings_submit($form_id, $form_values) {
-  $per_node_old = content_access_get_settings('per_node', $form_values['type']);
-  $settings = content_access_get_settings();
-
-  foreach (content_access_available_settings() as $setting) {
+/**
+ * Parse submitted settings from form into internal format.
+ * 
+ * @note
+ *   This automatically detects whether or not per-node or node type
+ *   configuration is being set.
+ * 
+ * @param $setting_set
+ *   Settings to use, usually either content_access_available_settings() or
+ *   content_access_get_ops().
+ * @param $settings
+ *   Settings array variable to save values to.
+ * @param $form_values
+ *   Form values array variable, modified settings are also reflected here.
+ */
+function content_access_parse_settings($setting_set, &$settings, &$form_values) {
+  foreach ($setting_set as $setting) {
     if (is_array($form_values[$setting])) {
       unset($form_values[$setting][0]);
       $form_values[$setting] = array_filter($form_values[$setting]);
     }
-    $settings[$setting][$form_values['type']] = $form_values[$setting];
+    if (isset($form_values['type'])) {
+      $settings[$setting][$form_values['type']] = $form_values[$setting];
+    } else {
+      $settings[$setting] = $form_values[$setting];
+    }
   }
+}
+
+function content_access_admin_settings_submit($form_id, $form_values) {
+  $per_node_old = content_access_get_settings('per_node', $form_values['type']);
+  
+  $settings = content_access_get_settings();
+  content_access_parse_settings(content_access_available_settings(), $settings, $form_values);
   content_access_set_settings($settings);
 
   // mass update all nodes that use default settings
@@ -257,7 +311,7 @@
   //apply per node settings if necessary
   if (content_access_get_settings('per_node', $node->type)) {
     $grants = array();
-    foreach (array('view', 'update', 'delete') as $op) {
+    foreach (content_access_get_ops() as $op) {
       foreach (content_access_per_node_setting($op, $node) as $rid) {
         $grants[$rid]['grant_'. $op] = 1;
       }
@@ -330,7 +384,7 @@
 
 /*
  * Returns the content_access' settings
- * @param $return One of the content_access_available_settings(), e.g. 'view' or 'pernode'
+ * @param $return One of the content_access_available_settings(), e.g. 'view' or 'per_node'
  * @param $type If not all, return the setting for the specified type
  */
 function content_access_get_settings($return = 'all', $type = NULL) {
@@ -366,12 +420,19 @@
 }
 
 /*
- * returns an array containing all available content_access settings
+ * Return an array containing all available content_access settings
  */
 function content_access_available_settings() {
   return array('view', 'update', 'delete', 'per_node', 'priority');
 }
 
+/**
+ * Return array of containing valid per node content_access settings.
+ */
+function content_access_get_ops() {
+  return array('view', 'update', 'delete');
+}
+
 /*
  * Defines default values for settings
  */
@@ -426,7 +487,7 @@
     $grants = array();  //apply the defaults
     $roles = content_access_get_roles_and_author();
 
-    foreach (array('view', 'update', 'delete') as $op) {
+    foreach (content_access_get_ops() as $op) {
       foreach (content_access_get_settings($op, $node->type) as $rid) {
         $grants[$rid]['grant_'. $op] = 1;
       }
@@ -512,11 +573,11 @@
  */
 function content_access_optimize_grants(&$grants, $node) {
   //populate $view, $update and $delete with roles, that have access
-  foreach (array('view', 'update', 'delete') as $op) {
+  foreach (content_access_get_ops() as $op) {
     $$op = array(); 
   }
   foreach ($grants as $key => $grant) {
-    foreach (array('view', 'update', 'delete') as $op) {
+    foreach (content_access_get_ops() as $op) {
       if ($grant['grant_'. $op]) {
         ${$op}[] = $key; 
       }
@@ -542,7 +603,7 @@
   }
   else {
     foreach ($grants as $key => $grant) {
-      foreach (array('view', 'update', 'delete') as $op) {
+      foreach (content_access_get_ops() as $op) {
         if ($grant['grant_'. $op] && in_array($key, $$op)) {
           //it's still here, so we can't remove this grant
           continue 2;
@@ -579,3 +640,8 @@
       break;
   }
 }
+
+if (module_exists('workflow_ng')) {
+  include_once dirname(__FILE__) . '/content_access.workflow_ng.inc';
+}
+
