Bug 23151: Tweak to use the new database structure
[koha-equinox.git] / Koha / Patron / Modifications.pm
1 package Koha::Patron::Modifications;
2
3 # Copyright 2012 ByWater Solutions
4 # This file is part of Koha.
5 #
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.
10 #
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.
15 #
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>.
18
19 =head1 NAME
20
21 Koha::Patron::Modifications
22
23 =cut
24
25 use Modern::Perl;
26
27 use C4::Context;
28
29 use Koha::Patron::Attribute;
30 use Koha::Patron::Modification;
31
32 use JSON;
33 use List::Util qw /any none/;
34
35 use base qw(Koha::Objects);
36
37 =head2 pending_count
38
39 $count = Koha::Patron::Modifications->pending_count();
40
41 Returns the number of pending modifications for existing patrons.
42
43 =cut
44
45 sub pending_count {
46     my ( $self, $branchcode ) = @_;
47
48     my $dbh   = C4::Context->dbh;
49     my $query = "
50         SELECT COUNT(*) AS count
51         FROM borrower_modifications, borrowers
52         WHERE borrower_modifications.borrowernumber > 0
53         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
54     ";
55
56     my $userenv = C4::Context->userenv;
57     my @branchcodes;
58     if ( $userenv and $userenv->{number} ) {
59         my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
60         if ($branchcode) {
61             return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
62             @branchcodes = ( $branchcode );
63         }
64         else {
65             @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
66         }
67     }
68     my @sql_params;
69     if ( @branchcodes ) {
70         $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
71         push( @sql_params, @branchcodes );
72     }
73
74     my ( $count ) = $dbh->selectrow_array( $query, undef, @sql_params );
75     return $count;
76 }
77
78 =head2 pending
79
80 $arrayref = Koha::Patron::Modifications->pending();
81
82 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
83
84 =cut
85
86 sub pending {
87     my ( $self, $branchcode ) = @_;
88
89     my $dbh   = C4::Context->dbh;
90     my $query = "
91         SELECT borrower_modifications.*
92         FROM borrower_modifications, borrowers
93         WHERE borrower_modifications.borrowernumber > 0
94         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
95     ";
96
97     my $userenv = C4::Context->userenv;
98     my @branchcodes;
99     if ( $userenv ) {
100         my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
101         if ($branchcode) {
102             return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
103             @branchcodes = ( $branchcode );
104         }
105         else {
106             @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
107         }
108     }
109     my @sql_params;
110     if ( @branchcodes ) {
111         $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
112         push( @sql_params, @branchcodes );
113     }
114     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
115     my $sth = $dbh->prepare($query);
116     $sth->execute(@sql_params);
117
118     my @m;
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') {
123                 delete $row->{$key};
124                 next;
125             }
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}
135                         }
136                         );
137                 }
138
139                 $row->{$key} = \@pending_attributes;
140             }
141             if (none { $_ eq $key } @changed_keys) {
142                 delete $row->{$key} unless defined $row->{$key};
143             }
144         }
145
146         push( @m, $row );
147     }
148
149     return \@m;
150 }
151
152 sub _type {
153     return 'BorrowerModification';
154 }
155
156 =head3 object_class
157
158 =cut
159
160 sub object_class {
161     return 'Koha::Patron::Modification';
162 }
163
164 1;