1 package Koha::Patron::Modifications;
3 # Copyright 2012 ByWater Solutions
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 Koha::Patron::Modifications
29 use Koha::Patron::Attribute;
30 use Koha::Patron::Modification;
33 use List::Util qw /any none/;
35 use base qw(Koha::Objects);
39 $count = Koha::Patron::Modifications->pending_count();
41 Returns the number of pending modifications for existing patrons.
46 my ( $self, $branchcode ) = @_;
48 my $dbh = C4::Context->dbh;
50 SELECT COUNT(*) AS count
51 FROM borrower_modifications, borrowers
52 WHERE borrower_modifications.borrowernumber > 0
53 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
56 my $userenv = C4::Context->userenv;
58 if ( $userenv and $userenv->{number} ) {
59 my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
61 return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
62 @branchcodes = ( $branchcode );
65 @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
70 $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
71 push( @sql_params, @branchcodes );
74 my ( $count ) = $dbh->selectrow_array( $query, undef, @sql_params );
80 $arrayref = Koha::Patron::Modifications->pending();
82 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
87 my ( $self, $branchcode ) = @_;
89 my $dbh = C4::Context->dbh;
91 SELECT borrower_modifications.*
92 FROM borrower_modifications, borrowers
93 WHERE borrower_modifications.borrowernumber > 0
94 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
97 my $userenv = C4::Context->userenv;
100 my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
102 return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
103 @branchcodes = ( $branchcode );
106 @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
110 if ( @branchcodes ) {
111 $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
112 push( @sql_params, @branchcodes );
114 $query .= " ORDER BY borrowers.surname, borrowers.firstname";
115 my $sth = $dbh->prepare($query);
116 $sth->execute(@sql_params);
119 while ( my $row = $sth->fetchrow_hashref() ) {
120 my @changed_keys = split /,/, $row->{changed_fields};
121 foreach my $key ( keys %$row ) {
122 if ($key eq 'changed_fields') {
126 if ( defined $row->{$key} && $key eq 'extended_attributes' ) {
127 my $attributes = from_json( $row->{$key} );
128 my @pending_attributes;
129 foreach my $attr ( @{$attributes} ) {
130 push @pending_attributes,
131 Koha::Patron::Attribute->new(
132 { borrowernumber => $row->{borrowernumber},
133 code => $attr->{code},
134 attribute => $attr->{value}
139 $row->{$key} = \@pending_attributes;
141 if (none { $_ eq $key } @changed_keys) {
142 delete $row->{$key} unless defined $row->{$key};
153 return 'BorrowerModification';
161 return 'Koha::Patron::Modification';