View source
<?php
function autotag_form_alter(&$form, &$form_state, $form_id) {
if (in_array($form['type']['#value'], variable_get('autotag_broken_node_types', array()))) {
return;
}
$autotag_vids = _autotag_get_vids_for_type($form['type']['#value']);
if ($form['#id'] == 'node-form' && isset($form['taxonomy']) && count($autotag_vids)) {
$form['taxonomy'] += array(
'#type' => 'fieldset',
'#title' => t('Vocabularies'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#prefix' => '<div id="taxonomy-fieldset">',
'#suffix' => '</div>',
);
$form['taxonomy']['autotag'] = array(
'#prefix' => '<div id="taxonomy-autotag">',
'#suffix' => '</div>',
'#weight' => 1000,
);
$form['taxonomy']['autotag-message'] = array(
'#weight' => -1000,
'#value' => '<div id="autotag-message" class="messages status"></div>',
);
$form['taxonomy']['autotag']['taxonomyautotagbutton'] = array(
'#tree' => FALSE,
'#type' => 'button',
'#value' => t('Autotag'),
'#ahah' => array(
'path' => 'autotag/' . $form['type']['#value'] . '/' . $form['nid']['#value'],
'wrapper' => 'node-form',
'progress' => array(
'type' => 'bar',
'message' => t('Autotagging...'),
),
),
);
$form['taxonomy']['autotag']['dagger'] = array(
'#value' => '<p style="margin:0"><small>† ' . t('Only the marked vocabularies will use Autotag') . '</small>' . theme('advanced_help_topic', 'autotag', 'autotag') . '</p>',
);
foreach ($autotag_vids as $vid) {
$form['taxonomy'][$vid]['#title'] .= ' †';
}
$checkboxes = variable_get('autotag_save_checkbox', array());
if ($form_state['submitted'] && $form_state['clicked_button']['#submit'][0] == 'node_form_build_preview') {
$checkbox_default_value = $form_state['values']['taxonomyautotagcheckbox'];
}
else {
$checkbox_default_value = isset($checkboxes[$form['type']['#value']]) ? $checkboxes[$form['type']['#value']] : 0;
}
$form['taxonomy']['autotag']['taxonomyautotagcheckbox'] = array(
'#tree' => FALSE,
'#type' => 'checkbox',
'#title' => t('Autotag on save'),
'#description' => t('If checked, after clicking "Save", the node will be associated with all relevant terms - you will not be given the option to review these.'),
'#default_value' => $checkbox_default_value,
);
drupal_add_css(drupal_get_path('module', 'autotag') . '/autotag.css');
drupal_add_js(drupal_get_path('module', 'autotag') . '/autotag.js');
drupal_add_js(array(
'ahah' => array(
'edit-taxonomyautotagbutton' => array(
'submitextra' => 'autotag_submitextra',
),
),
), 'setting');
}
}
function autotag_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'presave':
$checkbox = variable_get('autotag_save_checkbox', array());
if (isset($node->taxonomyautotagcheckbox) && $node->taxonomyautotagcheckbox || $checkbox[$node->type] && !count($_POST)) {
module_load_include('ahah.inc', 'autotag');
if (count($_POST)) {
$data = $_POST;
}
else {
$data = $node;
}
$tids = autotag_search_form_array($data, $node->type);
if ($node->nid) {
$result = db_query('SELECT a.tid, vid FROM {autotag} a, {term_data} t WHERE nid = %d AND a.tid = t.tid', $node->nid);
while ($row = db_fetch_array($result)) {
$key = array_search($row, $tids);
if ($key !== FALSE) {
unset($tids[$key]);
}
}
}
foreach ($tids as $tid) {
if (is_array($node->taxonomy[$tid['vid']])) {
$node->taxonomy[$tid['vid']][$tid['tid']] = $tid['tid'];
}
else {
$node->taxonomy[$tid['vid']] = array(
$tid['tid'] => $tid['tid'],
);
}
}
}
break;
case 'update':
case 'insert':
if (isset($_SESSION['autotag_tids'][$node->form_build_id])) {
$result = db_query('SELECT n.tid, t.vid FROM {term_node} n, {term_data} t WHERE n.tid = t.tid AND nid = %d', $node->nid);
while ($row = db_fetch_array($result)) {
$key = array_search($row, $_SESSION['autotag_tids'][$node->form_build_id]);
if ($key !== FALSE) {
unset($_SESSION['autotag_tids'][$node->form_build_id][$key]);
}
}
db_query('DELETE FROM {autotag} WHERE nid = %d', $node->nid);
if (count($_SESSION['autotag_tids'][$node->form_build_id])) {
$tids_string = array();
foreach ($_SESSION['autotag_tids'][$node->form_build_id] as $tid) {
$tids_string[] = $node->nid . ', ' . $tid['tid'];
}
db_query('INSERT INTO {autotag} (nid, tid) VALUES (' . implode('),(', $tids_string) . ')');
}
}
else {
if (isset($node->taxonomyautotagcheckbox) && $node->taxonomyautotagcheckbox) {
db_query('DELETE FROM {autotag} WHERE tid IN (SELECT tid FROM {term_node} WHERE {autotag}.nid = nid AND {autotag}.tid = tid) AND nid IN (SELECT nid FROM {term_node} WHERE {autotag}.nid = nid AND {autotag}.tid = tid)');
}
}
break;
}
}
function _autotag_get_vids_for_type($type) {
static $static_vids;
if (isset($static_vids[$type])) {
return $static_vids[$type];
}
$vocabularies = taxonomy_get_vocabularies();
$vids = array();
foreach ($vocabularies as $vocabulary) {
if (in_array($type, $vocabulary->nodes) && $vocabulary->required == 0 && $vocabulary->multiple == 1) {
$vids[] = $vocabulary->vid;
}
}
$static_vids[$type] = $vids;
return $vids;
}
function autotag_menu() {
return array(
'autotag/%' => array(
'title' => 'Autotag callback',
'page callback' => 'autotag_callback',
'file' => 'autotag.ahah.inc',
'access arguments' => array(
1,
),
'access callback' => 'autotag_callback_access',
'page arguments' => array(
1,
),
'type' => MENU_CALLBACK,
),
'admin/settings/autotag' => array(
'title' => 'Autotag settings',
'description' => 'Change the default autotag settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'autotag_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'autotag.settings.inc',
),
);
}
function autotag_callback_access($content_type, $nid = FALSE) {
if ($nid) {
$node = node_load(array(
'nid' => $nid,
));
return node_access('update', $node);
}
else {
return node_access('create', $content_type);
}
}