Bug 18336: Add explicit index names to kohastructure.sql
[koha.git] / Koha / Patron / Modification.pm
1 package Koha::Patron::Modification;
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 use Koha::Exceptions::Patron::Modification;
26 use Koha::Patron::Attribute;
27 use Koha::Patron::Attributes;
28 use Koha::Patron::Modifications;
29
30 use JSON;
31 use List::MoreUtils qw( uniq );
32 use Try::Tiny;
33
34 use base qw(Koha::Object);
35
36 =head1 NAME
37
38 Koha::Patron::Modification - Class represents a request to modify or create a patron
39
40 =head2 Class Methods
41
42 =cut
43
44 =head2 store
45
46 =cut
47
48 sub store {
49     my ($self) = @_;
50
51     if ( $self->verification_token ) {
52         if (Koha::Patron::Modifications->search(
53                 { verification_token => $self->verification_token }
54             )->count()
55             )
56         {
57             Koha::Exceptions::Patron::Modification::DuplicateVerificationToken->throw(
58                 "Duplicate verification token " . $self->verification_token );
59         }
60     }
61
62     if ( $self->extended_attributes ) {
63         try {
64             my $json_parser = JSON->new;
65             $json_parser->decode( $self->extended_attributes );
66         }
67         catch {
68             Koha::Exceptions::Patron::Modification::InvalidData->throw(
69                 'The passed extended_attributes is not valid JSON');
70         };
71     }
72
73     return $self->SUPER::store();
74 }
75
76 =head2 approve
77
78 $m->approve();
79
80 Commits the pending modifications to the borrower record and removes
81 them from the modifications table.
82
83 =cut
84
85 sub approve {
86     my ($self) = @_;
87
88     my $data = $self->unblessed();
89     my $extended_attributes;
90
91     delete $data->{timestamp};
92     delete $data->{verification_token};
93     delete $data->{extended_attributes};
94
95     foreach my $key ( keys %$data ) {
96         delete $data->{$key} unless ( defined( $data->{$key} ) );
97     }
98
99     my $patron = Koha::Patrons->find( $self->borrowernumber );
100
101     return unless $patron;
102
103     $patron->set($data);
104
105     # Take care of extended attributes
106     if ( $self->extended_attributes ) {
107         $extended_attributes = try { from_json( $self->extended_attributes ) }
108         catch {
109             Koha::Exceptions::Patron::Modification::InvalidData->throw(
110                 'The passed extended_attributes is not valid JSON');
111         };
112     }
113
114     $self->_result->result_source->schema->txn_do(
115         sub {
116             try {
117                 $patron->store();
118
119                 # Deal with attributes
120                 my @codes
121                     = uniq( map { $_->{code} } @{$extended_attributes} );
122                 foreach my $code (@codes) {
123                     map { $_->delete } Koha::Patron::Attributes->search(
124                         {   borrowernumber => $patron->borrowernumber,
125                             code           => $code
126                         }
127                     );
128                 }
129                 foreach my $attr ( @{$extended_attributes} ) {
130                     Koha::Patron::Attribute->new(
131                         {   borrowernumber => $patron->borrowernumber,
132                             code           => $attr->{code},
133                             attribute      => $attr->{value}
134                         }
135                     )->store
136                         if $attr->{value} # there's a value
137                            or
138                           (    defined $attr->{value} # there's a value that is 0, and not
139                             && $attr->{value} ne ""   # the empty string which means delete
140                             && $attr->{value} == 0
141                           );
142                 }
143             }
144             catch {
145                 if ( $_->isa('DBIx::Class::Exception') ) {
146                     Koha::Exceptions::Patron::Modification->throw( $_->{msg} );
147                 }
148                 else {
149                     Koha::Exceptions::Patron::Modification->throw($@);
150                 }
151             };
152         }
153     );
154
155     return $self->delete();
156 }
157
158 =head3 type
159
160 =cut
161
162 sub _type {
163     return 'BorrowerModification';
164 }
165
166 =head1 AUTHOR
167
168 Kyle M Hall <kyle@bywatersolutions.com>
169
170 =cut
171
172 1;