Filename | /home/vagrant/kohaclone/Koha/Borrower/Modifications.pm |
Statements | Executed 16 statements in 1.43ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 37µs | 262µs | GetPendingModificationsCount | Koha::Borrower::Modifications::
1 | 1 | 1 | 34µs | 108µs | BEGIN@28 | Koha::Borrower::Modifications::
1 | 1 | 1 | 26µs | 111µs | BEGIN@25 | Koha::Borrower::Modifications::
1 | 1 | 1 | 12µs | 15µs | BEGIN@27 | Koha::Borrower::Modifications::
0 | 0 | 0 | 0s | 0s | AddModifications | Koha::Borrower::Modifications::
0 | 0 | 0 | 0s | 0s | ApproveModifications | Koha::Borrower::Modifications::
0 | 0 | 0 | 0s | 0s | DelModifications | Koha::Borrower::Modifications::
0 | 0 | 0 | 0s | 0s | DenyModifications | Koha::Borrower::Modifications::
0 | 0 | 0 | 0s | 0s | GetModifications | Koha::Borrower::Modifications::
0 | 0 | 0 | 0s | 0s | GetPendingModifications | Koha::Borrower::Modifications::
0 | 0 | 0 | 0s | 0s | Verify | Koha::Borrower::Modifications::
0 | 0 | 0 | 0s | 0s | new | Koha::Borrower::Modifications::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Koha::Borrower::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 | C4::Borrowers::Modifications | ||||
22 | |||||
23 | =cut | ||||
24 | |||||
25 | 2 | 136µs | 2 | 197µs | # spent 111µs (26+86) within Koha::Borrower::Modifications::BEGIN@25 which was called:
# once (26µs+86µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@31 at line 25 # spent 111µs making 1 call to Koha::Borrower::Modifications::BEGIN@25
# spent 86µs making 1 call to Modern::Perl::import |
26 | |||||
27 | 2 | 34µs | 2 | 18µs | # spent 15µs (12+3) within Koha::Borrower::Modifications::BEGIN@27 which was called:
# once (12µs+3µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@31 at line 27 # spent 15µs making 1 call to Koha::Borrower::Modifications::BEGIN@27
# spent 3µs making 1 call to C4::Context::import |
28 | 2 | 1.16ms | 2 | 182µs | # spent 108µs (34+74) within Koha::Borrower::Modifications::BEGIN@28 which was called:
# once (34µs+74µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@31 at line 28 # spent 108µs making 1 call to Koha::Borrower::Modifications::BEGIN@28
# spent 74µs making 1 call to Exporter::import |
29 | |||||
30 | sub new { | ||||
31 | my ( $class, %args ) = @_; | ||||
32 | |||||
33 | return bless( \%args, $class ); | ||||
34 | } | ||||
35 | |||||
36 | =head2 AddModifications | ||||
37 | |||||
38 | Koha::Borrower::Modifications->AddModifications( $data ); | ||||
39 | |||||
40 | Adds or updates modifications for a borrower. | ||||
41 | |||||
42 | Requires either the key borrowernumber, or verification_token | ||||
43 | to be part of the passed in hash. | ||||
44 | |||||
45 | =cut | ||||
46 | |||||
47 | sub AddModifications { | ||||
48 | my ( $self, $data ) = @_; | ||||
49 | |||||
50 | delete $data->{borrowernumber}; | ||||
51 | if( $self->{borrowernumber} ) { | ||||
52 | return if( not keys %$data ); | ||||
53 | $data->{borrowernumber} = $self->{borrowernumber}; | ||||
54 | $data->{verification_token} = ''; | ||||
55 | } | ||||
56 | elsif( $self->{verification_token} ) { | ||||
57 | $data->{verification_token} = $self->{verification_token}; | ||||
58 | $data->{borrowernumber} = 0; | ||||
59 | } | ||||
60 | else { | ||||
61 | return; | ||||
62 | } | ||||
63 | |||||
64 | my $rs = Koha::Database->new()->schema->resultset('BorrowerModification'); | ||||
65 | return $rs->update_or_create($data, { key => 'primary' } ); | ||||
66 | } | ||||
67 | |||||
68 | =head2 Verify | ||||
69 | |||||
70 | $verified = Koha::Borrower::Modifications->Verify( $verification_token ); | ||||
71 | |||||
72 | Returns true if the passed in token is valid. | ||||
73 | |||||
74 | =cut | ||||
75 | |||||
76 | sub Verify { | ||||
77 | my ( $self, $verification_token ) = @_; | ||||
78 | |||||
79 | $verification_token = | ||||
80 | ($verification_token) | ||||
81 | ? $verification_token | ||||
82 | : $self->{'verification_token'}; | ||||
83 | |||||
84 | my $dbh = C4::Context->dbh; | ||||
85 | my $query = " | ||||
86 | SELECT COUNT(*) AS count | ||||
87 | FROM borrower_modifications | ||||
88 | WHERE verification_token = ? | ||||
89 | "; | ||||
90 | my $sth = $dbh->prepare($query); | ||||
91 | $sth->execute($verification_token); | ||||
92 | my $result = $sth->fetchrow_hashref(); | ||||
93 | |||||
94 | return $result->{'count'}; | ||||
95 | } | ||||
96 | |||||
97 | =head2 GetPendingModificationsCount | ||||
98 | |||||
99 | $count = Koha::Borrower::Modifications->GetPendingModificationsCount(); | ||||
100 | |||||
101 | Returns the number of pending modifications for existing borrowers. | ||||
102 | |||||
103 | =cut | ||||
104 | |||||
105 | # spent 262µs (37+225) within Koha::Borrower::Modifications::GetPendingModificationsCount which was called:
# once (37µs+225µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::__ANON__[/home/vagrant/kohaclone/mainpage.pl:107] at line 68 of mainpage.pl | ||||
106 | 1 | 600ns | my ( $self, $branchcode ) = @_; | ||
107 | |||||
108 | 1 | 10µs | 1 | 119µs | my $dbh = C4::Context->dbh; # spent 119µs making 1 call to C4::Context::dbh |
109 | 1 | 400ns | my $query = " | ||
110 | SELECT COUNT(*) AS count | ||||
111 | FROM borrower_modifications, borrowers | ||||
112 | WHERE borrower_modifications.borrowernumber > 0 | ||||
113 | AND borrower_modifications.borrowernumber = borrowers.borrowernumber | ||||
114 | "; | ||||
115 | |||||
116 | 1 | 300ns | my @params; | ||
117 | 1 | 300ns | if ($branchcode) { | ||
118 | $query .= " AND borrowers.branchcode = ? "; | ||||
119 | push( @params, $branchcode ); | ||||
120 | } | ||||
121 | |||||
122 | 1 | 6µs | 2 | 57µs | my $sth = $dbh->prepare($query); # spent 31µs making 1 call to DBI::db::prepare
# spent 26µs making 1 call to DBD::mysql::db::prepare |
123 | 1 | 42µs | 1 | 36µs | $sth->execute(@params); # spent 36µs making 1 call to DBI::st::execute |
124 | 1 | 9µs | 2 | 65µs | my $result = $sth->fetchrow_hashref(); # spent 35µs making 1 call to DBI::st::fetchrow_hashref
# spent 29µs making 1 call to DBD::mysql::st::__ANON__[DBD/mysql.pm:799] |
125 | |||||
126 | 1 | 22µs | return $result->{'count'}; | ||
127 | } | ||||
128 | |||||
129 | =head2 GetPendingModifications | ||||
130 | |||||
131 | $arrayref = Koha::Borrower::Modifications->GetPendingModifications(); | ||||
132 | |||||
133 | Returns an arrayref of hashrefs for all pending modifications for existing borrowers. | ||||
134 | |||||
135 | =cut | ||||
136 | |||||
137 | sub GetPendingModifications { | ||||
138 | my ( $self, $branchcode ) = @_; | ||||
139 | |||||
140 | my $dbh = C4::Context->dbh; | ||||
141 | my $query = " | ||||
142 | SELECT borrower_modifications.* | ||||
143 | FROM borrower_modifications, borrowers | ||||
144 | WHERE borrower_modifications.borrowernumber > 0 | ||||
145 | AND borrower_modifications.borrowernumber = borrowers.borrowernumber | ||||
146 | "; | ||||
147 | |||||
148 | my @params; | ||||
149 | if ($branchcode) { | ||||
150 | $query .= " AND borrowers.branchcode = ? "; | ||||
151 | push( @params, $branchcode ); | ||||
152 | } | ||||
153 | $query .= " ORDER BY borrowers.surname, borrowers.firstname"; | ||||
154 | my $sth = $dbh->prepare($query); | ||||
155 | $sth->execute(@params); | ||||
156 | |||||
157 | my @m; | ||||
158 | while ( my $row = $sth->fetchrow_hashref() ) { | ||||
159 | foreach my $key ( keys %$row ) { | ||||
160 | delete $row->{$key} unless defined $row->{$key}; | ||||
161 | } | ||||
162 | |||||
163 | push( @m, $row ); | ||||
164 | } | ||||
165 | |||||
166 | return \@m; | ||||
167 | } | ||||
168 | |||||
169 | =head2 ApproveModifications | ||||
170 | |||||
171 | Koha::Borrower::Modifications->ApproveModifications( $borrowernumber ); | ||||
172 | |||||
173 | Commits the pending modifications to the borrower record and removes | ||||
174 | them from the modifications table. | ||||
175 | |||||
176 | =cut | ||||
177 | |||||
178 | sub ApproveModifications { | ||||
179 | my ( $self, $borrowernumber ) = @_; | ||||
180 | |||||
181 | $borrowernumber = | ||||
182 | ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'}; | ||||
183 | |||||
184 | return unless $borrowernumber; | ||||
185 | |||||
186 | my $data = $self->GetModifications( { borrowernumber => $borrowernumber } ); | ||||
187 | delete $data->{timestamp}; | ||||
188 | delete $data->{verification_token}; | ||||
189 | |||||
190 | my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({ | ||||
191 | borrowernumber => $data->{borrowernumber}, | ||||
192 | }); | ||||
193 | if( $rs->update($data) ) { | ||||
194 | $self->DelModifications( { borrowernumber => $borrowernumber } ); | ||||
195 | } | ||||
196 | } | ||||
197 | |||||
198 | =head2 DenyModifications | ||||
199 | |||||
200 | Koha::Borrower::Modifications->DenyModifications( $borrowernumber ); | ||||
201 | |||||
202 | Removes the modifications from the table for the given borrower, | ||||
203 | without commiting the changes to the borrower record. | ||||
204 | |||||
205 | =cut | ||||
206 | |||||
207 | sub DenyModifications { | ||||
208 | my ( $self, $borrowernumber ) = @_; | ||||
209 | |||||
210 | $borrowernumber = | ||||
211 | ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'}; | ||||
212 | |||||
213 | return unless $borrowernumber; | ||||
214 | |||||
215 | return $self->DelModifications( { borrowernumber => $borrowernumber } ); | ||||
216 | } | ||||
217 | |||||
218 | =head2 DelModifications | ||||
219 | |||||
220 | Koha::Borrower::Modifications->DelModifications({ | ||||
221 | [ borrowernumber => $borrowernumber ], | ||||
222 | [ verification_token => $verification_token ] | ||||
223 | }); | ||||
224 | |||||
225 | Deletes the modifications for the given borrowernumber or verification token. | ||||
226 | |||||
227 | =cut | ||||
228 | |||||
229 | sub DelModifications { | ||||
230 | my ( $self, $params ) = @_; | ||||
231 | |||||
232 | my ( $field, $value ); | ||||
233 | |||||
234 | if ( $params->{'borrowernumber'} ) { | ||||
235 | $field = 'borrowernumber'; | ||||
236 | $value = $params->{'borrowernumber'}; | ||||
237 | } | ||||
238 | elsif ( $params->{'verification_token'} ) { | ||||
239 | $field = 'verification_token'; | ||||
240 | $value = $params->{'verification_token'}; | ||||
241 | } | ||||
242 | |||||
243 | return unless $value; | ||||
244 | |||||
245 | my $dbh = C4::Context->dbh; | ||||
246 | |||||
247 | $field = $dbh->quote_identifier($field); | ||||
248 | |||||
249 | my $query = " | ||||
250 | DELETE | ||||
251 | FROM borrower_modifications | ||||
252 | WHERE $field = ? | ||||
253 | "; | ||||
254 | |||||
255 | my $sth = $dbh->prepare($query); | ||||
256 | return $sth->execute($value); | ||||
257 | } | ||||
258 | |||||
259 | =head2 GetModifications | ||||
260 | |||||
261 | $hashref = Koha::Borrower::Modifications->GetModifications({ | ||||
262 | [ borrowernumber => $borrowernumber ], | ||||
263 | [ verification_token => $verification_token ] | ||||
264 | }); | ||||
265 | |||||
266 | Gets the modifications for the given borrowernumber or verification token. | ||||
267 | |||||
268 | =cut | ||||
269 | |||||
270 | sub GetModifications { | ||||
271 | my ( $self, $params ) = @_; | ||||
272 | |||||
273 | my ( $field, $value ); | ||||
274 | |||||
275 | if ( defined( $params->{'borrowernumber'} ) ) { | ||||
276 | $field = 'borrowernumber'; | ||||
277 | $value = $params->{'borrowernumber'}; | ||||
278 | } | ||||
279 | elsif ( defined( $params->{'verification_token'} ) ) { | ||||
280 | $field = 'verification_token'; | ||||
281 | $value = $params->{'verification_token'}; | ||||
282 | } | ||||
283 | |||||
284 | return unless $value; | ||||
285 | |||||
286 | my $dbh = C4::Context->dbh; | ||||
287 | |||||
288 | $field = $dbh->quote_identifier($field); | ||||
289 | |||||
290 | my $query = " | ||||
291 | SELECT * | ||||
292 | FROM borrower_modifications | ||||
293 | WHERE $field = ? | ||||
294 | "; | ||||
295 | |||||
296 | my $sth = $dbh->prepare($query); | ||||
297 | $sth->execute($value); | ||||
298 | my $data = $sth->fetchrow_hashref(); | ||||
299 | |||||
300 | foreach my $key ( keys %$data ) { | ||||
301 | delete $data->{$key} unless ( defined( $data->{$key} ) ); | ||||
302 | } | ||||
303 | |||||
304 | return $data; | ||||
305 | } | ||||
306 | |||||
307 | 1 | 3µs | 1; |