You are here

function private_nodeapi in Private 5

Same name and namespace in other branches
  1. 6 private.module \private_nodeapi()

Implementation of hook_nodeapi().

  • "delete", "insert", and "update":

The module must track the access status of the node.

File

./private.module, line 171
This is an example illustrating how to restrict access to nodes based on some criterion associated with the user.

Code

function private_nodeapi(&$node, $op, $arg = 0) {
  switch ($op) {
    case 'load':
      $result = db_fetch_object(db_query('SELECT * FROM {private} WHERE nid = %d', $node->nid));
      $node->private = $result->private;
      break;
    case 'delete':
      db_query('DELETE FROM {private} WHERE nid = %d', $node->nid);
      break;
    case 'insert':
      db_query('INSERT INTO {private} (nid, private) VALUES (%d, %d)', $node->nid, $node->private);
      break;
    case 'update':
      $already_exists = db_result(db_query('SELECT 1 FROM {private} WHERE nid = %d', $node->nid));
      if ($already_exists) {
        db_query('UPDATE {private} SET private = %d WHERE nid = %d', $node->private, $node->nid);
      }
      elseif ($node->private) {
        db_query('INSERT INTO {private} (nid, private) VALUES (%d, %d)', $node->nid, $node->private);
      }
      break;
  }
}