|
Lines 18-28
package Koha::Borrowers;
Link Here
|
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
|
|
21 |
use Scalar::Util qw(blessed); |
| 21 |
|
22 |
|
| 22 |
use Carp; |
23 |
use Carp; |
| 23 |
|
24 |
|
| 24 |
use Koha::Database; |
25 |
use Koha::Database; |
| 25 |
|
26 |
use Koha::AuthUtils; |
| 26 |
use Koha::Borrower; |
27 |
use Koha::Borrower; |
| 27 |
|
28 |
|
| 28 |
use base qw(Koha::Objects); |
29 |
use base qw(Koha::Objects); |
|
Lines 49-54
sub object_class {
Link Here
|
| 49 |
return 'Koha::Borrower'; |
50 |
return 'Koha::Borrower'; |
| 50 |
} |
51 |
} |
| 51 |
|
52 |
|
|
|
53 |
=head castToBorrower |
| 54 |
|
| 55 |
my $borrower = Koha::Borrowers::castToBorrower('cardnumber'); |
| 56 |
my $borrower = Koha::Borrowers::castToBorrower($Koha::Borrower); |
| 57 |
my $borrower = Koha::Borrowers::castToBorrower('userid'); |
| 58 |
my $borrower = Koha::Borrowers::castToBorrower('borrowernumber'); |
| 59 |
my $borrower = Koha::Borrowers::castToBorrower({borrowernumber => 123, |
| 60 |
}); |
| 61 |
my $borrower = Koha::Borrowers::castToBorrower({firstname => 'Olli-Antti', |
| 62 |
surname => 'Kivi', |
| 63 |
address => 'Koskikatu 25', |
| 64 |
}); |
| 65 |
|
| 66 |
Because there are gazillion million ways in Koha to invoke a Borrower, this is a |
| 67 |
omnibus for easily creating a Borrower-object from all the arcane invocations present |
| 68 |
in many parts of Koha. |
| 69 |
Just throw the crazy and unpredictable return values from myriad subroutines returning |
| 70 |
some kind of an borrowerish value to this casting function to get a brand new Koha::Borrower. |
| 71 |
@PARAM1 Scalar, or HASHRef. |
| 72 |
@RETURNS Koha::Borrower, possibly already in DB or a completely new one if nothing was |
| 73 |
inferred from the DB. |
| 74 |
@THROWS Koha::Exception::BadParameter, if no idea what to do with the input. |
| 75 |
@THROWS Koha::Exception::UnknownObject, if we cannot find a Borrower with the given input. |
| 76 |
=cut |
| 77 |
|
| 78 |
sub castToBorrower { |
| 79 |
my ($input) = @_; |
| 80 |
|
| 81 |
unless ($input) { |
| 82 |
Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> No parameter given!"); |
| 83 |
} |
| 84 |
if (blessed($input) && $input->isa('Koha::Borrower')) { |
| 85 |
return $input; |
| 86 |
} |
| 87 |
if (blessed($input) && $input->isa('Koha::Schema::Result::Borrower')) { |
| 88 |
return Koha::Borrower->_new_from_dbic($input); |
| 89 |
} |
| 90 |
|
| 91 |
my ($borrowernumber, $cardnumber, $userid, $borrower); |
| 92 |
#Extract unique keys and try to get the borrower from them. |
| 93 |
if (ref($input) eq 'HASH') { |
| 94 |
$borrowernumber = $input->{borrowernumber}; |
| 95 |
$cardnumber = $input->{cardnumber}; |
| 96 |
$userid = $input->{userid}; |
| 97 |
} |
| 98 |
elsif (not(ref($input))) { #We have a scalar |
| 99 |
$borrowernumber = $input; |
| 100 |
$cardnumber = $input; |
| 101 |
$userid = $input; |
| 102 |
} |
| 103 |
if ($borrowernumber || $cardnumber || $userid) { |
| 104 |
$borrower = Koha::Borrowers->search({'-or' => [{borrowernumber => $borrowernumber}, |
| 105 |
{cardnumber => $cardnumber}, |
| 106 |
{userid => $userid}, |
| 107 |
] |
| 108 |
})->next(); |
| 109 |
unless ($borrower) { |
| 110 |
$borrower = Koha::AuthUtils::checkKohaSuperuserFromUserid($userid); |
| 111 |
unless ($borrower) { |
| 112 |
Koha::Exception::UnknownObject->throw(error => "Koha::Borrower::castToBorrower->new():> Cannot find an existing Borrower from borrowernumber|cardnumber|userid '$borrowernumber|$cardnumber|$userid'."); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $borrower; |
| 118 |
} |
| 119 |
|
| 52 |
=head1 AUTHOR |
120 |
=head1 AUTHOR |
| 53 |
|
121 |
|
| 54 |
Kyle M Hall <kyle@bywatersolutions.com> |
122 |
Kyle M Hall <kyle@bywatersolutions.com> |
| 55 |
- |
|
|