You are here

function simple_access_nodeapi in Simple Access 5.2

Same name and namespace in other branches
  1. 5 simple_access.module \simple_access_nodeapi()
  2. 6.2 simple_access.module \simple_access_nodeapi()

Implementation of hook_nodeapi()

File

./simple_access.module, line 171
This module allows administrators to make nodes viewable by specific 'access groups'. Each access group can contain any number of roles. If a node is not assigned to any access groups, it will remain viewable by all users.

Code

function simple_access_nodeapi(&$node, $op) {
  switch ($op) {
    case 'prepare':
      if (empty($node->simple_access_owner)) {
        $node->simple_access_owner = array(
          'sa_view' => 0,
          'sa_update' => 0,
          'sa_delete' => 0,
        );
      }
      if (empty($node->simple_access)) {
        $node->simple_access = array();
      }
      if (empty($node->simple_access_profiles)) {
        $node->simple_access_profiles = array();
      }
      break;
    case 'load':
      if ($row = db_fetch_array(db_query('SELECT no.sa_view, no.sa_update, no.sa_delete FROM {simple_access_owner} no WHERE no.nid = %d', $node->nid))) {
        $node->simple_access_owner = $row;
      }
      else {
        $node->simple_access_owner = array(
          'sa_view' => 0,
          'sa_update' => 0,
          'sa_delete' => 0,
        );
      }
      $extra = array();
      $result = db_query('SELECT na.gid, na.sa_view, na.sa_update, na.sa_delete FROM {simple_access_node} na WHERE na.nid = %d', $node->nid);
      while ($grant = db_fetch_array($result)) {
        $gid = $grant['gid'];
        unset($grant['gid']);
        $extra[$gid] = $grant;
      }
      $node->simple_access = $extra;
      $result = db_query('SELECT pid FROM {simple_access_profiles_node} WHERE nid = %d', $node->nid);
      while ($row = db_fetch_array($result)) {
        $node->simple_access_profiles[] = $row['pid'];
      }
      break;
    case 'update':
    case 'insert':
      if ($node->uid) {
        $node->simple_access_owner['nid'] = $node->nid;
        db_query('UPDATE {simple_access_owner} o SET o.sa_view = %d, o.sa_update = %d, o.sa_delete = %d WHERE o.nid = %d', array_values($node->simple_access_owner));
        if (!db_affected_rows()) {
          db_query('INSERT INTO {simple_access_owner} (sa_view, sa_update, sa_delete, nid) VALUES (%d, %d, %d, %d)', $node->simple_access_owner);
        }
      }
      db_query('DELETE FROM {simple_access_node} WHERE nid = %d', $node->nid);
      if (isset($node->simple_access)) {
        foreach ($node->simple_access as $gid => $access) {
          if ($access['sa_view'] || $access['sa_update'] || $access['sa_delete']) {
            db_query("INSERT INTO {simple_access_node} (nid, gid, sa_view, sa_update, sa_delete) VALUES (%d, %d, %d, %d, %d)", $node->nid, $gid, $access['sa_view'], $access['sa_update'], $access['sa_delete']);
          }
        }
      }
      db_query('DELETE FROM {simple_access_profiles_node} WHERE nid = %d', $node->nid);
      if (isset($node->simple_access_profiles)) {
        foreach (array_filter($node->simple_access_profiles) as $pid) {
          db_query('INSERT INTO {simple_access_profiles_node} (nid, pid) VALUES (%d, %d)', $node->nid, $pid);
        }
      }
      break;
    case 'delete':
      db_query('DELETE FROM {simple_access_node} WHERE nid = %d', $node->nid);
      db_query('DELETE FROM {simple_access_owner} WHERE nid = %d', $node->nid);
      db_query('DELETE FROM {simple_access_profiles_node} WHERE nid = %d', $node->nid);
      break;
  }
}