View | Details | Raw Unified | Return to bug 14539
Collapse All | Expand All

(-)a/Koha/Borrowers.pm (-1 / +80 lines)
Lines 18-23 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
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
=cut
76
77
sub castToBorrower {
78
    my ($input) = @_;
79
80
    unless ($input) {
81
        Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> No parameter given!");
82
    }
83
    if (blessed($input) && $input->isa('Koha::Borrower')) {
84
        return $input;
85
    }
86
    if (blessed($input) && $input->isa('Koha::Schema::Result::Borrower')) {
87
        return Koha::Borrower->_new_from_dbic($input);
88
    }
89
90
    my ($borrowernumber, $cardnumber, $userid, $borrower);
91
    #Extract unique keys and try to get the borrower from them.
92
    if (ref($input) eq 'HASH') {
93
        $borrowernumber = $input->{borrowernumber};
94
        $cardnumber = $input->{cardnumber};
95
        $userid = $input->{userid};
96
    }
97
    elsif (not(ref($input))) { #We have a scalar
98
        $borrowernumber = $input;
99
        $cardnumber = $input;
100
        $userid = $input;
101
    }
102
    if ($borrowernumber || $cardnumber || $userid) {
103
        $borrower = Koha::Borrowers->search({'-or' => [{borrowernumber => $borrowernumber},
104
                                                     {cardnumber => $cardnumber},
105
                                                     {userid => $userid},
106
                                                    ]
107
                                        })->next();
108
        unless ($borrower) {
109
            Koha::Exception::UnknownObject->throw(error => "Koha::Borrower::castToBorrower->new():> Cannot find an existing Borrower from borrowernumber|cardnumber|userid '$borrowernumber|$cardnumber|$userid'.");
110
        }
111
    }
112
113
#    ##If we have a borrower now, overwrite it with given hash keys
114
#    #if not, try to make one if we have a hash as input.
115
#    if (ref($input) eq 'HASH') {
116
#        $borrower = Koha::Borrower->new() unless $borrower;
117
#        my $columns = $borrower->_columns();
118
#        my $setHash = {};
119
#        foreach my $columnName (@$columns) {
120
#            $setHash->{$columnName} = $input->{$columnName};
121
#        }
122
#        Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> Given HASH contains no Borrower column parameters, cannot build a Borrower out of them.")
123
#                        unless scalar($setHash);
124
#        $borrower->set($setHash);
125
#    }
126
#
127
#    unless ($borrower) {
128
#        Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> Cannot find an existing Borrower from borrowernumber|cardnumber|userid '$borrowernumber|$cardnumber|$userid'.");
129
#    }
130
}
131
52
=head1 AUTHOR
132
=head1 AUTHOR
53
133
54
Kyle M Hall <kyle@bywatersolutions.com>
134
Kyle M Hall <kyle@bywatersolutions.com>
55
- 

Return to bug 14539