diff --git a/taxonomy_menu.database.inc b/taxonomy_menu.database.inc index 0b785b6..8613312 100644 --- a/taxonomy_menu.database.inc +++ b/taxonomy_menu.database.inc @@ -135,11 +135,11 @@ function _taxonomy_menu_delete_item($vid, $tid) { * @return array of $tid */ function _taxonomy_menu_get_terms($vid) { - $result = db_select('taxonomy_term_data', 'td') - ->condition('vid', $vid) - ->addField('td', 'tid') - ->execute(); - return $result->fetchAll(); + $query = db_select('taxonomy_term_data', 'td'); + $query->condition('vid', $vid); + $query->addField('td', 'tid'); + $result = $query->execute()->fetchAll(); + return $result; } /** @@ -149,14 +149,14 @@ function _taxonomy_menu_get_terms($vid) { * * @param $tid */ -/*function _taxonomy_menu_term_count($tid) { - $result = db_select('term_node', 'tn'); - $result->condition('tid', $tid); - $result->join('node', 'n', 'n.nid = tn.nid AND n.status = 1'); - $result->addExpression('COUNT(n.nid)', 'term_count'); - $result->execute(); - return $result->fetchField(); -}*/ +function _taxonomy_menu_term_count($tid) { + $query = db_select('taxonomy_index', 'tn'); + $query->condition('tid', $tid); + $query->join('node', 'n', 'n.nid = tn.nid AND n.status = 1'); + $query->addExpression('COUNT(n.nid)', 'term_count'); + $result = $query->execute()->fetchField(); + return $result; +} /** * Get tid for a given mlid diff --git a/taxonomy_menu.module b/taxonomy_menu.module index 87cd626..04ffec7 100644 --- a/taxonomy_menu.module +++ b/taxonomy_menu.module @@ -70,8 +70,14 @@ 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']); - // + if(isset($form['vid']) && $form['vid']['#value']) { + $vid = $form['vid']['#value']; + } + else { + $vid = 0; + } + $form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($vid); + //rebuild the menu $form['taxonomy_menu']['options']['rebuild'] = array( '#type' => 'checkbox', @@ -474,6 +480,35 @@ function _taxonomy_menu_nodeapi_helper($op, $terms = array(), $node) { $op = 'update'; } taxonomy_menu_handler($op, $args, $node); + if (variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $term->vid), FALSE)) { + _taxonomy_menu_update_all_parents($term, $menu_name); + } + } + } +} + +/** + * Update all parent items + * + * @param $op + * options are 'insert', 'update', 'delete' or path + * + * @param $node + * The node object. + */ +function _taxonomy_menu_update_all_parents($term, $menu_name) { + $parents = taxonomy_get_parents($term->tid); + if ($parents) { + foreach ($parents as $parent) { + $parent->parents = array_keys(taxonomy_get_parents($parent->tid)); + $item = array( + 'term' => $parent, + 'menu_name' => $menu_name, + 'mlid' => _taxonomy_menu_get_mlid($parent->tid, $parent->vid), + 'remove' => FALSE, + ); + taxonomy_menu_handler('update', $item); + _taxonomy_menu_update_all_parents($parent, $menu_name); } } } @@ -612,9 +647,8 @@ function _taxonomy_menu_save($item) { } // If remove is true then set hidden to 1 - if (isset($item['remove']) && $item['remove']) { - $link['hidden'] = 1; - } + $link['hidden'] = (isset($item['remove']) && $item['remove']) ? 1 : 0; + // Save the menu item if ($mlid = menu_link_save($link)) { //if inserting a new menu item then insert a record into the table @@ -797,21 +831,21 @@ function _taxonomy_menu_create_item($args = array(), $node) { /** * Helper function to see if any of the children have any nodes * - * @TODO Needs updating since terms are associated to nodes via fields - * * @param $tid * @param $vid * @return boolean - * -function _taxonomy_menu_children_has_nodes($tid, $vid) { + */ +function _taxonomy_menu_children_has_nodes($tid, $vid, $return = FALSE) { $children = taxonomy_get_children($tid, $vid); foreach ($children as $tid => $term) { if (_taxonomy_menu_term_count($tid) > 0) { - return FALSE; + $return = TRUE; + } else { + $return = _taxonomy_menu_children_has_nodes($tid, $vid, $return); } } - return TRUE; -}*/ + return $return; +} /** * Helper function for insert and update hooks @@ -822,15 +856,12 @@ function _taxonomy_menu_item($item) { //if tid is 0 then do not chagne any settings if ($item['tid'] > 0) { //get the number of node attached to this term - // @TODO Needs Updating - //$num = _taxonomy_menu_term_count($item['tid']); + $num = _taxonomy_menu_term_count($item['tid']); //if hide menu is selected and the term count is 0 and the term has no children then do not create the menu item - // @TODO Needs updating - /* if ($num == 0 && variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $item['vid']), FALSE) && - _taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) { + !_taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) { $item['remove'] = TRUE; return $item; @@ -840,10 +871,10 @@ function _taxonomy_menu_item($item) { if (variable_get(_taxonomy_menu_build_variable('display_num', $item['vid']), FALSE)) { //if number > 0 and display decendants, then count all of the children if (variable_get(_taxonomy_menu_build_variable('display_descendants', $item['vid']), FALSE)) { - $num = taxonomy_term_count_nodes($item['tid']); + $num = taxonomy_menu_term_count_nodes($item['tid'], $item['vid']); } $item['name'] .= " ($num)"; - }*/ + } } elseif ($item['tid'] == 0) { //if custom name is provided, use that name $custom_name = variable_get(_taxonomy_menu_build_variable('voc_name',$item['vid']), ''); @@ -855,6 +886,23 @@ function _taxonomy_menu_item($item) { return $item; } + +/** + * Count the number of nodes linked to the term and all children + * @param $tid + * @param $vid + * @return integer + */ +function taxonomy_menu_term_count_nodes ($tid, $vid, $count = 0) { + $count += _taxonomy_menu_term_count($tid); + $children = taxonomy_get_children($tid, $vid); + foreach ($children as $tid => $term) { + $count = taxonomy_menu_term_count_nodes($term->tid, $term->vid, $count); + } + return $count; +} + + /** * Implementation of hook_taxonomy_menu_insert(). * @@ -931,7 +979,13 @@ function _taxonomy_menu_create_options($vid) { function _taxonomy_menu_build_variable($name, $vid) { $vocabulary = taxonomy_vocabulary_load($vid); - return 'taxonomy_menu_' . $name . '_' . $vocabulary->machine_name; + if($vocabulary) { + return 'taxonomy_menu_' . $name . '_' . $vocabulary->machine_name; + } + else { + return false; + } + } /** @@ -967,7 +1021,8 @@ function taxonomy_menu_taxonomy_menu_options() { $options['voc_item'] = array( '#title' => t('Add item for vocabulary'), '#description' => t('Shows the vocabulary name as the top level menu item of the taxonomy menu.'), - 'default' => TRUE, + 'default' => FALSE, + '#disabled' => TRUE, ); $options['expanded'] = array( @@ -981,9 +1036,9 @@ function taxonomy_menu_taxonomy_menu_options() { '#title' => t('Custom name for vocabulary item'), '#description' => t('Changes the name of the vocabulary item (if enabled above). Leave blank to use the name of the vocabulary.'), 'default' => '', + '#disabled' => TRUE, ); - $options['display_descendants'] = array( '#title' => t('Display descendants'), '#description' => t('Changes the default path to taxonomy/term/tid+tid+tid for all terms thave have child terms.'), @@ -994,6 +1049,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 Display descendants has been selected.
Only used if Menu path type is "Default path".
Works with default taxonomy page.'), + '#disabled' => TRUE, ); return $options; @@ -1019,21 +1075,20 @@ function taxonomy_menu_translated_menu_link_alter(&$item, $map) { $display_num = ''; - /* TODO: Needs to be updated, see _taxonomy_menu_item(). $num = _taxonomy_menu_term_count($t->tid); // If hide menu is selected and the term count is 0 and the term has no children then do not create the menu item - if ($num == 0 && variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $t['vid']), FALSE) && _taxonomy_menu_children_has_nodes($t->tid, $t->vid)) { + if ($num == 0 && variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $t->vid), FALSE) && !_taxonomy_menu_children_has_nodes($t->tid, $t->vid)) { $display_num = ''; } // if display number is selected and $num > 0 then change the title - else if (variable_get(_taxonomy_menu_build_variable('display_num', $t['vid']), FALSE)) { + else if (variable_get(_taxonomy_menu_build_variable('display_num', $t->vid), FALSE)) { // if number > 0 and display decendants, then count all of the children - if (variable_get(_taxonomy_menu_build_variable('display_descendants', $t['vid']), FALSE)) { - $num = taxonomy_term_count_nodes($t->tid); + if (variable_get(_taxonomy_menu_build_variable('display_descendants', $t->vid), FALSE)) { + $num = taxonomy_menu_term_count_nodes($t->tid, $t->vid); } $display_num = " ($num)"; - }*/ + } if ($item['title'] != ($term->name . $display_num)) { // Should not happen