/[drupal]/contributions/modules/slicedbook_navigation/slicedbook_navigation.module
ViewVC logotype

Contents of /contributions/modules/slicedbook_navigation/slicedbook_navigation.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Sat Aug 9 19:28:44 2008 UTC (15 months, 2 weeks ago) by btopro
Branch: MAIN
CVS Tags: DRUPAL-5--2-0, HEAD
Branch point for: DRUPAL-5--2
Changes since 1.2: +541 -456 lines
File MIME type: text/x-php
This is the start of new development for the 5.x-2 version
1 <?php
2 // $Id $
3
4 drupal_add_js(drupal_get_path('module','slicedbook_navigation') . '/script.js','module');
5
6 /**
7 * Implementation of hook_help().
8 */
9 function slicedbook_navigation_help($section = '') {
10 $output = '';
11
12 switch ($section) {
13 case "admin/help#slicedbook_navigation" :
14 $output = t("<p>Slice book navigation into blocks (one per level)</p>");
15 break;
16 }
17
18 return $output;
19 }
20
21 function slicedbook_navigation_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
22 switch ($op) {
23 case 'update':
24 db_query("DELETE FROM {slicedbook_navigation} WHERE nid = %d", $node->nid);
25 case 'insert':
26 if (isset($node->subclass)) {
27 db_query("INSERT INTO {slicedbook_navigation} (nid, attribute) VALUES (".$node->nid.", '".$node->subclass."')");
28 } else {
29 db_query("INSERT INTO {slicedbook_navigation} (nid, attribute) VALUES (".$node->nid.", '')");
30 }
31 break;
32
33 case 'delete':
34 db_query('DELETE FROM {slicedbook_navigation} WHERE nid = %d', $node->nid);
35 break;
36
37 case 'load':
38 $object = new stdClass();
39 $object->subclass = slicedbook_navigation_get_subclass($node->nid);
40 return array('subclass' => $object->subclass);
41
42 case 'view':
43
44 break;
45 }
46 }
47
48 function slicedbook_navigation_get_subclass($nid) {
49 $query = "SELECT attribute FROM {slicedbook_navigation} WHERE nid = ".$nid;
50 $result = db_query($query);
51 $subclass = db_result($result);
52
53 return $subclass;
54 }
55
56 function slicedbook_navigation_form_alter($form_id, &$form) {
57 switch ($form_id) {
58 case 'book_node_form' :
59 $form['slicedbook_navigation'] = array(
60 '#type' => "fieldset",
61 '#title' => t("Sliced Book Navigation"),
62 '#weight' => "100",
63 '#collapsible' => true,
64 '#collapsed' => true,
65 );
66 $form['slicedbook_navigation']['subclass'] = array(
67 '#type' => "textfield",
68 '#title' => t("Item subclass"),
69 '#required' => "0",
70 '#default_value' => $form['#node']->subclass,
71 );
72 break;
73 }
74 return $form;
75 }
76
77 /**
78 * Implementation of hook_perm().
79 */
80 function slicedbook_navigation_perm() {
81 return array('access content');
82 }
83
84 /**
85 * Implementation of hook_menu().
86 */
87 function slicedbook_navigation_menu() {
88 $items = array();
89
90 $items[] = array(
91 'path' => 'admin/settings/slicedbook_navigation',
92 'title' => t('Sliced Book Navigation'),
93 'description' => t('Specify the book to divide into block per level.'),
94 'callback' => 'drupal_get_form',
95 'callback arguments' => 'slicedbook_navigation_admin',
96 'access' => user_access('access administration pages'),
97 'type' => MENU_NORMAL_ITEM,
98 );
99
100 return $items;
101 }
102
103 /**
104 * Administration form.
105 */
106 function slicedbook_navigation_admin() {
107 $form = array();
108 $book_options = slicedbook_navigation_get_book_root();
109 $nodes_to_blocks = variable_get('slicedbook_navigation_nodes_to_blocks',array());
110 $block_table = '<table><th>Nid</th><th>Depth</th>';
111 foreach($nodes_to_blocks as $node){
112 $block_table.= '<tr><td>' . $node['nid'] . '</td><td>' . $node['depth'] . '</td></tr>';
113 }
114 $block_table.= '</table>';
115
116 $form['menu_multiple_blocks_config_book'] = array(
117 '#type' => 'select',
118 '#title' => t('Book to divide into blocks'),
119 '#default_value' => variable_get('menu_multiple_blocks_config_book', 0),
120 '#options' => $book_options,
121 '#description' => t('The book you select will be explode in multiple level blocks'),
122 );
123
124 $form['slicedbook_navigation_default_book_id'] = array(
125 '#type' => 'textfield',
126 '#title' => t('Default book id'),
127 '#default_value' => variable_get('slicedbook_navigation_default_book_id', -1),
128 '#description' => t('The default book id when visiting a non-book node'),
129 );
130
131 $form['slicedbook_navigation_node'] = array(
132 '#type' => 'select',
133 '#title' => t('Node to divide into a block'),
134 '#options' => book_toc(0),
135 '#description' => t('The book will be exploded at the node you have selected'),
136 );
137 $form['slicedbook_navigation_depth'] = array(
138 '#type' => 'select',
139 '#title' => t('Levels down to go'),
140 '#options' => array(0,1,2,3,4,5,6,7,8,9,10),
141 '#description' => t('This is how many levels deep navigation will be rendered (0 = unlimited)'),
142 '#suffix' => 'Current Blocks Generated from Split: <br />' . $block_table,
143 );
144 $form['slicedbook_navigation_reset'] = array(
145 '#type' => 'checkbox',
146 '#title' => t('Reset generated blocks'),
147 '#description' => t('This will clear all blocks generated via this method'),
148 );
149
150 return system_settings_form($form);
151 }
152
153 function slicedbook_navigation_admin_submit($form_id, $form_values){
154 variable_set('menu_multiple_blocks_config_book',$form_values['menu_multiple_blocks_config_book']);
155 variable_set('slicedbook_navigation_default_book_id',$form_values['slicedbook_navigation_default_book_id']);
156 if($form_values['slicedbook_navigation_reset'] == true){
157 $nodes_to_blocks = array();
158 }else{
159 $nodes_to_blocks = variable_get('slicedbook_navigation_nodes_to_blocks', array());
160 $nodes_to_blocks[] = array('nid' => $form_values['slicedbook_navigation_node'],'depth' => $form_values['slicedbook_navigation_depth']);
161 }
162 variable_set('slicedbook_navigation_nodes_to_blocks',$nodes_to_blocks);
163 }
164
165 /**
166 * Implementation of hook_block().
167 */
168 function slicedbook_navigation_block($op = 'list', $delta = 0) {
169 $book_id = variable_get('menu_multiple_blocks_config_book', 0);
170 $GLOBALS['book_tree'] = slicedbook_navigation_get_tree($book_id);
171 $active_book_id = slicedbook_navigation_get_active_id();
172 $book_tree = slicedbook_navigation_get_subtree($book_id);
173 $active_book_depth = slicedbook_navigation_get_active_depth($active_book_id, $book_tree);
174 $book_depth = slicedbook_navigation_get_depth($book_tree);
175 $active_book_branche = slicedbook_navigation_get_branch($active_book_id, $book_tree);
176
177 $nodes_to_blocks = variable_get('slicedbook_navigation_nodes_to_blocks',array());
178
179 if ($op == "list") {
180 for ($j = 0; $j <= $book_depth; $j++) {
181 $block[$j]["info"] = $GLOBALS['book_tree'][$book_id]->title ."_". t("level") ."_". $j;
182 }
183 foreach($nodes_to_blocks as $key => $node){
184 $tmpnode = node_load($node['nid']);
185 $block[$book_depth + 1 + $key]['info'] = 'Block ' . $tmpnode->title . '(' . $node['nid'] . '): depth ' . $node['depth'];
186 }
187 return $block;
188 }
189 elseif ($op == "view") {
190 for ($i = 0; $i <= $book_depth; $i++) {
191 if ($delta == $i) {
192 $block['subject'] = "";
193 $block['content'] = slicedbook_navigation_generate_level($active_book_id, $i, $book_tree, $active_book_branche, $depth);
194 return $block;
195 }
196 }
197 foreach($nodes_to_blocks as $key => $node){
198 if ($delta == ($book_depth + 1 + $key) ) {
199 if($node['nid'] != 0){
200 $tmpnode = node_load($node['nid']);
201 $title = $tmpnode->title;
202 }else{
203 $title = '';
204 }
205 $block['subject'] = $title;
206 $block['content'] = _slicedbook_navigation_walk_tree($node['nid'],$node['depth']);
207 return $block;
208 }
209 }
210 }
211 }
212
213 /**
214 * Return the available books root.
215 */
216 function slicedbook_navigation_get_book_root() {
217 $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = 0 AND n.status = 1 ORDER BY b.weight, n.title'));
218 $books = array(0 => t("None"));
219
220 while ($node = db_fetch_object($result)) {
221 $books[$node->nid] = $node->title;
222 }
223
224 return $books;
225 }
226
227 /**
228 * Return the active book item id.
229 */
230 function slicedbook_navigation_get_active_id() {
231 $path = slicedbook_navigation_get_active_path();
232
233 if (preg_match("/node/", $path)) {
234 $book_id = str_replace("node/", "", $path);
235 }
236 else {
237 $book_id = -1;
238 $default_book_id = variable_get("slicedbook_navigation_default_book_id", -1);
239 if ($default_book_id > 0) {
240 $book_id = $default_book_id;
241 }
242 }
243
244 return $book_id;
245 }
246
247 /**
248 * Return the active path looking at url.
249 */
250 function slicedbook_navigation_get_active_path() {
251 $page = "";
252
253 $path = $_GET['q'];
254
255 return $path;
256 }
257
258 /**
259 * Make a well formated tree from the database
260 *
261 * @param $book_id
262 * The id of the book to get the tree of.
263 */
264 function slicedbook_navigation_get_tree_from_db($book_id = array()) {
265 $tab_book_id = slicedbook_navigation_array_2_list($book_id);
266 $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid IN '.$tab_book_id.' ORDER BY b.weight');
267 while ($row = db_fetch_object($result)) {
268 $node[$row->nid] = $row;
269 }
270
271 return $node;
272 }
273
274 /**
275 *
276 */
277 function slicedbook_navigation_get_tree($book_id) {
278 if (isset($GLOBALS['book_tree'])) {
279 return $GLOBALS['book_tree'];
280 }
281 else {
282 $query = "SELECT nid FROM {book}";
283 $result = db_query($query);
284
285 while ($node = db_fetch_array($result)) {
286 $tree[$node['nid']] = $node['nid'];
287 $tab_id[] = $node['nid'];
288 }
289
290 $book = slicedbook_navigation_get_tree_from_db($tab_id);
291
292 foreach ($book as $key => $page) {
293 $children = slicedbook_navigation_get_children($page->nid, $book);
294 if (isset($children)) {
295 $book[$key]->children = $children;
296 }
297 }
298
299 return $book;
300 }
301 }
302
303 /**
304 *
305 */
306 function slicedbook_navigation_get_children($book_id, $book) {
307 foreach ($book as $page) {
308 if ($page->parent == $book_id) {
309 $children[] = $page->nid;
310 }
311 }
312
313 return $children;
314 }
315
316 /**
317 *
318 */
319 function slicedbook_navigation_get_flat($book_id, $book_tree) {
320 $flat = array();
321 $parent = $book_tree[$book_id]->parent;
322
323 foreach ($book_tree as $key => $page) {
324 if ($page->parent == $parent) {
325 $flat[] = $page->nid;
326 }
327 }
328
329 return $flat;
330 }
331
332 /**
333 *
334 */
335 function slicedbook_navigation_get_branch($book_id, $book_tree) {
336 $branch = array();
337
338 $parent = $book_tree[$book_id]->parent;
339 $branche[] = $book_tree[$book_id]->nid;
340 $branche[] = $parent;
341 while ($parent != 0) {
342 $parent_next = $book_tree[$parent]->parent;
343 $branche[] = $parent_next;
344 $parent = $parent_next;
345 }
346
347 krsort($branche);
348 $count_branche = count($branche) - 1;
349
350 unset($branche[$count_branche]);
351
352 return $branche;
353 }
354
355 /**
356 *
357 */
358 function slicedbook_navigation_get_active_depth($book_id, $book_tree) {
359 $depth = $book_tree[$book_id]->depth;
360
361 return $depth;
362 }
363
364 /**
365 *
366 */
367 function slicedbook_navigation_get_depth($book) {
368 $depth = $book[0]->depth;
369
370 foreach ($book as $node) {
371 if ($node->depth >= $depth) {
372 $depth = $node->depth;
373 }
374 }
375
376 return $depth;
377 }
378
379 /**
380 *
381 */
382 function slicedbook_navigation_get_subtree($book_id, $depth = 0, $tree = array()) {
383 $tree[$book_id] = $GLOBALS['book_tree'][$book_id];
384 $tree[$book_id]->nid = $book_id;
385 $tree[$book_id]->path = "node/". $book_id;
386 $tree[$book_id]->depth = $depth;
387 $depth++;
388
389 if (isset($GLOBALS['book_tree'][$book_id]->children)) {
390 foreach ($GLOBALS['book_tree'][$book_id]->children as $child) {
391 $tree = slicedbook_navigation_get_subtree($child, $depth, $tree) + $tree;
392 }
393 }
394
395 return $tree;
396 }
397
398 function slicedbook_navigation_get_subclasses() {
399 $array = array();
400 $query = "SELECT nid, attribute FROM {slicedbook_navigation}";
401 $result = db_query($query);
402 while ($row = db_fetch_array($result)) {
403 $array[$row['nid']] = $row['attribute'];
404 }
405
406 return $array;
407 }
408
409 /**
410 *
411 */
412 function slicedbook_navigation_generate_level($active_book_id, $depth, $book_tree, $branche) {
413 $subclasses = array();
414 $subclasses = slicedbook_navigation_get_subclasses();
415 $class = "";
416 $flat = slicedbook_navigation_get_flat($active_book_id, $book_tree);
417 $active_book_depth = slicedbook_navigation_get_active_depth($active_book_id, $book_tree);
418 $book_sub_menu = "";
419
420 foreach ($book_tree as $key => $page) {
421 if ($page->nid == $active_book_id) {
422 $children = $page->children;
423 }
424 }
425
426 $pre_ul = "
427 <ul class='book_block_". $depth ."'>";
428
429 reset($book_tree);
430 $first_object = current($book_tree);
431 $first_nid = $first_object->nid;
432 foreach ($book_tree as $key => $link) {
433 $class = "inactive";
434 if (in_array($link->nid, $branche)) {
435 $class = "active";
436 }
437 $class .= " ".$subclasses[$link->nid];
438
439 if ($depth == $active_book_depth) {
440 if ($link->depth == $depth && in_array($link->nid, $flat)) {
441 $book_sub_menu .= "<li class='". $class ."'><a href='". $GLOBALS['base_url'] ."/". slicedbook_navigation_get_page_url($link->path) ."' >". $link->title ."</a></li>";
442 }
443 }
444 elseif ($depth == $active_book_depth + 1 && !empty($children)) {
445 if ($link->depth == $depth && in_array($link->nid, $children)) {
446 $book_sub_menu .= "<li class='". $class ."'><a href='". $GLOBALS['base_url'] ."/". slicedbook_navigation_get_page_url($link->path) ."' >". $link->title ."</a></li>";
447 }
448 }
449 else {
450 foreach ($branche as $nid) {
451 if (!isset($GLOBALS['slicedbook_navigation_generate_level'][$first_nid][$nid])) {
452 $GLOBALS['slicedbook_navigation_generate_level'][$first_nid][$nid] = array();
453 $GLOBALS['slicedbook_navigation_generate_level'][$first_nid][$nid] = slicedbook_navigation_get_flat($nid, $book_tree);
454 }
455 $flat = $GLOBALS['slicedbook_navigation_generate_level'][$first_nid][$nid];
456 if ($link->depth == $depth && in_array($link->nid, $flat)) {
457 $book_sub_menu .= "<li class='". $class ."'><a href='". $GLOBALS['base_url'] ."/". slicedbook_navigation_get_page_url($link->path) ."' >". $link->title ."</a></li>";
458 }
459 }
460 }
461 }
462 $post_ul = "</ul>";
463
464 if (!empty($book_sub_menu)) {
465
466 return $pre_ul.$book_sub_menu.$post_ul;
467
468 }
469 else {
470
471 return "";
472
473 }
474 }
475
476 /**
477 *
478 */
479 function slicedbook_navigation_get_page_url($path) {
480 $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path));
481
482 $q = "?q=";
483 if (variable_get('clean_url', 0)) {
484 $q = "";
485 }
486
487 if (empty($alias)) {
488 $alias = $path;
489 }
490
491 return $q.$alias;
492 }
493
494 /**
495 *
496 */
497 function slicedbook_navigation_array_2_list($array, $delimeter = "") {
498 if (is_array($array)) {
499 $array = array_unique($array);
500 if (sizeof($array) == 0) {
501 $array = array(0);
502 }
503 $list = implode($delimeter .",". $delimeter, $array);
504 $list = $delimeter.$list.$delimeter;
505 $list = "(". $list .")";
506 }
507 else {
508 $list = "(". $delimeter ."0". $delimeter .")";
509 }
510
511 return $list;
512 }
513
514 /* Btopro additional functions to handle advanced rendering of levels based on more complex slices */
515 //walk the tree starting at any node and only go down max number of levels (0 means go till you run out)
516 function _slicedbook_navigation_walk_tree($nid,$max = 0,$level = 0){
517 if($max != 0 && $level == $max){
518 return $output;
519 }
520 $level++;
521 $output = '';
522 $tmp_output = '';
523 $result = db_query('SELECT node.title,node.nid,book.parent FROM book JOIN node ON book.vid=node.vid WHERE book.parent=%d ORDER BY book.weight',$nid);
524 while($value = db_fetch_array($result)){
525 $temp_output = _slicedbook_navigation_walk_tree($value['nid'],$max,$level);
526
527 if($temp_output != ''){
528 if(strpos(l(' ','node/' . $value['nid']),'active')){
529 $style = 'display:block;';
530 $output.='<li class="expanded sb_node_' . $value['nid'] . '" style="cursor:pointer;" id="sb_node_' . $value['nid'] . '" onclick="slicedbook_navigation_toggle_menu(' . "'sb_node_" . $value['nid'] . "'" . ');">' . l(t($value['title']),'node/' . $value['nid'],array('class' => 'sbn_link')) . '</li>' . "\n";
531 }else{
532 $style = 'display:none;';
533 $output.='<li class="collapsed sb_node_' . $value['nid'] . '" style="cursor:pointer;" id="sb_node_' . $value['nid'] . '" onclick="slicedbook_navigation_toggle_menu(' . "'sb_node_" . $value['nid'] . "'" . ');">' . l(t($value['title']),'node/' . $value['nid'],array('class' => 'sbn_link')) . '</li>' . "\n";
534 }
535 $output.= '<ul class="mL sb_menu_' . $value['nid'] . '" style="' . $style . '" id="sb_menu_' . $value['nid'] . '">' . $temp_output . '</ul>' . "\n";
536 }else{
537 $output.='<li class="menu_leaf sb_node_' . $value['nid'] . '" style="cursor:pointer;" id="sb_node_' . $value['nid'] . '">' . l(t($value['title']),'node/' . $value['nid']) . '</li>' . "\n";
538 }
539 }
540 return $output;
541 }

  ViewVC Help
Powered by ViewVC 1.1.2