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', '');
$hashtags_string = hashtags_get_tags($node->body);
$node->taxonomy['tags'][$vid] = $hashtags_string;
break;
}
}
function hashtags_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Hashtags filter'),
);
case 'description':
return t('Turn #words into links which lead to taxonomy terms');
case 'settings':
break;
case 'no cache':
return FALSE;
case 'prepare':
return $text;
case 'process':
$hashtags_string = hashtags_get_tags($text);
if (empty($hashtags_string)) {
return $text;
}
$hashtags_tids = hashtags_get_terms_by_names($hashtags_string);
$replace_parameter = new hashtags_replace_parameter();
$replace_parameter->hashtags_tids = $hashtags_tids;
$pattern = "/([\\s>]+?)(#[[:alpha:]][[:alnum:]_]*[^<\\s[:punct:]])/iu";
$text = preg_replace_callback($pattern, array(
&$replace_parameter,
'replace',
), $text);
return $text;
case 'default':
return $text;
}
}
function hashtags_get_tags($text, $capture_position = FALSE) {
if ($capture_position) {
$flag = PREG_OFFSET_CAPTURE;
}
else {
$flag = PREG_PATTERN_ORDER;
}
$tags_list = array();
$pattern = "/[\\s>]+?(#[[:alpha:]][[:alnum:]_]*[^<\\s[:punct:]])/iu";
preg_match_all($pattern, '<htest>' . $text . '<htest>', $tags_list, $flag);
if (isset($tags_list[0]) && !sizeof($tags_list[0])) {
return '';
}
if ($capture_position) {
foreach ($tags_list[1] as $key => $data) {
$result[$data[1]] = strtolower($data[0]);
}
}
else {
foreach ($tags_list[1] as $key => $tag) {
$tags_list[1][$key] = '"' . strtolower($tag) . '"';
}
$result = implode(',', $tags_list[1]);
}
return $result;
}
function hashtags_add_filter() {
$added_status = array();
$filtered_html_format = 1;
$is_hashtag_filter_exists = db_result(db_query('SELECT COUNT(*) FROM {filters} WHERE format = %d AND module = "%s" AND delta = 0', $filtered_html_format, 'hashtags'));
if (!$is_hashtag_filter_exists) {
$max_filter_weight = db_result(db_query('SELECT MAX(weight) FROM {filters} WHERE format = %d', $filtered_html_format));
db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $filtered_html_format, 'hashtags', 0, $max_filter_weight + 1);
$added_status[] = $filtered_html_format;
drupal_set_message(t('Hashtags module: Hashtags filter has been added to "Filter HTML" input format'));
}
$full_html_format = 2;
$is_format_exists = db_result(db_query('SELECT COUNT(*) FROM {filter_formats} WHERE format = %d', $full_html_format));
if ($is_format_exists) {
$is_hashtag_filter_exists = db_result(db_query('SELECT COUNT(*) FROM {filters} WHERE format = %d AND module = "%s" AND delta = 0', $full_html_format, 'hashtags'));
if (!$is_hashtag_filter_exists) {
$max_filter_weight = db_result(db_query('SELECT MAX(weight) FROM {filters} WHERE format = %d', $full_html_format));
db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $full_html_format, 'hashtags', 0, $max_filter_weight + 1);
$added_status[] = $full_html_format;
drupal_set_message(t('Hashtags module: Hashtags filter has been added to "Full HTML" input format'));
}
}
if (sizeof($added_status)) {
return TRUE;
}
return FALSE;
}
function hashtags_remove_filter() {
$module = 'hashtags';
db_query("DELETE FROM {filters} WHERE module = '%s'", $module);
drupal_set_message(t('Hashtags module: Hashtags filter has been removed from all input format'));
}
function hashtags_get_terms_by_names($names) {
$terms = array();
$vid = variable_get('hashtags_vocabulary', '');
$sql = "SELECT td.name, td.tid FROM {term_data} td \n WHERE lower(td.name) IN (" . $names . ") AND td.vid = %d";
$result = db_query($sql, $vid);
while ($term = db_fetch_object($result)) {
$terms[$term->name] = $term->tid;
}
return $terms;
}
function hashtags_node_get_terms($nid) {
$terms = array();
$vid = variable_get('hashtags_vocabulary', '');
$sql = "SELECT lower(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;
}
class hashtags_replace_parameter {
function replace($matches) {
if (isset($this->hashtags_tids)) {
$hashtags_tids = $this->hashtags_tids;
}
$first_delimeter = $matches[1];
$hashtag_name = $matches[2];
$hashtag_tid = $hashtags_tids[strtolower($hashtag_name)];
$hashtag_link = l($hashtag_name, 'taxonomy/term/' . $hashtag_tid, array(
'attributes' => array(
'class' => 'hashtag',
),
));
return $first_delimeter . $hashtag_link;
}
}