diff --git a/site_map.admin.inc b/site_map.admin.inc index 98c97aa..a6d6205 100644 --- a/site_map.admin.inc +++ b/site_map.admin.inc @@ -114,6 +114,12 @@ function site_map_admin_settings_form() { '#type' => 'fieldset', '#title' => t('Categories settings'), ); + $form['site_map_taxonomy_options']['site_map_count_children'] = array( + '#type' => 'checkbox', + '#title' => t('Counts node of children categories'), + '#default_value' => variable_get('site_map_count_children', 0), + '#description' => t('When enabled, this option will count the number of nodes of children terms in each taxonomy term.'), + ); $form['site_map_taxonomy_options']['site_map_show_count'] = array( '#type' => 'checkbox', '#title' => t('Show node counts by categories'), diff --git a/site_map.module b/site_map.module index fe1756e..af529f1 100644 --- a/site_map.module +++ b/site_map.module @@ -514,8 +514,10 @@ function _site_map_taxonomy_tree($vid, $name = NULL, $description = NULL) { // taxonomy_get_tree() honors access controls. $tree = taxonomy_get_tree($vid); + $count_children = variable_get('site_map_count_children'); foreach ($tree as $term) { - $term->count = site_map_taxonomy_term_count_nodes($term->tid); + //$term->count = site_map_taxonomy_term_count_nodes($term->tid); + $term->count = site_map_taxonomy_term_count_nodes($term->tid, $count_children); if ($term->count <= $threshold) { continue; } @@ -609,15 +611,39 @@ function _site_map_taxonomy_tree($vid, $name = NULL, $description = NULL) { * @return string * An integer representing a number of nodes. Results are statically cached. */ -function site_map_taxonomy_term_count_nodes($tid) { +function site_map_taxonomy_term_count_nodes($tid, $count_children) { + $tids = array($tid); + + if ($count_children){ + site_map_taxonomy_get_term_children_ids($tid, $tids); + } $query = db_select('taxonomy_index', 'ti'); $query->addExpression('COUNT(ti.nid)'); $count = $query - ->condition('ti.tid', $tid) + ->condition('ti.tid', $tids) ->execute()->fetchCol(); return $count[0]; } +/** + * Retrieve ids of term children . + * + * @param $tid + * The term's ID. + * @param $tids + * An array where ids of term children will be added + */ +function site_map_taxonomy_get_term_children_ids($tid, &$tids) { + $children = taxonomy_get_children($tid); + if (!empty($children)) + { + foreach($children as $child) + { + $tids[] = $child->tid; + site_map_taxonomy_get_term_children_ids($child->tid, $tids); + } + } +} /** * This is a clone of the core menu_tree_output() function with the exception