You are here

function spaces_og_nodeapi in Spaces 6.3

Same name and namespace in other branches
  1. 5.2 spaces_og.module \spaces_og_nodeapi()
  2. 6 spaces_og/spaces_og.module \spaces_og_nodeapi()
  3. 6.2 spaces_og/spaces_og.module \spaces_og_nodeapi()

Implementation of hook_nodeapi().

File

spaces_og/spaces_og.module, line 238

Code

function spaces_og_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {

  // Groups
  if (og_is_group_type($node->type)) {
    switch ($op) {
      case 'load':
        $modifier = purl_load(array(
          'id' => isset($node->nid) ? $node->nid : NULL,
          'provider' => 'spaces_og',
        ));
        $node->purl = $modifier['value'];
        break;
      case 'insert':
      case 'update':
        $space = spaces_load('og', $node->nid, TRUE);
        if ($space) {

          // Save preset values.
          if (!empty($node->spaces_preset_og)) {
            $space->controllers->variable
              ->set('spaces_preset_og', $node->spaces_preset_og);
          }

          // Configure OG directly from space object as node_save() may be
          // called from outside of actual group space.
          $selective = $space->controllers->variable
            ->get('spaces_og_selective');
          $node->og_selective = $selective === NULL ? OG_OPEN : $selective;
          $register = $space->controllers->variable
            ->get('spaces_og_register');
          $node->og_register = $register === NULL ? TRUE : $register;
          $directory = $space->controllers->variable
            ->get('spaces_og_directory');
          $node->og_directory = $directory === NULL ? TRUE : $directory;
          $private = $space->controllers->variable
            ->get('spaces_og_private');
          if ($op == 'update' && $private != $node->og_private) {

            // Privacy has changed, update og_public flags, and queue a rebuid.
            db_query("UPDATE {og_access_post} og_p \n                      INNER JOIN {og_ancestry} og_a ON og_p.nid = og_a.nid\n                      INNER JOIN {node} n ON og_p.nid = n.nid\n                      SET og_public = %d WHERE og_a.group_nid = %d", !$private, $node->nid);
            node_access_needs_rebuild(TRUE);
          }
          $node->og_private = $private === NULL ? FALSE : $private;
          og_update_group($node);
        }

        // Save PURL modifier.
        $modifier = array(
          'provider' => 'spaces_og',
          'id' => $node->nid,
        );
        if (is_array($node->purl) && !empty($node->purl['value'])) {
          $modifier['value'] = $node->purl['value'];
          purl_save($modifier);
        }
        else {
          if (empty($node->purl)) {
            $modifier['value'] = "{$node->type}-{$node->nid}";
            purl_save($modifier);
          }
        }
        break;
      case 'delete':
        spaces_delete('og', $node->nid);
        purl_delete(array(
          'provider' => 'spaces_og',
          'id' => $node->nid,
        ));
        $space = spaces_get_space();
        if ($space->type == 'og' && $space->id == $node->nid) {
          spaces_set_space(FALSE);
        }
        break;
    }
  }
  else {
    if (!og_is_omitted_type($node->type)) {
      switch ($op) {
        case 'validate':

          // Enforce groups for OG. OG enforces audiences via required form
          // elements that Spaces removes.
          // @see og_form_add_og_audience()
          // @see _spaces_og_form_alter_node()
          if (variable_get('og_audience_required', FALSE) && empty($node->og_groups)) {
            if (isset($node->spaces_og_audience) && (int) $node->spaces_og_audience !== 0) {
              $node->og_groups[$node->spaces_og_audience] = $node->spaces_og_audience;
            }
            else {

              // @todo This error will leave none 'administer nodes' users in a
              // dead end. Ideally, access should be restricted on the menu
              // level.
              form_set_error('spaces_og_audience', t('This content type must be posted in a group.'));
            }
          }
          break;
        case 'prepare':
        case 'presave':
          $space = spaces_get_space();
          if ($space && $space->type == 'og') {
            _spaces_og_enforce_privacy($space->id, $node);
          }
          else {
            if (!empty($node->og_groups)) {
              _spaces_og_enforce_privacy(current($node->og_groups), $node);
            }
          }
          if (isset($node->spaces_og_audience) && (int) $node->spaces_og_audience !== 0) {
            $node->og_groups[$node->spaces_og_audience] = $node->spaces_og_audience;
          }
          break;
      }
    }
  }
}