Bug 20473: Whitespace
[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
67 =cut
68
69 sub search_for_display {
70     my ( $self, $params ) = @_;
71
72     my $search_params;
73     $search_params->{location} = $params->{location};
74     $search_params->{branchcode} = $params->{library_id} ? [ $params->{library_id}, undef ] : undef;
75     $search_params->{published_on} = { '<=' => \'CAST(NOW() AS DATE)' };
76     $search_params->{-or} = [ expirationdate => { '>=' => \'CAST(NOW() AS DATE)' },
77                               expirationdate => undef ];
78     $search_params->{category} = $params->{category} if $params->{category};
79
80     if ( $params->{lang} ) {
81         # FIXME I am failing to translate the following query
82         # SELECT   a1.category,   a1.code,   COALESCE(a2.title, a1.title)
83         # FROM additional_contents a1
84         # LEFT JOIN additional_contents a2 on a1.code=a2.code AND a2.lang="es-ES"
85         # WHERE a1.lang = 'default';
86
87         # So we are retrieving the code with a translated content, then the other ones
88         my $translated_contents =
89           $self->SUPER::search( { %$search_params, lang => $params->{lang} } );
90         my $default_contents = $self->SUPER::search(
91             {
92                 %$search_params,
93                 lang => 'default',
94                 code =>
95                   { '-not_in' => [ $translated_contents->get_column('code') ] }
96             }
97         );
98
99         return $self->SUPER::search(
100             {
101                 idnew => [
102                     $translated_contents->get_column('idnew'),
103                     $default_contents->get_column('idnew')
104                 ]
105             },
106             { order_by => 'number' }
107         );
108     }
109
110     return $self->SUPER::search({%$search_params, lang => 'default'}, { order_by => 'number'});
111 }
112
113 =head3 find_best_match
114
115     Koha::AdditionalContents->find_best_match({
116         category => , location => , lang => , library_id =>
117     });
118
119     When choosing the best match, a match on lang and library is preferred.
120     Next a match on library and default lang. Then match on All libs and lang.
121     Finally a match with All libs and default lang.
122
123 =cut
124
125 sub find_best_match {
126     my ( $self, $params ) = @_;
127     my $library_id = $params->{library_id};
128     my $lang = $params->{lang};
129
130     my $rs = $self->SUPER::search({
131         category => $params->{category},
132         location => $params->{location},
133         lang => [ $lang, 'default' ],
134         branchcode => [ $library_id, undef ],
135     });
136
137     # Pick the best
138     my ( $alt1, $alt2, $alt3 );
139     while( my $rec = $rs->next ) {
140         return $rec if $library_id && $rec->branchcode && $rec->branchcode eq $library_id && $lang && $rec->lang eq $lang;
141         $alt1 = $rec if !$alt1 && $library_id && $rec->branchcode && $rec->branchcode eq $library_id;
142         $alt2 = $rec if !$alt2 && $lang && $rec->lang eq $lang;
143         $alt3 = $rec if !$alt3;
144     }
145     return $alt1 // $alt2 // $alt3;
146 }
147
148 =head3 _type
149
150 =cut
151
152 sub _type {
153     return 'AdditionalContent';
154 }
155
156 =head3 object_class
157
158 =cut
159
160 sub object_class {
161     return 'Koha::AdditionalContent';
162 }
163
164 =head1 AUTHOR
165
166 Kyle M Hall <kyle@bywatersolutions.com>
167
168 =cut
169
170 1;