Bug 23681: Move to ::Patron::Restriction::Type(s)
[koha.git] / Koha / Patron / Restriction / Type.pm
1 package Koha::Patron::Restriction::Type;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use base qw(Koha::Object);
21
22 use Koha::Exceptions;
23
24 use Koha::Patron::Restriction::Types;
25 use C4::Context;
26
27 =head1 NAME
28
29 Koha::Patron::Restriction::Type - Koha RestrictionType Object class
30
31 =head1 API
32
33 =head2 Class methods
34
35 =head3 delete
36
37 Overloaded delete method that does extra clean up:
38 - Reset all restrictions using the restriction type about to be deleted
39   back to whichever restriction is marked as default
40
41 =cut
42
43 sub delete {
44     my ($self) = @_;
45
46     # Ensure we can't delete the current default
47     Koha::Exceptions::CannotDeleteDefault->throw if $self->is_default;
48
49     # Ensure we can't delete system values
50     Koha::Exceptions::CannotDeleteSystem->throw if $self->is_system;
51
52     return $self->_result->result_source->schema->txn_do(
53         sub {
54             # Update all linked restrictions to the default
55             my $default =
56               Koha::Patron::Restriction::Types->find( { is_default => 1 } )->code;
57
58             # We can't use Koha objects here because Koha::Patron::Debarments
59             # is not a Koha object. So we'll do it old skool
60             my $rows = C4::Context->dbh->do(
61                 "UPDATE borrower_debarments SET type = ? WHERE type = ?",
62                 undef, ( $default, $self->code ) );
63
64             return $self->SUPER::delete;
65         }
66     );
67 }
68
69 =head3 make_default
70
71 Set the current restriction type as the default for manual restrictions
72
73 =cut
74
75 sub make_default {
76     my ( $self ) = @_;
77
78     $self->_result->result_source->schema->txn_do(
79         sub {
80             my $types =
81               Koha::Patron::Restriction::Types->search( { code => { '!=' => $self->code } } );
82             $types->update( { is_default => 0 } );
83             $self->set( { is_default => 1 } );
84             $self->SUPER::store;
85         }
86     );
87
88     return $self;
89 }
90
91 =head2 Internal methods
92
93 =head3 _type
94
95 =cut
96
97 sub _type {
98     return 'RestrictionType';
99 }
100
101 1;