From 3bab8bcc26a9b4379a51c846017cdc8fa2207a8d Mon Sep 17 00:00:00 2001 From: ggppdk Date: Thu, 1 Mar 2018 21:35:17 +0200 Subject: [PATCH] Fixed configuration issue in filtering module categories include mode + none selected should display all viewable categories --- modules/mod_flexifilter/helper.php | 76 ++++++---- modules/mod_flexifilter/mod_flexifilter.php | 145 ++++++++++++-------- modules/mod_flexifilter/tmpl/default.php | 2 +- 3 files changed, 140 insertions(+), 83 deletions(-) diff --git a/modules/mod_flexifilter/helper.php b/modules/mod_flexifilter/helper.php index 1657f425d65..1338040d494 100644 --- a/modules/mod_flexifilter/helper.php +++ b/modules/mod_flexifilter/helper.php @@ -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; } - -} -?> \ No newline at end of file +} \ No newline at end of file diff --git a/modules/mod_flexifilter/mod_flexifilter.php b/modules/mod_flexifilter/mod_flexifilter.php index fea1bd64332..6d073017137 100644 --- a/modules/mod_flexifilter/mod_flexifilter.php +++ b/modules/mod_flexifilter/mod_flexifilter.php @@ -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) ) { @@ -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 @@ -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); } @@ -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); @@ -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 ); } @@ -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') ) diff --git a/modules/mod_flexifilter/tmpl/default.php b/modules/mod_flexifilter/tmpl/default.php index b9f8f5ae53a..1a445ecf13b 100644 --- a/modules/mod_flexifilter/tmpl/default.php +++ b/modules/mod_flexifilter/tmpl/default.php @@ -36,7 +36,7 @@ } ?> -
+