-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaxonomy_node_tree.module
executable file
·389 lines (319 loc) · 13.3 KB
/
taxonomy_node_tree.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/* Taxonomy Node Tree Module */
/* Builds a tree based on taxonomy structure with the nodes for each term embedded */
/* A MFB Joint */
/* 07/27/10 */
/*
Ensure max depth setting works
Ensure starting at a specific term works
test term & content filters
*/
/* Implementation of hook_init() */
function taxonomy_node_tree_init() {
if(variable_get('use_accordion', 1)){
drupal_add_js(drupal_get_path('module', 'taxonomy_node_tree') . '/js/taxonomy_node_tree_accordion.js');
}
if(variable_get('use_taxonomy_node_tree_css', 1)){
drupal_add_css(drupal_get_path('module', 'taxonomy_node_tree') . '/css/taxonomy_node_tree.css');
}
}
/**
* Alters the block admin form to add delete links next to menu blocks.
*/
function taxonomy_node_tree_form_block_admin_display_form_alter(&$form, $form_state) {
module_load_include('inc', 'taxonomy_node_tree', 'includes/taxonomy_node_tree.admin');
_taxonomy_node_tree_form_block_admin_display_form_alter($form, $form_state);
}
/**
* Implementation of hook_menu().
*/
function taxonomy_node_tree_menu() {
$items = array();
$items['admin/build/block/add-taxonomy-node-tree-block'] = array(
'title' => 'Add taxonomy node tree block',
'description' => 'Add a new taxonomy node tree block.',
'access arguments' => array('administer blocks'),
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_node_tree_add_block_form'),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/taxonomy_node_tree.admin.inc',
);
$items['admin/build/block/delete-taxonomy-node-tree-block'] = array(
'title' => 'Delete taxonomy node tree block',
'access arguments' => array('administer blocks'),
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_node_tree_delete'),
'type' => MENU_CALLBACK,
'file' => 'includes/taxonomy_node_tree.admin.inc',
);
$items['admin/settings/taxonomy-node-tree'] = array(
'title' => 'Taxonomy Node Tree',
'description' => 'Configure taxonomy node tree settings',
'access arguments' => array('administer blocks'),
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_node_tree_admin_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/taxonomy_node_tree.admin.inc',
);
return $items;
}
/* Recursively iterates through the taxonomy tree, either from the beginning or */
/* starting at a certain level, and builds a nested tree of terms and the */
/* nodes attached to them */
function taxonomy_node_tree_build ( $terms = array(), $options = array(), $parents_index = array(), $current_depth = 0 ) {
// Get default configuration settings
$config = taxonomy_node_tree_get_config();
// Get settings from options array, otherwise use defaults from config
$parent = ( isset($options['parent']) ) ? $options['parent'] : 0;
$max_depth = ( isset($options['max_depth']) ) ? $options['max_depth'] : $config['max_depth'];
$node_visibility = ( isset($options['node_visibility']) ) ? $options['node_visibility'] : $config['node_visibility'];
$term_filters = ( isset($options['term_filters']) ) ? $options['term_filters'] : $config['term_filters'];
$content_filters['filters'] = ( isset($options['content_filters']) ) ? $options['content_filters'] : $config['content_filters'];
$content_filters['method'] = ( isset($options['content_filter_method']) ) ? $options['content_filter_method'] : $config['content_filter_method'];
// if we don't have a set of terms to work with, get the initial tree
if (is_string($terms) || is_int($terms)) {
$vid = $terms;
$terms = taxonomy_get_tree($vid, $parent);
}
// iterate over set of terms
foreach ($terms as $term) {
foreach ($term->parents as $parent_tid) {
if ($parent_tid == $parent) {
$tree[$term->tid] = $term;
}
else {
$parents_index[$parent_tid][$term->tid] = $term;
}
}
}
foreach ($tree as &$term) {
if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $current_depth < $max_depth)) {
$options['parent'] = $term->tid;
$term->children = taxonomy_node_tree_build($parents_index[$term->tid], $options, $parents_index, $current_depth + 1);
}
if (taxonomy_node_tree_count_direct_node_children($term->tid, $term->vid)) {
$term->nodes = taxonomy_node_tree_get_term_nodes($term->tid, $content_filters);
}
}
return $tree;
}
/* Generates nested ul - li markup for listing children nodes and terms based on taxonomy structure */
function taxonomy_node_tree_output ( $vid, $options, $depth = 0 ) {
$tree = taxonomy_node_tree_build($vid, $options);
$active_tids = array();
$active_tids = taxonomy_node_tree_find_active_trail($tree, $vid);
if ($depth == 0) {
$extra = ' tree-root';
}
else {
$extra = ' tree-branch';
}
$html = '<ul class="term-tree level-' . $depth . $extra . '">';
foreach($tree as $term){
$term_link = l($term->name, 'taxonomy/term/' . $term->tid);
$node_tree = $term_tree = '';
$term_is_active = (in_array($term->tid, $active_tids));
$term_has_children = ($term->nodes && count($term->nodes));
if($term->nodes && count($term->nodes)){
$node_tree = '<ul class="node-tree">';
foreach($term->nodes as $node){
$node_item_active = ($node->in_taxonomy_active_trail);
$node_tree .= theme('menu_item', l($node->title, 'node/' . $node->nid), false, '', $node_item_active);
}
$node_tree .= '</ul>';
}
if($term->children && count($term->children)){
$options['parent'] = $term->tid;
$term_tree = taxonomy_node_tree_output($vid, $options, $depth + 1);
}
$html .= theme('menu_item', $term_link, $term_has_children, $node_tree . $term_tree, $term_is_active);
}
$html .= '</ul>';
return $html;
}
/**
* Implements hook_block().
*/
function taxonomy_node_tree_block($op = 'list', $delta = NULL, $edit = NULL) {
$function = '_taxonomy_node_tree_block_' . $op;
if (function_exists($function)) {
return $function($delta, $edit);
}
else {
// "op"s besides "view" are seldom used, so we store them in a separate file.
module_load_include('inc', 'taxonomy_node_tree', 'includes/taxonomy_node_tree.admin');
if (function_exists($function)) {
return $function($delta, $edit);
}
}
}
/**
* Returns the 'view' $op info for hook_block().
*
* @param $delta
* string The name of the block to render.
*/
function _taxonomy_node_tree_block_view($delta) {
$config = taxonomy_node_tree_get_config($delta);
$parent_parts = explode('-', $config['parent']);
$vid = $parent_parts[0];
$parent = $parent_parts[1];
$options = array();
$options['parent'] = $parent_parts[1];
$options['max_depth'] = $config['max_depth'];
$options['node_visibility'] = $config['node_visibility'];
$options['term_filters'] = $config['term_filters'];
$options['content_filters'] = $config['content_filters'];
$options['content_filter_method'] = $config['content_filter_method'];
$options['collapse_inactive'] = $config['collapse_inactive'];
$options['delta'] = $delta;
$data = array();
$data['subject'] = $config['admin_title'];
$data['content'] = taxonomy_node_tree_output($vid, $options);
return $data;
}
/**
* Returns the configuration for the requested block delta.
*
* @param $delta
* string The delta that uniquely identifies the block in the block system. If
* not specified, the default configuration will be returned.
* @return
* array An associated array of configuration options.
*/
function taxonomy_node_tree_get_config($delta = NULL) {
$config = array(
'delta' => $delta,
'parent' => '1-0',
'admin_title' => '',
'max_depth' => null,
'node_visibility' => 1,
'term_filters' => array(),
'content_filters' => array(),
'content_filter_method' => 'exclude',
'collapse_inactive' => 0
);
// Get the block configuration options.
if ($delta) {
$config['admin_title'] = variable_get("taxonomy_node_tree_{$delta}_admin_title", $config['admin_title']);
$config['max_depth'] = variable_get("taxonomy_node_tree_{$delta}_max_depth", $config['max_depth']);
$config['node_visibility'] = variable_get("taxonomy_node_tree_{$delta}_node_visibility", $config['node_visibility']);
$config['parent'] = variable_get("taxonomy_node_tree_{$delta}_parent", $config['parent']);
$config['term_filters'] = variable_get("taxonomy_node_tree_{$delta}_term_filters", $config['term_filters']);
$config['content_filters'] = variable_get("taxonomy_node_tree_{$delta}_content_filters", $config['content_filters']);
$config['content_filter_method'] = variable_get("taxonomy_node_tree_{$delta}_content_filter_method", $config['content_filter_method']);
$config['collapse_inactive'] = variable_get("taxonomy_node_tree_{$delta}_collapse_inactive", $config['collapse_inactive']);
}
return $config;
}
/* Helper function */
/* taxonomy_term_count_nodes() counts not just node children for the term, but also all children below it */
/* Thus, this function usefully removes the children of sub terms and gives a count of nodes for */
/* just the current term */
function taxonomy_node_tree_count_direct_node_children ( $tid, $vid ) {
$children_nodes_count = taxonomy_term_count_nodes($tid);
$child_terms = taxonomy_get_children($tid, $vid);
foreach($child_terms as $child_term){
$sub_children_nodes_count = taxonomy_term_count_nodes($child_term->tid);
$children_nodes_count = $children_nodes_count - $sub_children_nodes_count;
}
return $children_nodes_count;
}
/* Gets the child nodes attached directly to the given taxonomy term ID ($tid) */
/* Applies either exclusive or inclusive filters by content type, if any are set */
function taxonomy_node_tree_get_term_nodes ( $tid, $content_filters = array() ) {
$node_children = array();
if(module_exists('draggableviews')){
module_load_include('inc', 'taxonomy_node_tree', 'includes/taxonomy_node_tree.draggable');
if(taxonomy_node_tree_tid_has_sorted_nodes($tid, $content_filters)){
$sorted_children = array();
$get_sorted_nodes = taxonomy_node_tree_build_sort_query($tid, $content_filters);
$sorted_nodes_result = db_query($get_sorted_nodes);
while($sorted_node = db_fetch_object($sorted_nodes_result)){
$node_child = node_load($sorted_node->nid);
$node_children[] = $node_child;
}
}
}
$taxonomy_term_children = taxonomy_select_nodes( array( $tid ) );
while($taxonomy_child_obj = db_fetch_object($taxonomy_term_children)){
$taxonomy_child_node = node_load($taxonomy_child_obj->nid);
if(!in_array($taxonomy_child_node, $node_children)){
if($content_filters['filters'] && count($content_filters['filters'])){
$is_string = (is_string($content_filters['filters'][$taxonomy_child_node->type])) ? true : false;
$not_excluded = ($content_filters['filters'][$taxonomy_child_node->type] == 0) ? true : false;
$is_included = ($content_filters['filters'][$taxonomy_child_node->type] == $taxonomy_child_node->type) ? true : false;
$is_term_type = ($taxonomy_child_node->type == $content_filters['term_content_type']) ? true : false;
switch($content_filters['method']){
case 'exclude':
if( !$is_string && $not_excluded && !$is_term_type){
$node_children[] = $taxonomy_child_node;
}
break;
case 'include':
if( $is_string && $is_included && !$is_term_type){
$node_children[] = $taxonomy_child_node;
}
break;
}
}
else{
$node_children[] = $taxonomy_child_node;
}
}
}
return $node_children;
}
/* Provides active-trail classes based on the position of the current node in the taxonomy tree */
function taxonomy_node_tree_find_active_trail( $taxonomy_node_tree, $vid ){
$active_tids = array();
$active_nid = taxonomy_node_tree_get_active_nid();
array_walk_recursive($taxonomy_node_tree, 'taxonomy_node_tree_highlight_active_node', $active_nid);
$deepest_tid = taxonomy_node_tree_return_most_specific_node_term($active_nid, $vid);
$active_parents = taxonomy_get_parents_all($deepest_tid);
foreach($active_parents as $parent){
$active_tids[] = $parent->tid;
}
return $active_tids;
}
function taxonomy_node_tree_get_active_nid(){
if (arg(0) == 'node' && is_numeric(arg(1))) {
$active_nid = arg(1);
return $active_nid;
}
return null;
}
/* Wrapper function that either retuns the only term or calls for help to find the most specific term */
function taxonomy_node_tree_return_most_specific_node_term ( $nid, $vid ){
$node = node_load($nid);
if(count($node->taxonomy) == 1){
$term = end($node->taxonomy);
return $term->tid;
}
else{
$tid = taxonomy_node_tree_get_most_specific_term($node, $vid);
return $tid;
}
}
/* If there are multiple terms for a node, the deepest term in the tree is assumed as the most specific/applicable term */
function taxonomy_node_tree_get_most_specific_term($node, $vid){
$terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
$parent_counts = array();
foreach($terms as $term){
$parents_count = count(taxonomy_get_parents_all($term->tid));
$parent_counts[$term->tid] = $parents_count;
}
if(count($parent_counts)){
$deepest = max($parent_counts);
}
$tid = end(array_keys($parent_counts, $deepest));
return $tid;
}
/* Provides the flag that will be used to add an active class to the current node in the taxonomy node tree */
function taxonomy_node_tree_highlight_active_node($item, $key, $active_nid){
if(is_object($item)){
if($item->nid == $active_nid){
$item->in_taxonomy_active_trail = true;
}
}
}