Bug 35962: (bug 35843 follow-up) Fix BackgroundJob.t on D10
[koha.git] / Koha / AdditionalContents.pm
1 package Koha::AdditionalContents;
2
3 # Copyright ByWater Solutions 2015
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Array::Utils qw( array_minus );
23
24 use Koha::Database;
25 use Koha::Exceptions;
26 use Koha::AdditionalContent;
27 use Koha::AdditionalContentsLocalizations;
28
29 use base qw(Koha::Objects);
30
31 =head1 NAME
32
33 Koha::AdditionalContents - Koha Additional content object set class
34
35 =head1 API
36
37 =head2 Class Methods
38
39 =cut
40
41 =head3 search_for_display
42
43 my $contents = Koha::AdditionalContents->search_for_display({
44     category => 'news', # news or html_customizations
45     location => 'slip',
46     lang => 'es-ES',
47     library_id => $branchcode
48 })
49
50 Return Koha::AdditionalContents set for display to user
51
52 You can limit the results by location, language and library by optional params
53
54 library_id should be valid branchcode of defined library
55
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
62 - opacheader
63 - OpacCustomSearch
64 - OpacMainUserBlock
65 - opaccredits
66 - OpacLoginInstructions
67 - OpacSuggestionInstructions
68 - ArticleRequestsDisclaimerText
69 - CookieConsentBar
70 - CookieConsentPopup
71
72 =cut
73
74 sub search_for_display {
75     my ( $self, $params ) = @_;
76     my $lang = $params->{lang};
77
78     # If lang is not default, we will search for entries matching $lang but fallback to default if $lang is not found
79     # Then we need a subquery count in where clause; DBIx::Class/SQL::Abstract does not support it, fallback to literal SQL
80     my $subquery =
81         qq|(SELECT COUNT(*) FROM additional_contents_localizations WHERE lang='$lang' AND additional_content_id=me.additional_content_id)=0|;
82
83     my $search_params;
84     $search_params->{location}       = $params->{location};
85     $search_params->{branchcode}     = $params->{library_id} ? [ $params->{library_id}, undef ] : undef;
86     $search_params->{published_on}   = { '<=' => \'CAST(NOW() AS DATE)' };
87     $search_params->{expirationdate} = [ '-or', { '>=' => \'CAST(NOW() AS DATE)' }, undef ];
88     $search_params->{category}       = $params->{category} if $params->{category};
89     $search_params->{lang}           = 'default' if !$lang || $lang eq 'default';
90     $search_params->{-or}            = [ { 'lang' => $lang }, '-and' => [ 'lang', 'default', \$subquery ] ]
91         if !$search_params->{lang};
92
93     my $attribs = { prefetch => 'additional_content', order_by => 'additional_content.number' };
94     return Koha::AdditionalContentsLocalizations->search( $search_params, $attribs );
95 }
96
97 =head3 find_best_match
98
99     Koha::AdditionalContents->find_best_match({
100         category => , location => , lang => , library_id =>
101     });
102
103     When choosing the best match, a match on lang and library is preferred.
104     Next a match on library and default lang. Then match on All libs and lang.
105     Finally a match with All libs and default lang.
106
107 =cut
108
109 sub find_best_match {
110     my ( $self, $params ) = @_;
111     my $library_id = $params->{library_id};
112     my $lang = $params->{lang};
113
114     my $contents = $self->SUPER::search({
115         category => $params->{category},
116         location => $params->{location},
117         branchcode => [ $library_id, undef ],
118     });
119
120     my $rs = Koha::AdditionalContentsLocalizations->search({
121             additional_content_id => [ $contents->get_column('id') ],
122             lang => [ $lang, 'default' ],
123         });
124
125     # Pick the best
126     my ( $alt1, $alt2, $alt3 );
127     while( my $rec = $rs->next ) {
128         return $rec if $library_id && $rec->branchcode && $rec->branchcode eq $library_id && $lang && $rec->lang eq $lang;
129         $alt1 = $rec if !$alt1 && $library_id && $rec->branchcode && $rec->branchcode eq $library_id;
130         $alt2 = $rec if !$alt2 && $lang && $rec->lang eq $lang;
131         $alt3 = $rec if !$alt3;
132     }
133     return $alt1 // $alt2 // $alt3;
134 }
135
136 =head3 _type
137
138 =cut
139
140 sub _type {
141     return 'AdditionalContent';
142 }
143
144 =head3 object_class
145
146 =cut
147
148 sub object_class {
149     return 'Koha::AdditionalContent';
150 }
151
152 =head1 AUTHOR
153
154 Kyle M Hall <kyle@bywatersolutions.com>
155
156 =cut
157
158 1;