|
Line 0
Link Here
|
|
|
1 |
package Koha::Display; |
| 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; |
| 21 |
|
| 22 |
use C4::Context; |
| 23 |
use C4::Log qw( logaction ); |
| 24 |
use Koha::Database; |
| 25 |
use Koha::Displays; |
| 26 |
use Koha::DisplayItems; |
| 27 |
use Koha::Library; |
| 28 |
use Koha::ItemType; |
| 29 |
|
| 30 |
use base qw(Koha::Object); |
| 31 |
|
| 32 |
=head1 NAME |
| 33 |
|
| 34 |
Koha::Display - Koha Display Object class |
| 35 |
|
| 36 |
=head1 API |
| 37 |
|
| 38 |
=head2 Class methods |
| 39 |
|
| 40 |
=cut |
| 41 |
|
| 42 |
=head3 display_items |
| 43 |
|
| 44 |
my $display_items = $display->display_items; |
| 45 |
|
| 46 |
Returns the related Koha::DisplayItems object for this display. |
| 47 |
|
| 48 |
=cut |
| 49 |
|
| 50 |
sub display_items { |
| 51 |
my ( $self, $display_items ) = @_; |
| 52 |
|
| 53 |
if ($display_items) { |
| 54 |
my $schema = $self->_result->result_source->schema; |
| 55 |
$schema->txn_do( |
| 56 |
sub { |
| 57 |
my $existing_items = $self->display_items; |
| 58 |
my %existing_map = map { $_->itemnumber => $_ } $existing_items->as_list; |
| 59 |
my %new_map = map { $_->{itemnumber} => $_ } @$display_items; |
| 60 |
|
| 61 |
for my $existing_item ( values %existing_map ) { |
| 62 |
unless ( exists $new_map{ $existing_item->itemnumber } ) { |
| 63 |
$existing_item->delete; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
for my $new_item (@$display_items) { |
| 68 |
unless ( exists $existing_map{ $new_item->{itemnumber} } ) { |
| 69 |
Koha::DisplayItem->new( |
| 70 |
{ |
| 71 |
display_id => $self->display_id, |
| 72 |
itemnumber => $new_item->{itemnumber}, |
| 73 |
biblionumber => $new_item->{biblionumber}, |
| 74 |
date_remove => $new_item->{date_remove}, |
| 75 |
} |
| 76 |
)->store; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
my $display_items_rs = $self->_result->display_items; |
| 84 |
return Koha::DisplayItems->_new_from_dbic($display_items_rs); |
| 85 |
} |
| 86 |
|
| 87 |
=head3 library |
| 88 |
|
| 89 |
my $library = $display->library; |
| 90 |
|
| 91 |
Returns the related Koha::Library object for this display's branch. |
| 92 |
|
| 93 |
=cut |
| 94 |
|
| 95 |
sub library { |
| 96 |
my ($self) = @_; |
| 97 |
my $rs = $self->_result->display_branch; |
| 98 |
return unless $rs; |
| 99 |
return Koha::Library->_new_from_dbic($rs); |
| 100 |
} |
| 101 |
|
| 102 |
=head3 item_type |
| 103 |
|
| 104 |
my $item_type = $display->item_type; |
| 105 |
|
| 106 |
Returns the related Koha::ItemType object for this display's item type. |
| 107 |
|
| 108 |
=cut |
| 109 |
|
| 110 |
sub item_type { |
| 111 |
my ($self) = @_; |
| 112 |
my $rs = $self->_result->display_itype; |
| 113 |
return unless $rs; |
| 114 |
return Koha::ItemType->_new_from_dbic($rs); |
| 115 |
} |
| 116 |
|
| 117 |
=head3 store |
| 118 |
|
| 119 |
$display->store(); |
| 120 |
|
| 121 |
Overloaded store method to add action logging. |
| 122 |
|
| 123 |
=cut |
| 124 |
|
| 125 |
sub store { |
| 126 |
my ($self) = @_; |
| 127 |
|
| 128 |
my $action = $self->in_storage ? 'update' : 'create'; |
| 129 |
my $original = $self->in_storage ? Koha::Displays->find( $self->display_id ) : undef; |
| 130 |
|
| 131 |
my $enabled_before = $original ? $original->enabled : undef; |
| 132 |
my $enabled_after = $self->enabled; |
| 133 |
|
| 134 |
my $result = $self->SUPER::store; |
| 135 |
|
| 136 |
if ( C4::Context->preference("DisplayItemsLog") ) { |
| 137 |
if ( $action eq 'create' ) { |
| 138 |
logaction( "DISPLAYS", "CREATE", $self->display_id, undef, undef, $self ); |
| 139 |
} else { |
| 140 |
logaction( "DISPLAYS", "MODIFY", $self->display_id, $self, undef, $original ); |
| 141 |
|
| 142 |
if ( defined $enabled_before && defined $enabled_after && $enabled_before != $enabled_after ) { |
| 143 |
my $enable_action = $enabled_after ? "ENABLE" : "DISABLE"; |
| 144 |
logaction( "DISPLAYS", $enable_action, $self->display_id, undef, undef, $self ); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $result; |
| 150 |
} |
| 151 |
|
| 152 |
=head3 delete |
| 153 |
|
| 154 |
$display->delete(); |
| 155 |
|
| 156 |
Overloaded delete method to add action logging. |
| 157 |
|
| 158 |
=cut |
| 159 |
|
| 160 |
sub delete { |
| 161 |
my ($self) = @_; |
| 162 |
|
| 163 |
my $result = $self->SUPER::delete; |
| 164 |
|
| 165 |
logaction( "DISPLAYS", "DELETE", $self->display_id, undef, undef, $self ) |
| 166 |
if C4::Context->preference("DisplayItemsLog"); |
| 167 |
|
| 168 |
return $result; |
| 169 |
} |
| 170 |
|
| 171 |
=head2 Internal methods |
| 172 |
|
| 173 |
=head3 _type |
| 174 |
|
| 175 |
=cut |
| 176 |
|
| 177 |
sub _type { |
| 178 |
return 'Display'; |
| 179 |
} |
| 180 |
|
| 181 |
=head1 AUTHOR |
| 182 |
|
| 183 |
Koha Development Team <http://koha-community.org/> |
| 184 |
|
| 185 |
=cut |
| 186 |
|
| 187 |
1; |