View source
<?php
function autotag_callback($content_type, $nid = FALSE) {
$tids = autotag_search_form_array($_POST, $content_type);
if (!isset($_SESSION['autotag_tids'])) {
$_SESSION['autotag_tids'] = array();
}
$_SESSION['autotag_tids'][$_POST['form_build_id']] = $tids;
if ($nid) {
$result = db_query('SELECT a.tid, vid FROM {autotag} a, {term_data} t WHERE nid = %d AND a.tid = t.tid', $nid);
while ($row = db_fetch_array($result)) {
$key = array_search($row, $tids);
if ($key !== FALSE) {
unset($tids[$key]);
}
}
}
if ($nid) {
$node = node_load(array(
'nid' => $nid,
));
}
else {
$node = array(
'uid' => $user->uid,
'name' => isset($user->name) ? $user->name : '',
'type' => $type,
'language' => '',
);
}
if (count($tids)) {
$term_names = array();
foreach ($tids as $tid) {
$term = taxonomy_get_term($tid['tid']);
$term_names[] = $term->name;
if (!isset($_POST['taxonomy'])) {
$_POST['taxonomy'] = array();
}
if (!isset($_POST['taxonomy'][$tid['vid']])) {
if (function_exists('batax_use_batax') && !batax_use_batax($tid['vid'], $content_type)) {
$_POST['taxonomy'][$tid['vid']] = array();
}
}
if (is_array($_POST['taxonomy'][$tid['vid']])) {
$_POST['taxonomy'][$tid['vid']][] = $tid['tid'];
}
else {
$term_text = $term->name . ':' . $term->tid;
if (strpos($_POST['taxonomy'][$tid['vid']], $term_text) === FALSE) {
$_POST['taxonomy'][$tid['vid']] .= (strlen(trim($_POST['taxonomy'][$tid['vid']])) ? ', ' : '') . $term_text;
}
}
}
$message = t('Autotag found the following terms in this node:') . ' ' . check_plain(implode(", ", $term_names));
if (strlen($message) > 500) {
$message = substr($message, 0, 500) . "... (too many to list)";
}
}
else {
$message = t('Autotag found no terms in this node');
}
print drupal_to_js(array(
'status' => TRUE,
'data' => drupal_get_form($content_type . '_node_form', $node),
'action' => 'autotag_uncheckbox(response.message)',
'message' => $message,
));
drupal_get_messages();
exit;
}
function autotag_search_form_array($form_parts, $content_type) {
$tids = array();
foreach ($form_parts as $part) {
if (is_object($part)) {
$part = (array) $part;
}
if (is_array($part)) {
$tids = array_merge($tids, autotag_search_form_array($part, $content_type));
}
else {
$tids = array_merge($tids, autotag_search_form_part($part, $content_type));
}
}
return $tids;
}
function autotag_search_form_part($text, $content_type) {
$terms = array();
if (trim($text) == '') {
return array();
}
$vids = _autotag_get_vids_for_type($content_type);
$words_including_small = preg_split('/[\\ `!"£$%^&*()_\\-+={\\[}\\]:;@\'~#<,>.?\\/|\\\\]/', strtolower($text), -1, PREG_SPLIT_NO_EMPTY);
$words = array();
$words_placeholder = array();
foreach ($words_including_small as $key => $word) {
if (strlen(trim($word)) > 3) {
$words[$key] = $word;
$words_placeholder[] = "'%s'";
}
}
$tag_only_leaves = variable_get('autotag_only_leaves', FALSE);
if ($tag_only_leaves) {
$tag_only_leaves_sql = ' AND t.tid NOT IN (SELECT parent FROM {term_hierarchy}) ';
}
$vids_placeholder = array();
foreach ($vids as $doesntmatter) {
$vids_placeholder[] = "vid = %d";
}
if (count($words_placeholder) && count($vids)) {
$results = db_query(db_rewrite_sql("\n SELECT \n t.tid, t.vid \n FROM \n {term_data} t\n INNER JOIN \n {term_lowername} l\n ON \n t.tid = l.tid \n WHERE \n lowername IN (" . implode(",", $words_placeholder) . ") AND \n (" . implode(" OR ", $vids_placeholder) . ") {$tag_only_leaves_sql} ", 't', 'tid') . '
UNION
' . db_rewrite_sql("SELECT \n t.tid, t.vid \n FROM \n {term_data} t\n INNER JOIN \n {term_synonym} s\n ON\n s.tid = t.tid \n WHERE \n LOWER(s.name) IN (" . implode(",", $words_placeholder) . ") AND \n (" . implode(" OR ", $vids_placeholder) . ") {$tag_only_leaves_sql}", 't', 'tid'), array_merge($words, $vids, $words, $vids));
while ($row = db_fetch_array($results)) {
$terms[] = array(
'tid' => $row['tid'],
'vid' => $row['vid'],
);
}
$total_words = count($words);
$sql_array = array();
$sql_array_syn = array();
$words_array = array();
$words_array_syn = array();
foreach ($words as $key => $word) {
if (isset($words_including_small[$key + 1])) {
$sql_array[] = " lowername LIKE '%s%%%s%%' ";
$words_array[] = $word;
$words_array[] = $words_including_small[$key + 1];
$sql_array_syn[] = " LOWER(t.name) LIKE '%s%%%s%%' ";
$words_array_syn[] = $word;
$words_array_syn[] = $words_including_small[$key + 1];
}
}
if ($tag_only_leaves) {
$tag_only_leaves_sql = ' AND tid NOT IN (SELECT parent FROM {term_hierarchy}) ';
}
if (count($sql_array)) {
$results = db_query(db_rewrite_sql("\n SELECT \n tid\n FROM \n {term_lowername} t \n WHERE \n (" . implode(" OR ", $sql_array) . ") {$tag_only_leaves_sql} ", 't', 'tid') . '
UNION
' . db_rewrite_sql("SELECT \n tid \n FROM \n {term_synonym} t\n WHERE \n (" . implode(" OR ", $sql_array_syn) . ") {$tag_only_leaves_sql}", 't', 'tid'), array_merge($words_array, $words_array_syn));
while ($row = db_fetch_array($results)) {
$results_2 = db_query(db_rewrite_sql("\n SELECT \n tid, vid, LOWER(name) AS name\n FROM \n {term_data} t\n WHERE \n tid = %d AND\n (" . implode(" OR ", $vids_placeholder) . ")", 't', 'tid') . '
UNION
' . db_rewrite_sql("SELECT\n s.tid, vid, LOWER(s.name) AS name\n FROM \n {term_data} t\n INNER JOIN \n {term_synonym} s\n ON \n s.tid = %d\n WHERE \n t.tid=s.tid AND \n (" . implode(" OR ", $vids_placeholder) . ")", 't', 'tid'), array_merge(array(
$row['tid'],
), $vids, array(
$row['tid'],
), $vids));
while ($row_2 = db_fetch_array($results_2)) {
if (strpos(strtolower($text), $row_2['name']) !== FALSE) {
$terms[] = array(
'tid' => $row_2['tid'],
'vid' => $row_2['vid'],
);
}
}
}
}
}
return $terms;
}