Bug 17069: Koha::Patron::Category->store must default checkprevcheckout to 'inherit'
[koha.git] / Koha / Patron / Category.pm
1 package Koha::Patron::Category;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Carp;
21
22 use C4::Members::Messaging;
23
24 use Koha::Database;
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::Patron;;Category - Koha Patron;;Category Object class
31
32 =head1 API
33
34 =head2 Class Methods
35
36 =cut
37
38 sub store {
39     my ( $self ) = @_;
40     $self->checkprevcheckout('inherit')
41         unless defined $self->checkprevcheckout;
42     return $self->SUPER::store;
43 }
44
45 =head3 default_messaging
46
47 my $messaging = $category->default_messaging();
48
49 =cut
50
51 sub default_messaging {
52     my ( $self ) = @_;
53     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
54     my @messaging;
55     foreach my $option (@$messaging_options) {
56         my $pref = C4::Members::Messaging::GetMessagingPreferences(
57             {
58                 categorycode => $self->categorycode,
59                 message_name => $option->{message_name}
60             }
61         );
62         next unless $pref->{transports};
63         my $brief_pref = {
64             message_attribute_id      => $option->{message_attribute_id},
65             message_name              => $option->{message_name},
66             $option->{'message_name'} => 1,
67         };
68         foreach my $transport ( keys %{ $pref->{transports} } ) {
69             push @{ $brief_pref->{transports} }, { transport => $transport };
70         }
71         push @messaging, $brief_pref;
72     }
73     return \@messaging;
74 }
75
76 =head3 branch_limitations
77
78 my $limitations = $category->branch_limitations();
79
80 $category->branch_limitations( \@branchcodes );
81
82 =cut
83
84 sub branch_limitations {
85     my ( $self, $branchcodes ) = @_;
86
87     if ($branchcodes) {
88         return $self->replace_branch_limitations($branchcodes);
89     }
90     else {
91         return $self->get_branch_limitations();
92     }
93
94 }
95
96 =head3 get_branch_limitations
97
98 my $limitations = $category->get_branch_limitations();
99
100 =cut
101
102 sub get_branch_limitations {
103     my ($self) = @_;
104
105     my @branchcodes =
106       $self->_catb_resultset->search( { categorycode => $self->categorycode } )
107       ->get_column('branchcode')->all();
108
109     return \@branchcodes;
110 }
111
112 =head3 add_branch_limitation
113
114 $category->add_branch_limitation( $branchcode );
115
116 =cut
117
118 sub add_branch_limitation {
119     my ( $self, $branchcode ) = @_;
120
121     croak("No branchcode passed in!") unless $branchcode;
122
123     my $limitation = $self->_catb_resultset->update_or_create(
124         { categorycode => $self->categorycode, branchcode => $branchcode } );
125
126     return $limitation ? 1 : undef;
127 }
128
129 =head3 del_branch_limitation
130
131 $category->del_branch_limitation( $branchcode );
132
133 =cut
134
135 sub del_branch_limitation {
136     my ( $self, $branchcode ) = @_;
137
138     croak("No branchcode passed in!") unless $branchcode;
139
140     my $limitation =
141       $self->_catb_resultset->find(
142         { categorycode => $self->categorycode, branchcode => $branchcode } );
143
144     unless ($limitation) {
145         my $categorycode = $self->categorycode;
146         carp(
147 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
148         );
149         return;
150     }
151
152     return $limitation->delete();
153 }
154
155 =head3 replace_branch_limitations
156
157 $category->replace_branch_limitations( \@branchcodes );
158
159 =cut
160
161 sub replace_branch_limitations {
162     my ( $self, $branchcodes ) = @_;
163
164     $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
165
166     my @return_values =
167       map { $self->add_branch_limitation($_) } @$branchcodes;
168
169     return \@return_values;
170 }
171
172 =head3 Koha::Objects->_catb_resultset
173
174 Returns the internal resultset or creates it if undefined
175
176 =cut
177
178 sub _catb_resultset {
179     my ($self) = @_;
180
181     $self->{_catb_resultset} ||=
182       Koha::Database->new->schema->resultset('CategoriesBranch');
183
184     return $self->{_catb_resultset};
185 }
186
187 =head3 type
188
189 =cut
190
191 sub _type {
192     return 'Category';
193 }
194
195 1;