Index: node_access_example/node_access_example.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/examples/node_access_example/node_access_example.module,v
retrieving revision 1.1
diff -u -p -r1.1 node_access_example.module
--- node_access_example/node_access_example.module	3 Oct 2009 14:35:27 -0000	1.1
+++ node_access_example/node_access_example.module	31 Jul 2010 21:08:34 -0000
@@ -15,6 +15,14 @@
  * and delete the node by providing an additional access realm that grants
  * privileges to the node's author.
  *
+ * Database definition:
+ * @code
+ *   CREATE TABLE node_access_example (
+ *     nid int(10) unsigned NOT NULL default '0' PRIMARY KEY,
+ *     private int,
+ *     KEY `node_example_nid` (nid)
+ *   )
+ * @endcode
  */
 
 /**
@@ -24,16 +32,7 @@
  * has access to "private" content. This permission is defined here.
  */
 function node_access_example_perm() {
-  return array(
-    'access private content' => array(
-      'title' => t('Access private content'),
-      'description' => t('May view posts that are marked private.'),
-    ),
-    'edit private content' => array(
-      'title' => t('Edit private content'),
-      'description' => t('May edit posts that are marked private.'),
-    ),
-  );
+  return array('access private content', 'edit private content');
 }
 
 /**
@@ -74,14 +73,14 @@ function node_access_example_node_grants
 function node_access_example_node_access_records($node) {
   // We only care about the node if it's been marked private. If not, it is
   // treated just like any other node and we completely ignore it.
-  if (!empty($node->private)) {
+  if ($node->private) {
     $grants = array();
     $grants[] = array(
       'realm' => 'example',
-      'gid' => 1,
-      'grant_view' => 1,
-      'grant_update' => 0,
-      'grant_delete' => 0,
+      'gid' => TRUE,
+      'grant_view' => TRUE,
+      'grant_update' => FALSE,
+      'grant_delete' => FALSE,
       'priority' => 0,
     );
 
@@ -90,9 +89,9 @@ function node_access_example_node_access
     $grants[] = array(
       'realm' => 'example_author',
       'gid' => $node->uid,
-      'grant_view' => 1,
-      'grant_update' => 1,
-      'grant_delete' => 1,
+      'grant_view' => TRUE,
+      'grant_update' => TRUE,
+      'grant_delete' => TRUE,
       'priority' => 0,
     );
     return $grants;
@@ -119,38 +118,24 @@ function node_access_example_form_alter(
 
 /**
  * Implementation of hook_nodeapi().
- */
-
-function node_access_example_nodeapi_load(&$node) {
-  $node->private = db_result(db_query('SELECT private FROM {node_access_example} WHERE nid = :nid', array(':nid' => $node->nid)));
-}
-
-/**
- * Implementation of hook_nodeapi_delete().
- *
- * The module must track the access status of the node.
- */
-
-function node_access_example_nodeapi_delete(&$node) {
-  db_delete('node_access_example')->condition('nid', $node->nid)->execute();
-}
-
-/**
- * Implementation of hook_nodeapi_insert().
  *
+ * - "delete", "insert", and "update":
  * The module must track the access status of the node.
  */
-function node_access_example_nodeapi_insert(&$node) {
-  if (isset($node->private)) {
-    db_insert('node_access_example')->fields(array('nid' => $node->nid, 'private' => (int)$node->private))->execute();
+function node_access_example_nodeapi(&$node, $op, $arg = 0) {
+  switch ($op) {
+    case 'load':
+      $result = db_fetch_object(db_query('SELECT * FROM {node_access_example} WHERE nid = %d', $node->nid));
+      $node->private = $result->private;
+      break;
+    case 'delete':
+      db_query('DELETE FROM {node_access_example} WHERE nid = %d', $node->nid);
+      break;
+    case 'insert':
+      db_query('INSERT INTO {node_access_example} (nid, private) VALUES (%d, %d)', $node->nid, $node->private);
+      break;
+    case 'update':
+      db_query('UPDATE {node_access_example} SET private = %d WHERE nid = %d', $node->private, $node->nid);
+      break;
   }
 }
-
-/**
- * Implementation of hook_nodeapi_update().
- *
- * The module must track the access status of the node.
- */
-function node_access_example_nodeapi_update(&$node) {
-  db_query('UPDATE {node_access_example} SET private = %d WHERE nid = %d', $node->private, $node->nid);
-}
