From d31c77513b14d3424474bf65d207e6b0f9b585ac Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Wed, 5 Nov 2008 12:47:23 +0000
Subject: [PATCH] - Patch #329140 by catch, pwolanin: improve consistency of
 vocabulary API and term API.

---
 modules/forum/forum.install         | 13 +++---
 modules/forum/forum.test            |  5 +--
 modules/taxonomy/taxonomy.admin.inc | 47 +++++++++----------
 modules/taxonomy/taxonomy.module    | 51 ++++++++++-----------
 modules/taxonomy/taxonomy.test      | 70 +++++++++++++----------------
 5 files changed, 89 insertions(+), 97 deletions(-)

diff --git a/modules/forum/forum.install b/modules/forum/forum.install
index b53b8f649664..d35dd91e6d6f 100644
--- a/modules/forum/forum.install
+++ b/modules/forum/forum.install
@@ -17,13 +17,13 @@ function forum_enable() {
     // vocabulary still exists. Keep all other node types intact there.
     $vocabulary = (array) $vocabulary;
     $vocabulary['nodes']['forum'] = 1;
-    taxonomy_save_vocabulary($vocabulary);
+    taxonomy_vocabulary_save($vocabulary);
   }
   else {
     // Create the forum vocabulary if it does not exist. Assign the vocabulary
     // a low weight so it will appear first in forum topic create and edit
     // forms.
-    $vocabulary = array(
+    $edit = array(
       'name' => t('Forums'),
       'multiple' => 0,
       'required' => 0,
@@ -33,9 +33,10 @@ function forum_enable() {
       'weight' => -10,
       'nodes' => array('forum' => 1),
     );
-    taxonomy_save_vocabulary($vocabulary);
+    $vocabulary = (object) $edit;
+    taxonomy_vocabulary_save($vocabulary);
 
-    variable_set('forum_nav_vocabulary', $vocabulary['vid']);
+    variable_set('forum_nav_vocabulary', $vocabulary->vid);
   }
 }
 
@@ -47,8 +48,8 @@ function forum_uninstall() {
   drupal_load('module', 'taxonomy');
 
   // Delete the vocabulary.
-  $vid = variable_get('forum_nav_vocabulary', '');
-  taxonomy_del_vocabulary($vid);
+  $vid = variable_get('forum_nav_vocabulary', 0);
+  taxonomy_vocabulary_delete($vid);
 
   db_query('DROP TABLE {forum}');
   variable_del('forum_containers');
diff --git a/modules/forum/forum.test b/modules/forum/forum.test
index acad2096a228..2dfb55f7bdec 100644
--- a/modules/forum/forum.test
+++ b/modules/forum/forum.test
@@ -143,10 +143,9 @@ class ForumTestCase extends DrupalWebTestCase {
     $this->assertEqual($current_settings->description, $description, t('The description was updated'));
 
     // Restore the original vocabulary.
-    $original_settings = (array) $original_settings;
-    taxonomy_save_vocabulary($original_settings);
+    taxonomy_vocabulary_save($original_settings);
     $current_settings = taxonomy_vocabulary_load($vid, TRUE);
-    $this->assertEqual($current_settings->name, $original_settings['name'], 'The original vocabulary settings were restored');
+    $this->assertEqual($current_settings->name, $original_settings->name, 'The original vocabulary settings were restored');
   }
 
   /**
diff --git a/modules/taxonomy/taxonomy.admin.inc b/modules/taxonomy/taxonomy.admin.inc
index 17b53e2b7149..0c26e7313e51 100644
--- a/modules/taxonomy/taxonomy.admin.inc
+++ b/modules/taxonomy/taxonomy.admin.inc
@@ -22,7 +22,7 @@ function taxonomy_overview_vocabularies() {
       $node_type = node_get_types('name', $type);
       $types[] = $node_type ? check_plain($node_type) : check_plain($type);
     }
-    $form[$vocabulary->vid]['#vocabulary'] = (array)$vocabulary;
+    $form[$vocabulary->vid]['#vocabulary'] = $vocabulary;
     $form[$vocabulary->vid]['name'] = array('#markup' => check_plain($vocabulary->name));
     $form[$vocabulary->vid]['types'] = array('#markup' => implode(', ', $types));
     $form[$vocabulary->vid]['weight'] = array('#type' => 'weight', '#delta' => 10, '#default_value' => $vocabulary->weight);
@@ -49,9 +49,9 @@ function taxonomy_overview_vocabularies() {
  */
 function taxonomy_overview_vocabularies_submit($form, &$form_state) {
   foreach ($form_state['values'] as $vid => $vocabulary) {
-    if (is_numeric($vid) && $form[$vid]['#vocabulary']['weight'] != $form_state['values'][$vid]['weight']) {
-      $form[$vid]['#vocabulary']['weight'] = $form_state['values'][$vid]['weight'];
-      taxonomy_save_vocabulary($form[$vid]['#vocabulary']);
+    if (is_numeric($vid) && $form[$vid]['#vocabulary']->weight != $form_state['values'][$vid]['weight']) {
+      $form[$vid]['#vocabulary']->weight = $form_state['values'][$vid]['weight'];
+      taxonomy_vocabulary_save($form[$vid]['#vocabulary']);
     }
   }
 }
@@ -210,18 +210,19 @@ function taxonomy_form_vocabulary_submit($form, &$form_state) {
   }
   // Fix up the nodes array to remove unchecked nodes.
   $form_state['values']['nodes'] = array_filter($form_state['values']['nodes']);
-  switch (taxonomy_save_vocabulary($form_state['values'])) {
+  $vocabulary = (object) $form_state['values'];
+  switch (taxonomy_vocabulary_save($vocabulary)) {
     case SAVED_NEW:
-      drupal_set_message(t('Created new vocabulary %name.', array('%name' => $form_state['values']['name'])));
-      watchdog('taxonomy', 'Created new vocabulary %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/' . $form_state['values']['vid']));
+      drupal_set_message(t('Created new vocabulary %name.', array('%name' => $vocabulary->name)));
+      watchdog('taxonomy', 'Created new vocabulary %name.', array('%name' => $vocabulary->name), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/' . $vocabulary->vid));
       break;
     case SAVED_UPDATED:
-      drupal_set_message(t('Updated vocabulary %name.', array('%name' => $form_state['values']['name'])));
-      watchdog('taxonomy', 'Updated vocabulary %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/' . $form_state['values']['vid']));
+      drupal_set_message(t('Updated vocabulary %name.', array('%name' => $vocabulary->name)));
+      watchdog('taxonomy', 'Updated vocabulary %name.', array('%name' => $vocabulary->name), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/' . $vocabulary->vid));
       break;
   }
 
-  $form_state['vid'] = $form_state['values']['vid'];
+  $form_state['vid'] = $vocabulary->vid;
   $form_state['redirect'] = 'admin/content/taxonomy';
   return;
 }
@@ -245,7 +246,7 @@ function taxonomy_overview_terms(&$form_state, $vocabulary) {
   }
 
   $form = array(
-    '#vocabulary' => (array)$vocabulary,
+    '#vocabulary' => $vocabulary,
     '#tree' => TRUE,
     '#parent_fields' => FALSE,
   );
@@ -452,7 +453,7 @@ function taxonomy_overview_terms_submit($form, &$form_state) {
   $hierarchy = 0; // Update the current hierarchy type as we go.
 
   $changed_terms = array();
-  $tree = taxonomy_get_tree($vocabulary['vid']);
+  $tree = taxonomy_get_tree($vocabulary->vid);
 
   if (empty($tree)) {
     return;
@@ -517,9 +518,9 @@ function taxonomy_overview_terms_submit($form, &$form_state) {
   }
 
   // Update the vocabulary hierarchy to flat or single hierarchy.
-  if ($vocabulary['hierarchy'] != $hierarchy) {
-    $vocabulary['hierarchy'] = $hierarchy;
-    taxonomy_save_vocabulary($vocabulary);
+  if ($vocabulary->hierarchy != $hierarchy) {
+    $vocabulary->hierarchy = $hierarchy;
+    taxonomy_vocabulary_save($vocabulary);
   }
 }
 
@@ -626,8 +627,8 @@ function taxonomy_form_term(&$form_state, $vocabulary, $edit = array()) {
   $parent = array_keys(taxonomy_get_parents($edit['tid']));
   $form['#term'] = $edit;
   $form['#term']['parent'] = $parent;
-  $form['#vocabulary'] = (array)$vocabulary;
-  $form['#vocabulary']['nodes'] = drupal_map_assoc($vocabulary->nodes);
+  $form['#vocabulary'] = $vocabulary;
+  $form['#vocabulary']->nodes = drupal_map_assoc($vocabulary->nodes);
 
   // Check for confirmation forms.
   if (isset($form_state['confirm_delete'])) {
@@ -741,7 +742,7 @@ function taxonomy_form_term_submit($form, &$form_state) {
     return;
   }
   // Rebuild the form to confirm enabling multiple parents.
-  elseif ($form_state['clicked_button']['#value'] == t('Save') && !$form['#vocabulary']['tags'] && count($form_state['values']['parent']) > 1 && $form['#vocabulary']['hierarchy'] < 2) {
+  elseif ($form_state['clicked_button']['#value'] == t('Save') && !$form['#vocabulary']->tags && count($form_state['values']['parent']) > 1 && $form['#vocabulary']->hierarchy < 2) {
     $form_state['rebuild'] = TRUE;
     $form_state['confirm_parents'] = TRUE;
     return;
@@ -759,7 +760,7 @@ function taxonomy_form_term_submit($form, &$form_state) {
       break;
   }
 
-  if (!$form['#vocabulary']['tags']) {
+  if (!$form['#vocabulary']->tags) {
     $current_parent_count = count($form_state['values']['parent']);
     $previous_parent_count = count($form['#term']['parent']);
     // Root doesn't count if it's the only parent.
@@ -775,9 +776,9 @@ function taxonomy_form_term_submit($form, &$form_state) {
     }
     // If we've increased the number of parents and this is a single or flat
     // hierarchy, update the vocabulary immediately.
-    elseif ($current_parent_count > $previous_parent_count && $form['#vocabulary']['hierarchy'] < 2) {
-      $form['#vocabulary']['hierarchy'] = $current_parent_count == 1 ? 1 : 2;
-      taxonomy_save_vocabulary($form['#vocabulary']);
+    elseif ($current_parent_count > $previous_parent_count && $form['#vocabulary']->hierarchy < 2) {
+      $form['#vocabulary']->hierarchy = $current_parent_count == 1 ? 1 : 2;
+      taxonomy_vocabulary_save($form['#vocabulary']);
     }
   }
 
@@ -871,7 +872,7 @@ function taxonomy_vocabulary_confirm_delete(&$form_state, $vid) {
  * @see taxonomy_vocabulary_confirm_delete()
  */
 function taxonomy_vocabulary_confirm_delete_submit($form, &$form_state) {
-  $status = taxonomy_del_vocabulary($form_state['values']['vid']);
+  $status = taxonomy_vocabulary_delete($form_state['values']['vid']);
   drupal_set_message(t('Deleted vocabulary %name.', array('%name' => $form_state['values']['name'])));
   watchdog('taxonomy', 'Deleted vocabulary %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
   $form_state['redirect'] = 'admin/content/taxonomy';
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 44c4c575fef6..79d4b89a2f1b 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -209,34 +209,31 @@ function taxonomy_admin_vocabulary_title_callback($vocabulary) {
 }
 
 /**
- * Save a vocabulary given form values or an equivalent array.
+ * Save a vocabulary given a vocabulary object..
  */
-function taxonomy_save_vocabulary(&$edit) {
-  $edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes'];
+function taxonomy_vocabulary_save($vocabulary) {
+  if (empty($vocabulary->nodes)) {
+    $vocabulary->nodes = array();
+  }
 
-  if (!isset($edit['module'])) {
-    $edit['module'] = 'taxonomy';
+  if (!isset($vocabulary->module)) {
+    $vocabulary->module = 'taxonomy';
   }
 
-  if (!empty($edit['vid']) && !empty($edit['name'])) {
-    drupal_write_record('vocabulary', $edit, 'vid');
-    db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $edit['vid']);
-    foreach ($edit['nodes'] as $type => $selected) {
-      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
+  if (!empty($vocabulary->vid) && !empty($vocabulary->name)) {
+    $status = drupal_write_record('vocabulary', $vocabulary, 'vid');
+    db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $vocabulary->vid);
+    foreach ($vocabulary->nodes as $type => $selected) {
+      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $vocabulary->vid, $type);
     }
-    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
-    $status = SAVED_UPDATED;
-  }
-  elseif (!empty($edit['vid'])) {
-    $status = taxonomy_del_vocabulary($edit['vid']);
+    module_invoke_all('taxonomy_vocabulary_update', $vocabulary);
   }
-  else {
-    drupal_write_record('vocabulary', $edit);
-    foreach ($edit['nodes'] as $type => $selected) {
-      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
+  elseif (empty($vocabulary->vid)) {
+    $status = drupal_write_record('vocabulary', $vocabulary);
+    foreach ($vocabulary->nodes as $type => $selected) {
+      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $vocabulary->vid, $type);
     }
-    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
-    $status = SAVED_NEW;
+    module_invoke_all('taxonomy_vocabulary_insert', $vocabulary);
   }
 
   cache_clear_all();
@@ -252,7 +249,7 @@ function taxonomy_save_vocabulary(&$edit) {
  * @return
  *   Constant indicating items were deleted.
  */
-function taxonomy_del_vocabulary($vid) {
+function taxonomy_vocabulary_delete($vid) {
   $vocabulary = (array) taxonomy_vocabulary_load($vid);
 
   db_query('DELETE FROM {vocabulary} WHERE vid = %d', $vid);
@@ -280,12 +277,12 @@ function taxonomy_del_vocabulary($vid) {
  * hieararchy of 2.
  *
  * @param $vocabulary
- *   An array of the vocabulary structure.
+ *   A vocabulary object.
  * @param $changed_term
  *   An array of the term structure that was updated.
  */
 function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) {
-  $tree = taxonomy_get_tree($vocabulary['vid']);
+  $tree = taxonomy_get_tree($vocabulary->vid);
   $hierarchy = 0;
   foreach ($tree as $term) {
     // Update the changed term with the new parent value before comparision.
@@ -302,9 +299,9 @@ function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) {
       $hierarchy = 1;
     }
   }
-  if ($hierarchy != $vocabulary['hierarchy']) {
-    $vocabulary['hierarchy'] = $hierarchy;
-    taxonomy_save_vocabulary($vocabulary);
+  if ($hierarchy != $vocabulary->hierarchy) {
+    $vocabulary->hierarchy = $hierarchy;
+    taxonomy_vocabulary_save($vocabulary);
   }
 
   return $hierarchy;
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index 17505f3da874..a80f46b3aa96 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -56,18 +56,18 @@ class TaxonomyVocabularyLoadTestCase extends DrupalWebTestCase {
   function testTaxonomyVocabularyLoadStaticReset() {
     // Load the first available vocabulary.
     $original_vocabulary = taxonomy_vocabulary_load(1);
+    $this->assertTrue(is_object($original_vocabulary));
 
     // Change the name and description.
-    $edit = (array) $original_vocabulary;
-    $edit['name'] = $this->randomName();
-    $edit['description'] = $this->randomName();
-    $this->assertNotEqual($edit['name'], $original_vocabulary->name);
-    taxonomy_save_vocabulary($edit);
+    $vocabulary = $original_vocabulary;
+    $vocabulary->name = $this->randomName();
+    $vocabulary->description = $this->randomName();
+    taxonomy_vocabulary_save($vocabulary);
 
     // Load the vocabulary with $reset TRUE.
     $new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid, TRUE);
-    $this->assertEqual($new_vocabulary->name, $edit['name']);
-    $this->assertEqual($new_vocabulary->name, $edit['name']);
+    $this->assertEqual($new_vocabulary->name, $vocabulary->name);
+    $this->assertEqual($new_vocabulary->name, $vocabulary->name);
   }
 }
 
@@ -120,7 +120,8 @@ class TaxonomyVocabularyFunctionsTestCase extends DrupalWebTestCase {
       $edit[$key] = $$key;
 
     // Exec save function.
-    taxonomy_save_vocabulary($edit);
+    $vocabulary = (object) $edit;
+    taxonomy_vocabulary_save($vocabulary);
     // After save we use $nodesBak.
     ksort($nodesBak);
     $edit['nodes'] = $nodesBak;
@@ -138,13 +139,8 @@ class TaxonomyVocabularyFunctionsTestCase extends DrupalWebTestCase {
       $this->assertEqual($value, $edit[$key], t('Checking value of @key.', array('@key' => $key)));
     }
 
-    // Delete vocabulary to avoid exception messages we create array with empty fields.
-    $deleteArray = array();
-    foreach($getEdit as $key => $v) {
-      $deleteArray[$key] = 0;
-    }
-    $deleteArray['vid'] = $vid;
-    taxonomy_save_vocabulary($deleteArray);
+    // Delete vocabulary.
+    taxonomy_vocabulary_delete($vid);
     // Checking if we deleted voc.
     $vocabularies = taxonomy_get_vocabularies();
     $vid = 0;
@@ -184,7 +180,9 @@ class TaxonomyTermFunctionsTestCase extends DrupalWebTestCase {
     $name = $this->randomName(20);
     $relation = 1;
     $edit['name'] = $name;
-    taxonomy_save_vocabulary($edit);
+    $vocabulary = (object) $edit;
+    taxonomy_vocabulary_save($vocabulary);
+    $edit['vid'] = $vocabulary->vid;
 
     // Create term.
     $termname = $this->randomName(20);
@@ -234,8 +232,7 @@ class TaxonomyTermFunctionsTestCase extends DrupalWebTestCase {
     }
 
     // Delete vocabulary.
-    $edit['name'] = 0;
-    taxonomy_save_vocabulary($edit);
+    taxonomy_vocabulary_delete($edit['vid']);
   }
 }
 
@@ -268,7 +265,9 @@ class TaxonomyTermSingleTestCase extends DrupalWebTestCase {
     $name = $this->randomName(20);
     $edit['hierarchy'] = 1;
     $edit['name'] = $name;
-    taxonomy_save_vocabulary($edit);
+    $vocabulary = (object) $edit;
+    taxonomy_vocabulary_save($vocabulary);
+    $edit['vid'] = $vocabulary->vid;
 
     // Create 1st term.
     $termname = $this->randomName(20);
@@ -291,8 +290,7 @@ class TaxonomyTermSingleTestCase extends DrupalWebTestCase {
     $this->assertEqual($children,$getChildren[$children->tid], t('Checking children.'));
 
     // Delete vocabulary.
-    $edit['name'] = 0;
-    taxonomy_save_vocabulary($edit);
+    taxonomy_vocabulary_delete($edit['vid']);
   }
 }
 
@@ -323,7 +321,9 @@ class TaxonomyTermMultipleTestCase extends DrupalWebTestCase {
     $name = $this->randomName(20);
     $edit['hierarchy'] = 1;
     $edit['name'] = $name;
-    taxonomy_save_vocabulary($edit);
+    $vocabulary = (object) $edit;
+    taxonomy_vocabulary_save($vocabulary);
+    $edit['vid'] = $vocabulary->vid;
 
     // Create 1st term.
     $parent = array();
@@ -354,8 +354,7 @@ class TaxonomyTermMultipleTestCase extends DrupalWebTestCase {
     }
 
     // Delete vocabulary.
-    $edit['name'] = 0;
-    taxonomy_save_vocabulary($edit);
+    taxonomy_vocabulary_delete($edit['vid']);
   }
 
 }
@@ -423,12 +422,13 @@ class TaxonomyTestNodeApiTestCase extends DrupalWebTestCase {
     }
 
     $name = $this->randomName(20);
-    $edit['hierarchy'] = 1;
-    $edit['multiple'] = 1;
-    $edit['name'] = $name;
-    $edit['nodes'] = array('article' => 'article');
-    taxonomy_save_vocabulary($edit);
-    $vid = $edit['vid']; // We need to persist vid after $edit is unset().
+    $vocabulary->hierarchy = 1;
+    $vocabulary->multiple = 1;
+    $vocabulary->name = $name;
+    $vocabulary->nodes = array('article' => 'article');
+    taxonomy_vocabulary_save($vocabulary);
+    $vid = $vocabulary->vid;
+
 
     $parent = array();
     $patternArray = array();
@@ -524,14 +524,8 @@ class TaxonomyTestNodeApiTestCase extends DrupalWebTestCase {
     $num_rows = db_result(db_query('SELECT COUNT(*) FROM {term_node} WHERE nid = %d', $node->nid));
     $this->assertEqual($num_rows, 0, t('Checking database field after deletion'));
 
-    // Delete vocabulary to avoid exception messages create array with empty fields.
-    $edit = array();
-    foreach($_t as $key ) {
-      $edit[$key] = 0;
-    }
-    $edit['name'] = 0;
-    $edit['vid'] = $vid;
-    taxonomy_save_vocabulary($edit);
+    // Delete vocabulary.
+    taxonomy_vocabulary_delete($vid);
   }
 }
 
-- 
GitLab