|
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 |
=head3 add_branch_limitation |
| 92 |
|
| 93 |
$av->del_branch_limitation( $branchcode ); |
| 94 |
|
| 95 |
=cut |
| 96 |
|
| 97 |
sub del_branch_limitation { |
| 98 |
my ( $self, $branchcode ) = @_; |
| 99 |
|
| 100 |
croak("No branchcode passed in!") unless $branchcode; |
| 101 |
|
| 102 |
my $limitation = |
| 103 |
$self->_avb_resultset->find( |
| 104 |
{ av_id => $self->id(), branchcode => $branchcode } ); |
| 105 |
|
| 106 |
unless ($limitation) { |
| 107 |
my $id = $self->id(); |
| 108 |
carp( |
| 109 |
"No branch limit for branch $branchcode found for av_id $id to delete!" |
| 110 |
); |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
return $limitation->delete(); |
| 115 |
} |
| 116 |
|
| 117 |
=head3 replace_branch_limitations |
| 118 |
|
| 119 |
$av->replace_branch_limitations( \@branchcodes ); |
| 120 |
|
| 121 |
=cut |
| 122 |
|
| 123 |
sub replace_branch_limitations { |
| 124 |
my ( $self, $branchcodes ) = @_; |
| 125 |
|
| 126 |
$self->_avb_resultset->search( { av_id => $self->id() } )->delete(); |
| 127 |
|
| 128 |
my @return_values = |
| 129 |
map { $self->add_branch_limitation($_) } @$branchcodes; |
| 130 |
|
| 131 |
return \@return_values; |
| 132 |
} |
| 133 |
|
| 134 |
=head3 lib_opac |
| 135 |
|
| 136 |
my $description = $av->lib_opac(); |
| 137 |
|
| 138 |
$av->lib_opac( $description ); |
| 139 |
|
| 140 |
=cut |
| 141 |
|
| 142 |
sub opac_description { |
| 143 |
my ( $self, $value ) = @_; |
| 144 |
|
| 145 |
return $self->lib_opac() || $self->lib(); |
| 146 |
} |
| 147 |
|
| 148 |
=head3 Koha::Objects->_resultset |
| 149 |
|
| 150 |
Returns the internal resultset or creates it if undefined |
| 151 |
|
| 152 |
=cut |
| 153 |
|
| 154 |
sub _avb_resultset { |
| 155 |
my ($self) = @_; |
| 156 |
|
| 157 |
$self->{_avb_resultset} ||= |
| 158 |
Koha::Database->new()->schema()->resultset('AuthorisedValuesBranch'); |
| 159 |
|
| 160 |
$self->{_avb_resultset}; |
| 161 |
} |
| 162 |
|
| 163 |
=head3 type |
| 164 |
|
| 165 |
=cut |
| 166 |
|
| 167 |
sub type { |
| 168 |
return 'AuthorisedValue'; |
| 169 |
} |
| 170 |
|
| 171 |
=head1 AUTHOR |
| 172 |
|
| 173 |
Kyle M Hall <kyle@bywatersolutions.com> |
| 174 |
|
| 175 |
=cut |
| 176 |
|
| 177 |
1; |
| 178 |
## Please see file perltidy.ERR |