You are here

function discussthis_link in Discuss This! 6

Same name and namespace in other branches
  1. 5 discussthis.module \discussthis_link()

\brief Implementation of hook_link().

This adds the custom link to the node view

\param[in] $type An identifier declaring what kind of link is being requested. \param[in] $node A node object being passed in the case of node links. \param[in] $teaser A boolean flag depending on whether the node is displayed as a teaser or full form.

\return An array of the requested links

1 string reference to 'discussthis_link'
discussthis_admin_settings in ./discussthis.admin.inc
\brief Generate the administration form.

File

./discussthis.module, line 380
Associations discussions in forums with specific nodes

Code

function discussthis_link($type, $node = NULL, $teaser = FALSE) {
  if ($type == 'node' && $node) {
    $links = array();

    // verify that the type is selected
    $node_types = node_get_types('names');
    $discussthis_nodetypes = variable_get('discussthis_nodetypes', $node_types);
    if (empty($discussthis_nodetypes[$node->type])) {
      return $links;
    }

    // verify that the type has a valid discussion forum selected
    $discussthis_forum = _discussthis_get_forum($node->nid, $node->type);
    if ($discussthis_forum['forum_tid'] <= 0) {
      return $links;
    }

    // lookup for topic nid, if it exists (otherwise we get 0)
    $topic_nid = _discussthis_get_topic($node->nid);
    $discussthis_link = variable_get('discussthis_link', '');
    if (!$discussthis_link) {
      $discussthis_link = t('Discuss This!');
    }
    $participate_link = variable_get('discussthis_participate', '');
    if (!$participate_link) {
      $participate_link = t('Participate in this discussion');
    }

    // if the topic exists, and the user has access to link to it
    if ($topic_nid && user_access('access discuss this links')) {
      $all = 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['discussthis'] = array(
        // use "Discuss This!" when no discussion started yet
        '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) {

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

    // still no link?
    if (count($links) == 0) {

      // if the user is not logged in, offer him to do so if he otherwise
      // would have access to the link
      global $user;
      if ($user->uid == 0 && variable_get('discussthis_login', 1) && user_access('access discuss this links')) {
        if ($topic_nid) {
          $all = 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--note: if someone else creates it
          // before this user comes back, the proper post will be shown
          // automatically (if not, the bug is not here.)
          $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
            $new_user = variable_get('discussthis_new_user', '');
            if (!$new_user) {
              $new_user = t('(new users have to return to the discussion after validating their new account by e-mail)');
            }
            $appeal .= ' ' . $new_user;
            $href = t('!login or !register to @appeal', array(
              '!login' => l(t('Log in'), 'user/login', array(
                'query' => $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' => $destination,
              )),
              '!register' => l(t('Register'), 'user/register', array(
                'query' => $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' => $destination,
              'attributes' => $attributes,
            )),
            '@appeal' => $appeal,
          ));
        }
        $links['discussthis'] = array(
          'title' => $href,
          'html' => TRUE,
        );
      }
    }
    return $links;
  }
}