Bug 33036: REST API: Merge biblio records implements merging of records
[koha.git] / Koha / AdditionalContent.pm
1 package Koha::AdditionalContent;
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::DateUtils qw( dt_from_string );
24 use Koha::Libraries;
25 use Koha::Patrons;
26 use Koha::AdditionalContentsLocalizations;
27
28 use base qw(Koha::Object);
29
30 =head1 NAME
31
32 Koha::AdditionalContent - Koha Additional content object class
33
34 =head1 API
35
36 =head2 Class Methods
37
38 =cut
39
40 =head3 author
41
42     $additional_content->author;
43
44 Return the Koha::Patron object for the patron who authored this additional content
45
46 =cut
47
48 sub author {
49     my ($self) = @_;
50     my $author_rs = $self->_result->borrowernumber;
51     return unless $author_rs;
52     return Koha::Patron->_new_from_dbic($author_rs);
53 }
54
55 =head3 is_expired
56
57 my $is_expired = $additional_content->is_expired;
58
59 Returns 1 if the additional content is expired or 0;
60
61 =cut
62
63 sub is_expired {
64     my ($self) = @_;
65
66     return 0 unless $self->expirationdate;
67     return 1 if dt_from_string( $self->expirationdate ) < dt_from_string->truncate( to => 'day' );
68     return 0;
69 }
70
71 =head3 library
72
73 my $library = $additional_content->library;
74
75 Returns Koha::Library object or undef
76
77 =cut
78
79 sub library {
80     my ($self) = @_;
81
82     my $library_rs = $self->_result->branchcode;
83     return unless $library_rs;
84     return Koha::Library->_new_from_dbic($library_rs);
85 }
86
87 =head3 translated_contents
88
89 my $translated_contents = $additional_content->translated_contents;
90 $additional_content->translated_contents(\@contents)
91
92 =cut
93
94 sub translated_contents {
95     my ( $self, $localizations ) = @_;
96     if ($localizations) {
97         my $schema = $self->_result->result_source->schema;
98         $schema->txn_do(
99             sub {
100                 $self->translated_contents->delete;
101
102                 for my $localization (@$localizations) {
103                     $self->_result->add_to_additional_contents_localizations($localization);
104                 }
105             }
106         );
107     }
108
109     my $rs = $self->_result->additional_contents_localizations;
110     return Koha::AdditionalContentsLocalizations->_new_from_dbic($rs);
111 }
112
113 =head3 default_localization
114
115 my $default_content = $additional_content->default_localization;
116
117 Return the default content.
118
119 =cut
120
121 sub default_localization {
122     my ($self) = @_;
123     my $rs = $self->_result->additional_contents_localizations->find( { lang => 'default' } );
124     return Koha::AdditionalContentsLocalization->_new_from_dbic($rs);
125 }
126
127 =head3 translated_content
128
129 my $translated_content = $additional_content->translated_content($lang);
130
131 Return the translated content for a given language. The default is returned if none exist.
132
133 =cut
134
135 sub translated_content {
136     my ( $self, $lang ) = @_;
137     my $content = $self->translated_contents->search(
138         { lang => [ 'default', ( $lang ? $lang : () ) ] },
139         { ( $lang ? ( order_by => \[ "field(lang, '" . $lang . "', 'default')" ] ) : () ) }
140     )->next;
141     return $content;
142 }
143
144 =head3 _type
145
146 =cut
147
148 sub _type {
149     return 'AdditionalContent';
150 }
151
152 =head1 AUTHOR
153
154 Kyle M Hall <kyle@bywatersolutions.com>
155
156 =cut
157
158 1;