Bug 31383: Create a parent-child DB relation for additional content
[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
77     my $search_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
84     ];
85     $search_params->{category} = $params->{category} if $params->{category};
86
87     my $contents = $self->SUPER::search( $search_params, { order_by => 'number' } );
88
89     if ( $params->{lang} ) {
90         my $translated_contents = Koha::AdditionalContentsLocalizations->search(
91             {
92                 additional_content_id => [$contents->get_column('id')],
93                 lang => $params->{lang},
94             }
95         );
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(
99             {
100                 additional_content_id => [array_minus(@all_content_id, @translated_content_id)],
101                 lang                  => 'default',
102             }
103         );
104         return Koha::AdditionalContentsLocalizations->search(
105             {
106                 id => [
107                     $translated_contents->get_column('id'),
108                     $default_contents->get_column('id'),
109                 ]
110             },
111         );
112     }
113     return $contents->search( { lang => 'default' }, { order_by => 'number' } )->translated_contents;
114 }
115
116 =head3 find_best_match
117
118     Koha::AdditionalContents->find_best_match({
119         category => , location => , lang => , library_id =>
120     });
121
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.
125
126 =cut
127
128 sub find_best_match {
129     my ( $self, $params ) = @_;
130     my $library_id = $params->{library_id};
131     my $lang = $params->{lang};
132
133     my $contents = $self->SUPER::search({
134         category => $params->{category},
135         location => $params->{location},
136         branchcode => [ $library_id, undef ],
137     });
138
139     my $rs = Koha::AdditionalContentsLocalizations->search({
140             additional_content_id => [ $contents->get_column('id') ],
141             lang => [ $lang, 'default' ],
142         });
143
144     # Pick the best
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;
151     }
152     return $alt1 // $alt2 // $alt3;
153 }
154
155 =head3 _type
156
157 =cut
158
159 sub _type {
160     return 'AdditionalContent';
161 }
162
163 =head3 object_class
164
165 =cut
166
167 sub object_class {
168     return 'Koha::AdditionalContent';
169 }
170
171 =head1 AUTHOR
172
173 Kyle M Hall <kyle@bywatersolutions.com>
174
175 =cut
176
177 1;