From 921f679bca0b80a38fdc9ada8e445c1458833a83 Mon Sep 17 00:00:00 2001 From: HWFord <54360213+HWFord@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:21:49 +0200 Subject: [PATCH] fixes ##2195 if conf['rate'] show rating filter only allow rating filter if gallery uses rating update api, mcs.js and tpl to check if rating search_filters.inc.php update indentation due to extra if --- include/functions_search.inc.php | 2 +- include/search_filters.inc.php | 78 ++++++++++--------- include/ws_functions/pwg.images.php | 4 +- themes/default/js/mcs.js | 22 ++++-- .../template/include/search_filters.inc.tpl | 12 ++- 5 files changed, 72 insertions(+), 46 deletions(-) diff --git a/include/functions_search.inc.php b/include/functions_search.inc.php index b45558068..011978913 100644 --- a/include/functions_search.inc.php +++ b/include/functions_search.inc.php @@ -510,7 +510,7 @@ SELECT // // ratings // - if (!empty($search['fields']['ratings'])) + if ($conf['rate'] and !empty($search['fields']['ratings'])) { $has_filters_filled = true; diff --git a/include/search_filters.inc.php b/include/search_filters.inc.php index b9fcf4d5a..5807eb993 100644 --- a/include/search_filters.inc.php +++ b/include/search_filters.inc.php @@ -367,17 +367,21 @@ SELECT } // For rating - if (isset($my_search['fields']['ratings'])) + if ($conf['rate']) { - $filter_clause = get_clause_for_filter('ratings'); - - $cache_key = $persistent_cache->make_key('filter_ratings'.$user['id'].$user['cache_update_time']); - - $set_persistent_cache = !preg_match('/^image_id IN/', $filter_clause) and !$persistent_cache->get($cache_key, $ratings); - - if (!isset($ratings)) + $template->assign('SHOW_FILTER_RATINGS', true); + + if (isset($my_search['fields']['ratings'])) { - $query = ' + $filter_clause = get_clause_for_filter('ratings'); + + $cache_key = $persistent_cache->make_key('filter_ratings'.$user['id'].$user['cache_update_time']); + + $set_persistent_cache = !preg_match('/^image_id IN/', $filter_clause) and !$persistent_cache->get($cache_key, $ratings); + + if (!isset($ratings)) + { + $query = ' SELECT DISTINCT id, rating_score @@ -385,43 +389,47 @@ SELECT JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.image_id = i.id WHERE '.$filter_clause; - $filter_rows = query2array($query); + $filter_rows = query2array($query); - $ratings = array_fill(0, 6, 0); + $ratings = array_fill(0, 6, 0); - foreach ($filter_rows as $row) - { - $r = 5; - - if (!isset($row['rating_score'])) + foreach ($filter_rows as $row) { - $r = 0; - } - else - { - for ($i=1; $i<=4; $i++) + $r = 5; + + if (!isset($row['rating_score'])) { - if ($row['rating_score'] < $i) + $r = 0; + } + else + { + for ($i=1; $i<=4; $i++) { - $r = $i; - break; + if ($row['rating_score'] < $i) + { + $r = $i; + break; + } } } + + $ratings[$r]++; } - $ratings[$r]++; - } - - if ($set_persistent_cache) - { - // for this filter, we do not store in cache the $filter_rows : for a big gallery it may - // take more than 10MB. It is smarter to store in cache the result of the computation, - // which is just around 100 bytes. - $persistent_cache->set($cache_key, $ratings); + if ($set_persistent_cache) + { + // for this filter, we do not store in cache the $filter_rows : for a big gallery it may + // take more than 10MB. It is smarter to store in cache the result of the computation, + // which is just around 100 bytes. + $persistent_cache->set($cache_key, $ratings); + } } + $template->assign('RATING', $ratings); } - - $template->assign('RATING', $ratings); + } + else + { + $template->assign('SHOW_FILTER_RATINGS', false); } // For filesize diff --git a/include/ws_functions/pwg.images.php b/include/ws_functions/pwg.images.php index 8efe4515b..d2ffb88de 100644 --- a/include/ws_functions/pwg.images.php +++ b/include/ws_functions/pwg.images.php @@ -724,7 +724,7 @@ SELECT * */ function ws_images_filteredSearch_create($params, $service) { - global $user; + global $user, $conf; include_once(PHPWG_ROOT_PATH.'include/functions_search.inc.php'); @@ -938,7 +938,7 @@ function ws_images_filteredSearch_create($params, $service) $search['fields']['ratios'] = $params['ratios']; } - if (isset($params['ratings'])) + if ($conf['rate'] and isset($params['ratings'])) { $search['fields']['ratings'] = $params['ratings']; } diff --git a/themes/default/js/mcs.js b/themes/default/js/mcs.js index f2225adac..3a3a6d693 100644 --- a/themes/default/js/mcs.js +++ b/themes/default/js/mcs.js @@ -443,21 +443,29 @@ $(document).ready(function () { } // Setup rating filter - if (global_params.fields.ratings) { + if (global_params.fields.ratings && show_filter_ratings) { $(".filter-ratings").css("display", "flex"); $(".filter-manager-controller.ratings").prop("checked", true); ratings_search_str = ""; - global_params.fields.ratings.forEach(function(ft){ + global_params.fields.ratings.forEach(function(ft, i){ if(0 == ft ) { - ratings_search_str = str_no_rating + ", "; + ratings_search_str += str_no_rating + if(global_params.fields.ratings.length > 1) + { + ratings_search_str += ", "; + } } else { - ratings_search_str = str_between_rating.split("%d"); - ratings_search_str = ratings_search_str[0] + (ft-1) + ratings_search_str[1] + ft + ratings_search_str[2]; + str_between = str_between_rating.split("%d"); + ratings_search_str += str_between[0] + (ft-1) + str_between[1] + ft + str_between[2]; + if(global_params.fields.ratings.length-1 != i) + { + ratings_search_str += ", "; + } } }); @@ -482,6 +490,10 @@ $(document).ready(function () { empty_filters_list.push(PS_params.ratings); } + else if (!show_filter_ratings) + { + updateFilters('ratings', 'add'); + } // Setup filesize filter if (global_params.fields.filesize_min != null && global_params.fields.filesize_max != null) { diff --git a/themes/default/template/include/search_filters.inc.tpl b/themes/default/template/include/search_filters.inc.tpl index ed7f5612f..1b29810ea 100644 --- a/themes/default/template/include/search_filters.inc.tpl +++ b/themes/default/template/include/search_filters.inc.tpl @@ -85,6 +85,11 @@ const prefix_icon = 'gallery-icon-'; {/if} }; + {if $SHOW_FILTER_RATINGS and isset($RATING)} + var show_filter_ratings = true; + {else} + var show_filter_ratings = false; + {/if} {/footer_script} @@ -129,10 +134,12 @@ const prefix_icon = 'gallery-icon-'; {'Ratio'|@translate} +{if $SHOW_FILTER_RATINGS and isset($SHOW_FILTER_RATINGS)} +{/if}