Bug 14836: Move Koha::PatronCategor* to Koha::Patron::Categor*
[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 =head3 default_messaging
39
40 my $messaging = $category->default_messaging();
41
42 =cut
43
44 sub default_messaging {
45     my ( $self ) = @_;
46     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
47     my @messaging;
48     foreach my $option (@$messaging_options) {
49         my $pref = C4::Members::Messaging::GetMessagingPreferences(
50             {
51                 categorycode => $self->categorycode,
52                 message_name => $option->{message_name}
53             }
54         );
55         next unless $pref->{transports};
56         my $brief_pref = {
57             message_attribute_id      => $option->{message_attribute_id},
58             message_name              => $option->{message_name},
59             $option->{'message_name'} => 1,
60         };
61         foreach my $transport ( keys %{ $pref->{transports} } ) {
62             push @{ $brief_pref->{transports} }, { transport => $transport };
63         }
64         push @messaging, $brief_pref;
65     }
66     return \@messaging;
67 }
68
69 =head3 branch_limitations
70
71 my $limitations = $category->branch_limitations();
72
73 $category->branch_limitations( \@branchcodes );
74
75 =cut
76
77 sub branch_limitations {
78     my ( $self, $branchcodes ) = @_;
79
80     if ($branchcodes) {
81         return $self->replace_branch_limitations($branchcodes);
82     }
83     else {
84         return $self->get_branch_limitations();
85     }
86
87 }
88
89 =head3 get_branch_limitations
90
91 my $limitations = $category->get_branch_limitations();
92
93 =cut
94
95 sub get_branch_limitations {
96     my ($self) = @_;
97
98     my @branchcodes =
99       $self->_catb_resultset->search( { categorycode => $self->categorycode } )
100       ->get_column('branchcode')->all();
101
102     return \@branchcodes;
103 }
104
105 =head3 add_branch_limitation
106
107 $category->add_branch_limitation( $branchcode );
108
109 =cut
110
111 sub add_branch_limitation {
112     my ( $self, $branchcode ) = @_;
113
114     croak("No branchcode passed in!") unless $branchcode;
115
116     my $limitation = $self->_catb_resultset->update_or_create(
117         { categorycode => $self->categorycode, branchcode => $branchcode } );
118
119     return $limitation ? 1 : undef;
120 }
121
122 =head3 del_branch_limitation
123
124 $category->del_branch_limitation( $branchcode );
125
126 =cut
127
128 sub del_branch_limitation {
129     my ( $self, $branchcode ) = @_;
130
131     croak("No branchcode passed in!") unless $branchcode;
132
133     my $limitation =
134       $self->_catb_resultset->find(
135         { categorycode => $self->categorycode, branchcode => $branchcode } );
136
137     unless ($limitation) {
138         my $categorycode = $self->categorycode;
139         carp(
140 "No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
141         );
142         return;
143     }
144
145     return $limitation->delete();
146 }
147
148 =head3 replace_branch_limitations
149
150 $category->replace_branch_limitations( \@branchcodes );
151
152 =cut
153
154 sub replace_branch_limitations {
155     my ( $self, $branchcodes ) = @_;
156
157     $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
158
159     my @return_values =
160       map { $self->add_branch_limitation($_) } @$branchcodes;
161
162     return \@return_values;
163 }
164
165 =head3 Koha::Objects->_catb_resultset
166
167 Returns the internal resultset or creates it if undefined
168
169 =cut
170
171 sub _catb_resultset {
172     my ($self) = @_;
173
174     $self->{_catb_resultset} ||=
175       Koha::Database->new->schema->resultset('CategoriesBranch');
176
177     return $self->{_catb_resultset};
178 }
179
180 =head3 type
181
182 =cut
183
184 sub type {
185     return 'Category';
186 }
187
188 1;