| 1 |
<?php |
<?php |
| 2 |
// $Id: flexifilter.module,v 1.16.2.2 2008/04/17 23:30:14 cwgordon7 Exp $ |
// $Id: flexifilter.module,v 1.16.2.3 2008/05/12 19:59:15 cwgordon7 Exp $ |
| 3 |
|
|
| 4 |
$path = drupal_get_path('module', 'flexifilter'); |
$path = drupal_get_path('module', 'flexifilter'); |
| 5 |
include_once ($path .'/flexifilter.components.inc'); |
include_once ($path .'/flexifilter.components.inc'); |
| 287 |
// For prepare and process, force the return value to TRUE or FALSE |
// For prepare and process, force the return value to TRUE or FALSE |
| 288 |
case 'prepare': |
case 'prepare': |
| 289 |
case 'process': |
case 'process': |
| 290 |
if ($return_value) { |
return (bool)$return_value; |
|
return TRUE; |
|
|
} |
|
|
else { |
|
|
return FALSE; |
|
|
} |
|
| 291 |
} |
} |
| 292 |
} |
} |
| 293 |
|
|
| 306 |
function flexifilter_invoke_components($components, $op, $text) { |
function flexifilter_invoke_components($components, $op, $text) { |
| 307 |
$component_list = flexifilter_get_component_list(); |
$component_list = flexifilter_get_component_list(); |
| 308 |
foreach ($components as $key => $component) { |
foreach ($components as $key => $component) { |
| 309 |
if ($key !== 'id_prefix' && $key !== 'id_next') { |
$class = $component_list[$component['class']]; |
| 310 |
$class = $component_list[$component['class']]; |
if ($class['step'] == 'both' || $component['settings']['step'] == $op || $class['step'] == $op) { |
| 311 |
if ($class['step'] == 'both' || $component['settings']['step'] == $op || $class['step'] == $op) { |
$text = flexifilter_invoke_component($class, $op, $component['settings'], $text); |
|
$text = flexifilter_invoke_component($class, $op, $component['settings'], $text); |
|
|
} |
|
| 312 |
} |
} |
| 313 |
} |
} |
| 314 |
return $text; |
return $text; |
| 325 |
function flexifilter_components_involve_step($components, $step) { |
function flexifilter_components_involve_step($components, $step) { |
| 326 |
$component_list = flexifilter_get_component_list(); |
$component_list = flexifilter_get_component_list(); |
| 327 |
foreach ($components as $key => $component) { |
foreach ($components as $key => $component) { |
| 328 |
if ($key !== 'id_prefix' && $key !== 'id_next') { |
$class = $component_list[$component['class']]; |
| 329 |
$class = $component_list[$component['class']]; |
// If step is explicity set to the one being tested, then it is involved |
| 330 |
// If step is explicity set to the one being tested, then it is involved |
if ((isset($component['step']) && $component['step'] == $step) || $class['step'] == $step) { |
| 331 |
if ((isset($component['step']) && $component['step'] == $step) || $class['step'] == $step) { |
return TRUE; |
| 332 |
return TRUE; |
} |
| 333 |
} |
// If step is set to both, then it is involved (unless it is a container) |
| 334 |
// If step is set to both, then it is involved (unless it is a container) |
if ($class['is_container'] !== TRUE && $class['step'] == 'both') { |
| 335 |
if ($class['is_container'] !== TRUE && $class['step'] == 'both') { |
return TRUE; |
| 336 |
return TRUE; |
} |
| 337 |
} |
// Finally, if one of its children is involed, then it is |
| 338 |
// Finally, if one of its children is involed, then it is |
if (isset($component['components']) && flexifilter_components_involve_step($component['components'], $step)) { |
| 339 |
if (isset($component['components']) && flexifilter_components_involve_step($component['components'], $step)) { |
return TRUE; |
|
return TRUE; |
|
|
} |
|
| 340 |
} |
} |
| 341 |
} |
} |
| 342 |
return FALSE; |
return FALSE; |
| 405 |
function flexifilter_filter_tips($delta, $format, $long = FALSE) { |
function flexifilter_filter_tips($delta, $format, $long = FALSE) { |
| 406 |
$flexifilter = flexifilter_get_filter_by_delta($delta); |
$flexifilter = flexifilter_get_filter_by_delta($delta); |
| 407 |
if ($long) { |
if ($long) { |
| 408 |
return str_replace('<--break-->', '', $flexifilter['description']); |
return str_replace('<!--break-->', '', $flexifilter['description']); |
| 409 |
} |
} |
| 410 |
else { |
else { |
| 411 |
$pos = strpos($flexifilter['description'], '<!--break-->'); |
$pos = strpos($flexifilter['description'], '<!--break-->'); |
| 421 |
* |
* |
| 422 |
* @param $fid |
* @param $fid |
| 423 |
* The uniqe flexifilter id of the flexifilter to load. |
* The uniqe flexifilter id of the flexifilter to load. |
| 424 |
|
* @param $refresh |
| 425 |
|
* If TRUE, refreshes the static cache. Defaults to FALSE. |
| 426 |
* @return |
* @return |
| 427 |
* The flexifilter upon success; FALSE upon failure. |
* The flexifilter upon success; FALSE upon failure. |
| 428 |
*/ |
*/ |
| 429 |
function flexifilter_load($fid) { |
function flexifilter_load($fid, $refresh = FALSE) { |
| 430 |
if (!is_numeric($fid)) { |
if (!is_numeric($fid) && !is_null($fid)) { |
| 431 |
return FALSE; |
return FALSE; |
| 432 |
} |
} |
| 433 |
$result = db_query('SELECT * FROM {flexifilters} WHERE fid = %d', $fid); |
static $flexifilters = array(); |
| 434 |
if ($row = db_fetch_object($result)) { |
if ($refresh) { |
| 435 |
return _flexifilter_filter_from_db_row($row, TRUE); |
$flexifilters = array(); |
| 436 |
|
} |
| 437 |
|
if (!isset($flexifilters[$fid])) { |
| 438 |
|
if (!is_null($fid)) { |
| 439 |
|
$result = db_query('SELECT * FROM {flexifilters} f LEFT JOIN {flexifilters_parts} fp ON f.fid = fp.fid WHERE f.fid = %d ORDER BY fp.weight DESC', $fid); |
| 440 |
|
} |
| 441 |
|
else { |
| 442 |
|
$result = db_query('SELECT * FROM {flexifilters} f LEFT JOIN {flexifilters_parts} fp ON f.fid = fp.fid ORDER BY fp.weight DESC'); |
| 443 |
|
} |
| 444 |
|
$parts = array(); |
| 445 |
|
$structure = array(); |
| 446 |
|
$flexifilter = array(); |
| 447 |
|
while ($row = db_fetch_array($result)) { |
| 448 |
|
if (!empty($row['class_name'])) { |
| 449 |
|
if (!empty($row['parent_pid'])) { |
| 450 |
|
$structure[$row['parent_pid']][] = $row['pid']; |
| 451 |
|
} |
| 452 |
|
else { |
| 453 |
|
$structure['root'][] = $row['pid']; |
| 454 |
|
} |
| 455 |
|
$parts[$row['pid']] = array( |
| 456 |
|
'pid' => $row['pid'], |
| 457 |
|
'fid' => $row['fid'], |
| 458 |
|
'parent_pid' => $row['parent_pid'], |
| 459 |
|
'type' => $row['type'], |
| 460 |
|
'class_name' => $row['class_name'], |
| 461 |
|
'weight' => $row['weight'], |
| 462 |
|
'settings' => unserialize($row['settings']), |
| 463 |
|
); |
| 464 |
|
} |
| 465 |
|
if (!empty($row['label'])) { |
| 466 |
|
$flexifilter = array( |
| 467 |
|
'fid' => $row['fid'], |
| 468 |
|
'label' => $row['label'], |
| 469 |
|
'description' => $row['description'], |
| 470 |
|
'enabled' => $row['enabled'], |
| 471 |
|
'delta' => $row['delta'], |
| 472 |
|
'advanced' => $row['advanced'], |
| 473 |
|
'cache' => $row['cache'], |
| 474 |
|
); |
| 475 |
|
} |
| 476 |
|
} |
| 477 |
|
if (!empty($structure)) { |
| 478 |
|
//_flexifilter_arrange_parts($flexifilter, $parts, $structure); |
| 479 |
|
} |
| 480 |
|
$flexifilters[$fid] = $flexifilter; |
| 481 |
|
print_r($flexifilter); |
| 482 |
|
} |
| 483 |
|
return $flexifilters[$fid]; |
| 484 |
|
} |
| 485 |
|
|
| 486 |
|
/** |
| 487 |
|
* Helper function for flexifilter_load(). |
| 488 |
|
*/ |
| 489 |
|
function _flexifilter_arrange_parts(&$flexifilter, &$parts, $structure) { |
| 490 |
|
foreach ($structure['root'] as $pid) { |
| 491 |
|
$flexifilter['components'][$pid] = $parts[$pid]; |
| 492 |
|
unset($structure['root'][$pid]); |
| 493 |
|
_flexifilter_arrange_parts($flexifilter['components'][$pid], $parts, $structure); |
| 494 |
|
} |
| 495 |
|
unset($structure['root']); |
| 496 |
|
foreach ($structure as $parent => $pids) { |
| 497 |
|
foreach ($pids as $pid) { |
| 498 |
|
switch ($parts[$pid]) { |
| 499 |
|
case FLEXIFILTER_PART_TYPE_COMPONENT: |
| 500 |
|
$flexifilter['components'][$pid] = $parts[$pid]; |
| 501 |
|
unset($structure['root'][$pid]); |
| 502 |
|
_flexifilter_arrange_parts($flexifilter['components'][$pid], $parts, $structure); |
| 503 |
|
break; |
| 504 |
|
|
| 505 |
|
case FLEXIFILTER_PART_TYPE_CONDITION: |
| 506 |
|
switch($parts[$parts[$pid]['parent_pid']]['type']) { |
| 507 |
|
case FLEXIFILTER_PART_TYPE_COMPONENT: |
| 508 |
|
$flexifilter['condition'][$pid] = $parts[$pid]; |
| 509 |
|
unset($structure[$parent][$pid]); |
| 510 |
|
_flexifilter_arrange_parts($flexifilter['condition'][$pid], $parts, $structure); |
| 511 |
|
break; |
| 512 |
|
|
| 513 |
|
case FLEXIFILTER_PART_TYPE_CONDITION: |
| 514 |
|
$flexifilter['condition'][$pid] = $parts[$pid]; |
| 515 |
|
unset($structure[$parent][$pid]); |
| 516 |
|
_flexifilter_arrange_parts($flexifilter['condition'][$pid], $parts, $structure); |
| 517 |
|
break; |
| 518 |
|
} |
| 519 |
|
} |
| 520 |
|
} |
| 521 |
} |
} |
|
return FALSE; |
|
| 522 |
} |
} |
| 523 |
|
|
| 524 |
function flexifilter_get_field($flexifilter, $field) { |
function flexifilter_get_field($flexifilter, $field) { |
| 544 |
function flexifilter_get_filters($include_components = TRUE, $reset = FALSE) { |
function flexifilter_get_filters($include_components = TRUE, $reset = FALSE) { |
| 545 |
static $cache = array(); |
static $cache = array(); |
| 546 |
if (!isset($cache[$include_components]) || $reset) { |
if (!isset($cache[$include_components]) || $reset) { |
| 547 |
$filters = array(); |
$cache[$include_components] = flexifilter_load(NULL); |
|
$result = db_query('SELECT * FROM {flexifilters}'); |
|
|
while ($row = db_fetch_object($result)) { |
|
|
$filters[$row->fid] = _flexifilter_filter_from_db_row($row, $include_components); |
|
|
} |
|
|
$cache[$include_components] = $filters; |
|
| 548 |
} |
} |
| 549 |
return $cache[$include_components]; |
return $cache[$include_components]; |
| 550 |
} |
} |
| 552 |
/** |
/** |
| 553 |
* Saves a flexifilter to the database. |
* Saves a flexifilter to the database. |
| 554 |
* |
* |
| 555 |
* @param $filter A flexifilter to save. Should be of same form as the flexifilters returned from flexifilter_get_filters |
* @param $filter |
| 556 |
* (e.g. an array containing the label,description,id,enabled,advanced,delta and components fields), although id can be |
* A flexifilter to save. Should be of same form as the flexifilters returned |
| 557 |
* set to 'new' to create a new flexifilter rather than update an existing one (in which case, delta is ignored). Do |
* from flexifilter_get_filters (e.g. an array containing the label, |
| 558 |
* not use |
* description, id, enabled, advanced, delta and components fields), although |
| 559 |
|
* id can be set to 'new' to create a new flexifilter rather than update an |
| 560 |
|
* existing one (in which case, delta is ignored). |
| 561 |
* |
* |
| 562 |
* @return The fid. |
* @return |
| 563 |
|
* The fid. |
| 564 |
*/ |
*/ |
| 565 |
function flexifilter_save_filter($filter) { |
function flexifilter_save_filter($filter) { |
| 566 |
$fid = $filter['fid']; |
$fid = $filter['fid']; |
| 569 |
$advanced = $filter['advanced']; |
$advanced = $filter['advanced']; |
| 570 |
$cache = isset($filter['cache']) ? $filter['cache'] : 1; |
$cache = isset($filter['cache']) ? $filter['cache'] : 1; |
| 571 |
if ($fid === 'new') { |
if ($fid === 'new') { |
| 572 |
db_query("INSERT INTO {flexifilters} (label, description, enabled, delta, pid_root, advanced, cache) VALUES ('%s', '%s', 0, 0, 0, %d, %d)", |
db_query("INSERT INTO {flexifilters} (label, description, enabled, delta, advanced, cache) VALUES ('%s', '%s', 0, 0, %d, %d)", $label, $description, $advanced, $cache); |
| 573 |
$label, $description, $advanced, $cache); |
$fid = db_last_insert_id('flexifilters'); |
| 574 |
$fid = db_last_insert_id('flexifilters', 'fid'); |
_flexifilter_save_filter_components($filter['components'], $fid); |
|
$pids_to_reuse = array(); |
|
|
$pid_root = _flexifilter_save_filter_components($filter['components'], $fid, $pids_to_reuse); |
|
|
db_query('UPDATE {flexifilters} SET pid_root = %d WHERE fid = %d', $pid_root, $fid); |
|
| 575 |
} |
} |
| 576 |
else { |
else { |
| 577 |
$pids_to_reuse = array(); |
$pids_to_reuse = array(); |
| 606 |
return $fid; |
return $fid; |
| 607 |
} |
} |
| 608 |
|
|
| 609 |
function _flexifilter_save_filter_components($components, $fid, &$pids_to_reuse, $parent = FALSE) { |
function _flexifilter_save_filter_components($components, $fid, $parent = FALSE) { |
| 610 |
if ($parent === FALSE) { |
if ($parent === FALSE) { |
| 611 |
if ($reuse_pid = array_shift($pids_to_reuse)) { |
if ($reuse_pid = array_shift($pids_to_reuse)) { |
| 612 |
db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = 0, type = %d, class_name = '', settings = '' WHERE pid = %d", |
db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = 0, type = %d, class_name = '', settings = '' WHERE pid = %d", |
| 691 |
} |
} |
| 692 |
|
|
| 693 |
/** |
/** |
|
* Helper function for flexifilter_get_filters; converts a row from the flexifilters table |
|
|
* into a full flexifilter. |
|
|
*/ |
|
|
function _flexifilter_filter_from_db_row($row, $include_components) { |
|
|
$filter = array( |
|
|
'label' => $row->label, |
|
|
'description' => $row->description, |
|
|
'id' => $row->fid, |
|
|
'enabled' => ($row->enabled == 1), |
|
|
'advanced' => ($row->advanced == 1), |
|
|
'delta' => $row->delta, |
|
|
'cache' => $row->cache, |
|
|
); |
|
|
$pid_root = $row->pid_root; |
|
|
|
|
|
if ($include_components) { |
|
|
// Fetch all the parts of the filter and store them in a flat array |
|
|
$result = db_query('SELECT * FROM {flexifilters_parts} WHERE fid = %d', $row->fid); |
|
|
$components_flat = array(); |
|
|
$component_children = array(); |
|
|
$id_next = 0; |
|
|
while ($row = db_fetch_object($result)) { |
|
|
$components_flat[$row->pid] = array( |
|
|
'class' => $row->class_name, |
|
|
'settings' => $row->settings, |
|
|
'type' => $row->type, |
|
|
'pid' => $row->pid, |
|
|
); |
|
|
$id_next = max($id_next, $row->pid + 1); |
|
|
if (!isset($component_children[$row->parent_pid])) { |
|
|
$component_children[$row->parent_pid] = array($row->pid); |
|
|
} |
|
|
else { |
|
|
$component_children[$row->parent_pid][] = $row->pid; |
|
|
} |
|
|
} |
|
|
$filter['components'] = _flexifilter_reconstruct_components($pid_root, $components_flat, $component_children); |
|
|
$filter['components']['id_next'] = $id_next; |
|
|
$filter['components']['id_prefix'] = 'flexifilter_component_'; |
|
|
} |
|
|
return $filter; |
|
|
} |
|
|
|
|
|
/** |
|
| 694 |
* Helper function for _flexifilter_filter_from_db_row; recursively collects the component |
* Helper function for _flexifilter_filter_from_db_row; recursively collects the component |
| 695 |
* children of a flexifilter part. |
* children of a flexifilter part. |
| 696 |
*/ |
*/ |