Bug 23895: Move installer file into the mandatory directory
[koha.git] / Koha / Patron / Modification.pm
blob51f182a80b9044bd05fb1a173ec4c7071ce3e74c
1 package Koha::Patron::Modification;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use Carp;
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;
30 use JSON;
31 use List::MoreUtils qw( uniq any );
32 use Try::Tiny;
34 use base qw(Koha::Object);
36 =head1 NAME
38 Koha::Patron::Modification - Class represents a request to modify or create a patron
40 =head2 Class Methods
42 =cut
44 =head2 store
46 =cut
48 sub store {
49 my ($self) = @_;
51 if ( $self->verification_token ) {
52 if (Koha::Patron::Modifications->search(
53 { verification_token => $self->verification_token }
54 )->count()
57 Koha::Exceptions::Patron::Modification::DuplicateVerificationToken->throw(
58 "Duplicate verification token " . $self->verification_token );
62 if ( $self->extended_attributes ) {
63 try {
64 my $json_parser = JSON->new;
65 $json_parser->decode( $self->extended_attributes );
67 catch {
68 Koha::Exceptions::Patron::Modification::InvalidData->throw(
69 'The passed extended_attributes is not valid JSON');
73 return $self->SUPER::store();
76 =head2 approve
78 $m->approve();
80 Commits the pending modifications to the borrower record and removes
81 them from the modifications table.
83 =cut
85 sub approve {
86 my ($self) = @_;
88 my $data = $self->unblessed();
89 my $extended_attributes;
91 delete $data->{timestamp};
92 delete $data->{verification_token};
93 delete $data->{extended_attributes};
94 my $changed_fields = $data->{changed_fields};
95 delete $data->{changed_fields};
97 my $patron = Koha::Patrons->find( $self->borrowernumber );
98 return unless $patron;
100 my @keep_keys = split /,/, $changed_fields;
101 my @all_keys = keys %$data;
102 foreach my $key ( @all_keys ) {
103 next if (any { $_ eq $key } @keep_keys);
104 delete $data->{$key};
107 $patron->set($data);
109 # Take care of extended attributes
110 if ( $self->extended_attributes ) {
111 $extended_attributes = try { from_json( $self->extended_attributes ) }
112 catch {
113 Koha::Exceptions::Patron::Modification::InvalidData->throw(
114 'The passed extended_attributes is not valid JSON');
118 $self->_result->result_source->schema->txn_do(
119 sub {
120 try {
121 $patron->store();
123 # Deal with attributes
124 my @codes
125 = uniq( map { $_->{code} } @{$extended_attributes} );
126 foreach my $code (@codes) {
127 map { $_->delete } Koha::Patron::Attributes->search(
128 { borrowernumber => $patron->borrowernumber,
129 code => $code
133 foreach my $attr ( @{$extended_attributes} ) {
134 $attr->{attribute} = exists $attr->{attribute} ? $attr->{attribute} : $attr->{value};
135 Koha::Patron::Attribute->new(
136 { borrowernumber => $patron->borrowernumber,
137 code => $attr->{code},
138 attribute => $attr->{attribute},
140 )->store
141 if $attr->{attribute} # there's a value
143 ( defined $attr->{attribute} # there's a value that is 0, and not
144 && $attr->{attribute} ne "" # the empty string which means delete
145 && $attr->{attribute} == 0
149 catch {
150 if ( $_->isa('DBIx::Class::Exception') ) {
151 Koha::Exceptions::Patron::Modification->throw( $_->{msg} );
153 else {
154 Koha::Exceptions::Patron::Modification->throw($_);
160 return $self->delete();
163 =head3 type
165 =cut
167 sub _type {
168 return 'BorrowerModification';
171 =head1 AUTHOR
173 Kyle M Hall <kyle@bywatersolutions.com>
175 =cut