| Filename | /home/vagrant/kohaclone/Koha/Borrower/Modifications.pm |
| Statements | Executed 16 statements in 1.80ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 38µs | 295µs | Koha::Borrower::Modifications::GetPendingModificationsCount |
| 1 | 1 | 1 | 32µs | 152µs | Koha::Borrower::Modifications::BEGIN@25 |
| 1 | 1 | 1 | 17µs | 21µs | Koha::Borrower::Modifications::BEGIN@27 |
| 1 | 1 | 1 | 14µs | 95µs | Koha::Borrower::Modifications::BEGIN@28 |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Modifications::AddModifications |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Modifications::ApproveModifications |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Modifications::DelModifications |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Modifications::DenyModifications |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Modifications::GetModifications |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Modifications::GetPendingModifications |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Modifications::Verify |
| 0 | 0 | 0 | 0s | 0s | Koha::Borrower::Modifications::new |
| 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 | 173µs | 2 | 271µs | # spent 152µs (32+120) within Koha::Borrower::Modifications::BEGIN@25 which was called:
# once (32µs+120µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@31 at line 25 # spent 152µs making 1 call to Koha::Borrower::Modifications::BEGIN@25
# spent 120µs making 1 call to Modern::Perl::import |
| 26 | |||||
| 27 | 2 | 50µs | 2 | 25µs | # spent 21µs (17+4) within Koha::Borrower::Modifications::BEGIN@27 which was called:
# once (17µs+4µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@31 at line 27 # spent 21µs making 1 call to Koha::Borrower::Modifications::BEGIN@27
# spent 4µs making 1 call to C4::Context::import |
| 28 | 2 | 1.47ms | 2 | 176µs | # spent 95µs (14+81) within Koha::Borrower::Modifications::BEGIN@28 which was called:
# once (14µs+81µs) by CGI::Compile::ROOT::home_vagrant_kohaclone_mainpage_2epl::BEGIN@31 at line 28 # spent 95µs making 1 call to Koha::Borrower::Modifications::BEGIN@28
# spent 81µ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 295µs (38+258) within Koha::Borrower::Modifications::GetPendingModificationsCount which was called:
# once (38µs+258µ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 | 141µs | my $dbh = C4::Context->dbh; # spent 141µs making 1 call to C4::Context::dbh |
| 109 | 1 | 600ns | 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 | 500ns | my @params; | ||
| 117 | 1 | 200ns | if ($branchcode) { | ||
| 118 | $query .= " AND borrowers.branchcode = ? "; | ||||
| 119 | push( @params, $branchcode ); | ||||
| 120 | } | ||||
| 121 | |||||
| 122 | 1 | 14µs | 2 | 67µs | my $sth = $dbh->prepare($query); # spent 40µs making 1 call to DBI::db::prepare
# spent 27µ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 | 66µs | my $result = $sth->fetchrow_hashref(); # spent 36µs making 1 call to DBI::st::fetchrow_hashref
# spent 30µs making 1 call to DBD::mysql::st::__ANON__[DBD/mysql.pm:799] |
| 125 | |||||
| 126 | 1 | 28µ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; |