You are here

function taxonomy_save_term in Drupal 5

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

Helper function for taxonomy_form_term_submit().

Parameters

$form_values:

Return value

Status constant indicating if term was inserted or updated.

3 calls to taxonomy_save_term()
forum_form_submit in modules/forum/forum.module
Process forum form and container form submissions.
taxonomy_form_term_submit in modules/taxonomy/taxonomy.module
Accept the form submission for a taxonomy term and save the result.
taxonomy_node_save in modules/taxonomy/taxonomy.module
Save term associations for a given node.

File

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

Code

function taxonomy_save_term(&$form_values) {
  if ($form_values['tid'] && $form_values['name']) {
    db_query("UPDATE {term_data} SET name = '%s', description = '%s', weight = %d WHERE tid = %d", $form_values['name'], $form_values['description'], $form_values['weight'], $form_values['tid']);
    $hook = 'update';
    $status = SAVED_UPDATED;
  }
  else {
    if ($form_values['tid']) {
      return taxonomy_del_term($form_values['tid']);
    }
    else {
      $form_values['tid'] = db_next_id('{term_data}_tid');
      db_query("INSERT INTO {term_data} (tid, name, description, vid, weight) VALUES (%d, '%s', '%s', %d, %d)", $form_values['tid'], $form_values['name'], $form_values['description'], $form_values['vid'], $form_values['weight']);
      $hook = 'insert';
      $status = SAVED_NEW;
    }
  }
  db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $form_values['tid'], $form_values['tid']);
  if ($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 ($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;
}