Skip to content

Commit

Permalink
Fixed configuration issue in filtering module categories include mode…
Browse files Browse the repository at this point in the history
… + none selected should display all viewable categories
  • Loading branch information
ggppdk committed Mar 1, 2018
1 parent a0b6b53 commit 3bab8bc
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 83 deletions.
76 changes: 49 additions & 27 deletions modules/mod_flexifilter/helper.php
Expand Up @@ -33,46 +33,68 @@
class modFlexifilterHelper
{

public static function decideCats( &$params )
public static function decideCats(& $params)
{
global $globalcats;

$display_cat_list = $params->get('display_cat_list', 0);
$display_cat_list = (int) $params->get('display_cat_list', 0);
$catids = $params->get('catids', array());
$usesubcats = $params->get('usesubcats', 0 );

$usesubcats = (int) $params->get('usesubcats', 0);

// Get user's allowed categories
if ($display_cat_list)
{
$tree = flexicontent_cats::getCategoriesTree();

// (only) For include mode, use all categories if no cats were selected via configuration
if ($display_cat_list === 1 && empty($catids))
{
global $globalcats;
$catids = array_keys($globalcats);
}
}

// Find descendants of the categories
if ($usesubcats)
{
$subcats = array();
foreach ($catids as $catid) {
$subcats = array_merge($subcats, array_map('trim',explode(",",$globalcats[$catid]->descendants)) );

foreach ($catids as $catid)
{
$subcats = array_merge($subcats, array_map('trim', explode(',', $globalcats[$catid]->descendants)) );
}

$catids = array_unique($subcats);
}


// Find categories to display
$tree = flexicontent_cats::getCategoriesTree();

if ( $display_cat_list == 1 ) // include method
{
foreach ($catids as $catid) $allowedtree[$catid] = $tree[$catid];
}

else if ( $display_cat_list == 2 ) // exclude method
{
foreach ($catids as $catid) unset($tree[$catid]);
$allowedtree = & $tree;
}

else
switch ($display_cat_list)
{
$allowedtree = & $tree;
// Include method
case 1:
$allowedtree = array();

foreach ($catids as $catid)
{
$allowedtree[$catid] = $tree[$catid];
}
break;

// Exclude method
case 2:
foreach ($catids as $catid)
{
unset($tree[$catid]);
}
$allowedtree = & $tree;
break;

// Not using category selector
case 0:
default:
$allowedtree = array();
break;
}

return $allowedtree;
}

}
?>
}
145 changes: 90 additions & 55 deletions modules/mod_flexifilter/mod_flexifilter.php
Expand Up @@ -170,9 +170,9 @@


// FIELD FILTERS
$display_filter_list = $params->get('display_filter_list', 0);
$display_filter_list = (int) $params->get('display_filter_list', 0);
$filter_ids = $params->get('filters', array());
$limit_filters_to_cat = $display_filter_list==1 || $display_filter_list==3;
$limit_filters_to_cat = $display_filter_list === 1 || $display_filter_list === 3;

// Check if array or comma separated list
if ( !is_array($filter_ids) ) {
Expand Down Expand Up @@ -214,14 +214,23 @@
$_fld_name = 'cids[]';

$mcats_list = $jinput->get('cids', '', 'string');

if ( !is_array($mcats_list) )
{
$mcats_list = preg_replace( '/[^0-9,]/i', '', (string) $mcats_list );
$mcats_list = explode(',', $mcats_list);
}

// Make sure given data are integers ... !!
$cids = array();
foreach ($mcats_list as $i => $_id) if ((int)$_id) $cids[] = (int)$_id;

foreach ($mcats_list as $i => $_id)
{
if ((int)$_id)
{
$cids[] = (int) $_id;
}
}
}

// CASE 2: Single category selector, targeting single category view
Expand All @@ -232,12 +241,15 @@
$_fld_onchange = ' onchange="update_'.$form_name.'();" ';
$_fld_name = $catid_fieldname;
}

$_fld_attributes = ' class="'.$_fld_classes.'" '.$_fld_size.$_fld_onchange.$_fld_multiple;

$allowedtree = modFlexifilterHelper::decideCats($params);
$selected_cats = $mcats_selection ? $cids : ($catid ? $catid : '');
$top = false;
$cats_select_field = flexicontent_cats::buildcatselect($allowedtree, $_fld_name, $selected_cats, $top, $_fld_attributes, $check_published = true, $check_perms = false, array(), $require_all=false);
$selected_cats = $mcats_selection
? $cids
: ($catid ? $catid : '');

$cats_select_field = flexicontent_cats::buildcatselect($allowedtree, $_fld_name, $selected_cats, $top = false, $_fld_attributes, $check_published = true, $check_perms = false, array(), $require_all=false);
}


Expand Down Expand Up @@ -275,48 +287,59 @@
// Category parameters from category or from multi-category menu item
$view_params = !empty($mcats_menu) ? $menu_params : $cat_params;

// ALL filters
if ($display_filter_list==0)
{
// WARNING: this CASE is supposed to get ALL filters regardless category,
// but __ALL_FILTERS__ ignores the 'use_filters' parameter, so we must check it separetely
// ... $params->set('filters_order', 1); // respect filters ordering if so configured in category
$filters = ! $params->get('use_filters', 0) ? array() : FlexicontentFields::getFilters('filters', '__ALL_FILTERS__', $view_params);
}

// Filter selected in category configuration
else if ($display_filter_list==1)
{
// ... $params->set('filters_order', 1); // respect filters ordering if so configured in category
$filters = FlexicontentFields::getFilters('filters', 'use_filters', $view_params);
}

// Filters selected in module
else if ($display_filter_list==2)
/**
* Decide which filters to display
*/
switch ($display_filter_list)
{
$params->set('filters_order', 1); // respect filters ordering
$filters = FlexicontentFields::getFilters('filters', 'use_filters', $params);
// ALL filters
case 0:
/**
* WARNING: this CASE is supposed to get ALL filters regardless category,
* but __ALL_FILTERS__ ignores the 'use_filters' parameter, so we must check it separetely
* ... $params->set('filters_order', 1); // respect filters ordering if so configured in category
*/
$filters = ! $params->get('use_filters', 0) ? array() : FlexicontentFields::getFilters('filters', '__ALL_FILTERS__', $view_params);
break;

// Filter selected in category configuration
case 1:
// ... $params->set('filters_order', 1); // respect filters ordering if so configured in category
$filters = FlexicontentFields::getFilters('filters', 'use_filters', $view_params);
break;

// Filters selected in module
case 2:
$params->set('filters_order', 1); // respect filters ordering
$filters = FlexicontentFields::getFilters('filters', 'use_filters', $params);
break;

// Filters selected in module and intersect with current category
case 3:
$params->set('filters_order', 1); // respect filters ordering
$cat_filters = FlexicontentFields::getFilters('filters', 'use_filters', $params);

// Intersection of selected filters and of category assigned filters
$filters = array();
$filter_ids_indexed = array_flip($filter_ids);

foreach ($cat_filters as $filter_name => $filter)
{
if (isset($filter_ids_indexed[$filter->id]))
{
$filters[] = $filter;
}
}
break;
}

// Filters selected in module and intersect with current category
else if ($display_filter_list) // ==3
// Remove categories filter, if using module's custom category selector
if ($display_cat_list)
{
$params->set('filters_order', 1); // respect filters ordering
$cat_filters = FlexicontentFields::getFilters('filters', 'use_filters', $params);

// Intersection of selected filters and of category assigned filters
$filters = array();
$filter_ids_indexed = array_flip($filter_ids);
foreach ($cat_filters as $filter_name => $filter) {
if ( isset($filter_ids_indexed[$filter->id]) ) {
$filters[] = $filter;
}
}
unset($filters[13]);
}

// Remove categories filter
if ($display_cat_list) unset($filters[13]);

// Set filter values (initial or locked) via configuration parameters
FlexicontentFields::setFilterValues( $params, 'persistent_filters', $is_persistent=1);
FlexicontentFields::setFilterValues( $params, 'initial_filters' , $is_persistent=0);
Expand All @@ -327,14 +350,16 @@

// Override text search auto-complete category ids with those of filter 13
$f13_val = $jinput->get('filter_13', '', 'string');
if ( isset($filters['categories']) && !empty($f13_val) )

if (isset($filters['categories']) && !empty($f13_val))
{
$params->set('txt_ac_cid', 'NA');
$params->set('txt_ac_cids', is_array($f13_val) ? $f13_val : array((string) $f13_val) );
}

// 4. Add html to filter objects
if ( !empty($filters) ) {
if (!empty($filters))
{
FlexicontentFields::renderFilters( $params, $filters, $form_name );
}

Expand Down Expand Up @@ -424,39 +449,49 @@
require(JModuleHelper::getLayoutPath('mod_flexifilter', $layout));

// Add needed js
$js = "";
/*if (!$display_cat_list || !empty($selected_cats)) {
$js = '';

/*
if (!$display_cat_list || !empty($selected_cats))
{
$js .= '
jQuery(document).ready(function() {
jQuery("#'.$form_name.'_filter_box").css("display", "block");
});
';
}
$document = JFactory::getDocument();
$document->addScriptDeclaration($js);*/
*/

if ($display_cat_list && !$mcats_selection)
{
$js .= '
function update_'.$form_name.'()
function update_' . $form_name . '()
{
form=document.getElementById("'.$form_name.'");
cid_val=form.'.$catid_fieldname.'.value;
if ( cid_val.length == 0 )
var form = document.getElementById("' . $form_name . '");
var cid_val = form.' . $catid_fieldname . '.value;
if (cid_val.length == 0)
{
var fcform = jQuery(form);
var _action = fcform.attr("data-fcform_default_action");
fcform.attr("action", _action);
fcform.attr("data-fcform_action", _action );
var default_action = fcform.attr("data-fcform_default_action");
fcform.attr("action", default_action);
fcform.attr("data-fcform_action", default_action);
adminFormPrepare(form, 1);
return;
}
getSEFurl("cid_loading_'.$module->id.'", "'.$loader_html.'", form,"'.$url_to_load.'"+cid_val, "'.$autosubmit_msg.'", '.$autosubmit.', "'.$default_mcats_target.'");
/*jQuery("#'.$form_name.'_filter_box").css("display", "block");*/
}
';
}
if ($js) JFactory::getDocument()->addScriptDeclaration($js);

if ($js)
{
JFactory::getDocument()->addScriptDeclaration($js);
}

// append performance stats to global variable
if ( $flexiparams->get('print_logging_info') )
Expand Down
2 changes: 1 addition & 1 deletion modules/mod_flexifilter/tmpl/default.php
Expand Up @@ -36,7 +36,7 @@
}
?>

<form id='<?php echo $form_id; ?>' action='<?php echo $form_target; ?>' data-fcform_default_action='<?php echo $default_target; ?>' method='<?php echo $form_method; ?>' >
<form id='<?php echo $form_id; ?>' action='<?php echo $form_target; ?>' data-fcform_default_action='<?php echo $form_target; ?>' method='<?php echo $form_method; ?>' >

<?php if ( !empty($cats_select_field) ) : ?>
<fieldset class="fc_filter_set" style="padding-bottom:0px;">
Expand Down

0 comments on commit 3bab8bc

Please sign in to comment.