Bug 27378: Introduce cookie consent to OPAC and staff client
[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 Koha::Database;
23 use Koha::Exceptions;
24 use Koha::AdditionalContent;
25
26 use base qw(Koha::Objects);
27
28 =head1 NAME
29
30 Koha::AdditionalContents - Koha Additional content object set class
31
32 =head1 API
33
34 =head2 Class Methods
35
36 =cut
37
38 =head3 search_for_display
39
40 my $contents = Koha::AdditionalContents->search_for_display({
41     category => 'news', # news or html_customizations
42     location => 'slip',
43     lang => 'es-ES',
44     library_id => $branchcode
45 })
46
47 Return Koha::AdditionalContents set for display to user
48
49 You can limit the results by location, language and library by optional params
50
51 library_id should be valid branchcode of defined library
52
53 location is one of this:
54 - slip - for ISSUESLIP notice
55 - staff_only - for intranet
56 - opac_only - for OPAC
57 - staff_and_opac - for intranet and online catalogue
58 - OpacNavRight - Right column in the online catalogue
59 - opacheader
60 - OpacCustomSearch
61 - OpacMainUserBlock
62 - opaccredits
63 - OpacLoginInstructions
64 - OpacSuggestionInstructions
65 - ArticleRequestsDisclaimerText
66 - CookieConsentBar
67 - CookieConsentPopup
68
69 =cut
70
71 sub search_for_display {
72     my ( $self, $params ) = @_;
73
74     my $search_params;
75     $search_params->{location} = $params->{location};
76     $search_params->{branchcode} = $params->{library_id} ? [ $params->{library_id}, undef ] : undef;
77     $search_params->{published_on} = { '<=' => \'CAST(NOW() AS DATE)' };
78     $search_params->{-or} = [ expirationdate => { '>=' => \'CAST(NOW() AS DATE)' },
79                               expirationdate => undef ];
80     $search_params->{category} = $params->{category} if $params->{category};
81
82     if ( $params->{lang} ) {
83         # FIXME I am failing to translate the following query
84         # SELECT   a1.category,   a1.code,   COALESCE(a2.title, a1.title)
85         # FROM additional_contents a1
86         # LEFT JOIN additional_contents a2 on a1.code=a2.code AND a2.lang="es-ES"
87         # WHERE a1.lang = 'default';
88
89         # So we are retrieving the code with a translated content, then the other ones
90         my $translated_contents =
91           $self->SUPER::search( { %$search_params, lang => $params->{lang} } );
92         my $default_contents = $self->SUPER::search(
93             {
94                 %$search_params,
95                 lang => 'default',
96                 code =>
97                   { '-not_in' => [ $translated_contents->get_column('code') ] }
98             }
99         );
100
101         return $self->SUPER::search(
102             {
103                 idnew => [
104                     $translated_contents->get_column('idnew'),
105                     $default_contents->get_column('idnew')
106                 ]
107             },
108             { order_by => 'number' }
109         );
110     }
111
112     return $self->SUPER::search({%$search_params, lang => 'default'}, { order_by => 'number'});
113 }
114
115 =head3 find_best_match
116
117     Koha::AdditionalContents->find_best_match({
118         category => , location => , lang => , library_id =>
119     });
120
121     When choosing the best match, a match on lang and library is preferred.
122     Next a match on library and default lang. Then match on All libs and lang.
123     Finally a match with All libs and default lang.
124
125 =cut
126
127 sub find_best_match {
128     my ( $self, $params ) = @_;
129     my $library_id = $params->{library_id};
130     my $lang = $params->{lang};
131
132     my $rs = $self->SUPER::search({
133         category => $params->{category},
134         location => $params->{location},
135         lang => [ $lang, 'default' ],
136         branchcode => [ $library_id, undef ],
137     });
138
139     # Pick the best
140     my ( $alt1, $alt2, $alt3 );
141     while( my $rec = $rs->next ) {
142         return $rec if $library_id && $rec->branchcode && $rec->branchcode eq $library_id && $lang && $rec->lang eq $lang;
143         $alt1 = $rec if !$alt1 && $library_id && $rec->branchcode && $rec->branchcode eq $library_id;
144         $alt2 = $rec if !$alt2 && $lang && $rec->lang eq $lang;
145         $alt3 = $rec if !$alt3;
146     }
147     return $alt1 // $alt2 // $alt3;
148 }
149
150 =head3 _type
151
152 =cut
153
154 sub _type {
155     return 'AdditionalContent';
156 }
157
158 =head3 object_class
159
160 =cut
161
162 sub object_class {
163     return 'Koha::AdditionalContent';
164 }
165
166 =head1 AUTHOR
167
168 Kyle M Hall <kyle@bywatersolutions.com>
169
170 =cut
171
172 1;