From b039354be2ec484fd7c657d46e126e2fd6adbb1d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 29 Aug 2013 13:58:34 +0200 Subject: [PATCH] Bug 10855: Search subscriptions by additional fields Now you will abble to search subscriptions by additional fields. The additional field values will be displayed in the table results. Test plan: - go on the subscriptions advanced search page (serials/serials-search.pl). - verify all searchable additional fields are displayed on the form. - combine 1 or more values and verify results are consistent. - verify the values are displayed in new columns of the table. - for field linked to an authorised value category, the description is displayed (not the code). --- C4/Serials.pm | 35 ++++++++++-- .../prog/en/includes/serials-menu.inc | 1 + .../prog/en/modules/serials/serials-search.tt | 57 ++++++++++++++++++-- serials/serials-search.pl | 17 ++++++ 4 files changed, 102 insertions(+), 8 deletions(-) diff --git a/C4/Serials.pm b/C4/Serials.pm index e950e30..46d8fe5 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -663,20 +663,32 @@ a table of hashref. Each hash containt the subscription. sub SearchSubscriptions { my ( $args ) = @_; - my $query = qq{ + my $additional_fields = $args->{additional_fields} // []; + my $matching_record_ids_for_additional_fields = []; + if ( @$additional_fields ) { + $matching_record_ids_for_additional_fields = Koha::AdditionalField->get_matching_record_ids({ + fields => $args->{additional_fields}, + tablename => 'subscription', + }); + return () unless @$matching_record_ids_for_additional_fields; + } + + my $query = q| SELECT subscription.*, subscriptionhistory.*, biblio.*, biblioitems.issn FROM subscription LEFT JOIN subscriptionhistory USING(subscriptionid) LEFT JOIN biblio ON biblio.biblionumber = subscription.biblionumber LEFT JOIN biblioitems ON biblioitems.biblionumber = subscription.biblionumber LEFT JOIN aqbooksellers ON subscription.aqbooksellerid = aqbooksellers.id - }; + |; + $query .= q| WHERE 1|; my @where_strs; my @where_args; if( $args->{biblionumber} ) { push @where_strs, "biblio.biblionumber = ?"; push @where_args, $args->{biblionumber}; } + if( $args->{title} ){ my @words = split / /, $args->{title}; my (@strs, @args); @@ -713,16 +725,29 @@ sub SearchSubscriptions { push @where_strs, "subscription.closed = ?"; push @where_args, "$args->{closed}"; } + if(@where_strs){ - $query .= " WHERE " . join(" AND ", @where_strs); + $query .= ' AND ' . join(' AND ', @where_strs); + } + if ( @$additional_fields ) { + $query .= ' AND subscriptionid IN (' + . join( ', ', @$matching_record_ids_for_additional_fields ) + . ')'; } my $dbh = C4::Context->dbh; my $sth = $dbh->prepare($query); $sth->execute(@where_args); - my $results = $sth->fetchall_arrayref( {} ); - $sth->finish; + my $results = $sth->fetchall_arrayref( {} ); + + for my $subscription ( @$results ) { + my $additional_field_values = Koha::AdditionalField->fetch_all_values({ + record_id => $subscription->{subscriptionid}, + tablename => 'subscription' + }); + %$subscription = ( %$subscription, additional_fields => $additional_field_values->{$subscription->{subscriptionid}} ); + } return @$results; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/serials-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/serials-menu.inc index 8083192..e14567d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/serials-menu.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/serials-menu.inc @@ -20,4 +20,5 @@ [% IF ( CAN_user_serials_check_expiration ) %]
  • Check expiration
  • [% END %] +
  • Add subscription fields
  • diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-search.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-search.tt index 459a80a..6847684 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-search.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-search.tt @@ -1,5 +1,6 @@ [% INCLUDE 'doc-head-open.inc' %] [% USE KohaDates %] +[% USE KohaAuthorisedValues %] Koha › Serials [% biblionumber %] [% INCLUDE 'doc-head-close.inc' %] @@ -14,6 +15,7 @@ [% END %] var osrlt = $("#osrlt").dataTable($.extend(true, {}, dataTablesDefaults, { + // FIXME sort function of additional_fields! "aoColumnDefs": [ { "aTargets": [ -1, -2, -3 ], "bSortable": false, "bSearchable": false }, ], @@ -21,6 +23,7 @@ } ) ); var csrlt = $("#csrlt").dataTable($.extend(true, {}, dataTablesDefaults, { + // FIXME sort function of additional_fields! "aoColumnDefs": [ { "aTargets": [ -1, -2 ], "bSortable": false, "bSearchable": false }, ], @@ -92,6 +95,21 @@ [% END %] + [% FOR field IN additional_fields_for_subscription %] +
  • + + [% IF field.authorised_value_choices %] + + [% ELSE %] + + [% END %] +
  • + [% END %]
    @@ -119,6 +137,9 @@ Library Call number Expiration date + [% FOR field IN additional_fields_for_subscription %] + [% field.name %] + [% END %] [% IF ( routing && CAN_user_serials_routing ) %] Routing list [% END %] @@ -134,6 +155,9 @@ + [% FOR field IN additional_fields_for_subscription %] + + [% END %] [% IF ( routing && CAN_user_serials_routing ) %][% END %] @@ -161,6 +185,16 @@ [% IF ( subscription.enddate ) %][% subscription.enddate | $KohaDates %][% END %] [% IF ( routing && CAN_user_serials_routing ) %] + [% END %] + [% FOR field IN additional_fields_for_subscription %] + [% IF field.authorised_value_category %] + [% KohaAuthorisedValues.GetByCode( field.authorised_value_category, subscription.additional_fields.${field.name} ) %] + [% ELSE %] + [% subscription.additional_fields.${field.name} %] + [% END %] + [% END %] + Issue history + [% IF ( subscription.cannotedit ) %]   @@ -173,9 +207,6 @@ [% END %] [% END %] - [% END %] - Issue history - [% IF ( subscription.cannotedit ) %]   @@ -196,6 +227,7 @@
    [% IF closedsubscriptions %] + @@ -312,6 +344,25 @@ [% END %] + [% FOR field IN additional_fields_for_subscription %] +
  • + + [% IF field.authorised_value_choices %] + + [% ELSE %] + + [% END %] +
  • + [% END %]
    diff --git a/serials/serials-search.pl b/serials/serials-search.pl index bc84ed6..7685994 100755 --- a/serials/serials-search.pl +++ b/serials/serials-search.pl @@ -33,8 +33,10 @@ use CGI; use C4::Auth; use C4::Branch; use C4::Context; +use C4::Koha qw( GetAuthorisedValues ); use C4::Output; use C4::Serials; +use Koha::AdditionalField; my $query = new CGI; my $title = $query->param('title_filter') || ''; @@ -70,6 +72,18 @@ if ( $op and $op eq "close" ) { } } + +my $additional_fields = Koha::AdditionalField->all( { table => 'subscription', searchable => 1 } ); +my $additional_field_filters; +for my $field ( @$additional_fields ) { + if ( my $filter_value = $query->param('additional_field_' . $field->{name} . '_filter') ) { + $additional_field_filters->{ $field->{name} } = $filter_value; + } + if ( $field->{authorised_value_category} ) { + $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} ); + } +} + my @subscriptions; if ($searched){ @subscriptions = SearchSubscriptions( @@ -81,6 +95,7 @@ if ($searched){ publisher => $publisher, bookseller => $bookseller, branch => $branch, + additional_fields => [ map{ { name => $_, value => $additional_field_filters->{$_}} } keys %$additional_field_filters ], } ); } @@ -127,6 +142,8 @@ $template->param( branches_loop => \@branches_loop, done_searched => $searched, routing => $routing, + additional_field_filters => $additional_field_filters, + additional_fields_for_subscription => $additional_fields, marcflavour => (uc(C4::Context->preference("marcflavour"))) ); -- 1.7.10.4
    ISSN