You are here

function og_nodeapi in Organic groups 5

Same name and namespace in other branches
  1. 5.8 og.module \og_nodeapi()
  2. 5.2 og.module \og_nodeapi()
  3. 5.3 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 1324

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':
      if (og_is_group_type($node->type)) {

        // Email address is required for contacting the Manager
        $account = user_load(array(
          'uid' => $node->uid,
        ));
        if ($account->uid > 0 && empty($account->mail)) {
          form_set_error('name', t('The group manager, %name, must have an email address in his <a href="!profile">profile</a>.', array(
            '%name' => $account->name,
            '!profile' => url("user/{$node->uid}/edit"),
          )));
        }
      }
      else {

        // 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),
        );

        // alert the user that they are now the admin of the group
        $from = variable_get('site_mail', ini_get('sendmail_from'));
        drupal_mail('og_new_admin', $account->mail, _og_user_mail_text('og_new_admin_subject', $variables), _og_user_mail_text('og_new_admin_body', $variables), $from);
      }
      else {
        og_save_ancestry($node);
      }
      $skip_notification = FALSE;

      // Any module that returns TRUE from its hook_og_notify($node) will prevent sending notifications.
      // og2list and og_subscriptions use this to send own notifications.
      foreach (module_implements('og_notify') as $module) {
        if (module_invoke($module, 'og_notify', $node)) {
          $skip_notification = TRUE;
          break;
        }
      }
      if (!$skip_notification && $node->status && og_is_mail_type($node->type) && $node->og_groups) {
        if (module_exists('job_queue')) {
          $description = t('OG: notify group members about node %nid - !link.', array(
            '%nid' => $node->nid,
            '!link' => l($node->title, "node/{$node->nid}"),
          ));
          job_queue_add('og_mail', $description, array(
            'node',
            $node->nid,
          ));
        }
        else {
          og_mail('node', $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),
            ),
          );
        }

        // 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;
  }
}