You are here

function og_access_nodeapi in Organic groups 6.2

Implementation of hook_nodeapi().

File

modules/og_access/og_access.module, line 32

Code

function og_access_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'load':
      if (og_is_group_post_type($node->type)) {

        // Can't assume that og_access post records already exist. See http://drupal.org/node/522728.
        $public_value = db_result(db_query("SELECT og_public FROM {og_access_post} WHERE nid = %d", $node->nid));
        $node->og_public = $public_value === '0' ? FALSE : TRUE;
      }
      break;
    case 'presave':

      // If no groups are selected, the post is always public.
      if (og_is_group_post_type($node->type)) {
        if (empty($node->og_groups)) {
          $node->og_public = TRUE;
        }
      }
      break;
    case 'insert':

      // Save og_public.
      if (og_is_group_post_type($node->type)) {
        drupal_write_record('og_access_post', $node);
      }
      break;
    case 'update':

      // Save og_public.
      if (og_is_group_post_type($node->type)) {

        // Can't assume that og_access post records already exist.
        // See http://drupal.org/node/522728.
        if (!db_result(db_query_range("SELECT nid FROM {og_access_post} WHERE nid = %d", $node->nid, 0, 1))) {
          drupal_write_record('og_access_post', $node);
        }
        else {
          drupal_write_record('og_access_post', $node, 'nid');
        }
      }
      break;
    case 'delete':
      if (og_is_group_post_type($node->type)) {
        $sql = "DELETE FROM {og_access_post} WHERE nid = %d";
        db_query($sql, $node->nid);
      }
      break;
  }
}