Bug 13757: (followup) Only touch opac_editable attributes
[koha-ffzg.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
58                 ->throw(
59                 "Duplicate verification token " . $self->verification_token );
60         }
61     }
62
63     if ( $self->extended_attributes ) {
64         try {
65             my $json_parser = JSON->new;
66             $json_parser->decode( $self->extended_attributes );
67         }
68         catch {
69             Koha::Exceptions::Patron::Modification::InvalidData->throw(
70                 'The passed extended_attributes is not valid JSON');
71         };
72     }
73
74     return $self->SUPER::store();
75 }
76
77
78 =head2 approve
79
80 $m->approve();
81
82 Commits the pending modifications to the borrower record and removes
83 them from the modifications table.
84
85 =cut
86
87 sub approve {
88     my ($self) = @_;
89
90     my $data = $self->unblessed();
91     my $extended_attributes;
92
93     delete $data->{timestamp};
94     delete $data->{verification_token};
95     delete $data->{extended_attributes};
96
97     foreach my $key ( keys %$data ) {
98         delete $data->{$key} unless ( defined( $data->{$key} ) );
99     }
100
101     my $patron = Koha::Patrons->find( $self->borrowernumber );
102
103     return unless $patron;
104
105     $patron->set($data);
106
107     # Take care of extended attributes
108     if ( $self->extended_attributes ) {
109         $extended_attributes
110             = try { decode_json( $self->extended_attributes ) }
111         catch {
112             Koha::Exceptions::Patron::Modification::InvalidData->throw(
113                 'The passed extended_attributes is not valid JSON');
114         };
115     }
116
117     $self->_result->result_source->schema->txn_do(
118         sub {
119             try {
120                 $patron->store();
121
122                 # Deal with attributes
123                 my @codes
124                     = uniq( map { $_->{code} } @{$extended_attributes} );
125                 foreach my $code (@codes) {
126                     map { $_->delete } Koha::Patron::Attributes->search(
127                         {   borrowernumber => $patron->borrowernumber,
128                             code           => $code
129                         }
130                     );
131                 }
132                 foreach my $attr ( @{$extended_attributes} ) {
133                     Koha::Patron::Attribute->new(
134                         {   borrowernumber => $patron->borrowernumber,
135                             code           => $attr->{code},
136                             attribute      => $attr->{value}
137                         }
138                     )->store;
139                 }
140             }
141             catch {
142                 if ( $_->isa('DBIx::Class::Exception') ) {
143                     Koha::Exceptions::Patron::Modification->throw(
144                         $_->{msg} );
145                 }
146                 else {
147                     Koha::Exceptions::Patron::Modification->throw($@);
148                 }
149             };
150         }
151     );
152
153     return $self->delete();
154 }
155
156
157 =head3 type
158
159 =cut
160
161 sub _type {
162     return 'BorrowerModification';
163 }
164
165 =head1 AUTHOR
166
167 Kyle M Hall <kyle@bywatersolutions.com>
168
169 =cut
170
171 1;