You are here

function taxonomy_save_term in Drupal 6

Same name and namespace in other branches
  1. 4 modules/taxonomy.module \taxonomy_save_term()
  2. 5 modules/taxonomy/taxonomy.module \taxonomy_save_term()

Helper function for taxonomy_form_term_submit().

Parameters

$form_state['values']:

Return value

Status constant indicating if term was inserted or updated.

4 calls to taxonomy_save_term()
forum_form_submit in modules/forum/forum.admin.inc
Process forum form and container form submissions.
taxonomy_form_term_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to insert or update a term.
taxonomy_node_save in modules/taxonomy/taxonomy.module
Save term associations for a given node.
taxonomy_overview_terms_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler for terms overview form.

File

modules/taxonomy/taxonomy.module, line 306
Enables the organization of content into categories.

Code

function taxonomy_save_term(&$form_values) {
  $form_values += array(
    'description' => '',
    'weight' => 0,
  );
  if (!empty($form_values['tid']) && $form_values['name']) {
    drupal_write_record('term_data', $form_values, 'tid');
    $hook = 'update';
    $status = SAVED_UPDATED;
  }
  else {
    if (!empty($form_values['tid'])) {
      return taxonomy_del_term($form_values['tid']);
    }
    else {
      drupal_write_record('term_data', $form_values);
      $hook = 'insert';
      $status = SAVED_NEW;
    }
  }
  db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $form_values['tid'], $form_values['tid']);
  if (!empty($form_values['relations'])) {
    foreach ($form_values['relations'] as $related_id) {
      if ($related_id != 0) {
        db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $form_values['tid'], $related_id);
      }
    }
  }
  db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $form_values['tid']);
  if (!isset($form_values['parent']) || empty($form_values['parent'])) {
    $form_values['parent'] = array(
      0,
    );
  }
  if (is_array($form_values['parent'])) {
    foreach ($form_values['parent'] as $parent) {
      if (is_array($parent)) {
        foreach ($parent as $tid) {
          db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $tid);
        }
      }
      else {
        db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $parent);
      }
    }
  }
  else {
    db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $form_values['parent']);
  }
  db_query('DELETE FROM {term_synonym} WHERE tid = %d', $form_values['tid']);
  if (!empty($form_values['synonyms'])) {
    foreach (explode("\n", str_replace("\r", '', $form_values['synonyms'])) as $synonym) {
      if ($synonym) {
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $form_values['tid'], chop($synonym));
      }
    }
  }
  if (isset($hook)) {
    module_invoke_all('taxonomy', $hook, 'term', $form_values);
  }
  cache_clear_all();
  return $status;
}