| 1 |
<?php |
<?php |
| 2 |
|
|
| 3 |
// $Id: taxonomy_sifter.module,v 1.3.2.2 2008/11/15 10:57:50 tbarregren Exp $ |
// $Id: taxonomy_sifter.module,v 1.3.2.3 2008/11/15 11:12:08 tbarregren Exp $ |
| 4 |
|
|
| 5 |
|
|
| 6 |
/** |
/** |
| 7 |
* @file |
* @file |
| 8 |
* Taxonomy Sifter - a Drupal module that provides a block for |
* Taxonomy Sifter - provides a block for selecting a term to filter nodes. |
|
* selecting a term to view or filter nodes. |
|
| 9 |
* |
* |
| 10 |
* Authors: |
* Authors: |
| 11 |
* Thomas Barregren <http://drupal.org/user/16678>. |
* Thomas Barregren <http://drupal.org/user/16678>. |
| 25 |
* Implementation of hook_perm(). |
* Implementation of hook_perm(). |
| 26 |
*/ |
*/ |
| 27 |
function taxonomy_sifter_perm() { |
function taxonomy_sifter_perm() { |
| 28 |
return array('administer', 'view sifter block'); |
return array('administer taxonomy sifter', 'view sifter block'); |
| 29 |
} |
} |
| 30 |
|
|
| 31 |
|
|
| 34 |
*/ |
*/ |
| 35 |
function taxonomy_sifter_menu($may_cache) { |
function taxonomy_sifter_menu($may_cache) { |
| 36 |
$items = array(); |
$items = array(); |
| 37 |
if ($may_cache) { |
if (!$may_cache) { |
|
$items[] = array( |
|
|
'path' => 'admin/settings/taxonomy_sifter', |
|
|
'title' => t('Taxonomy Sifter'), |
|
|
'callback' => 'taxonomy_sifter_admin', |
|
|
'description' => t('Provides a block for selecting a term to view or filter nodes'), |
|
|
'access' => user_access('administer') |
|
|
); |
|
|
} |
|
|
else { |
|
| 38 |
$items[] = array( |
$items[] = array( |
| 39 |
'path' => 'taxonomy/term', |
'path' => 'taxonomy/term', |
|
'callback' => 'taxonomy_sifter_taxonomy_term_filter', |
|
| 40 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 41 |
|
'callback' => 'taxonomy_sifter_taxonomy_term_filter', |
| 42 |
'access' => user_access('access content'), |
'access' => user_access('access content'), |
| 43 |
); |
); |
| 44 |
} |
} |
| 71 |
|
|
| 72 |
|
|
| 73 |
/***************************************************************************** |
/***************************************************************************** |
| 74 |
* SYSTEM SETTINGS |
* BLOCK API |
| 75 |
*****************************************************************************/ |
*****************************************************************************/ |
| 76 |
|
|
| 77 |
function taxonomy_sifter_admin() { |
/** |
| 78 |
|
* Blocks are listed. |
| 79 |
|
*/ |
| 80 |
|
function _taxonomy_sifter_block_list() { |
| 81 |
|
$blocks[0]['info'] = t('Taxonomy Sifter'); |
| 82 |
|
return $blocks; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
function _taxonomy_sifter_block_configure($delta, $edit) { |
| 87 |
|
|
| 88 |
|
// Check that the user has permission to use the taxonomy sifter. |
| 89 |
|
if (!user_access('administer taxonomy sifter')) { |
| 90 |
|
$form['taxonomy_sifter'] = array( |
| 91 |
|
'#type' => 'markup', |
| 92 |
|
'#value' => '<p>'. t('You do not have permission to use the taxonomy sifter.') .'</p>', |
| 93 |
|
); |
| 94 |
|
return $form; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
// Get all available vocabularies. |
// Get all available vocabularies. |
| 98 |
$vocabularies = _taxonomy_sifter_db_get_vocabularies(); |
$vocabularies = _taxonomy_sifter_db_get_vocabularies(); |
| 99 |
|
|
| 100 |
// Show checkboxes with roles that can be delegated if any. |
// Show checkboxes with roles that can be delegated if any. |
| 101 |
if ($vocabularies) { |
if ($vocabularies) { |
| 102 |
$form['taxonomy_sifter_vocabularies'] = array( |
$form['taxonomy_sifter'] = array( |
| 103 |
|
'#type' => 'fieldset', |
| 104 |
|
'#title' => t('Taxonomy Sifter Settings'), |
| 105 |
|
'#collapsible' => true, |
| 106 |
|
'#collapsed' => false, |
| 107 |
|
); |
| 108 |
|
$form['taxonomy_sifter']['taxonomy_sifter_vocabularies'] = array( |
| 109 |
'#type' => 'checkboxes', |
'#type' => 'checkboxes', |
| 110 |
'#title' => t('Vocabularies'), |
'#title' => t('Vocabularies'), |
| 111 |
'#options' => $vocabularies, |
'#options' => $vocabularies, |
| 112 |
'#default_value' => _taxonomy_sifter_variable_get_vocabularies(), |
'#default_value' => _taxonomy_sifter_variable_vocabularies(), |
| 113 |
'#description' => t('Select vocabularies that should be available in the Taxonomy Sifter block.'), |
'#description' => t('Select vocabularies that should be available in the Taxonomy Sifter block.'), |
| 114 |
|
'#prefix' => '<div class="taxonomy-sifter-checkboxes">', |
| 115 |
|
'#suffix' => '</div>', |
| 116 |
); |
); |
| 117 |
$form['taxonomy_sifter_operator'] = array( |
$form['taxonomy_sifter']['taxonomy_sifter_operator'] = array( |
| 118 |
'#type' => 'radios', |
'#type' => 'radios', |
| 119 |
'#title' => t('Deafult operator'), |
'#title' => t('Default operator'), |
| 120 |
'#options' => array('or (any term)', 'and (all terms)'), |
'#options' => array(t('or (any term)'), t('and (all terms)')), |
| 121 |
'#default_value' => _taxonomy_sifter_variable_get_operator(), |
'#default_value' => _taxonomy_sifter_variable_operator(), |
| 122 |
'#description' => t('Select the default term filter. When a taxonomy term page or feed is requested, the given terms are combined with those selected in the Taxonomy Sifter block. If more than one term is given in the request, the combination is done with the operator used in the request. If only one term is given, the operator selected above is used.'), |
'#description' => t('Select the default term filter. When a taxonomy term page or feed is requested, the given terms are combined with those selected in the Taxonomy Sifter block. If more than one term is given in the request, the combination is done with the operator used in the request. If only one term is given, the operator selected above is used.'), |
| 123 |
|
'#prefix' => '<div class="taxonomy-sifter-radios">', |
| 124 |
|
'#suffix' => '</div>', |
| 125 |
); |
); |
| 126 |
} |
} |
| 127 |
else { |
else { |
| 128 |
$form['taxonomy_sifter_vocabularies'] = array( |
$form['taxonomy_sifter'] = array( |
| 129 |
'#type' => 'markup', |
'#type' => 'markup', |
| 130 |
'#value' => '<p>No vocabulary is available.</p>', |
'#value' => '<p>'. t('No vocabulary is available.') .'</p>', |
| 131 |
); |
); |
| 132 |
} |
} |
| 133 |
|
|
| 134 |
// Return system settings form. |
return $form; |
|
return system_settings_form('taxonomy_sifter_admin', $form); |
|
| 135 |
|
|
| 136 |
} |
} |
| 137 |
|
|
| 138 |
|
|
|
/***************************************************************************** |
|
|
* BLOCK API |
|
|
*****************************************************************************/ |
|
|
|
|
| 139 |
/** |
/** |
| 140 |
* Blocks are listed. |
* Save block setings. |
| 141 |
*/ |
*/ |
| 142 |
function _taxonomy_sifter_block_list() { |
function _taxonomy_sifter_block_save($delta, $edit) { |
| 143 |
$blocks[0]['info'] = t('Taxonomy Sifter'); |
_taxonomy_sifter_variable_vocabularies($edit['vocabularies']); |
| 144 |
return $blocks; |
_taxonomy_sifter_variable_operator($edit['operator']); |
| 145 |
} |
} |
| 146 |
|
|
| 147 |
|
|
| 173 |
$vocabularies = _taxonomy_sifter_db_get_vocabularies(); |
$vocabularies = _taxonomy_sifter_db_get_vocabularies(); |
| 174 |
|
|
| 175 |
// Get the available vocabularies. |
// Get the available vocabularies. |
| 176 |
$vids = _taxonomy_sifter_variable_get_vocabularies(); |
$vids = _taxonomy_sifter_variable_vocabularies(); |
| 177 |
$vids = array_filter($vids); |
$vids = array_filter($vids); |
| 178 |
|
|
| 179 |
// For each vocabulary, add a select element of its terms. |
// For each vocabulary, add a select element of its terms. |
| 183 |
$options = array(); |
$options = array(); |
| 184 |
$tree = taxonomy_get_tree($vid); |
$tree = taxonomy_get_tree($vid); |
| 185 |
foreach ($tree as $term) { |
foreach ($tree as $term) { |
| 186 |
$options[$term->tid] = _taxonomy_depth($term->depth, '-') . $term->name; |
$options[$term->tid] = str_repeat($term->depth, '-') . $term->name; |
| 187 |
} |
} |
| 188 |
|
|
| 189 |
// Build and return the select element. |
// Build and return the select element. |
| 190 |
$form["taxonomy_sifter_terms_$vid"] = array( |
$form["taxonomy_sifter_terms_$vid"] = array( |
| 191 |
'#title' => 'Sift by ' . $vocabularies[$vid], |
'#title' => 'Sift by '. $vocabularies[$vid], |
| 192 |
'#type' => 'select', // TODO: Replace with checkboxes. See http://drupal.org/node/187413. |
'#type' => 'select', // TODO: Replace with checkboxes. See http://drupal.org/node/187413. |
| 193 |
'#multiple' => true, |
'#multiple' => true, |
| 194 |
'#options' => $options, |
'#options' => $options, |
| 228 |
|
|
| 229 |
|
|
| 230 |
/***************************************************************************** |
/***************************************************************************** |
| 231 |
* Filtering by taxonomy |
* FILTERING BY TAXONOMY |
| 232 |
*****************************************************************************/ |
*****************************************************************************/ |
| 233 |
|
|
| 234 |
/** |
/** |
| 235 |
* Intercept the 'taxonomy/term' callback function to complement the terms' id |
* Intercept the 'taxonomy/term' callback function to complement the terms' id |
| 236 |
* in the URL with those of he terms selcted in the block. |
* in the URL with those of the terms selcted in the block. |
| 237 |
*/ |
*/ |
| 238 |
function taxonomy_sifter_taxonomy_term_filter($terms, $depth = 0, $feed = 'page') { |
function taxonomy_sifter_taxonomy_term_filter($terms, $depth = 0, $feed = 'page') { |
| 239 |
$terms = _taxonomy_sifter_rewrite_filter($terms); |
$terms = _taxonomy_sifter_rewrite_filter($terms); |
| 259 |
// no such operator is avaiable, we use the configured default operator. |
// no such operator is avaiable, we use the configured default operator. |
| 260 |
// Note 2: a plus sign in the URL is converted to space. |
// Note 2: a plus sign in the URL is converted to space. |
| 261 |
$op_lut = array(' ', ','); |
$op_lut = array(' ', ','); |
| 262 |
$op_def = _taxonomy_sifter_variable_get_operator(); |
$op_def = _taxonomy_sifter_variable_operator(); |
| 263 |
$op = $op_lut[$op_def]; |
$op = $op_lut[$op_def]; |
| 264 |
$filter = explode($op, $filter); |
$filter = explode($op, $filter); |
| 265 |
if (count($filter) == 1) { |
if (count($filter) == 1) { |
| 314 |
*****************************************************************************/ |
*****************************************************************************/ |
| 315 |
|
|
| 316 |
/** |
/** |
| 317 |
* Returns the selected vocabularies. |
* Sets and gets selected vocabularies. |
| 318 |
|
*/ |
| 319 |
|
function _taxonomy_sifter_variable_vocabularies($vocabularies = null) { |
| 320 |
|
return _taxonomy_sifter_variable('taxonomy_sifter_vocabularies', $vocabularies, array()); |
| 321 |
|
} |
| 322 |
|
|
| 323 |
|
|
| 324 |
|
/** |
| 325 |
|
* Sets and gets selected operator. |
| 326 |
*/ |
*/ |
| 327 |
function _taxonomy_sifter_variable_get_vocabularies() { |
function _taxonomy_sifter_variable_operator($operator = null) { |
| 328 |
return variable_get('taxonomy_sifter_vocabularies', array()); |
return _taxonomy_sifter_variable('taxonomy_sifter_operator', $operator, 0); |
| 329 |
} |
} |
| 330 |
|
|
| 331 |
|
|
| 332 |
/** |
/** |
| 333 |
* Returns the selected operator. |
* Sets and gets the named persisted variable. |
| 334 |
*/ |
*/ |
| 335 |
function _taxonomy_sifter_variable_get_operator() { |
function _taxonomy_sifter_variable($name, $value = null, $default = null) { |
| 336 |
return variable_get('taxonomy_sifter_operator', 'all'); |
if (isset($value)) { |
| 337 |
|
variable_set($name, $value); |
| 338 |
|
} |
| 339 |
|
return variable_get($name, $default); |
| 340 |
} |
} |
| 341 |
|
|
| 342 |
|
|
| 366 |
return $terms; |
return $terms; |
| 367 |
} |
} |
| 368 |
|
|
| 369 |
|
|
| 370 |
/** |
/** |
| 371 |
* Set the selected terms. |
* Set the selected terms. |
| 372 |
*/ |
*/ |
| 373 |
function _taxonomy_sifter_session_set_terms($form_id, $terms = array()) { |
function _taxonomy_sifter_session_set_terms($form_id, $terms = array()) { |
| 374 |
$_SESSION[$form_id] = $terms; |
$_SESSION['taxonomy_sifter_terms'] = $terms; |
| 375 |
} |
} |
| 376 |
|
|
| 377 |
|
|
| 391 |
if (!$help) return; |
if (!$help) return; |
| 392 |
|
|
| 393 |
// Substitute variables and translate. |
// Substitute variables and translate. |
| 394 |
$version = str_replace(array('$Re'.'vision:', ' $'), array('', ''), '$Revision: 1.3.2.2 $'); |
$version = str_replace(array('$Re'.'vision:', ' $'), array('', ''), '$Revision: 1.3.2.3 $'); |
| 395 |
$year = substr('$Date: 2008/11/15 10:57:50 $', 7, 4); |
$year = substr('$Date: 2008/11/15 11:12:08 $', 7, 4); |
| 396 |
$help = t($help, array('!version' => $version, '!year' => $year)); |
$help = t($help, array('!version' => $version, '!year' => $year)); |
| 397 |
|
|
| 398 |
// Add some style. (This is really dirty, but...) |
// Add some style. (This is really dirty, but...) |