You are here

function discussthis_node_view in Discuss This! 7

Same name and namespace in other branches
  1. 7.2 discussthis.node.inc \discussthis_node_view()

Implements hook_node_view().

File

./discussthis.node.inc, line 12
File with node discussion display methods.

Code

function discussthis_node_view($node, $view_mode, $langcode) {
  global $user;
  if ($node) {
    $links = array();

    // Verify that the content type is eligible for discussion.
    $node_types = node_type_get_names();
    $discussthis_types_config = variable_get('discussthis_types_config', array());
    if (!isset($discussthis_types_config[$node->type][$node->type . '_enable']) || !$discussthis_types_config[$node->type][$node->type . '_enable']) {
      return $links;
    }

    // Lookup for topic nid, if it exists (otherwise we get 0).
    $topic_nid = _discussthis_get_topic($node->nid);

    // Only pages being displayed full page have comments shown.
    if ($view_mode == 'full' && $topic_nid) {
      $node->content['discussion'] = _discussthis_discussion_load($topic_nid);
    }

    // Verify that the type has a valid discussion forum selected.
    $forum_tid = _discussthis_get_forum($node->nid, $node->type);
    if (!$forum_tid) {
      return $links;
    }

    // Get the DiscusssThis! link text.
    $discussthis_link = variable_get('discussthis_link', '');
    if (!$discussthis_link) {
      $discussthis_link = t('Discuss This!');
    }

    // Get the participate in DiscusssThis! link text.
    $participate_link = variable_get('discussthis_participate', '');
    if (!$participate_link) {
      $participate_link = t('Participate in this discussion');
    }

    // If the user is connected and have permission
    if ($user->uid && user_access('access discuss this links')) {
      if ($topic_nid) {
        $all = _discussthis_comment_num_all($topic_nid);
        if (variable_get('discussthis_showcounts', 1)) {
          $new = comment_num_new($topic_nid);
          $counts = t(' (@new new/@all total)', array(
            '@new' => $new,
            '@all' => $all,
          ));
        }
        $links = array(
          // use "Discuss This!" when no discussion started yet
          '#type' => 'link',
          '#title' => $all == 0 ? $discussthis_link : $participate_link . $counts,
          '#href' => 'node/' . $topic_nid,
          '#attributes' => array(
            '#class' => 'discussthis-link',
            '#title' => t('Participate to the discussion about this page'),
          ),
        );
      }
      elseif ($topic_nid == 0) {

        // Check whether the user has permission to initiate a new topics.
        if (user_access('initiate discuss this topics')) {
          $links = array(
            '#type' => 'link',
            '#title' => $discussthis_link,
            '#href' => 'discussthis/create/' . $node->nid,
            '#attributes' => array(
              '#class' => 'discussthis-link',
              '#title' => t('Start a discussion about this page'),
              '#rel' => 'nofollow',
            ),
          );
        }
      }
    }
    else {

      // This happens for anonymous users
      // If the user is not logged in, offer him to do so.
      // Otherwise, would have access to the link.
      if (variable_get('discussthis_login', 1) && user_access('access discuss this links')) {
        if ($topic_nid) {
          $all = _discussthis_comment_num_all($topic_nid);
          if (variable_get('discussthis_showcounts', 1)) {
            $new = comment_num_new($topic_nid);
            $counts = t(' (@new new/@all total)', array(
              '@new' => $new,
              '@all' => $all,
            ));
          }
          $destination = 'destination=node/' . $topic_nid;
          $appeal = $all == 0 ? $discussthis_link : $participate_link . $counts;
        }
        else {

          // The topic does not exist.
          $destination = 'destination=discussthis/new/' . $node->nid;
          $appeal = $discussthis_link;
        }
        $attributes = array(
          'class' => 'discussthis-login',
          'title' => t('Log in or register and start a discussion about this page'),
          'rel' => 'nofollow',
        );
        if (variable_get('user_register', 1)) {
          if (variable_get('user_email_verification', 1)) {

            // Users can register themselves but have to go through e-mail verification process
            $href = t('!login or !register to @appeal', array(
              '!login' => l(t('Log in'), 'user/login', array(
                'query' => drupal_get_destination(),
                'attributes' => $attributes,
              )),
              '!register' => l(t('Register'), 'user/register'),
              '@appeal' => $appeal,
            ));
          }
          else {

            // Users can register themselves without e-mail verification
            $href = t('!login or !register to @appeal', array(
              '!login' => l(t('Log in'), 'user/login', array(
                'query' => drupal_get_destination(),
              )),
              '!register' => l(t('Register'), 'user/register', array(
                'query' => drupal_get_destination(),
                'attributes' => $attributes,
              )),
              '@appeal' => $appeal,
            ));
          }
        }
        else {

          // Only admins can add new users, no public registration.
          $href = t('!login to @appeal', array(
            '!login' => l('Log in', 'user/login', array(
              'query' => drupal_get_destination(),
              'attributes' => $attributes,
            )),
            '@appeal' => $appeal,
          ));
        }
        $links = array(
          '#type' => 'link',
          '#title' => $href,
          '#href' => '',
          '#options' => array(
            'html' => TRUE,
          ),
        );
      }
    }
    $node->content['discussthis'] = $links;
  }
}