You are here

function og_nodeapi in Organic groups 6.2

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.3 og.module \og_nodeapi()
  5. 5.7 og.module \og_nodeapi()
  6. 6 og.module \og_nodeapi()

Implementation of hook_nodeapi().

1 string reference to 'og_nodeapi'
og_form_add_og_audience in ./og.module
Add OG audience fields to a given form.

File

./og.module, line 1557
Code for the Organic Groups module.

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 && !empty($node->og_groups)) {
        $bc = og_get_breadcrumb($group_node);
        drupal_set_breadcrumb($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);
      }
      elseif ($grps = og_get_node_groups($node)) {

        // TODO: Refactor so we don't need 2 arrays.
        $node->og_groups = drupal_map_assoc(array_keys($grps));
        $node->og_groups_both = $grps;
      }
      else {
        $node->og_groups = $node->og_groups_both = array();
      }
      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 <a href="@join">join a group</a> before posting on this web site.', array(
            '@join' => url('og'),
          )));
        }
      }

      // Ensure that the number of groups specified as node audience do not exceed group limit
      if (isset($node->og_groups)) {
        $og_groups_filtered = array_filter($node->og_groups);
        $max_number = variable_get('og_max_groups_' . $node->type, '');
        if ($og_groups_filtered && $max_number) {
          if (count($og_groups_filtered) > $max_number && !user_access('administer nodes')) {
            form_set_error('og_groups', format_plural($max_number, "The audience for this post may not exceed !max_number group.", "The audience for this post may not exceed !max_number groups.", array(
              '!max_number' => $max_number,
            )));
          }
        }
      }
      break;
    case 'presave':
      og_presave_group($node);
      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}", array(
            'absolute' => TRUE,
          )),
          '@username' => strip_tags(theme('username', $account)),
          '!invite_url' => url("og/invite/{$node->nid}", array(
            'absolute' => TRUE,
          )),
        );
        $message = array(
          'subject' => _og_mail_text('og_new_admin_subject', $variables),
          'body' => _og_mail_text('og_new_admin_body', $variables),
        );

        // Skip the alert if we are auto-generating nodes.
        if (empty($node->devel_generate)) {

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

          // 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($user->uid, 1, TRUE);
          }
        }
      }
      else {
        og_save_ancestry($node);
      }
      break;
    case 'search result':

      // Similar code in og_preprocess_node()
      $current_groups['accessible'] = array();
      if ($node->og_groups) {
        $current_groups = og_node_groups_distinguish($node->og_groups_both, FALSE);
      }
      $msg = format_plural(count($current_groups['accessible']), '1 group', '@count groups');
      return array(
        'og_msg' => $msg,
      );

      // TODOL: bad formatting. commented out.
      // foreach ($current_groups['accessible'] as $gid => $item) {
      //           $og_links['og_'. $gid] = array('title' => $item['title'], 'href' => "node/$gid");
      //         }
      // return theme('links', $og_links, array('class' => 'groups links'));
      break;
    case 'rss item':
      if (isset($node->og_groups)) {
        $ret = array();
        $append = array();
        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}", array(
                'absolute' => TRUE,
              )),
              'xmlns' => 'http://drupal.org/project/og',
            ),
          );
        }
        return $ret;
      }
      break;
  }
}