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 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
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.
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.
25 use Koha::Exceptions::Patron::Modification;
26 use Koha::Patron::Attribute;
27 use Koha::Patron::Attributes;
28 use Koha::Patron::Modifications;
31 use List::MoreUtils qw( uniq );
34 use base qw(Koha::Object);
38 Koha::Patron::Modification - Class represents a request to modify or create a patron
51 if ( $self->verification_token ) {
52 if (Koha::Patron::Modifications->search(
53 { verification_token => $self->verification_token }
57 Koha::Exceptions::Patron::Modification::DuplicateVerificationToken->throw(
58 "Duplicate verification token " . $self->verification_token );
62 if ( $self->extended_attributes ) {
64 my $json_parser = JSON->new;
65 $json_parser->decode( $self->extended_attributes );
68 Koha::Exceptions::Patron::Modification::InvalidData->throw(
69 'The passed extended_attributes is not valid JSON');
73 return $self->SUPER::store();
80 Commits the pending modifications to the borrower record and removes
81 them from the modifications table.
88 my $data = $self->unblessed();
89 my $extended_attributes;
91 delete $data->{timestamp};
92 delete $data->{verification_token};
93 delete $data->{extended_attributes};
95 my $patron = Koha::Patrons->find( $self->borrowernumber );
96 return unless $patron;
98 foreach my $key ( keys %$data ) {
100 if $key eq 'dateofbirth'
101 && $patron->dateofbirth
102 && not defined $data->{$key};
104 delete $data->{$key} unless defined $data->{$key};
109 # Take care of extended attributes
110 if ( $self->extended_attributes ) {
111 $extended_attributes = try { from_json( $self->extended_attributes ) }
113 Koha::Exceptions::Patron::Modification::InvalidData->throw(
114 'The passed extended_attributes is not valid JSON');
118 $self->_result->result_source->schema->txn_do(
123 # Deal with attributes
125 = uniq( map { $_->{code} } @{$extended_attributes} );
126 foreach my $code (@codes) {
127 map { $_->delete } Koha::Patron::Attributes->search(
128 { borrowernumber => $patron->borrowernumber,
133 foreach my $attr ( @{$extended_attributes} ) {
134 Koha::Patron::Attribute->new(
135 { borrowernumber => $patron->borrowernumber,
136 code => $attr->{code},
137 attribute => $attr->{value}
140 if $attr->{value} # there's a value
142 ( defined $attr->{value} # there's a value that is 0, and not
143 && $attr->{value} ne "" # the empty string which means delete
144 && $attr->{value} == 0
149 if ( $_->isa('DBIx::Class::Exception') ) {
150 Koha::Exceptions::Patron::Modification->throw( $_->{msg} );
153 Koha::Exceptions::Patron::Modification->throw($_);
159 return $self->delete();
167 return 'BorrowerModification';
172 Kyle M Hall <kyle@bywatersolutions.com>