You are here

hashtags.module in Hashtags 6.2

File

hashtags.module
View source
<?php

// $Id$

/**
 * Implementation of hook_form_alter().
 */
function hashtags_form_alter(&$form, $form_state, $form_id) {
  $vid = variable_get('hashtags_vocabulary', '');
  $voc = taxonomy_vocabulary_load($vid);

  // Hide hashtag textfield from node edit page;
  // check only for hashtag content types.
  if (isset($form['type']) && isset($form['#node']) && in_array($form['type']['#value'], $voc->nodes) && $form['type']['#value'] . '_node_form' == $form_id) {
    drupal_add_js("if (Drupal.jsEnabled) { \$(document).ready(function() { \$('div#edit-taxonomy-tags-{$vid}-wrapper').css('display', 'none'); }); }", 'inline');
  }
  elseif (isset($form['vid']) && $form['vid']['#value'] == $vid) {

    // Hide critical options from hashtag vocabulary.
    if ($form_id == 'taxonomy_form_vocabulary') {
      $form['help_hashtags_vocab'] = array(
        '#value' => t('This is the designated hashtags vocabulary. Some of the normal vocabulary options have been removed.'),
        '#weight' => -1,
      );
      $form['identification']['description']['#access'] = FALSE;
      $form['identification']['help']['#access'] = FALSE;
      $form['content_types']['nodes']['#required'] = TRUE;
      $form['settings']['#access'] = FALSE;
      unset($form['delete']);
    }
    elseif ($form_id == 'taxonomy_form_term') {
      $form['advanced']['parent']['#access'] = FALSE;
    }
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function hashtags_nodeapi(&$node, $op, $teaser, $page) {
  $vid = variable_get('hashtags_vocabulary', '');
  $voc = taxonomy_vocabulary_load($vid);
  if (!in_array($node->type, $voc->nodes)) {
    return;
  }
  switch ($op) {
    case 'presave':
      $vid = variable_get('hashtags_vocabulary', '');

      // Parse body to get all hashtags (#some_word) and pass like commas separated string.
      $node->taxonomy['tags'][$vid] = hashtags_get_tags($node->body);
      break;
    case 'load':
      $vid = variable_get('hashtags_vocabulary', '');
      $terms = hashtags_node_get_terms($node->nid);

      // transform hashtag words to links
      $node->teaser = hashtags_node_transform_to_links($node->teaser, $terms);

      // transform hashtag words to links
      $node->body = hashtags_node_transform_to_links($node->body, $terms);
      break;
  }
}

/*
 * Create and return commas separated string from hashtag words (#some_word)
 */
function hashtags_get_tags($text) {
  $tags_list = array();
  $pattern = "/#[0-9A-Za-z_]+/";
  preg_match_all($pattern, $text, $tags_list);
  $result = implode(',', $tags_list[0]);
  return $result;
}

/*
 * Returns an array of hashtags for $nid node
 * Array['term_name'] = term_id;
 */
function hashtags_node_get_terms($nid) {
  $terms = array();
  $vid = variable_get('hashtags_vocabulary', '');
  $sql = "SELECT td.name, td.tid FROM {term_data} td \n  INNER JOIN {term_node} tn ON td.tid = tn.tid \n  WHERE tn.nid = %d AND td.vid = %d";
  $result = db_query($sql, $nid, $vid);
  while ($term = db_fetch_object($result)) {
    $terms[$term->name] = $term->tid;
  }
  return $terms;
}

/*
 * Find each hashtag word (#some_word) and turn it into the link
 * (<a href="taxonomy/term/$tid">#some_word</a>). $tid are taken
 * from $terms array. 
 */
function hashtags_node_transform_to_links($text, $terms) {
  foreach ($terms as $word => $tid) {
    if (strstr($text, $word)) {
      $link = l($word, "taxonomy/term/{$tid}", array(
        'attributes' => array(
          'class' => 'hashtag',
        ),
      ));
      $text = str_replace($word, $link, $text);
    }
  }
  return $text;
}