View source
<?php
function hashtags_form_alter(&$form, $form_state, $form_id) {
$vid = variable_get('hashtags_vocabulary', '');
$voc = taxonomy_vocabulary_load($vid);
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) {
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;
}
}
}
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', '');
$node->taxonomy['tags'][$vid] = hashtags_get_tags($node->body);
break;
case 'load':
$vid = variable_get('hashtags_vocabulary', '');
$terms = hashtags_node_get_terms($node->nid);
$node->teaser = hashtags_node_transform_to_links($node->teaser, $terms);
$node->body = hashtags_node_transform_to_links($node->body, $terms);
break;
}
}
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;
}
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;
}
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;
}