Index: content_access.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/content_access/Attic/content_access.module,v
retrieving revision 1.1.2.7
diff -u -p -r1.1.2.7 content_access.module
--- content_access.module	15 Jan 2008 10:57:18 -0000	1.1.2.7
+++ content_access.module	31 Mar 2008 12:50:28 -0000
@@ -136,7 +136,7 @@ function content_access_page_form($defau
   $form['settings']['clearer'] = array(
     '#value' => '<br clear="all" />',
   );
-  if (module_exists('acl') && $node) {
+  if (module_exists('acl')) {
     // This is disabled when there is no node passed.
     $form['acl'] = array(
       '#type' => 'fieldset', 
@@ -146,13 +146,19 @@ function content_access_page_form($defau
       '#tree' => TRUE,
     );
     foreach (array('view', 'update', 'delete') 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);
-        acl_node_add_acl($node->nid, $acl_id, $op == 'view', $op == 'update', $op == 'delete');
-      }
-      $form['acl'][$op] = acl_edit_form($acl_id, 'Grant '. $op .' access');
+      if (isset($node->nid)){
+        $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);
+          acl_node_add_acl($node->nid, $acl_id, $op == 'view', $op == 'update', $op == 'delete');
+        }
+          $form['acl'][$op] = acl_edit_form($acl_id, 'Grant '. $op .' access');
+        }
+        else{
+          // new node don't have acl_id
+          $form['acl'][$op] = acl_edit_form('content_access_new_node', 'Grant '. $op .' access', TRUE);   
+        }
       $form['acl'][$op]['#collapsed'] = !isset($_POST['acl'][$op]['add_button']) && !isset($_POST['acl'][$op]['delete_button']); 
     }
   }
@@ -161,14 +167,38 @@ function content_access_page_form($defau
 
 function content_access_page_submit($form_id, $form_values) {
   $node = $form_values['node'];
+  content_access_update($node,$form_values);
+}
+
+/*
+ *  Save settings
+ */
+function content_access_update($node, $form_values=NULL){
   $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]);
-
-    if (module_exists('acl') && isset($form_values['acl'][$op])) {
-      acl_save_form($form_values['acl'][$op]);
+    if ($form_values){
+      //Content Access page form
+      unset($form_values[$op][0]);
+      $settings[$op] = array_filter($form_values[$op]);
+  
+      if (module_exists('acl') && isset($form_values['acl'][$op])) {
+        acl_save_form($form_values['acl'][$op]);
+      }
+    }
+    else{
+      //Node edit form
+      $settings[$op] = array_filter($node->$op);
+      if (module_exists('acl') && isset($node->acl)) {
+        $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);
+          acl_node_add_acl($node->nid, $acl_id, $op == 'view', $op == 'update', $op == 'delete');
+        }
+        $acl= $node->acl[$op];
+        $acl['acl_id'] = $acl_id;
+        acl_save_form($acl);
+      }
     }
   }
   
@@ -323,8 +353,16 @@ function content_access_node_access_reco
  * Implementation of hook_nodeapi().
  */
 function content_access_nodeapi($node, $op, $teaser, $page) {
-  if ($op == 'delete') {
-    db_query("DELETE FROM {content_access} WHERE nid = %d", $node->nid);
+  switch($op){
+    case 'delete':
+      db_query("DELETE FROM {content_access} WHERE nid = %d", $node->nid);
+      break;
+    case 'insert':
+    case 'update':
+      if (content_access_get_settings('per_node', $node->type)) {
+        content_access_update($node);
+      }
+      break;
   }
 }
 
@@ -620,3 +658,18 @@ function content_access_node_type($op, $
       break;
   }
 }
+
+/**
+ * Implementation of hook_form_alter():
+ * Insert Content Access and ACL fieldsets
+ */
+function content_access_form_alter($form_id, &$form){
+  if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
+    if (content_access_get_settings('per_node', $form['#node']->type)) {
+      foreach (array('view', 'update', 'delete') as $i) {
+        $defaults[$i] = content_access_per_node_setting($i, $form['#node']);
+      }
+      $form['content_access'] = content_access_page_form($defaults, $form['#node']);  
+    }
+  }
+}
