Bug 23681: (QA follow-up) New schema files
[koha.git] / Koha / RestrictionType.pm
1 package Koha::RestrictionType;
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::RestrictionTypes;
23 use C4::Context;
24
25 =head1 NAME
26
27 Koha::RestrictionType - Koha RestrictionType Object class
28
29 =head1 API
30
31 =head2 Class methods
32
33 =head3 delete
34
35 Overloaded delete method that does extra clean up:
36 - Reset all restrictions using the restriction type about to be deleted
37   back to whichever restriction is marked as default
38
39 =cut
40
41 sub delete {
42     my ( $self ) = @_;
43
44     # Find out what the default is
45     my $default = Koha::RestrictionTypes->find({ is_default => 1 })->code;
46     # Ensure we're not trying to delete a is_system type (this includes
47     # the default type)
48     return 0 if $self->is_system == 1;
49     # We can't use Koha objects here because Koha::Patron::Debarments
50     # is not a Koha object. So we'll do it old skool
51     my $rows = C4::Context->dbh->do(
52         "UPDATE borrower_debarments SET type = ? WHERE type = ?",
53         undef,
54         ($default, $self->code)
55     );
56
57     # Now do the delete if the update was successful
58     if ($rows) {
59         my $deleted = $self->SUPER::delete($self);
60         return $deleted
61     }
62
63     return 0;
64 }
65
66 =head3 make_default
67
68 Set the current restriction type as the default for manual restrictions
69
70 =cut
71
72 sub make_default {
73     my ( $self ) = @_;
74
75     $self->_result->result_source->schema->txn_do(
76         sub {
77             my $types =
78               Koha::RestrictionTypes->search( { code => { '!=' => $self->code } } );
79             $types->update( { is_default => 0 } );
80             $self->set( { is_default => 1 } );
81             $self->SUPER::store;
82         }
83     );
84
85     return $self;
86 }
87
88 =head2 Internal methods
89
90 =head3 _type
91
92 =cut
93
94 sub _type {
95     return 'RestrictionType';
96 }
97
98 1;