You are here

function autotag_callback in Taxonomy Autotagger 6.2

Callback for ahah

1 string reference to 'autotag_callback'
autotag_menu in ./autotag.module
hook_menu

File

./autotag.ahah.inc, line 6

Code

function autotag_callback($content_type, $nid = FALSE) {

  // Here we process the form, and then return only the taxonomy fieldset to get
  // embeded back in to the form (but with shit selected).  We'll also add a
  // small message, to reassure the user that something has actually happened!
  $tids = autotag_search_form_array($_POST, $content_type);

  // Add every tid found to a session variable. The ones that are not present in
  // the term_node table once the node is saved, will be added to the autotag
  // table. We'll use the unique form_id to save the data
  if (!isset($_SESSION['autotag_tids'])) {
    $_SESSION['autotag_tids'] = array();
  }
  $_SESSION['autotag_tids'][$_POST['form_build_id']] = $tids;

  // Remove the tids that we don't want adding - we can only do this if we have
  // an nid
  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' => '',
    );
  }

  // Magic happens here because of the fact that the form data is in the $_POST
  // array, so we get back all the data in the form filled out!
  if (count($tids)) {
    $term_names = array();
    foreach ($tids as $tid) {
      $term = taxonomy_get_term($tid['tid']);
      $term_names[] = $term->name;

      // It's possible that the taxonomy array isn't set in $_POST, so we need
      // to set it first
      if (!isset($_POST['taxonomy'])) {
        $_POST['taxonomy'] = array();
      }
      if (!isset($_POST['taxonomy'][$tid['vid']])) {

        // We may be using batax here, lets check
        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 {

        // I'm guessing here, but it's likely that this is a result of Batax.
        // Perhaps we need some kind of hook function that allows a module that
        // is responsible for a taxonomy field to update that field with an
        // additional term.  For now though, we'll simply tag it on as we'd expect
        // batax to do.
        $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,
  ));

  // Clear all error messages.
  drupal_get_messages();
  exit;
}