| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* $Id: taxonomy_blocks.module,v 1.2 2008/07/27 07:21:18 owahab Exp $
|
| 4 |
*
|
| 5 |
* @file taxonomy_blocks.module
|
| 6 |
* Creates blocks with lists of a taxonomy vocabulary.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_block().
|
| 11 |
*/
|
| 12 |
function taxonomy_blocks_block($op = 'list', $delta = 0, $edit = array()) {
|
| 13 |
if ($op == 'list') {
|
| 14 |
// Get a list of all variables we declare in varibale table
|
| 15 |
$query = db_query('SELECT * FROM {variable} WHERE name LIKE "taxonomy_blocks_vid_%_block_enabled"');
|
| 16 |
while ($result = db_fetch_object($query)) {
|
| 17 |
$vid = NULL;
|
| 18 |
// Check for variables with value = 1
|
| 19 |
if ($result->value == serialize(1)) {
|
| 20 |
// Extract the vocabulary ID from the variable name
|
| 21 |
$vid = preg_replace('/(\w+)(\d+)(\w+)/', '$2', $result->name);
|
| 22 |
$count = variable_get('taxonomy_blocks_vid_' . $vid . '_count',1);
|
| 23 |
|
| 24 |
$vocab = taxonomy_vocabulary_load($vid);
|
| 25 |
|
| 26 |
if (!$vocab) // deleted vocab
|
| 27 |
{
|
| 28 |
variable_del($result->name);
|
| 29 |
continue;
|
| 30 |
}
|
| 31 |
|
| 32 |
// generate number of blocks
|
| 33 |
for ($i = 0;$i < $count;$i++)
|
| 34 |
{
|
| 35 |
$bid = $vid . '-' . $i;
|
| 36 |
$blocks[$bid]['info'] = t('Taxonomy Block: !vocab [!index]', array('!vocab' => $vocab->name,'!index' => $i));
|
| 37 |
}
|
| 38 |
}
|
| 39 |
}
|
| 40 |
}
|
| 41 |
elseif ($op == 'configure')
|
| 42 |
{
|
| 43 |
$options = array();
|
| 44 |
|
| 45 |
$options['0'] = t('<Root>');
|
| 46 |
|
| 47 |
foreach (taxonomy_get_tree($delta) as $term)
|
| 48 |
{
|
| 49 |
$options[$term->tid] = str_repeat(" -",$term->depth) . " " . t($term->name);
|
| 50 |
}
|
| 51 |
|
| 52 |
$form['root_term'] = array
|
| 53 |
(
|
| 54 |
'#type' => 'select',
|
| 55 |
'#title' => t('Root term'),
|
| 56 |
'#options' => $options,
|
| 57 |
'#default_value' => variable_set("taxonomy_blocks_vid_{$delta}_root",0),
|
| 58 |
);
|
| 59 |
|
| 60 |
$form['tree_depth'] = array(
|
| 61 |
'#type' => 'textfield',
|
| 62 |
'#size' => 3,
|
| 63 |
'#title' => t('Tree Depth'),
|
| 64 |
'#description' => t('Specify the number of levels shown from the vocabulary. Default is 0 which is equivilant to unlimited.'),
|
| 65 |
'#default_value' => variable_get('taxonomy_blocks_vid_'. $delta .'_tree_depth', 0),
|
| 66 |
);
|
| 67 |
|
| 68 |
$form['block']['show_empty'] = array(
|
| 69 |
'#type' => 'checkbox',
|
| 70 |
'#title' => t('Show Empty Terms'),
|
| 71 |
'#description' => t('If checked, terms with no members will be shown.'),
|
| 72 |
'#default_value' => variable_get('taxonomy_blocks_vid_'. $delta .'_show_empty', 1),
|
| 73 |
'#weight' => -8,
|
| 74 |
);
|
| 75 |
|
| 76 |
$form['block']['show_node_count'] = array(
|
| 77 |
'#type' => 'checkbox',
|
| 78 |
'#title' => t('Show Number Of Nodes'),
|
| 79 |
'#description' => t('If checked, terms with be suffixed by number of member nodes.'),
|
| 80 |
'#default_value' => variable_get('taxonomy_blocks_vid_'. $delta .'_show_node_count', 0),
|
| 81 |
'#weight' => -7,
|
| 82 |
);
|
| 83 |
|
| 84 |
return $form;
|
| 85 |
}
|
| 86 |
elseif ($op == 'save')
|
| 87 |
{
|
| 88 |
// save all to settings
|
| 89 |
variable_set("taxonomy_blocks_vid_{$delta}_root",(int)$edit['root_term']);
|
| 90 |
variable_set("taxonomy_blocks_vid_{$delta}_tree_depth",(int)$edit['tree_depth']);
|
| 91 |
variable_set("taxonomy_blocks_vid_{$delta}_show_empty",(int)$edit['show_empty']);
|
| 92 |
variable_set("taxonomy_blocks_vid_{$delta}_show_node_count",(int)$edit['show_node_count']);
|
| 93 |
}
|
| 94 |
elseif ($op == 'view') {
|
| 95 |
$bits = explode('-',$delta);
|
| 96 |
$vid = $bits[0];
|
| 97 |
$index = $bits[1];
|
| 98 |
$vocab = taxonomy_vocabulary_load($vid);
|
| 99 |
|
| 100 |
$blocks = array(
|
| 101 |
'subject' => $vocab->name . ($index ? " [$index]" : ""),
|
| 102 |
'content' => theme('taxonomy_blocks_block_content', $vid, $index)
|
| 103 |
);
|
| 104 |
}
|
| 105 |
return $blocks;
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
* Render a list of terms inside a block.
|
| 110 |
*
|
| 111 |
* @param $vid
|
| 112 |
* ID of the rendered vocabulary.
|
| 113 |
*
|
| 114 |
* @return HTML unordered list of terms.
|
| 115 |
*/
|
| 116 |
function theme_taxonomy_blocks_block_content($vid,$index) {
|
| 117 |
$delta = $vid . '-' . $index;
|
| 118 |
|
| 119 |
$depth = variable_get('taxonomy_blocks_vid_'. $delta .'_tree_depth', 0);
|
| 120 |
$parent = variable_get('taxonomy_blocks_vid_' . $delta . '_root',0);
|
| 121 |
$show_empty = variable_get('taxonomy_blocks_vid_'. $delta .'_show_empty', 0);
|
| 122 |
$show_node_count = variable_get('taxonomy_blocks_vid_'. $delta .'_show_node_count', 0);
|
| 123 |
|
| 124 |
$depth == 0? $depth = NULL : '';
|
| 125 |
// Get taxonomy tree
|
| 126 |
$tree = taxonomy_get_tree($vid, $parent, -1, $depth);
|
| 127 |
// Parse the tree
|
| 128 |
if ($tree) {
|
| 129 |
foreach ($tree as $term) {
|
| 130 |
// Filter using PHP code:
|
| 131 |
|
| 132 |
if ($code != '')
|
| 133 |
{
|
| 134 |
$code = '<?php $tid = '. $term->tid .'; $vid = '. $vid .'; ';
|
| 135 |
$code .= variable_get('taxonomy_blocks_vid_'. $vid .'_filter_php', '');
|
| 136 |
$code .= '?>';
|
| 137 |
$show = drupal_eval($code);
|
| 138 |
}
|
| 139 |
else
|
| 140 |
{
|
| 141 |
$show = false;
|
| 142 |
}
|
| 143 |
|
| 144 |
$count = taxonomy_blocks_count_nodes($term->tid);
|
| 145 |
|
| 146 |
if ($show == TRUE || $count > 0 || $show_empty == 1) {
|
| 147 |
if ($term->depth > $old_depth) {
|
| 148 |
$output .= '<ul>';
|
| 149 |
}
|
| 150 |
elseif ($term->depth < $old_depth) {
|
| 151 |
$output .= '</ul>';
|
| 152 |
}
|
| 153 |
else {
|
| 154 |
$output .= '</li>';
|
| 155 |
}
|
| 156 |
|
| 157 |
$output .= '<li>';
|
| 158 |
|
| 159 |
$path = 'taxonomy/term/' . $term->tid;
|
| 160 |
|
| 161 |
//$path = variable_get('taxonomy_blocks_vid_'. $vid .'_path_prefix', 'taxonomy/term');
|
| 162 |
//if (module_exists('token')) {
|
| 163 |
// $path = token_replace($path, 'taxonomy', $term);
|
| 164 |
//}
|
| 165 |
//else {
|
| 166 |
// $path .= '/'. $term->tid;
|
| 167 |
//}
|
| 168 |
|
| 169 |
$output .= l(t($term->name), $path);
|
| 170 |
|
| 171 |
if ($show_node_count == 1) {
|
| 172 |
$output .= theme('taxonomy_blocks_node_count', $count);
|
| 173 |
}
|
| 174 |
|
| 175 |
$old_depth = $term->depth;
|
| 176 |
}
|
| 177 |
}
|
| 178 |
if (!is_null($output)) {
|
| 179 |
$output = '<ul>'. $output .'</ul>';
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
return $output;
|
| 184 |
}
|
| 185 |
|
| 186 |
/**
|
| 187 |
* Implementation of hook_form_alter().
|
| 188 |
*/
|
| 189 |
function taxonomy_blocks_form_alter(&$form, &$form_state, $form_id) {
|
| 190 |
if ($form_id == 'taxonomy_form_vocabulary') {
|
| 191 |
// Force the form submission to be passed to our function as well
|
| 192 |
$form['#submit'][] = 'taxonomy_blocks_vocabulary_form_submit';
|
| 193 |
// Get the vocabulary ID
|
| 194 |
$vid = $form['vid']['#value'];
|
| 195 |
// Have to set these in order to show our form items at the bottom
|
| 196 |
$form['submit']['#weight'] = 10;
|
| 197 |
$form['delete']['#weight'] = 10;
|
| 198 |
// Adding our settings items
|
| 199 |
$form['block'] = array(
|
| 200 |
'#type' => 'fieldset',
|
| 201 |
'#title' => t('Block'),
|
| 202 |
'#collapsed' => FALSE,
|
| 203 |
'#collapsible' => TRUE,
|
| 204 |
'#weight' => 9,
|
| 205 |
);
|
| 206 |
$form['block']['taxonomy_blocks_vid_'. $vid .'_block_enabled'] = array(
|
| 207 |
'#type' => 'checkbox',
|
| 208 |
'#title' => t('Provide Block'),
|
| 209 |
'#description' => t('If checked, this vocabulary will provide a block.'),
|
| 210 |
'#default_value' => variable_get('taxonomy_blocks_vid_'. $vid .'_block_enabled', 0),
|
| 211 |
'#weight' => -11,
|
| 212 |
);
|
| 213 |
$form['block']['taxonomy_blocks_vid_'. $vid .'_count'] = array(
|
| 214 |
'#type' => 'textfield',
|
| 215 |
'#title' => t('Number of Blocks'),
|
| 216 |
'#description' => t('Enter number of blocks you want to create from this vocabulary.'),
|
| 217 |
'#default_value' => variable_get('taxonomy_blocks_vid_'. $vid .'_count', 1),
|
| 218 |
'#weight' => -10,
|
| 219 |
);
|
| 220 |
|
| 221 |
// this thing only confuse user
|
| 222 |
/*
|
| 223 |
$form['block']['taxonomy_blocks_vid_'. $vid .'_path_prefix'] = array(
|
| 224 |
'#type' => 'textfield',
|
| 225 |
'#title' => t('Menu Path'),
|
| 226 |
'#description' => t('Specify the path prefix for menu items shown in the block, by default this is set to "taxonomy/term".'),
|
| 227 |
'#default_value' => variable_get('taxonomy_blocks_vid_'. $vid .'_path_prefix', 'taxonomy/term'),
|
| 228 |
'#weight' => -6,
|
| 229 |
);
|
| 230 |
if (module_exists('token')) {
|
| 231 |
$form['block']['taxonomy_blocks_vid_'. $vid .'_path_prefix']['#description'] .= t('You may also use the following tokens:');
|
| 232 |
$form['block']['taxonomy_tokens'] = array(
|
| 233 |
'#type' => 'fieldset',
|
| 234 |
'#title' => t('Replacement patterns'),
|
| 235 |
'#collapsed' => TRUE,
|
| 236 |
'#collapsible' => TRUE,
|
| 237 |
'#weight' => -5,
|
| 238 |
);
|
| 239 |
$patterns = token_get_list('taxonomy');
|
| 240 |
foreach ($patterns as $type => $pattern_set) {
|
| 241 |
if ($type != 'global') {
|
| 242 |
foreach ($pattern_set as $pattern => $description) {
|
| 243 |
$form['block']['taxonomy_tokens']['#value'] .= '<dt>['. $pattern .']</dt><dd>'. $description .'</dd>';
|
| 244 |
}
|
| 245 |
}
|
| 246 |
}
|
| 247 |
}*/
|
| 248 |
|
| 249 |
$form['block']['taxonomy_blocks_vid_'. $vid .'_filter_php'] = array(
|
| 250 |
'#type' => 'textarea',
|
| 251 |
'#title' => t('Filter'),
|
| 252 |
'#description' => t('Enter PHP code (without %php) to be evaluated on each term and if the code returns <code>TRUE</code>, the term will be shown. Note that executing incorrect PHP-code can break your Drupal site. The code will have to deal with $tid as the term ID and $vid as the vocabulary ID.', array('%php' => '<?php ?>')),
|
| 253 |
'#default_value' => variable_get('taxonomy_blocks_vid_'. $vid .'_filter_php', NULL),
|
| 254 |
'#weight' => -4,
|
| 255 |
);
|
| 256 |
}
|
| 257 |
}
|
| 258 |
|
| 259 |
/**
|
| 260 |
* Handle submission of Taxonomy vocabulary form.
|
| 261 |
*/
|
| 262 |
function taxonomy_blocks_vocabulary_form_submit($form_id, &$form_state) {
|
| 263 |
$edit =& $form_state['values'];
|
| 264 |
|
| 265 |
foreach ($edit as $field => $value) {
|
| 266 |
if (strpos($field, 'taxonomy_blocks_') === 0) {
|
| 267 |
if (!isset($value) || $value === 0) {
|
| 268 |
variable_del($field);
|
| 269 |
}
|
| 270 |
else {
|
| 271 |
variable_set($field, $value);
|
| 272 |
}
|
| 273 |
}
|
| 274 |
}
|
| 275 |
// Must invalidate blocks cache
|
| 276 |
cache_clear_all();
|
| 277 |
drupal_set_message(t('Updated block settings.'));
|
| 278 |
}
|
| 279 |
|
| 280 |
function taxonomy_blocks_count_nodes($tid, $type = 0) {
|
| 281 |
static $count;
|
| 282 |
|
| 283 |
if (!isset($count[$type])) {
|
| 284 |
// $type == 0 always evaluates TRUE if $type is a string
|
| 285 |
if (is_numeric($type)) {
|
| 286 |
$result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid'));
|
| 287 |
}
|
| 288 |
else {
|
| 289 |
$result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
|
| 290 |
}
|
| 291 |
while ($term = db_fetch_object($result)) {
|
| 292 |
$count[$type][$term->tid] = $term->c;
|
| 293 |
}
|
| 294 |
}
|
| 295 |
|
| 296 |
return $count[$type][$tid] + $children_count;
|
| 297 |
}
|
| 298 |
|
| 299 |
/**
|
| 300 |
* Theme the count of nodes shown beside a term link.
|
| 301 |
*
|
| 302 |
* @param $count
|
| 303 |
*
|
| 304 |
* @return HTML representation of node count.
|
| 305 |
*/
|
| 306 |
function theme_taxonomy_blocks_node_count($count) {
|
| 307 |
return ' ('. $count .')';
|
| 308 |
}
|
| 309 |
|
| 310 |
/**
|
| 311 |
* Implementation of hook_theme().
|
| 312 |
*/
|
| 313 |
function taxonomy_blocks_theme() {
|
| 314 |
return array(
|
| 315 |
'taxonomy_blocks_block_content' => array(
|
| 316 |
'arguments' => array('vid','index'),
|
| 317 |
),
|
| 318 |
'taxonomy_blocks_node_count' => array(
|
| 319 |
'arguments' => array('count'),
|
| 320 |
)
|
| 321 |
);
|
| 322 |
}
|