You are here

function og_nodeapi in Organic groups 5.3

Same name and namespace in other branches
  1. 5.8 og.module \og_nodeapi()
  2. 5 og.module \og_nodeapi()
  3. 5.2 og.module \og_nodeapi()
  4. 5.7 og.module \og_nodeapi()
  5. 6.2 og.module \og_nodeapi()
  6. 6 og.module \og_nodeapi()

Implementation of hook_nodeapi().

File

./og.module, line 1330

Code

function og_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  global $user;
  switch ($op) {
    case 'view':
      $group_node = og_get_group_context();
      if ($group_node && $page && $node->og_groups) {

        // set breadcrumb and title on non group nodes
        $bc[] = array(
          'path' => "og",
          'title' => t('Groups'),
        );
        $bc[] = array(
          'path' => "node/{$group_node->nid}",
          'title' => $group_node->title,
        );
        $bc[] = array(
          'path' => "node/{$node->nid}",
          'title' => $node->title,
        );
        menu_set_location($bc);
      }
      if (og_is_group_type($node->type)) {
        og_view_group($node, $teaser, $page);
      }
      break;
    case 'load':
      if (og_is_group_type($node->type)) {
        og_load_group($node);
      }
      if ($grps = og_get_node_groups($node)) {

        // TODO: Refactor so we don't need 2 arrays.
        $node->og_groups = array_keys($grps);
        $node->og_groups_both = $grps;
        $public = db_result(db_query_range("SELECT is_public FROM {og_ancestry} WHERE nid = %d", $node->nid, 0, 1));
        $node->og_public = $public ? TRUE : FALSE;
      }
      break;
    case 'validate':

      // Ensure that a group is selected if groups are required. needed when author has no groups. In other cases, fapi does the validation
      if (og_is_group_post_type($node->type) && variable_get('og_audience_required', FALSE) && !user_access('administer nodes')) {
        if (!isset($node->og_groups)) {
          form_set_error('title', t('You must !join before posting on this web site.', array(
            '!join' => l(t('join a group'), 'og'),
          )));
        }
      }
      break;
    case 'submit':
      og_submit_group($node);
      break;
    case 'prepare':

      // TODO: some people don't like forcing comments to disabled for groups.
      if (og_is_group_type($node->type)) {
        $node->comment = COMMENT_NODE_DISABLED;
      }
      break;
    case 'delete':
      $sql = "DELETE FROM {og} WHERE nid = %d";
      db_query($sql, $node->nid);
      $sql = "DELETE FROM {og_ancestry} WHERE nid = %d";
      db_query($sql, $node->nid);
      $sql = "DELETE FROM {og_uid} WHERE nid = %d";
      db_query($sql, $node->nid);
      break;
    case 'insert':
      if (og_is_group_type($node->type)) {
        og_insert_group($node);

        // make sure the node owner is a full powered member
        og_save_subscription($node->nid, $node->uid, array(
          'is_active' => 1,
          'is_admin' => 1,
        ));

        // load new group into $user->og_groups so that author can get redirected to the new group
        if ($node->uid == $user->uid) {
          $user->og_groups = og_get_subscriptions($node->uid, 1, TRUE);
        }
        $account = user_load(array(
          'uid' => $node->uid,
        ));
        $variables = array(
          '@group' => $node->title,
          '!group_url' => url("node/{$node->nid}", NULL, NULL, TRUE),
          '@username' => $account->name,
          '!invite_url' => url("og/invite/{$node->nid}", NULL, NULL, TRUE),
        );
        $message = array(
          'subject' => _og_user_mail_text('og_new_admin_subject', $variables),
          'body' => _og_user_mail_text('og_new_admin_body', $variables),
        );

        // Alert the user that they are now the admin of the group.
        module_invoke_all('og', 'admin new', $gid, $account->uid, $message);
      }
      else {
        og_save_ancestry($node);
      }
      break;
    case 'update':
      if (og_is_group_type($node->type)) {
        og_update_group($node);

        // make sure the node owner is a full powered member
        og_save_subscription($node->nid, $node->uid, array(
          'is_active' => 1,
          'is_admin' => 1,
        ));

        // load new group into $user->og_groups so that author can get redirected to the new group
        if ($node->uid == $user->uid) {
          $user->og_groups = og_get_subscriptions($account->uid, 1, TRUE);
        }
      }
      else {
        og_save_ancestry($node);
      }
      break;
    case 'search result':

      // TODO: add group info
      break;
    case 'rss item':
      if ($node->og_groups) {
        foreach ($node->og_groups_both as $gid => $title) {

          // TODO: should be absolute link. core bug.
          $append['og_links'] = array(
            'title' => $title,
            'href' => "node/{$gid}",
          );
          $ret[] = array(
            'key' => 'group',
            'value' => check_plain($title),
            'attributes' => array(
              'domain' => url("node/{$gid}", NULL, NULL, TRUE),
              'xmlns' => 'http://drupal.org/project/og',
            ),
          );
        }

        // to get these modifications to work on 4.7, one needs to patch as per http://drupal.org/node/41703
        $node->body .= '<div class="og_rss_groups">' . theme('links', $append) . '</div>';
        $node->teaser .= '<div class="og_rss_groups">' . theme('links', $append) . '</div>';
        return $ret;
      }
      break;
  }
}