From da706aca6791752f649c4b9916eb16384a62f604 Mon Sep 17 00:00:00 2001
From: svendecabooter <svendecabooter@35369.no-reply.drupal.org>
Date: Wed, 7 Sep 2011 14:35:12 -0700
Subject: [PATCH] Issue #1104028 by svendecabooter, magicmyth, jessehs: Fixed Menu doesn't update when term is added or edited.

---
 taxonomy_menu.module |   57 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/taxonomy_menu.module b/taxonomy_menu.module
index 06f295d..fd7749d 100644
--- a/taxonomy_menu.module
+++ b/taxonomy_menu.module
@@ -70,8 +70,9 @@ function taxonomy_menu_form_alter(&$form, &$form_state, $form_id) {
     );
 
     //get taxonomy menu form options
-    //$form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($form['vid']['#value']);
+    $form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($form['vid']['#value']);
     //
+
     //rebuild the menu
     $form['taxonomy_menu']['options']['rebuild'] = array(
       '#type' => 'checkbox',
@@ -88,6 +89,10 @@ function taxonomy_menu_form_alter(&$form, &$form_state, $form_id) {
     $form['#submit'][] = 'taxonomy_menu_vocab_submit';
 
   }
+  else if ($form_id == "taxonomy_overview_terms") {
+    //TODO: saving this form should update the menu items associated with re-ordered terms
+    //$form['#submit'][] = 'taxonomy_menu_term_overview_submit';
+  }
 }
 
 /**
@@ -179,6 +184,16 @@ function taxonomy_menu_vocab_submit($form, &$form_state) {
 }
 
 /**
+ * Submit handler, reacting on taxonomy term overview page
+ */
+/*TODO: Re-ordering terms and saving should trigger the proper action without a clean rebuild.
+function taxonomy_menu_term_overview_submit($form, &$form_state) {
+  if (isset($form['#vocabulary']->vid)) {
+    _taxonomy_menu_rebuild($form['#vocabulary']->vid);
+  }
+}*/
+
+/**
  * rebuilds a menu
  *
  * @param $vid
@@ -294,7 +309,6 @@ function taxonomy_menu_taxonomy_term_insert($term) {
       'menu_name' => $menu_name,
     );
     $message = t('Term %term has been added to taxonomy menu %menu_name.', array('%term' => $term->name, '%menu_name' => $menu_name));
-
     // Run function.
     taxonomy_menu_handler('insert', $item);
     // Report status.
@@ -308,7 +322,7 @@ function taxonomy_menu_taxonomy_term_insert($term) {
 /**
  * Implementation of hook_taxonomy_term_update().
  */
-function taxonomy_menu_taxonomy_menu_term_update($term) {
+function taxonomy_menu_taxonomy_term_update($term) {
   // Only sync if taxonomy_menu is enabled for this vocab and the 'sync'
   // option has been checked.
   $menu_name = variable_get('taxonomy_menu_vocab_menu_' . $term->vid, 0);
@@ -321,8 +335,6 @@ function taxonomy_menu_taxonomy_menu_term_update($term) {
       'mlid' => _taxonomy_menu_get_mlid($term->tid, $term->vid),
     );
     $message = t('Term %term has been updated in taxonomy menu %menu_name.', array('%term' => $term->name, '%menu_name' => $menu_name));
-    break;
-
     // run function
     taxonomy_menu_handler('update', $item);
     // report status
@@ -738,7 +750,6 @@ function taxonomy_menu_taxonomy_menu_delete(&$item) {
  *  The node object.
  */
 function _taxonomy_menu_create_item($args = array(), $node) {
-
   //if tid = 0 then we are creating a vocab item
 
   if (@$args['tid'] == 0 && variable_get('taxonomy_menu_voc_item_'. @$args['vid'], 0)) {
@@ -759,23 +770,32 @@ function _taxonomy_menu_create_item($args = array(), $node) {
   else {
     $term = $args['term'];
   }
-
   // get the first parent
-  if (is_array($term->parents)) {
-    foreach ($term->parents as $key => $val) {
-      $ptid = $val;
+  // Sometimes $term->parents is not set so we find it.
+  if (!isset($term->parent)) {
+    $parents = taxonomy_get_parents($term->tid);
+    foreach ($parents as $index => $parent) {
+      $term->parent = array($index);
       break;
     }
   }
-  else {
-    $ptid = $term->parents;
+  if (isset($term->parent)) {
+    if (is_array($term->parent)) {
+      foreach ($term->parent as $parent) {
+        if (!empty($parent)) {
+          $ptid = $parent;
+          break;
+        }
+      }
+    }
+    else {
+      $ptid = $term->parent ? $term->parent : 0;
+    }
   }
-
-  //if ptid is empty, then set it to 0
-  if (empty($ptid)) {
+  // If ptid is empty, then set it to 0.
+  else {
     $ptid = 0;
   }
-
   // turn the term into the correct $item array form
   $item = array(
     'tid' => $term->tid,
@@ -791,7 +811,6 @@ function _taxonomy_menu_create_item($args = array(), $node) {
   if (isset($args['mlid'])) {
     $item['mlid'] = $args['mlid'];
   }
-
   return $item;
 }
 
@@ -954,7 +973,7 @@ function taxonomy_menu_taxonomy_menu_options() {
     'default' => TRUE,
   );
 
-  $options['display_num'] = array(
+  /*$options['display_num'] = array(
     '#title' => t('Display number of items'),
     '#description' => t('Display the number of items per taxonomy terms. Will not show up for vocabulary menu items.'),
     'default' => FALSE,
@@ -996,7 +1015,7 @@ function taxonomy_menu_taxonomy_menu_options() {
     '#title' => t("Use 'all' at the end of URL"),
     'default' => FALSE,
     '#description' => t('This changes tid+tid+tid to "All" in term when <em>Display descendants</em> has been selected.<br />Only used if <em>Menu path type</em> is "Default path".<br />Works with default taxonomy page.'),
-  );
+  );*/
 
   return $options;
 }
-- 
1.7.1

