Line 0
Link Here
|
|
|
1 |
package Koha::AuthorisedValue; |
2 |
|
3 |
# Copyright ByWater Solutions 2014 |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Carp; |
23 |
|
24 |
use Koha::Database; |
25 |
|
26 |
use base qw(Koha::Object); |
27 |
|
28 |
=head1 NAME |
29 |
|
30 |
Koha::AuthorisedValue - Koha Authorised Value Object class |
31 |
|
32 |
=head1 API |
33 |
|
34 |
=head2 Class Methods |
35 |
|
36 |
=cut |
37 |
|
38 |
=head3 branch_limitations |
39 |
|
40 |
my $limitations = $av->branch_limitations(); |
41 |
|
42 |
$av->branch_limitations( \@branchcodes ); |
43 |
|
44 |
=cut |
45 |
|
46 |
sub branch_limitations { |
47 |
my ( $self, $branchcodes ) = @_; |
48 |
|
49 |
if ($branchcodes) { |
50 |
return $self->replace_branch_limitations($branchcodes); |
51 |
} |
52 |
else { |
53 |
return $self->get_branch_limitations(); |
54 |
} |
55 |
|
56 |
} |
57 |
|
58 |
=head3 get_branch_limitations |
59 |
|
60 |
my $limitations = $av->get_branch_limitations(); |
61 |
|
62 |
=cut |
63 |
|
64 |
sub get_branch_limitations { |
65 |
my ($self) = @_; |
66 |
|
67 |
my @branchcodes = |
68 |
$self->_avb_resultset->search( { av_id => $self->id() } ) |
69 |
->get_column('branchcode')->all(); |
70 |
|
71 |
return \@branchcodes; |
72 |
} |
73 |
|
74 |
=head3 add_branch_limitation |
75 |
|
76 |
$av->add_branch_limitation( $branchcode ); |
77 |
|
78 |
=cut |
79 |
|
80 |
sub add_branch_limitation { |
81 |
my ( $self, $branchcode ) = @_; |
82 |
|
83 |
croak("No branchcode passed in!") unless $branchcode; |
84 |
|
85 |
my $limitation = $self->_avb_resultset->update_or_create( |
86 |
{ av_id => $self->id(), branchcode => $branchcode } ); |
87 |
|
88 |
return $limitation ? 1 : undef; |
89 |
} |
90 |
|
91 |
=cut |
92 |
|
93 |
=head3 add_branch_limitation |
94 |
|
95 |
$av->del_branch_limitation( $branchcode ); |
96 |
|
97 |
=cut |
98 |
|
99 |
sub del_branch_limitation { |
100 |
my ( $self, $branchcode ) = @_; |
101 |
|
102 |
croak("No branchcode passed in!") unless $branchcode; |
103 |
|
104 |
my $limitation = |
105 |
$self->_avb_resultset->find( |
106 |
{ av_id => $self->id(), branchcode => $branchcode } ); |
107 |
|
108 |
unless ($limitation) { |
109 |
my $id = $self->id(); |
110 |
carp( |
111 |
"No branch limit for branch $branchcode found for av_id $id to delete!" |
112 |
); |
113 |
return; |
114 |
} |
115 |
|
116 |
return $limitation->delete(); |
117 |
} |
118 |
|
119 |
=head3 replace_branch_limitations |
120 |
|
121 |
$av->replace_branch_limitations( \@branchcodes ); |
122 |
|
123 |
=cut |
124 |
|
125 |
sub replace_branch_limitations { |
126 |
my ( $self, $branchcodes ) = @_; |
127 |
|
128 |
$self->_avb_resultset->search( { av_id => $self->id() } )->delete(); |
129 |
|
130 |
my @return_values = |
131 |
map { $self->add_branch_limitation($_) } @$branchcodes; |
132 |
|
133 |
return \@return_values; |
134 |
} |
135 |
|
136 |
=head3 lib_opac |
137 |
|
138 |
my $description = $av->lib_opac(); |
139 |
|
140 |
$av->lib_opac( $description ); |
141 |
|
142 |
=cut |
143 |
|
144 |
sub opac_description { |
145 |
my ( $self, $value ) = @_; |
146 |
|
147 |
return $self->lib_opac() || $self->lib(); |
148 |
} |
149 |
|
150 |
=head3 Koha::Objects->_resultset |
151 |
|
152 |
Returns the internal resultset or creates it if undefined |
153 |
|
154 |
=cut |
155 |
|
156 |
sub _avb_resultset { |
157 |
my ($self) = @_; |
158 |
|
159 |
$self->{_avb_resultset} ||= |
160 |
Koha::Database->new()->schema()->resultset('AuthorisedValuesBranch'); |
161 |
|
162 |
$self->{_avb_resultset}; |
163 |
} |
164 |
|
165 |
=head3 type |
166 |
|
167 |
=cut |
168 |
|
169 |
sub type { |
170 |
return 'AuthorisedValue'; |
171 |
} |
172 |
|
173 |
=head1 AUTHOR |
174 |
|
175 |
Kyle M Hall <kyle@bywatersolutions.com> |
176 |
|
177 |
=cut |
178 |
|
179 |
1; |
180 |
## Please see file perltidy.ERR |