|
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] ) || Koha::Old::Holds->find( $_[0] ) }, |
| 48 |
}; |
| 49 |
|
| 50 |
unless ( exists $validators->{$link_type} ) { |
| 51 |
Koha::Exceptions::Account::InvalidLinkType->throw("Unknown link_type: $link_type"); |
| 52 |
} |
| 53 |
|
| 54 |
# Validate the linked entity exists |
| 55 |
unless ( $self->_validate_link_integrity() ) { |
| 56 |
Koha::Exceptions::Account::InvalidLinkTarget->throw( |
| 57 |
"Invalid link target: " . $link_type . " with ID " . $linked_id . " does not exist" ); |
| 58 |
} |
| 59 |
|
| 60 |
return $self->SUPER::store(); |
| 61 |
} |
| 62 |
|
| 63 |
=head3 linked_object |
| 64 |
|
| 65 |
Returns the linked object based on link_type and linked_id |
| 66 |
|
| 67 |
=cut |
| 68 |
|
| 69 |
sub linked_object { |
| 70 |
my ($self) = @_; |
| 71 |
|
| 72 |
return $self->_result->linked_object; |
| 73 |
} |
| 74 |
|
| 75 |
=head3 linked_koha_object |
| 76 |
|
| 77 |
Returns the Koha::Object representation of the linked object |
| 78 |
|
| 79 |
=cut |
| 80 |
|
| 81 |
sub linked_koha_object { |
| 82 |
my ($self) = @_; |
| 83 |
|
| 84 |
# Special handling for holds - they can return either Koha::Hold or Koha::Old::Hold |
| 85 |
if ( $self->link_type eq 'hold' ) { |
| 86 |
my $dbic_object = $self->linked_object; |
| 87 |
return unless $dbic_object; |
| 88 |
|
| 89 |
# Check which table the result came from by examining the result source |
| 90 |
my $source_name = $dbic_object->result_source->source_name; |
| 91 |
|
| 92 |
if ( $source_name eq 'Reserve' ) { |
| 93 |
require Koha::Hold; |
| 94 |
return Koha::Hold->_new_from_dbic($dbic_object); |
| 95 |
} elsif ( $source_name eq 'OldReserve' ) { |
| 96 |
require Koha::Old::Hold; |
| 97 |
return Koha::Old::Hold->_new_from_dbic($dbic_object); |
| 98 |
} |
| 99 |
|
| 100 |
return; # Unknown source |
| 101 |
} |
| 102 |
|
| 103 |
# Handle other link types normally |
| 104 |
my $class_map = { |
| 105 |
'checkout' => 'Koha::Checkout', |
| 106 |
'old_checkout' => 'Koha::Old::Checkout', |
| 107 |
'article_request' => 'Koha::ArticleRequest', |
| 108 |
}; |
| 109 |
|
| 110 |
my $class = $class_map->{ $self->link_type }; |
| 111 |
return unless $class; |
| 112 |
|
| 113 |
# Load the class if needed |
| 114 |
eval "require $class"; |
| 115 |
if ($@) { |
| 116 |
Koha::Exceptions::Account::InvalidLinkType->throw( |
| 117 |
"Could not load class $class for link_type '" . $self->link_type . "': $@" ); |
| 118 |
} |
| 119 |
|
| 120 |
return $class->_new_from_dbic( $self->linked_object ); |
| 121 |
} |
| 122 |
|
| 123 |
=head3 _validate_link_integrity |
| 124 |
|
| 125 |
Internal method to validate that the linked entity exists |
| 126 |
|
| 127 |
=cut |
| 128 |
|
| 129 |
sub _validate_link_integrity { |
| 130 |
my ($self) = @_; |
| 131 |
|
| 132 |
my $link_type = $self->link_type; |
| 133 |
my $linked_id = $self->linked_id; |
| 134 |
|
| 135 |
# Define valid link types and their validation methods |
| 136 |
my $validators = { |
| 137 |
'hold' => sub { Koha::Holds->find( $_[0] ) || Koha::Old::Holds->find( $_[0] ) }, |
| 138 |
}; |
| 139 |
|
| 140 |
my $validator = $validators->{$link_type}; |
| 141 |
return 0 unless $validator; # Invalid link type |
| 142 |
|
| 143 |
return $validator->($linked_id) ? 1 : 0; |
| 144 |
} |
| 145 |
|
| 146 |
=head2 Class Methods |
| 147 |
|
| 148 |
=head3 _type |
| 149 |
|
| 150 |
=cut |
| 151 |
|
| 152 |
sub _type { |
| 153 |
return 'AccountlineLink'; |
| 154 |
} |
| 155 |
|
| 156 |
=head1 AUTHOR |
| 157 |
|
| 158 |
Koha Development Team <http://koha-community.org/> |
| 159 |
|
| 160 |
=head1 COPYRIGHT |
| 161 |
|
| 162 |
Copyright 2025 Koha Development Team |
| 163 |
|
| 164 |
=head1 LICENSE |
| 165 |
|
| 166 |
This file is part of Koha. |
| 167 |
|
| 168 |
Koha is free software; you can redistribute it and/or modify it |
| 169 |
under the terms of the GNU General Public License as published by |
| 170 |
the Free Software Foundation; either version 3 of the License, or |
| 171 |
(at your option) any later version. |
| 172 |
|
| 173 |
Koha is distributed in the hope that it will be useful, but |
| 174 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 175 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 176 |
GNU General Public License for more details. |
| 177 |
|
| 178 |
You should have received a copy of the GNU General Public License |
| 179 |
along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 180 |
|
| 181 |
=cut |
| 182 |
|
| 183 |
1; |