From 47228046eecfa138de4c6be63eb9290b2bb2392e Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 14 Sep 2021 12:58:00 +0000 Subject: [PATCH] Bug 17170: DB Updates This patch adds the new table, permission, and a syspref to enable the feature Sponsored-by: Sponsored by: Round Rock Public Library [https://www.roundrocktexas.gov/departments/library/] Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- Koha/SearchFilter.pm | 92 +++++++++++++++++++ Koha/SearchFilters.pm | 52 +++++++++++ .../data/mysql/atomicupdate/bug_17170.pl | 36 ++++++++ installer/data/mysql/kohastructure.sql | 18 ++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../data/mysql/mandatory/userpermissions.sql | 1 + .../prog/en/includes/permissions.inc | 5 + .../modules/admin/preferences/searching.pref | 7 ++ 8 files changed, 212 insertions(+) create mode 100644 Koha/SearchFilter.pm create mode 100644 Koha/SearchFilters.pm create mode 100755 installer/data/mysql/atomicupdate/bug_17170.pl diff --git a/Koha/SearchFilter.pm b/Koha/SearchFilter.pm new file mode 100644 index 0000000000..f400a8f703 --- /dev/null +++ b/Koha/SearchFilter.pm @@ -0,0 +1,92 @@ +package Koha::SearchFilter; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::SearchFilter - Koha Search filter object class + +=head1 API + +=head2 Class methods + +=head3 to_api_mapping + +=cut + +sub to_api_mapping { + return { + id => 'search_filter_id', + query => 'filter_query', + limits => 'filter_limits', + }; +} + +=head3 expand_filter + + my $expanded_filter = $filter->expand_filter; + + Returns the filter as an arrayref of limit queries suitable to + be passed to QueryBuilder + +=cut + +sub expand_filter { + my $self = shift; + + my $query_part = $self->query; + my $limits_part = $self->limits; + + my $limits = decode_json($limits_part)->{limits}; + + my $query = decode_json($query_part); + my $operators = $query->{operators}; + my $operands = $query->{operands}; + my $indexes = $query->{indexes}; + + my $query_limit; + for( my $i = 0; $i < scalar @$operands; $i++ ){ + next unless @$operands[$i]; + my $index = @$indexes[$i] ? @$indexes[$i] . "=" : ""; + my $query = "(" . @$operands[$i] . ")"; + my $operator = ""; + $operator = @$operators[$i-1] ? " " . @$operators[$i-1] . " " : scalar @$operands > $i ? " AND " : "" if $i > 0; + my $limit = $operator . $index . $query; + $query_limit .= $limit; + } + + push @$limits, "(".$query_limit.")" if $query_limit; + + return $limits; +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'SearchFilter'; +} + +1; diff --git a/Koha/SearchFilters.pm b/Koha/SearchFilters.pm new file mode 100644 index 0000000000..3104cd3d14 --- /dev/null +++ b/Koha/SearchFilters.pm @@ -0,0 +1,52 @@ +package Koha::SearchFilters; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + + +use Koha::Database; + +use Koha::SearchFilter; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::SearchFilters - Koha Search filters object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'SearchFilter'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::SearchFilter'; +} + +1; diff --git a/installer/data/mysql/atomicupdate/bug_17170.pl b/installer/data/mysql/atomicupdate/bug_17170.pl new file mode 100755 index 0000000000..650e7831c3 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_17170.pl @@ -0,0 +1,36 @@ +use Modern::Perl; + +return { + bug_number => "17170", + description => "Add permission for creating saved search filters", + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + $dbh->do(q{ + INSERT IGNORE INTO permissions (module_bit, code, description) VALUES + (3, 'manage_search_filters', 'Manage custom search filters'); + }); + say $out "Added manage_search_filters permission"; + unless( TableExists( 'search_filters' ) ){ + $dbh->do(q{ + CREATE TABLE `search_filters` ( + id int(11) NOT NULL auto_increment, -- unique identifier for each search filter + name varchar(255) NOT NULL, -- display name for this filter + query mediumtext DEFAULT NULL, -- query part of the filter, can be blank + limits mediumtext DEFAULT NULL, -- limits part of the filter, can be blank + opac tinyint(1) NOT NULL DEFAULT 0, -- whether this filter is shown on OPAC + staff_client tinyint(1) NOT NULL DEFAULT 0, -- whether this filter is shown in staff client + PRIMARY KEY (id) + ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci; + }); + say $out "Added search_filters table"; + } else { + say $out "search_filters table already created"; + } + $dbh->do(q{ + INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES + ('SavedSearchFilters', '0', NULL, 'Allow staff with permission to create/edit custom search filters', 'YesNo') + }); + say $out "Added SavedSearchFilters system preference"; + }, +} diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index b0dea05b7b..8ec0fad779 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -4723,6 +4723,24 @@ CREATE TABLE `search_field` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `search_filters` +-- + +DROP TABLE IF EXISTS `search_filters`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `search_filters` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `query` mediumtext COLLATE utf8mb4_unicode_ci, + `limits` mediumtext COLLATE utf8mb4_unicode_ci, + `opac` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'whether this filter is shown on OPAC', + `staff_client` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'whether this filter is shown in staff client', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + -- -- Table structure for table `search_history` -- diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 68b6841c12..e3eb88a856 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -623,6 +623,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('RoutingListAddReserves','0','','If ON the patrons on routing lists are automatically added to holds on the issue.','YesNo'), ('RoutingListNote','To change this note edit RoutingListNote system preference.','70|10','Define a note to be shown on all routing lists','Textarea'), ('RoutingSerials','1',NULL,'If ON, serials routing is enabled','YesNo'), +('SavedSearchFilters', '0', NULL, 'Allow staff with permission to create/edit custom search filters', 'YesNo'), ('SCOAllowCheckin','0','','If enabled, patrons may return items through the Web-based Self Checkout','YesNo'), ('SCOMainUserBlock','','70|10','Add a block of HTML that will display on the self checkout screen','Textarea'), ('SCOUserCSS','',NULL,'Add CSS to be included in the SCO module in an embedded