|
Line 0
Link Here
|
|
|
1 |
package Koha::Account::Link; |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Carp qw( croak ); |
| 21 |
use Koha::Exceptions::Account; |
| 22 |
|
| 23 |
use base qw(Koha::Object); |
| 24 |
|
| 25 |
=head1 NAME |
| 26 |
|
| 27 |
Koha::Account::Link - Koha Account Link Object class |
| 28 |
|
| 29 |
=head1 API |
| 30 |
|
| 31 |
=head2 Class methods |
| 32 |
|
| 33 |
=head3 store |
| 34 |
|
| 35 |
Validates the link before storing |
| 36 |
|
| 37 |
=cut |
| 38 |
|
| 39 |
sub store { |
| 40 |
my ($self) = @_; |
| 41 |
|
| 42 |
my $link_type = $self->link_type; |
| 43 |
my $linked_id = $self->linked_id; |
| 44 |
|
| 45 |
# Check for valid link type first |
| 46 |
my $validators = { |
| 47 |
'hold' => sub { Koha::Holds->find( $_[0] ) }, |
| 48 |
'old_hold' => sub { Koha::Old::Holds->find( $_[0] ) }, |
| 49 |
'checkout' => sub { Koha::Checkouts->find( $_[0] ) }, |
| 50 |
'old_checkout' => sub { Koha::Old::Checkouts->find( $_[0] ) }, |
| 51 |
'article_request' => sub { Koha::ArticleRequests->find( $_[0] ) }, |
| 52 |
}; |
| 53 |
|
| 54 |
unless ( exists $validators->{$link_type} ) { |
| 55 |
Koha::Exceptions::Account::InvalidLinkType->throw("Unknown link_type: $link_type"); |
| 56 |
} |
| 57 |
|
| 58 |
# Validate the linked entity exists |
| 59 |
unless ( $self->_validate_link_integrity() ) { |
| 60 |
Koha::Exceptions::Account::InvalidLinkTarget->throw( |
| 61 |
"Invalid link target: " . $link_type . " with ID " . $linked_id . " does not exist" ); |
| 62 |
} |
| 63 |
|
| 64 |
return $self->SUPER::store(); |
| 65 |
} |
| 66 |
|
| 67 |
=head3 linked_object |
| 68 |
|
| 69 |
Returns the linked object based on link_type and linked_id |
| 70 |
|
| 71 |
=cut |
| 72 |
|
| 73 |
sub linked_object { |
| 74 |
my ($self) = @_; |
| 75 |
|
| 76 |
return $self->_result->linked_object; |
| 77 |
} |
| 78 |
|
| 79 |
=head3 linked_koha_object |
| 80 |
|
| 81 |
Returns the Koha::Object representation of the linked object |
| 82 |
|
| 83 |
=cut |
| 84 |
|
| 85 |
sub linked_koha_object { |
| 86 |
my ($self) = @_; |
| 87 |
|
| 88 |
my $class_map = { |
| 89 |
'hold' => 'Koha::Hold', |
| 90 |
'old_hold' => 'Koha::Old::Hold', |
| 91 |
'checkout' => 'Koha::Checkout', |
| 92 |
'old_checkout' => 'Koha::Old::Checkout', |
| 93 |
'article_request' => 'Koha::ArticleRequest', |
| 94 |
}; |
| 95 |
|
| 96 |
my $class = $class_map->{ $self->link_type }; |
| 97 |
return unless $class; |
| 98 |
|
| 99 |
# Load the class if needed |
| 100 |
eval "require $class"; |
| 101 |
if ($@) { |
| 102 |
Koha::Exceptions::Account::InvalidLinkType->throw( |
| 103 |
"Could not load class $class for link_type '" . $self->link_type . "': $@" ); |
| 104 |
} |
| 105 |
|
| 106 |
return $class->_new_from_dbic( $self->linked_object ); |
| 107 |
} |
| 108 |
|
| 109 |
=head3 _validate_link_integrity |
| 110 |
|
| 111 |
Internal method to validate that the linked entity exists |
| 112 |
|
| 113 |
=cut |
| 114 |
|
| 115 |
sub _validate_link_integrity { |
| 116 |
my ($self) = @_; |
| 117 |
|
| 118 |
my $link_type = $self->link_type; |
| 119 |
my $linked_id = $self->linked_id; |
| 120 |
|
| 121 |
# Define valid link types and their validation methods |
| 122 |
my $validators = { |
| 123 |
'hold' => sub { Koha::Holds->find( $_[0] ) }, |
| 124 |
'old_hold' => sub { Koha::Old::Holds->find( $_[0] ) }, |
| 125 |
'checkout' => sub { Koha::Checkouts->find( $_[0] ) }, |
| 126 |
'old_checkout' => sub { Koha::Old::Checkouts->find( $_[0] ) }, |
| 127 |
'article_request' => sub { Koha::ArticleRequests->find( $_[0] ) }, |
| 128 |
}; |
| 129 |
|
| 130 |
my $validator = $validators->{$link_type}; |
| 131 |
return 0 unless $validator; # Invalid link type |
| 132 |
|
| 133 |
return $validator->($linked_id) ? 1 : 0; |
| 134 |
} |
| 135 |
|
| 136 |
=head2 Class Methods |
| 137 |
|
| 138 |
=head3 _type |
| 139 |
|
| 140 |
=cut |
| 141 |
|
| 142 |
sub _type { |
| 143 |
return 'AccountlineLink'; |
| 144 |
} |
| 145 |
|
| 146 |
=head1 AUTHOR |
| 147 |
|
| 148 |
Koha Development Team <http://koha-community.org/> |
| 149 |
|
| 150 |
=head1 COPYRIGHT |
| 151 |
|
| 152 |
Copyright 2025 Koha Development Team |
| 153 |
|
| 154 |
=head1 LICENSE |
| 155 |
|
| 156 |
This file is part of Koha. |
| 157 |
|
| 158 |
Koha is free software; you can redistribute it and/or modify it |
| 159 |
under the terms of the GNU General Public License as published by |
| 160 |
the Free Software Foundation; either version 3 of the License, or |
| 161 |
(at your option) any later version. |
| 162 |
|
| 163 |
Koha is distributed in the hope that it will be useful, but |
| 164 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 165 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 166 |
GNU General Public License for more details. |
| 167 |
|
| 168 |
You should have received a copy of the GNU General Public License |
| 169 |
along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 170 |
|
| 171 |
=cut |
| 172 |
|
| 173 |
1; |