646d112f4c8b8948dc48fea9c7936c266fab5ed8
[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
34 use base qw(Koha::Objects);
35
36 =head2 pending_count
37
38 $count = Koha::Patron::Modifications->pending_count();
39
40 Returns the number of pending modifications for existing patrons.
41
42 =cut
43
44 sub pending_count {
45     my ( $self, $branchcode ) = @_;
46
47     my $dbh   = C4::Context->dbh;
48     my $query = "
49         SELECT COUNT(*) AS count
50         FROM borrower_modifications, borrowers
51         WHERE borrower_modifications.borrowernumber > 0
52         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
53     ";
54
55     my $userenv = C4::Context->userenv;
56     my @branchcodes;
57     if ( $userenv and $userenv->{number} ) {
58         my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
59         if ($branchcode) {
60             return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
61             @branchcodes = ( $branchcode );
62         }
63         else {
64             @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
65         }
66     }
67     my @sql_params;
68     if ( @branchcodes ) {
69         $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
70         push( @sql_params, @branchcodes );
71     }
72
73     my ( $count ) = $dbh->selectrow_array( $query, undef, @sql_params );
74     return $count;
75 }
76
77 =head2 pending
78
79 $arrayref = Koha::Patron::Modifications->pending();
80
81 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
82
83 =cut
84
85 sub pending {
86     my ( $self, $branchcode ) = @_;
87
88     my $dbh   = C4::Context->dbh;
89     my $query = "
90         SELECT borrower_modifications.*
91         FROM borrower_modifications, borrowers
92         WHERE borrower_modifications.borrowernumber > 0
93         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
94     ";
95
96     my $userenv = C4::Context->userenv;
97     my @branchcodes;
98     if ( $userenv ) {
99         my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
100         if ($branchcode) {
101             return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
102             @branchcodes = ( $branchcode );
103         }
104         else {
105             @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
106         }
107     }
108     my @sql_params;
109     if ( @branchcodes ) {
110         $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
111         push( @sql_params, @branchcodes );
112     }
113     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
114     my $sth = $dbh->prepare($query);
115     $sth->execute(@sql_params);
116
117     my @m;
118     while ( my $row = $sth->fetchrow_hashref() ) {
119         foreach my $key ( keys %$row ) {
120             if ( defined $row->{$key} && $key eq 'extended_attributes' ) {
121                 my $attributes = from_json( $row->{$key} );
122                 my @pending_attributes;
123                 foreach my $attr ( @{$attributes} ) {
124                     push @pending_attributes,
125                         Koha::Patron::Attribute->new(
126                         {   borrowernumber => $row->{borrowernumber},
127                             code           => $attr->{code},
128                             attribute      => $attr->{value}
129                         }
130                         );
131                 }
132
133                 $row->{$key} = \@pending_attributes;
134             }
135             if ( $key eq 'dateofbirth' and not defined $row->{$key}) {
136                 $row->{$key} = '';
137             } else {
138                 delete $row->{$key} unless defined $row->{$key};
139             }
140         }
141
142         push( @m, $row );
143     }
144
145     return \@m;
146 }
147
148 sub _type {
149     return 'BorrowerModification';
150 }
151
152 =head3 object_class
153
154 =cut
155
156 sub object_class {
157     return 'Koha::Patron::Modification';
158 }
159
160 1;