| 1 |
<?php
|
| 2 |
/**
|
| 3 |
*Views a vocabulary index page
|
| 4 |
*/
|
| 5 |
function vocabindex_view_page($path)
|
| 6 |
{
|
| 7 |
$vid=db_result(db_query("SELECT vid FROM {vocabindex} WHERE path='%s'", $path));
|
| 8 |
$list_style=variable_get('vocabindex_list_style', 'threaded');
|
| 9 |
$sort_by=variable_get('vocabindex_terms_order', 'weight');
|
| 10 |
|
| 11 |
$list_tmp=taxonomy_get_tree($vid);
|
| 12 |
//The eventual list. The different types of keys are used for sorting
|
| 13 |
$list=array();
|
| 14 |
if($sort_by=='weight')
|
| 15 |
{
|
| 16 |
foreach($list_tmp as $term)
|
| 17 |
{
|
| 18 |
//Sort by weight. Use the name as well to prevent duplicate keys and to sort by name after the terms have been sorted by weight
|
| 19 |
$list[$term->weight.$term->name]=$term;
|
| 20 |
}
|
| 21 |
}
|
| 22 |
else if($sort_by=='name')
|
| 23 |
{
|
| 24 |
foreach($list_tmp as $term)
|
| 25 |
{
|
| 26 |
//Sort by name
|
| 27 |
$list[$term->name]=$term;
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
//Check list-style and call the right function
|
| 32 |
if($list_style!='threaded')
|
| 33 |
{
|
| 34 |
$output=vocabindex_render_list_flat($list, $sort_by, $list_style);
|
| 35 |
}
|
| 36 |
else
|
| 37 |
{
|
| 38 |
$output=vocabindex_render_list_threaded($list, $sort_by);
|
| 39 |
}
|
| 40 |
|
| 41 |
if($output)
|
| 42 |
{
|
| 43 |
//And render the top list and page
|
| 44 |
$output=theme('vocabindex_list', $output, $list_style);
|
| 45 |
$vocab=taxonomy_vocabulary_load($vid);
|
| 46 |
$output=theme('vocabindex_page', $vocab->description, $output);
|
| 47 |
}
|
| 48 |
else
|
| 49 |
{
|
| 50 |
$output=t('There are no categories to display.');
|
| 51 |
}
|
| 52 |
|
| 53 |
//Only add the stylesheet if the site administrator wants it
|
| 54 |
if(variable_get('vocabindex_stylesheet', TRUE))
|
| 55 |
{
|
| 56 |
drupal_add_css('modules/vocabindex/vocabindex-style.css', 'module', 'screen', FALSE);
|
| 57 |
}
|
| 58 |
|
| 59 |
return $output;
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
*Function to render a flat list
|
| 64 |
*/
|
| 65 |
function vocabindex_render_list_flat($list, $sort_by, $list_style)
|
| 66 |
{
|
| 67 |
ksort($list);
|
| 68 |
|
| 69 |
if($list_style=='flat-toplevel')
|
| 70 |
{
|
| 71 |
//Filter $list so only terms with $term->parents[0]==0 will be kept
|
| 72 |
$list_tmp=array();
|
| 73 |
foreach($list as $key => $term)
|
| 74 |
{
|
| 75 |
if($term->parents[0]==0)
|
| 76 |
{
|
| 77 |
$list_tmp[$key]=$term;
|
| 78 |
}
|
| 79 |
}
|
| 80 |
$list=$list_tmp;
|
| 81 |
}
|
| 82 |
|
| 83 |
//Loop through all the terms from the list and render them
|
| 84 |
$i=1;
|
| 85 |
foreach($list as $term)
|
| 86 |
{
|
| 87 |
$zebra=($i%2==0?'even':'odd');
|
| 88 |
$url=url('taxonomy/term/'.$term->tid);
|
| 89 |
$output.=theme('vocabindex_list_item', $url, $term->name, $term->description, $zebra);
|
| 90 |
$i++;
|
| 91 |
}
|
| 92 |
|
| 93 |
return $output;
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
*Function to render a threaded list
|
| 98 |
*/
|
| 99 |
function vocabindex_render_list_threaded($list, $sort_by)
|
| 100 |
{
|
| 101 |
//A list of terms that are parents
|
| 102 |
$parents=array();
|
| 103 |
//A list of the depths all terms are at.
|
| 104 |
$depths=array();
|
| 105 |
//Maximum depth.
|
| 106 |
$max_depth=0;
|
| 107 |
|
| 108 |
//Set up all the arrays necessary for rendering the eventual list
|
| 109 |
foreach($list as $term)
|
| 110 |
{
|
| 111 |
foreach($term->parents as $parent)
|
| 112 |
{
|
| 113 |
//Build up a list of terms that are parents
|
| 114 |
$parents[$parent][]=$term->tid;
|
| 115 |
|
| 116 |
//Set up an array with all the depths so we can render the list from the inside out later on
|
| 117 |
$depths[$term->depth][]=$term;
|
| 118 |
if($term->depth>$max_depth)
|
| 119 |
{
|
| 120 |
//And to know where to start rendering we need to know the maximum depth
|
| 121 |
$max_depth=$term->depth;
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
}
|
| 126 |
|
| 127 |
//Render the list from the inside out
|
| 128 |
$rendered_terms=array();
|
| 129 |
for($i=$max_depth; $i>=0; $i--)
|
| 130 |
{
|
| 131 |
//Loop through all the terms at this depth
|
| 132 |
foreach($depths[$i] as $term)
|
| 133 |
{
|
| 134 |
//Check if term is a parent
|
| 135 |
if($parents[$term->tid])
|
| 136 |
{
|
| 137 |
//The term is a parent. Because we render inside out all the children have been rendered already and can simply be put together
|
| 138 |
foreach($parents[$term->tid] as $child)
|
| 139 |
{
|
| 140 |
print_r($child);
|
| 141 |
$children.=$rendered_terms[$child];
|
| 142 |
}
|
| 143 |
$children=theme('vocabindex_list', $children);
|
| 144 |
}
|
| 145 |
|
| 146 |
//Render term
|
| 147 |
$url=url('taxonomy/term/'.$term->tid);
|
| 148 |
$rendered_terms[$term->tid]=theme('vocabindex_list_item', $url, $term->name, $term->description, NULL, $children);
|
| 149 |
|
| 150 |
//And clear all children
|
| 151 |
$children=NULL;
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
//Put the top-level terms together
|
| 156 |
foreach($depths[0] as $term)
|
| 157 |
{
|
| 158 |
$output.=$rendered_terms[$term->tid];
|
| 159 |
}
|
| 160 |
|
| 161 |
return $output;
|
| 162 |
}
|