1b42223f048812e88cefa0a6f908f44d318f9085
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22
23 use Koha::Database;
24 use Koha::Exceptions::Patron::Modification;
25 use Koha::Patron::Attribute;
26 use Koha::Patron::Attributes;
27 use Koha::Patron::Modifications;
28
29 use JSON qw( from_json );
30 use List::MoreUtils qw( any uniq );
31 use Try::Tiny qw( catch try );
32
33 use base qw(Koha::Object);
34
35 =head1 NAME
36
37 Koha::Patron::Modification - Class represents a request to modify or create a patron
38
39 =head2 Class Methods
40
41 =cut
42
43 =head2 store
44
45 =cut
46
47 sub store {
48     my ($self) = @_;
49
50     if ( $self->verification_token ) {
51         if (Koha::Patron::Modifications->search(
52                 { verification_token => $self->verification_token }
53             )->count()
54             )
55         {
56             Koha::Exceptions::Patron::Modification::DuplicateVerificationToken->throw(
57                 "Duplicate verification token " . $self->verification_token );
58         }
59     }
60
61     if ( $self->extended_attributes ) {
62         try {
63             my $json_parser = JSON->new;
64             $json_parser->decode( $self->extended_attributes );
65         }
66         catch {
67             Koha::Exceptions::Patron::Modification::InvalidData->throw(
68                 'The passed extended_attributes is not valid JSON');
69         };
70     }
71
72     return $self->SUPER::store();
73 }
74
75 =head2 approve
76
77 $m->approve();
78
79 Commits the pending modifications to the borrower record and removes
80 them from the modifications table.
81
82 =cut
83
84 sub approve {
85     my ($self) = @_;
86
87     my $data = $self->unblessed();
88     my $extended_attributes;
89
90     delete $data->{timestamp};
91     delete $data->{verification_token};
92     delete $data->{extended_attributes};
93     my $changed_fields = $data->{changed_fields};
94     delete $data->{changed_fields};
95
96     my $patron = Koha::Patrons->find( $self->borrowernumber );
97     return unless $patron;
98
99     my @keep_keys = split /,/, $changed_fields;
100     my @all_keys = keys %$data;
101     foreach my $key ( @all_keys ) {
102         next if (any { $_ eq $key } @keep_keys);
103         delete $data->{$key};
104     }
105
106     $patron->set($data);
107
108     # Take care of extended attributes
109     if ( $self->extended_attributes ) {
110         $extended_attributes = try { from_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                     )->as_list;
131                 }
132                 foreach my $attr ( @{$extended_attributes} ) {
133                     $attr->{attribute} = exists $attr->{attribute} ? $attr->{attribute} : $attr->{value};
134                     Koha::Patron::Attribute->new(
135                         {   borrowernumber => $patron->borrowernumber,
136                             code           => $attr->{code},
137                             attribute      => $attr->{attribute},
138                         }
139                     )->store
140                         if $attr->{attribute} # there's a value
141                            or
142                           (    defined $attr->{attribute} # there's a value that is 0, and not
143                             && $attr->{attribute} ne ""   # the empty string which means delete
144                             && $attr->{attribute} == 0
145                           );
146                 }
147             }
148             catch {
149                 if ( $_->isa('DBIx::Class::Exception') ) {
150                     Koha::Exceptions::Patron::Modification->throw( $_->{msg} );
151                 }
152                 else {
153                     Koha::Exceptions::Patron::Modification->throw($_);
154                 }
155             };
156         }
157     );
158
159     return $self->delete();
160 }
161
162 =head3 type
163
164 =cut
165
166 sub _type {
167     return 'BorrowerModification';
168 }
169
170 =head1 AUTHOR
171
172 Kyle M Hall <kyle@bywatersolutions.com>
173
174 =cut
175
176 1;