| 1 |
<?php
|
| 2 |
/**
|
| 3 |
gem_cat_index provides a navigational tree listings per node.
|
| 4 |
This module depends on the Category module.
|
| 5 |
|
| 6 |
Copyright (C) 2007 Jared Eckersley <jared@jaredeckersley.com>
|
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify
|
| 9 |
it under the terms of the GNU General Public License as published by
|
| 10 |
the Free Software Foundation; either version 2 of the License, or
|
| 11 |
(at your option) any later version.
|
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful,
|
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 16 |
GNU General Public License for more details.
|
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License
|
| 19 |
along with this program; if not, write to the Free Software
|
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 21 |
*/
|
| 22 |
// $Id$
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_help().
|
| 26 |
* This is just adding some text to the module admin section.
|
| 27 |
*/
|
| 28 |
function gem_cat_index_help($section) {
|
| 29 |
switch ($section) {
|
| 30 |
case 'admin/modules#description':
|
| 31 |
return t('Enables navigation listings in the node body.');
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
|
| 36 |
/**
|
| 37 |
* hook_form_alter call.
|
| 38 |
* This injects a checkbox into the category form array.
|
| 39 |
* The checkbox is stored in $form['category']['index'].
|
| 40 |
*/
|
| 41 |
function gem_cat_index_form_alter($form_id, &$form) {
|
| 42 |
$type = $form['type']['#value'] .'_node_form';
|
| 43 |
|
| 44 |
if ($form_id == $type) {
|
| 45 |
if (is_array($form['category'])) {
|
| 46 |
$form['category']['index'] = array(
|
| 47 |
'#type' => 'radios',
|
| 48 |
'#title' => t('Navigation Layout'),
|
| 49 |
'#description' => t('Should this node have a navigation layout attached to it?'),
|
| 50 |
'#default_value' => 0,
|
| 51 |
'#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
|
| 52 |
);
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* hook_nodeapi
|
| 60 |
*
|
| 61 |
* Nothing fancy going on here. The
|
| 62 |
*/
|
| 63 |
|
| 64 |
function gem_cat_index_nodeapi(&$node, $op, $teaser, $page) {
|
| 65 |
switch ($op) {
|
| 66 |
case 'load':
|
| 67 |
$object = db_fetch_object(db_query('SELECT nid FROM {gem_cat_index} WHERE nid = %d', $node->nid));
|
| 68 |
return array('gem_cat_index' => $object->nid);
|
| 69 |
break;
|
| 70 |
case 'insert':
|
| 71 |
if ($node->category['index'] == 1) {
|
| 72 |
db_query('INSERT INTO {gem_cat_index} (nid) VALUES (%d)', $node->nid);
|
| 73 |
}
|
| 74 |
break;
|
| 75 |
case 'update':
|
| 76 |
db_query('DELETE FROM {gem_cat_index} WHERE nid = %d', $node->nid);
|
| 77 |
if ($node->category['index'] == 1) {
|
| 78 |
db_query('INSERT INTO {gem_cat_index} (nid) VALUES (%d)', $node->nid);
|
| 79 |
}
|
| 80 |
break;
|
| 81 |
case 'delete':
|
| 82 |
db_query('DELETE FROM {gem_cat_index} WHERE nid = %d', $node->nid);
|
| 83 |
break;
|
| 84 |
case 'view':
|
| 85 |
if ($node->gem_cat_index) {
|
| 86 |
$tree = _gem_cat_index($node->gem_cat_index);
|
| 87 |
$node->body .= $tree;
|
| 88 |
$node->teaser .= $tree;
|
| 89 |
}
|
| 90 |
break;
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Handle all the calls to helper function.
|
| 96 |
*
|
| 97 |
* @param $nid
|
| 98 |
* The parent node id.
|
| 99 |
*
|
| 100 |
* @param return
|
| 101 |
* a fully formatted navagational menu.
|
| 102 |
*/
|
| 103 |
function _gem_cat_index($nid) {
|
| 104 |
global $_menu;
|
| 105 |
|
| 106 |
// The category module maintains a mapping
|
| 107 |
// in category_menu_map table of the global $_menu
|
| 108 |
// array index key that corresponds to the node id.
|
| 109 |
$mid = category_menu_get_mapping($nid);
|
| 110 |
|
| 111 |
// grab all the child elements from $_menu array.
|
| 112 |
$menu = recurse_children($mid);
|
| 113 |
|
| 114 |
// manually add the parent array to our menu array
|
| 115 |
$menu[$mid] = $_menu['items'][$mid];
|
| 116 |
|
| 117 |
// return HTML formated navigational tree.
|
| 118 |
return gem_cat_index_get_tree($mid,$menu);
|
| 119 |
}
|
| 120 |
|
| 121 |
/**
|
| 122 |
* Format an array into a linked navigational tree
|
| 123 |
*
|
| 124 |
* @param $pid
|
| 125 |
* This is the initial starting point, but through
|
| 126 |
* recursive calls, all children get passed in as
|
| 127 |
* a parent.
|
| 128 |
*
|
| 129 |
* @param $menu
|
| 130 |
* This is the menu array to be formatted.
|
| 131 |
*
|
| 132 |
* @return
|
| 133 |
* HTML formatted navigational tree.
|
| 134 |
*/
|
| 135 |
function gem_cat_index_get_tree($pid = 1, $menu = NULL) {
|
| 136 |
|
| 137 |
if (isset($menu[$pid]) && $menu[$pid]['children']) {
|
| 138 |
foreach ($menu[$pid]['children'] as $mid) {
|
| 139 |
if ($mid > 0) {
|
| 140 |
$children = isset($menu[$mid]['children']) ? $menu[$mid]['children'] : NULL;
|
| 141 |
$menu_tree .= theme('menu_item', $mid, gem_cat_index_get_tree($mid, $menu), count($children) == 0);
|
| 142 |
}
|
| 143 |
}
|
| 144 |
}
|
| 145 |
|
| 146 |
if ($menu_tree) {
|
| 147 |
$output = '<ul class="tree">'. $menu_tree .'</ul>';
|
| 148 |
}
|
| 149 |
|
| 150 |
return $output;
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Build a multidimensional array of all the children
|
| 155 |
* of the given parent.
|
| 156 |
*
|
| 157 |
* @param $parent
|
| 158 |
* This is the initial starting point, but through
|
| 159 |
* recursive calls, all children get passed in as
|
| 160 |
* a parent.
|
| 161 |
*
|
| 162 |
* @return
|
| 163 |
* An array that matches the format of the global
|
| 164 |
* $_menu['visible'] array.
|
| 165 |
*/
|
| 166 |
function recurse_children($parent) {
|
| 167 |
global $_menu;
|
| 168 |
|
| 169 |
$children = array();
|
| 170 |
|
| 171 |
if ($_menu['items'][$parent]['children']) {
|
| 172 |
foreach ($_menu['items'][$parent]['children'] as $child) {
|
| 173 |
if ($child > 0) {
|
| 174 |
$children[$child] = array('title'=>$_menu['items'][$child]['title'],'path'=>$_menu['items'][$child]['path'],'children'=>$_menu['items'][$child]['children'],'type'=>$_menu['items'][$child]['type'],'pid'=>$parent,'id'=>$child);
|
| 175 |
}
|
| 176 |
}
|
| 177 |
}
|
| 178 |
|
| 179 |
foreach ($children as $child) {
|
| 180 |
$children += recurse_children($child['id']);
|
| 181 |
}
|
| 182 |
|
| 183 |
return $children;
|
| 184 |
}
|
| 185 |
|
| 186 |
?>
|