You are here

function spaces_nodeapi in Spaces 5

Same name and namespace in other branches
  1. 5.2 spaces.module \spaces_nodeapi()
  2. 6 spaces.module \spaces_nodeapi()
  3. 6.2 spaces.module \spaces_nodeapi()

Implentation of hook_nodeapi

File

./spaces.module, line 404

Code

function spaces_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if ($a4) {
        if (spaces_router('node view', $node->nid) === false) {
          drupal_access_denied();
          exit;
        }
      }
      break;
    case 'validate':
      if (og_is_group_type($node->type)) {
        if (!isset($node->spaces_path) || empty($node->spaces_path)) {
          form_set_error(t('Group path is required.'));
        }
        elseif (spaces_group_path($node->nid) != $node->spaces_path) {
          $group_context = array(
            'provider' => 'spaces',
            'prefix' => $node->spaces_path,
            'id' => $node->nid,
          );
          if (!context_prefix_api('validate', $group_context)) {
            form_set_error('spaces', t('There was an error registering the path "@path". It is either invalid or is already taken. Please choose another.', array(
              '@path' => $node->spaces_path,
            )));
          }
        }
      }
      break;
    case 'prepare':
      if (og_is_group_type($node->type)) {
      }
      else {
        if ($gid = spaces_gid()) {
          _spaces_enforce_feature($gid, $node);
        }
      }
      break;
    case 'submit':

      // switch node's group if specified
      if (!og_is_omitted_type($node->type)) {
        if (isset($node->spaces_og['gid']) && !in_array($node->spaces_og['gid'], $node->og_groups)) {
          $new_gid = $node->spaces_og['gid'];
          _spaces_enforce_feature($new_gid, $node);
        }
      }
      break;
    case 'insert':
    case 'update':

      // only attempt to insert or update the context path if it is different/does not exist
      if (og_is_group_type($node->type) && spaces_group_path($node->nid) != $node->spaces_path) {
        $group_context = array(
          'provider' => 'spaces',
          'id' => $node->nid,
        );

        // clear out old group path if exists
        context_prefix_api('delete', $group_context);

        // insert new group path
        $group_context['prefix'] = $node->spaces_path;
        context_prefix_api('insert', $group_context);
      }
      break;
    case 'load':
      if (og_is_group_type($node->type)) {
        $prefix = spaces_group_path($node->nid);
        $node->spaces_path = $prefix ? $prefix : '';
      }
      break;
    case 'delete':

      // Remove entries from spaces_features table when deleting group nodes.
      if (og_is_group_type($node->type)) {
        db_query('DELETE FROM {spaces_features} WHERE gid=%d', $node->nid);

        // Clear group's context path from context paths variable
        $group_context = array(
          'module' => 'spaces',
          'prefix' => $node->spaces_path,
          'id' => $node->nid,
        );
        context_prefix_api('delete', $group_context);
      }
      break;
  }
}