1 package Koha::AdditionalContents;
3 # Copyright ByWater Solutions 2015
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Array::Utils qw( array_minus );
26 use Koha::AdditionalContent;
27 use Koha::AdditionalContentsLocalizations;
29 use base qw(Koha::Objects);
33 Koha::AdditionalContents - Koha Additional content object set class
41 =head3 search_for_display
43 my $contents = Koha::AdditionalContents->search_for_display({
44 category => 'news', # news or html_customizations
47 library_id => $branchcode
50 Return Koha::AdditionalContents set for display to user
52 You can limit the results by location, language and library by optional params
54 library_id should be valid branchcode of defined library
56 location is one of this:
57 - slip - for ISSUESLIP notice
58 - staff_only - for intranet
59 - opac_only - for OPAC
60 - staff_and_opac - for intranet and online catalogue
61 - OpacNavRight - Right column in the online catalogue
66 - OpacLoginInstructions
67 - OpacSuggestionInstructions
68 - ArticleRequestsDisclaimerText
74 sub search_for_display {
75 my ( $self, $params ) = @_;
78 $search_params->{location} = $params->{location};
79 $search_params->{branchcode} = $params->{library_id} ? [ $params->{library_id}, undef ] : undef;
80 $search_params->{published_on} = { '<=' => \'CAST(NOW() AS DATE)' };
81 $search_params->{-or} = [
82 expirationdate => { '>=' => \'CAST(NOW() AS DATE)' },
83 expirationdate => undef
85 $search_params->{category} = $params->{category} if $params->{category};
87 my $contents = $self->SUPER::search( $search_params, { order_by => 'number' } );
89 if ( $params->{lang} ) {
90 my $translated_contents = Koha::AdditionalContentsLocalizations->search(
92 additional_content_id => [$contents->get_column('id')],
93 lang => $params->{lang},
96 my @all_content_id = $contents->get_column('id');
97 my @translated_content_id = $translated_contents->get_column('additional_content_id');
98 my $default_contents = Koha::AdditionalContentsLocalizations->search(
100 additional_content_id => [array_minus(@all_content_id, @translated_content_id)],
104 return Koha::AdditionalContentsLocalizations->search(
107 $translated_contents->get_column('id'),
108 $default_contents->get_column('id'),
113 return $contents->search( { lang => 'default' }, { order_by => 'number' } )->translated_contents;
116 =head3 find_best_match
118 Koha::AdditionalContents->find_best_match({
119 category => , location => , lang => , library_id =>
122 When choosing the best match, a match on lang and library is preferred.
123 Next a match on library and default lang. Then match on All libs and lang.
124 Finally a match with All libs and default lang.
128 sub find_best_match {
129 my ( $self, $params ) = @_;
130 my $library_id = $params->{library_id};
131 my $lang = $params->{lang};
133 my $contents = $self->SUPER::search({
134 category => $params->{category},
135 location => $params->{location},
136 branchcode => [ $library_id, undef ],
139 my $rs = Koha::AdditionalContentsLocalizations->search({
140 additional_content_id => [ $contents->get_column('id') ],
141 lang => [ $lang, 'default' ],
145 my ( $alt1, $alt2, $alt3 );
146 while( my $rec = $rs->next ) {
147 return $rec if $library_id && $rec->branchcode && $rec->branchcode eq $library_id && $lang && $rec->lang eq $lang;
148 $alt1 = $rec if !$alt1 && $library_id && $rec->branchcode && $rec->branchcode eq $library_id;
149 $alt2 = $rec if !$alt2 && $lang && $rec->lang eq $lang;
150 $alt3 = $rec if !$alt3;
152 return $alt1 // $alt2 // $alt3;
160 return 'AdditionalContent';
168 return 'Koha::AdditionalContent';
173 Kyle M Hall <kyle@bywatersolutions.com>