From 2bc3b99bc71dde756279c7e666348f09b7e20392 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 5 Nov 2008 14:08:11 +0000 Subject: [PATCH] - Patch #314147 by catch: more taxonomy standardization. --- modules/forum/forum.admin.inc | 5 +- modules/simpletest/tests/taxonomy_test.test | 2 +- modules/taxonomy/taxonomy.admin.inc | 15 ++--- modules/taxonomy/taxonomy.module | 61 +++++++++------------ modules/taxonomy/taxonomy.test | 36 ++++++++---- 5 files changed, 62 insertions(+), 57 deletions(-) diff --git a/modules/forum/forum.admin.inc b/modules/forum/forum.admin.inc index e8c8ad05074..d004920d73f 100644 --- a/modules/forum/forum.admin.inc +++ b/modules/forum/forum.admin.inc @@ -79,12 +79,13 @@ function forum_form_submit($form, &$form_state) { $type = t('forum'); } - $status = taxonomy_save_term($form_state['values']); + $term = (object) $form_state['values']; + $status = taxonomy_term_save($term); switch ($status) { case SAVED_NEW: if ($container) { $containers = variable_get('forum_containers', array()); - $containers[] = $form_state['values']['tid']; + $containers[] = $term->tid; variable_set('forum_containers', $containers); } drupal_set_message(t('Created new @type %term.', array('%term' => $form_state['values']['name'], '@type' => $type))); diff --git a/modules/simpletest/tests/taxonomy_test.test b/modules/simpletest/tests/taxonomy_test.test index 469c3e693f9..27dfe308be7 100644 --- a/modules/simpletest/tests/taxonomy_test.test +++ b/modules/simpletest/tests/taxonomy_test.test @@ -52,7 +52,7 @@ class TaxonomyHooksTestCase extends DrupalWebTestCase { $this->assertTrue(in_array($edit['antonyms'], $term->antonyms), t('Antonym was successfully edited')); // Delete the term. - taxonomy_del_term($term->tid); + taxonomy_term_delete($term->tid); $antonyms = db_query('SELECT taid FROM {term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField(); $this->assertFalse($antonyms, t('The antonyms were deleted from the database.')); } diff --git a/modules/taxonomy/taxonomy.admin.inc b/modules/taxonomy/taxonomy.admin.inc index 0c26e7313e5..2deb4eebafe 100644 --- a/modules/taxonomy/taxonomy.admin.inc +++ b/modules/taxonomy/taxonomy.admin.inc @@ -514,7 +514,7 @@ function taxonomy_overview_terms_submit($form, &$form_state) { // Save all updated terms. foreach ($changed_terms as $term) { - taxonomy_save_term($term); + taxonomy_term_save($term); } // Update the vocabulary hierarchy to flat or single hierarchy. @@ -748,15 +748,16 @@ function taxonomy_form_term_submit($form, &$form_state) { return; } - $status = taxonomy_save_term($form_state['values']); + $term = (object) $form_state['values']; + $status = taxonomy_term_save($term); switch ($status) { case SAVED_NEW: - drupal_set_message(t('Created new term %term.', array('%term' => $form_state['values']['name']))); - watchdog('taxonomy', 'Created new term %term.', array('%term' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'taxonomy/term/' . $form_state['values']['tid'] . '/edit')); + drupal_set_message(t('Created new term %term.', array('%term' => $term->name))); + watchdog('taxonomy', 'Created new term %term.', array('%term' => $term->name), WATCHDOG_NOTICE, l(t('edit'), 'taxonomy/term/' . $term->tid . '/edit')); break; case SAVED_UPDATED: - drupal_set_message(t('Updated term %term.', array('%term' => $form_state['values']['name']))); - watchdog('taxonomy', 'Updated term %term.', array('%term' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'taxonomy/term/' . $form_state['values']['tid'] . '/edit')); + drupal_set_message(t('Updated term %term.', array('%term' => $term->name))); + watchdog('taxonomy', 'Updated term %term.', array('%term' => $term->name), WATCHDOG_NOTICE, l(t('edit'), 'taxonomy/term/' . $term->tid . '/edit')); break; } @@ -782,7 +783,7 @@ function taxonomy_form_term_submit($form, &$form_state) { } } - $form_state['tid'] = $form_state['values']['tid']; + $form_state['tid'] = $term->tid; $form_state['redirect'] = 'admin/content/taxonomy'; return; } diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index 79d4b89a2f1..5f61fc8632c 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -256,7 +256,7 @@ function taxonomy_vocabulary_delete($vid) { db_query('DELETE FROM {vocabulary_node_types} WHERE vid = %d', $vid); $result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid); while ($term = db_fetch_object($result)) { - taxonomy_del_term($term->tid); + taxonomy_term_delete($term->tid); } module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary); @@ -308,67 +308,58 @@ function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) { } /** - * Helper function for taxonomy_form_term_submit(). + * Save a term object to the database. * - * @param $form_state['values'] + * @param $term + * A term object. * @return * Status constant indicating if term was inserted or updated. */ -function taxonomy_save_term(&$form_values) { - $form_values += array( - 'description' => '', - 'weight' => 0 - ); +function taxonomy_term_save($term) { - $term = (object) $form_values; - - if (!empty($form_values['tid']) && $form_values['name']) { - $status = drupal_write_record('term_data', $form_values, 'tid'); + if (!empty($term->tid) && $term->name) { + $status = drupal_write_record('term_data', $term, 'tid'); module_invoke_all('taxonomy_term_insert', $term); } - elseif (!empty($form_values['tid'])) { - return taxonomy_del_term($form_values['tid']); - } else { - $status = drupal_write_record('term_data', $form_values); - $term->tid = $form_values['tid']; + $status = drupal_write_record('term_data', $term); module_invoke_all('taxonomy_term_update', $term); } + db_delete('term_relation')->condition('tid1', $term->tid, 'OR')->condition('tid2', $term->tid)->execute(); - 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 (!empty($term->relations)) { + foreach ($term->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_insert('term_relation')->fields(array('tid1' => $term->tid, 'tid2' => $related_id))->execute(); } } } + db_delete('term_hierarchy')->condition('tid', $term->tid)->execute(); - 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 (!isset($term->parent) || empty($term->parent)) { + $term->parent = array(0); } - if (is_array($form_values['parent'])) { - foreach ($form_values['parent'] as $parent) { + if (is_array($term->parent)) { + foreach ($term->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); + db_insert('term_hierarchy')->fields(array('tid' => $term->tid, 'parent' => $tid))->execute(); } } else { - db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $parent); + db_insert('term_hierarchy')->fields(array('tid' => $term->tid, 'parent' => $parent))->execute(); } } } else { - db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $form_values['parent']); + db_insert('term_hierarchy')->field(array('tid' => $term_tid, 'parent' => $term->parent))->execute(); } - 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) { + db_delete('term_synonym')->condition('tid', $term->tid)->execute(); + if (!empty($term->synonyms)) { + foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) { if ($synonym) { - db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $form_values['tid'], chop($synonym)); + db_insert('term_synonym')->fields(array('tid' => $term->tid, 'name' => rtrim($synonym)))->execute(); } } } @@ -386,7 +377,7 @@ function taxonomy_save_term(&$form_values) { * @return * Status constant indicating deletion. */ -function taxonomy_del_term($tid) { +function taxonomy_term_delete($tid) { $tids = array($tid); while ($tids) { $children_tids = $orphans = array(); @@ -685,7 +676,7 @@ function taxonomy_node_save($node, $terms) { if (!$typed_term_tid) { $edit = array('vid' => $vid, 'name' => $typed_term); - $status = taxonomy_save_term($edit); + $status = taxonomy_term_save($edit); $typed_term_tid = $edit['tid']; } diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test index a80f46b3aa9..08f98dbb20b 100644 --- a/modules/taxonomy/taxonomy.test +++ b/modules/taxonomy/taxonomy.test @@ -195,7 +195,8 @@ class TaxonomyTermFunctionsTestCase extends DrupalWebTestCase { } $termsyn = implode("\n", $synonyms); $data = array('name' => $termname, 'description' => $termdesc, 'weight' => $termweight, 'synonyms' => $termsyn, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); // Retrieve term and check all fields. $_tArray = taxonomy_get_term_by_name($termname); @@ -213,7 +214,8 @@ class TaxonomyTermFunctionsTestCase extends DrupalWebTestCase { $relations[] = $staryTid; $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => 0, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => array($staryTid)); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $getTerm = $_tArray[0]; $relations[] = $getTerm->tid; @@ -221,7 +223,8 @@ class TaxonomyTermFunctionsTestCase extends DrupalWebTestCase { // Creating another term related to 2 terms above. $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => 0, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => array($staryTid, $getTerm->tid)); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $getTerm = $_tArray[0]; @@ -272,14 +275,16 @@ class TaxonomyTermSingleTestCase extends DrupalWebTestCase { // Create 1st term. $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $parent = $_tArray[0]; // Create 2nd term as a child. $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0, 'parent' => array($parent->tid)); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $children = $_tArray[0]; @@ -329,21 +334,24 @@ class TaxonomyTermMultipleTestCase extends DrupalWebTestCase { $parent = array(); $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $parent[] = $_tArray[0]->tid; // Create 2nd term. $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $parent[] = $_tArray[0]->tid; // Create 3rd term as a child. $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0, 'parent' => array($parent)); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $children = $_tArray[0]; @@ -436,7 +444,8 @@ class TaxonomyTestNodeApiTestCase extends DrupalWebTestCase { // Create 1st term. $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $parent[$_tArray[0]->tid] = $_tArray[0]->tid; $patternArray['term name 1'] = $termname; @@ -444,7 +453,8 @@ class TaxonomyTestNodeApiTestCase extends DrupalWebTestCase { // Create 2nd term. $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $parent[$_tArray[0]->tid] = $_tArray[0]->tid; $patternArray['term name 2'] = $termname; @@ -485,7 +495,8 @@ class TaxonomyTestNodeApiTestCase extends DrupalWebTestCase { // create 1st term $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $parent[] = $_tArray[0]->tid; $patternArray['term name 2'] = $termname; @@ -493,7 +504,8 @@ class TaxonomyTestNodeApiTestCase extends DrupalWebTestCase { // create 2nd term $termname = $this->randomName(20); $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0); - taxonomy_save_term($data); + $term = (object) $data; + taxonomy_term_save($term); $_tArray = taxonomy_get_term_by_name($termname); $parent[] = $_tArray[0]->tid; $patternArray['term name 3'] = $termname;