You are here

function simple_access_nodeapi in Simple Access 6.2

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

Implementation of hook_nodeapi()

File

./simple_access.module, line 135
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':

      // Only use the defaults if this is a new node.
      if (empty($node->nid)) {
        $defaults = variable_get('simple_access_' . $node->type, array(
          'simple_access' => array(),
          'simple_access_profiles' => array(),
          'simple_access_owner' => array(),
        ));
      }
      else {
        $defaults = array(
          'simple_access' => array(),
          'simple_access_profiles' => array(),
          'simple_access_owner' => array(),
        );
      }
      foreach ($defaults as $key => $value) {
        if (!isset($node->{$key})) {
          $node->{$key} = $value;
        }
      }
      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['sa_view'] || $node->simple_access_owner['sa_update'] || $node->simple_access_owner['sa_update'])) {
        $node->simple_access_owner['nid'] = $node->nid;
        drupal_write_record('simple_access_owner', $node->simple_access_owner, array(
          'nid',
        ));
        if (!db_affected_rows()) {
          drupal_write_record('simple_access_owner', $node->simple_access_owner);
        }
      }
      else {
        db_query('DELETE FROM {simple_access_owner} WHERE nid = %d', $node->nid);
      }
      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']) {
            $access['nid'] = $node->nid;
            $access['gid'] = $gid;
            drupal_write_record('simple_access_node', $access);
          }
        }
      }
      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) {
          $data = array(
            'nid' => $node->nid,
            'pid' => $pid,
          );
          drupal_write_record('simple_access_profiles_node', $data);
        }
      }
      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;
  }
}