| 1 |
<?php
|
| 2 |
// $Id: taxonomy_ezfilter.module,v 1.3 2006/06/05 14:30:22 zealy Exp $
|
| 3 |
// mailto: i.zealy AT gmail dot com
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Implementation of hook_help
|
| 7 |
*/
|
| 8 |
function taxonomy_ezfilter_help($section) {
|
| 9 |
$output = "";
|
| 10 |
//drupal_set_message('tabs:'.$_SESSION['taxonomy_ezfilter_rewrited']);
|
| 11 |
if ($_SESSION['taxonomy_ezfilter_rewrited']) {
|
| 12 |
$output = "<ul class=\"tabs secondary\">\n<li>".t('View').' '. _taxonomy_ezfilter_form(false) ."</li></ul>\n";
|
| 13 |
return $output;
|
| 14 |
}
|
| 15 |
|
| 16 |
switch ($section) {
|
| 17 |
case "admin/modules#description":
|
| 18 |
$output = t("easy setup a taxonomy filter by another vocublary");
|
| 19 |
break;
|
| 20 |
}
|
| 21 |
return $output;
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_menu
|
| 26 |
*/
|
| 27 |
function taxonomy_ezfilter_menu($may_cache) {
|
| 28 |
$items = array();
|
| 29 |
if ($may_cache) {
|
| 30 |
}
|
| 31 |
else{
|
| 32 |
_taxonomy_ezfilter_form(true);
|
| 33 |
}
|
| 34 |
|
| 35 |
return $items;
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* hook_settings: Declare administrative settings for module daily.
|
| 40 |
*
|
| 41 |
* @note See hook_settings for a description of parameters and return values.
|
| 42 |
*/
|
| 43 |
function taxonomy_ezfilter_settings() {
|
| 44 |
$form = array();
|
| 45 |
$form['Config Vocabulary'] = array('#type' => 'fieldset',
|
| 46 |
'#title' => t('Config Vocabulary'),
|
| 47 |
'#collapsible' => TRUE,
|
| 48 |
'#description' => t('Choosing which vocabulary will be used for filter'),
|
| 49 |
);
|
| 50 |
|
| 51 |
$vocabularies = taxonomy_get_vocabularies();
|
| 52 |
$vocs = array();
|
| 53 |
foreach ($vocabularies as $vocabulary) {
|
| 54 |
$vocs[$vocabulary->vid] = $vocabulary->name;
|
| 55 |
}
|
| 56 |
$form['Config Vocabulary']['taxonomy_ezfilter_vocabulary'] = array('#type' => 'select',
|
| 57 |
'#title' => t('EzFilter Vocabulary'),
|
| 58 |
'#default_value' => variable_get('taxonomy_ezfilter_vocabulary', false),
|
| 59 |
'#options' => $vocs,
|
| 60 |
'#required' => TRUE,
|
| 61 |
'#description' => t('The vocabulary of filter must have to selected.'),
|
| 62 |
);
|
| 63 |
|
| 64 |
$form['Define filter level'] = array('#type' => 'fieldset',
|
| 65 |
'#title' => t('Define filter level'),
|
| 66 |
'#collapsible' => TRUE,
|
| 67 |
'#description' => t('Config filter level. Now this module support 2 levels dropdown selection in user interface. Results of levels deeper than 2 will be merged into result of level 2.'),
|
| 68 |
);
|
| 69 |
|
| 70 |
$form['Define filter level']['taxonomy_ezfilter_lv_1_catption'] = array('#type' => 'textfield',
|
| 71 |
'#title' => t('Define Level 1 caption'),
|
| 72 |
'#required' => TRUE,
|
| 73 |
'#default_value' => variable_get('taxonomy_ezfilter_lv_1_catption', 'Level 1'),
|
| 74 |
'#description' => t('This caption will be displayed in first dropdown selection in user interface.'),
|
| 75 |
);
|
| 76 |
$form['Define filter level']['taxonomy_ezfilter_lv_2_catption'] = array('#type' => 'textfield',
|
| 77 |
'#title' => t('Define Level 2 caption'),
|
| 78 |
'#required' => TRUE,
|
| 79 |
'#default_value' => variable_get('taxonomy_ezfilter_lv_2_catption', 'Level 2'),
|
| 80 |
'#description' => t('This caption will be displayed in second dropdown selection in user interface.'),
|
| 81 |
);
|
| 82 |
|
| 83 |
$form['Define filtered type'] = array('#type' => 'fieldset',
|
| 84 |
'#title' => t('Define filtered type'),
|
| 85 |
'#collapsible' => TRUE,
|
| 86 |
'#description' => t('Config filtered content by node\'s type.'),
|
| 87 |
);
|
| 88 |
|
| 89 |
if (variable_get('taxonomy_ezfilter_vocabulary', false)){
|
| 90 |
$node_types = node_get_types();
|
| 91 |
$vocab = taxonomy_get_vocabulary(variable_get('taxonomy_ezfilter_vocabulary', false));
|
| 92 |
$node_types = array_intersect_key($node_types, array_flip($vocab->nodes));
|
| 93 |
//var_dump($vocab);
|
| 94 |
$form['Define filtered type']['taxonomy_ezfilter_node_types'] = array(
|
| 95 |
'#type' => 'checkboxes',
|
| 96 |
'#title' => t('Node types'),
|
| 97 |
'#required' => FALSE,
|
| 98 |
'#default_value' => variable_get('taxonomy_ezfilter_node_types', NULL),
|
| 99 |
'#options' => $node_types,
|
| 100 |
'#description' => t('Select the content types for which you wish to filter. Only content types associated with filter vocabulary will be displayed here.')
|
| 101 |
);
|
| 102 |
}
|
| 103 |
else{
|
| 104 |
$form['Define filtered type']['taxonomy_ezfilter_node_types'] = array(
|
| 105 |
'#type' => 'markup',
|
| 106 |
'#title' => NULL,
|
| 107 |
'#value' => '<font color=red>'.t('You have not save vocabulary settings above!').'</font>'
|
| 108 |
);
|
| 109 |
}
|
| 110 |
|
| 111 |
return $form;
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Implementation of hook_db_rewrite_sql()
|
| 116 |
*/
|
| 117 |
function taxonomy_ezfilter_db_rewrite_sql($query, $table, $field) {
|
| 118 |
// let's cache
|
| 119 |
static $taxonomy_ezfilter_sql_clause;
|
| 120 |
static $taxonomy_ezfilter_rewrited;
|
| 121 |
if (!isset($taxonomy_ezfilter_sql_clause)) {
|
| 122 |
$taxonomy_ezfilter_sql_clause = array();
|
| 123 |
}
|
| 124 |
if (!isset($taxonomy_ezfilter_rewrited)) {
|
| 125 |
$taxonomy_ezfilter_rewrited = false;
|
| 126 |
}
|
| 127 |
|
| 128 |
|
| 129 |
if ($table == 'n' && $field == 'nid'){
|
| 130 |
//drupal_set_message('query:'.$query);
|
| 131 |
|
| 132 |
$query_md5 = md5($query);
|
| 133 |
if ($taxonomy_ezfilter_sql_clause[$query_md5]){
|
| 134 |
//drupal_set_message($taxonomy_ezfilter_sql_clause[md5($query)]);
|
| 135 |
return $taxonomy_ezfilter_sql_clause[$query_md5];
|
| 136 |
}
|
| 137 |
else{
|
| 138 |
$sql = drupal_strtolower($query);
|
| 139 |
$subsqls = preg_split('/select\s|\sfrom\s|\swhere\s/', $sql);
|
| 140 |
//var_dump($subsqls);
|
| 141 |
$types = array();
|
| 142 |
if (strstr($subsqls[1], 'nid')){
|
| 143 |
$tid = _taxonomy_ezfilter_get_tid();
|
| 144 |
if ((strstr($subsqls[3],'n.type =')||strstr($subsqls[3],'n.type like'))){
|
| 145 |
preg_match("/n\.type[\s+](?:=|like)[\s+]\'(.*?)\'/", $subsqls[3], $matches);
|
| 146 |
$type = trim($matches[1]);
|
| 147 |
if ($tid){
|
| 148 |
$term = taxonomy_get_term($tid);
|
| 149 |
// term of filter don't need to display filter form
|
| 150 |
if ($term->vid != variable_get('taxonomy_ezfilter_vocabulary', false)){
|
| 151 |
$types[$type] = $type;
|
| 152 |
}
|
| 153 |
}
|
| 154 |
else{
|
| 155 |
$types[$type] = $type;
|
| 156 |
}
|
| 157 |
}
|
| 158 |
else if ($tid){
|
| 159 |
$term = taxonomy_get_term($tid);
|
| 160 |
// term of filter don't need to display filter form
|
| 161 |
if ($term->vid != variable_get('taxonomy_ezfilter_vocabulary', false)){
|
| 162 |
$voc = taxonomy_get_vocabulary($term->vid);
|
| 163 |
foreach($voc->nodes as $id=>$type){
|
| 164 |
$types[$type] = $type;
|
| 165 |
}
|
| 166 |
}
|
| 167 |
}
|
| 168 |
|
| 169 |
$filte_types = variable_get('taxonomy_ezfilter_node_types', NULL);
|
| 170 |
|
| 171 |
//if (in_array($type, $filte_types, true)){
|
| 172 |
if (is_array($filte_types)){
|
| 173 |
$result = array_intersect_assoc($types, $filte_types);
|
| 174 |
}
|
| 175 |
//var_dump($result);
|
| 176 |
if (count($result)){
|
| 177 |
//drupal_set_message('sql:'.$sql);
|
| 178 |
//drupal_set_message('type:'.$type);
|
| 179 |
$taxonomy_ezfilter_rewrited = TRUE;
|
| 180 |
$_SESSION['taxonomy_ezfilter_rewrited'] = TRUE;
|
| 181 |
$taxonomy_ezfilter_sql_clause[$query_md5]['join'] = 'INNER JOIN {term_node} filter_tn ON n.nid = filter_tn.nid';
|
| 182 |
unset($term_id);
|
| 183 |
if ($_SESSION['ezfilter_lv_2_selected']){
|
| 184 |
$term_id = $_SESSION['ezfilter_lv_2_selected'];
|
| 185 |
}
|
| 186 |
else if ($_SESSION['ezfilter_lv_1_selected']){
|
| 187 |
$term_id = $_SESSION['ezfilter_lv_1_selected'];
|
| 188 |
}
|
| 189 |
|
| 190 |
if ($term_id){
|
| 191 |
unset($children);
|
| 192 |
$children = taxonomy_get_children($term_id);
|
| 193 |
$children[$term_id] = $term_id;
|
| 194 |
$tids = implode(',', array_keys($children));
|
| 195 |
|
| 196 |
if (strpos($tids, ',') === false){
|
| 197 |
$taxonomy_ezfilter_sql_clause[$query_md5]['where'] = "filter_tn.tid = $tids";
|
| 198 |
}
|
| 199 |
else{
|
| 200 |
$taxonomy_ezfilter_sql_clause[$query_md5]['where'] = "filter_tn.tid in ($tids)";
|
| 201 |
}
|
| 202 |
}
|
| 203 |
else{
|
| 204 |
$taxonomy_ezfilter_sql_clause[$query_md5]['join'] = NULL;
|
| 205 |
$taxonomy_ezfilter_sql_clause[$query_md5]['where'] = NULL;
|
| 206 |
}
|
| 207 |
|
| 208 |
return $taxonomy_ezfilter_sql_clause[$query_md5];
|
| 209 |
}
|
| 210 |
else{
|
| 211 |
if (!$taxonomy_ezfilter_rewrited)
|
| 212 |
$_SESSION['taxonomy_ezfilter_rewrited'] = NULL;
|
| 213 |
}
|
| 214 |
}
|
| 215 |
}
|
| 216 |
}
|
| 217 |
else{
|
| 218 |
if (!$taxonomy_ezfilter_rewrited)
|
| 219 |
$_SESSION['taxonomy_ezfilter_rewrited'] = NULL;
|
| 220 |
}
|
| 221 |
}
|
| 222 |
|
| 223 |
// Supporting Function, Get current term id form current url
|
| 224 |
function _taxonomy_ezfilter_get_tid(){
|
| 225 |
$tid = 0;
|
| 226 |
// flexinode/list/$ftypeid/$tid/
|
| 227 |
if ((arg(0) == 'flexinode') && (arg(1) == 'list') && is_numeric(arg(2)) && is_numeric(arg(3))){
|
| 228 |
$tid = arg(3);
|
| 229 |
}
|
| 230 |
// forum/$tid
|
| 231 |
if ((arg(0) == 'forum') && is_numeric(arg(1))){
|
| 232 |
$tid = arg(1);
|
| 233 |
}
|
| 234 |
// image/tid/$tid
|
| 235 |
if ((arg(0) == 'image') && (arg(1) == 'tid') && is_numeric(arg(2))){
|
| 236 |
$tid = arg(2);
|
| 237 |
}
|
| 238 |
// taxonomy/term/$tid
|
| 239 |
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && is_numeric(arg(2))){
|
| 240 |
$tid = arg(2);
|
| 241 |
}
|
| 242 |
// node/add/forum/$tid/
|
| 243 |
/*
|
| 244 |
if ((arg(0) == 'node') && (arg(1) == 'add') && (arg(2) == 'forum') && is_numeric(arg(3))){
|
| 245 |
$tid = arg(3);
|
| 246 |
}
|
| 247 |
*/
|
| 248 |
// node/$nid
|
| 249 |
// WARNING: Can't process node assciated with more than one term except country term
|
| 250 |
/*
|
| 251 |
if ((arg(0) == 'node') && is_numeric(arg(1))) {
|
| 252 |
$term = _taxonomy_ezfilter_get_node_term(arg(1));
|
| 253 |
// Country Only node such as photoes and travelog will return false
|
| 254 |
if ($term){
|
| 255 |
$tid = $term->tid;
|
| 256 |
}
|
| 257 |
}
|
| 258 |
*/
|
| 259 |
// comment/reply/$nid
|
| 260 |
// WARNING: Can't process node assciated with more than one term except country term
|
| 261 |
/*
|
| 262 |
if ((arg(0) == 'comment') && (arg(1) == 'reply') && is_numeric(arg(2))) {
|
| 263 |
$term = _taxonomy_ezfilter_get_node_term(arg(2));
|
| 264 |
// Country Only node such as photoes and travelog will return false
|
| 265 |
if ($term){
|
| 266 |
$tid = $term->tid;
|
| 267 |
}
|
| 268 |
}
|
| 269 |
*/
|
| 270 |
return $tid;
|
| 271 |
}
|
| 272 |
|
| 273 |
function _taxonomy_ezfilter_get_node_term($nid){
|
| 274 |
$terms = taxonomy_ezfilter_get_terms_not_vocabulary($nid, variable_get('taxonomy_ezfilter_vocabulary', false));
|
| 275 |
$term = current($terms);
|
| 276 |
$tid = $term->tid;
|
| 277 |
return taxonomy_get_term($tid);
|
| 278 |
}
|
| 279 |
|
| 280 |
/**
|
| 281 |
* Find all terms associated to the given node, and NOT in one vocabulary.
|
| 282 |
*/
|
| 283 |
function taxonomy_ezfilter_get_terms_not_vocabulary($nid, $vid, $key = 'tid') {
|
| 284 |
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid != %d AND r.nid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
|
| 285 |
$terms = array();
|
| 286 |
while ($term = db_fetch_object($result)) {
|
| 287 |
$terms[$term->$key] = $term;
|
| 288 |
}
|
| 289 |
return $terms;
|
| 290 |
}
|
| 291 |
|
| 292 |
// Generate Filter Form
|
| 293 |
// Support 2 Level Terms Only
|
| 294 |
function _taxonomy_ezfilter_form($only_js = false){
|
| 295 |
static $js_head = false;
|
| 296 |
$form = array();
|
| 297 |
|
| 298 |
$countries = taxonomy_get_children(0,variable_get('taxonomy_ezfilter_vocabulary', false));
|
| 299 |
$countries_array = array();
|
| 300 |
$countries_array[0] = t(variable_get('taxonomy_ezfilter_lv_1_catption', 'Level 1'));
|
| 301 |
|
| 302 |
$javalet = "";
|
| 303 |
$sn = 1;
|
| 304 |
foreach ($countries as $country) {
|
| 305 |
$countries_array[$country->tid] = $country->name;
|
| 306 |
$javalet .= _taxonomy_ezfilter_form_javalet($sn, $country->tid);
|
| 307 |
$sn++;
|
| 308 |
}
|
| 309 |
|
| 310 |
$form['level_1_selected'] = array('#type' => 'select',
|
| 311 |
'#title' => '',
|
| 312 |
'#default_value' => $_SESSION['ezfilter_lv_1_selected'],
|
| 313 |
'#options' => $countries_array,
|
| 314 |
'#description' => '',
|
| 315 |
'#attributes' => empty($javalet)? NULL: array('onchange' => 'javascript:selectCities();')
|
| 316 |
);
|
| 317 |
|
| 318 |
$javascript = '
|
| 319 |
<script type="text/javascript" language="javascript">
|
| 320 |
function selectCities(){
|
| 321 |
//alert(document.all("edit-level_2_selected")(1).length);
|
| 322 |
//for(i = 0; i < document.all("edit-level_2_selected").length; i++){
|
| 323 |
while(document.getElementById("edit-level_2_selected").options.length)
|
| 324 |
{
|
| 325 |
document.getElementById("edit-level_2_selected").options[0] = null;
|
| 326 |
}
|
| 327 |
switch(document.getElementById("edit-level_1_selected").selectedIndex){
|
| 328 |
case 0:
|
| 329 |
var arrCityNames = new Array();
|
| 330 |
arrCityNames[0] = "'.variable_get('taxonomy_ezfilter_lv_2_catption', 'Level 2').';0";
|
| 331 |
document.getElementById("edit-level_2_selected").selectedIndex = 0;
|
| 332 |
break;
|
| 333 |
';
|
| 334 |
$javascript .= $javalet;
|
| 335 |
//////////////////////////////////////////////////////////
|
| 336 |
// JavaScriptlet looks like:
|
| 337 |
//--------------------------------------------------------
|
| 338 |
// case 1:
|
| 339 |
// var arrCityNames = new Array();
|
| 340 |
// arrCityNames[0] = "All Cities;0";
|
| 341 |
// arrCityNames[1] = "City Term Name;City Term ID";
|
| 342 |
// break;
|
| 343 |
///////////////////////////////////////////////////////////
|
| 344 |
$javascript .= '
|
| 345 |
default:
|
| 346 |
var arrCityNames = new Array();
|
| 347 |
arrCityNames[0] = "'.variable_get('taxonomy_ezfilter_lv_2_catption', 'Level 2').';0";
|
| 348 |
//alert(document.getElementById("edit-level_1_selected").selectedIndex);
|
| 349 |
}
|
| 350 |
for(j = 0; j < arrCityNames.length; j++){
|
| 351 |
Names = arrCityNames[j].split(";");
|
| 352 |
document.getElementById("edit-level_2_selected").options[j] = new Option(Names[0], Names[1]);
|
| 353 |
}
|
| 354 |
document.getElementById("edit-level_2_selected").selectedIndex = 0;
|
| 355 |
//}
|
| 356 |
}
|
| 357 |
</script>
|
| 358 |
';
|
| 359 |
if (!$js_head && !empty($javalet)){
|
| 360 |
drupal_set_html_head($javascript);
|
| 361 |
$js_head = true;
|
| 362 |
}
|
| 363 |
|
| 364 |
$city_array = array();
|
| 365 |
|
| 366 |
if ($_SESSION['ezfilter_lv_1_selected']){
|
| 367 |
$city_array[0] = t('All '.variable_get('taxonomy_ezfilter_lv_2_catption', 'Level 2'));
|
| 368 |
$cities = taxonomy_get_children($_SESSION['ezfilter_lv_1_selected'], variable_get('taxonomy_ezfilter_vocabulary', false));
|
| 369 |
foreach ($cities as $city) {
|
| 370 |
$city_array[$city->tid] = $city->name;
|
| 371 |
}
|
| 372 |
}
|
| 373 |
else
|
| 374 |
$city_array[0] = t(variable_get('taxonomy_ezfilter_lv_2_catption', 'Level 2'));
|
| 375 |
|
| 376 |
if (!empty($javalet)){
|
| 377 |
$form['level_2_selected'] = array('#type' => 'select',
|
| 378 |
'#title' => '',
|
| 379 |
'#default_value' => $_SESSION['ezfilter_lv_2_selected'],
|
| 380 |
'#options' => $city_array,
|
| 381 |
'#description' => '',
|
| 382 |
'#validated' => true
|
| 383 |
);
|
| 384 |
}
|
| 385 |
|
| 386 |
$form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Go') );
|
| 387 |
|
| 388 |
if ($only_js)
|
| 389 |
return '';
|
| 390 |
else
|
| 391 |
return drupal_get_form('ezfilter_select', $form, 'taxonomy_ezfilter_form');
|
| 392 |
}
|
| 393 |
|
| 394 |
function _taxonomy_ezfilter_form_javalet($sn, $tid){
|
| 395 |
$cities = taxonomy_get_children($tid, variable_get('taxonomy_ezfilter_vocabulary', false));
|
| 396 |
if ($cities){
|
| 397 |
$javalet = "case $sn:var arrCityNames = new Array();arrCityNames[0] = \"".t("All ".variable_get('taxonomy_ezfilter_lv_2_catption', 'Level 2')).";0\";";
|
| 398 |
$array_index = 1;
|
| 399 |
foreach ($cities as $city){
|
| 400 |
$javalet .= "arrCityNames[$array_index] = \"$city->name;$city->tid\";";
|
| 401 |
$array_index++;
|
| 402 |
}
|
| 403 |
$javalet .= 'break;';
|
| 404 |
}
|
| 405 |
return $javalet;
|
| 406 |
}
|
| 407 |
|
| 408 |
function taxonomy_ezfilter_form_submit($form_id, $form_values){
|
| 409 |
//drupal_set_message("$form_id");
|
| 410 |
$_SESSION['ezfilter_lv_1_selected'] = $form_values['level_1_selected'];
|
| 411 |
$_SESSION['ezfilter_lv_2_selected'] = $form_values['level_2_selected'];
|
| 412 |
}
|
| 413 |
|
| 414 |
if (!function_exists('array_intersect_key'))
|
| 415 |
{
|
| 416 |
function array_intersect_key($arr1, $arr2) {
|
| 417 |
$res = array();
|
| 418 |
foreach($arr1 as $key=>$value) {
|
| 419 |
$push = true;
|
| 420 |
for ($i = 1; $i < func_num_args(); $i++) {
|
| 421 |
$actArray = func_get_arg($i);
|
| 422 |
if (gettype($actArray) != 'array') return false;
|
| 423 |
if (!array_key_exists($key, $actArray)) $push = false;
|
| 424 |
}
|
| 425 |
if ($push) $res[$key] = $arr1[$key];
|
| 426 |
}
|
| 427 |
return $res;
|
| 428 |
}
|
| 429 |
}
|
| 430 |
|
| 431 |
?>
|