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 ) = @_;
76 my $lang = $params->{lang};
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
81 qq|(SELECT COUNT(*) FROM additional_contents_localizations WHERE lang='$lang' AND additional_content_id=me.additional_content_id)=0|;
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};
93 my $attribs = { prefetch => 'additional_content', order_by => 'additional_content.number' };
94 return Koha::AdditionalContentsLocalizations->search( $search_params, $attribs );
97 =head3 find_best_match
99 Koha::AdditionalContents->find_best_match({
100 category => , location => , lang => , library_id =>
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.
109 sub find_best_match {
110 my ( $self, $params ) = @_;
111 my $library_id = $params->{library_id};
112 my $lang = $params->{lang};
114 my $contents = $self->SUPER::search({
115 category => $params->{category},
116 location => $params->{location},
117 branchcode => [ $library_id, undef ],
120 my $rs = Koha::AdditionalContentsLocalizations->search({
121 additional_content_id => [ $contents->get_column('id') ],
122 lang => [ $lang, 'default' ],
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;
133 return $alt1 // $alt2 // $alt3;
141 return 'AdditionalContent';
149 return 'Koha::AdditionalContent';
154 Kyle M Hall <kyle@bywatersolutions.com>