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

(-)a/Koha/Display.pm (+187 lines)
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;
(-)a/Koha/DisplayItem.pm (+155 lines)
Line 0 Link Here
1
package Koha::DisplayItem;
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
use DateTime;
22
23
use C4::Context;
24
use C4::Log qw( logaction );
25
use Koha::Database;
26
use Koha::Display;
27
use Koha::Item;
28
use Koha::Biblio;
29
30
use base qw(Koha::Object);
31
32
=head1 NAME
33
34
Koha::DisplayItem - Koha Display Item Object class
35
36
=head1 API
37
38
=head2 Class methods
39
40
=cut
41
42
=head3 display
43
44
    my $display = $display_item->display;
45
46
Returns the related Koha::Display object for this display item.
47
48
=cut
49
50
sub display {
51
    my ($self) = @_;
52
    my $rs = $self->_result->display;
53
    return Koha::Display->_new_from_dbic($rs);
54
}
55
56
=head3 item
57
58
    my $item = $display_item->item;
59
60
Returns the related Koha::Item object for this display item.
61
62
=cut
63
64
sub item {
65
    my ($self) = @_;
66
    my $rs = $self->_result->itemnumber;
67
    return unless $rs;
68
    return Koha::Item->_new_from_dbic($rs);
69
}
70
71
=head3 biblio
72
73
    my $biblio = $display_item->biblio;
74
75
Returns the related Koha::Biblio object for this display item.
76
77
=cut
78
79
sub biblio {
80
    my ($self) = @_;
81
    my $rs = $self->_result->biblionumber;
82
    return unless $rs;
83
    return Koha::Biblio->_new_from_dbic($rs);
84
}
85
86
=head3 store
87
88
    $display_item->store();
89
90
Overloaded store method to add action logging.
91
92
=cut
93
94
sub store {
95
    my ($self) = @_;
96
97
    my $action = $self->in_storage ? 'update' : 'create';
98
99
    if ( $action eq 'create' && !$self->date_remove ) {
100
        my $display = $self->display;
101
        if ( $display && $display->display_days ) {
102
            my $dt = DateTime->now( time_zone => C4::Context->tz() );
103
            $dt->add( days => $display->display_days );
104
            $self->date_remove( $dt->ymd );
105
        }
106
    }
107
108
    my $result = $self->SUPER::store;
109
110
    if ( C4::Context->preference("DisplayItemsLog") && $action eq 'create' ) {
111
        logaction( "DISPLAYS", "ADD_ITEM", $self->display_id, "Item " . $self->itemnumber, undef, $self );
112
    }
113
114
    return $result;
115
}
116
117
=head3 delete
118
119
    $display_item->delete();
120
121
Overloaded delete method to add action logging.
122
123
=cut
124
125
sub delete {
126
    my ($self) = @_;
127
128
    my $display_id = $self->display_id;
129
    my $itemnumber = $self->itemnumber;
130
131
    my $result = $self->SUPER::delete;
132
133
    logaction( "DISPLAYS", "REMOVE_ITEM", $display_id, "Item " . $itemnumber, undef, $self )
134
        if C4::Context->preference("DisplayItemsLog");
135
136
    return $result;
137
}
138
139
=head2 Internal methods
140
141
=head3 _type
142
143
=cut
144
145
sub _type {
146
    return 'DisplayItem';
147
}
148
149
=head1 AUTHOR
150
151
Koha Development Team <http://koha-community.org/>
152
153
=cut
154
155
1;
(-)a/Koha/DisplayItems.pm (+116 lines)
Line 0 Link Here
1
package Koha::DisplayItems;
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
use DateTime;
22
23
use Koha::Database;
24
use Koha::DisplayItem;
25
26
use base qw(Koha::Objects);
27
28
=head1 NAME
29
30
Koha::DisplayItems - Koha Display Items Object set class
31
32
=head1 API
33
34
=head2 Class methods
35
36
=cut
37
38
=head3 for_display
39
40
    my $display_items = $display_items->for_display($display_id);
41
42
Returns display items for a specific display.
43
44
=cut
45
46
sub for_display {
47
    my ( $self, $display_id ) = @_;
48
    return $self->search( { display_id => $display_id } );
49
}
50
51
=head3 for_item
52
53
    my $display_items = $display_items->for_item($itemnumber);
54
55
Returns display items for a specific item.
56
57
=cut
58
59
sub for_item {
60
    my ( $self, $itemnumber ) = @_;
61
    return $self->search( { itemnumber => $itemnumber } );
62
}
63
64
=head3 for_biblio
65
66
    my $display_items = $display_items->for_biblio($biblionumber);
67
68
Returns display items for a specific biblio.
69
70
=cut
71
72
sub for_biblio {
73
    my ( $self, $biblionumber ) = @_;
74
    return $self->search( { biblionumber => $biblionumber } );
75
}
76
77
=head3 due_for_removal
78
79
    my $items_to_remove = $display_items->due_for_removal;
80
81
Returns display items that are due for removal based on date_remove.
82
83
=cut
84
85
sub due_for_removal {
86
    my ($self) = @_;
87
    my $today = DateTime->today->ymd;
88
89
    return $self->search( { date_remove => { '<=' => $today } } );
90
}
91
92
=head2 Internal methods
93
94
=head3 _type
95
96
=cut
97
98
sub _type {
99
    return 'DisplayItem';
100
}
101
102
=head3 object_class
103
104
=cut
105
106
sub object_class {
107
    return 'Koha::DisplayItem';
108
}
109
110
=head1 AUTHOR
111
112
Koha Development Team <http://koha-community.org/>
113
114
=cut
115
116
1;
(-)a/Koha/Displays.pm (-1 / +117 lines)
Line 0 Link Here
0
- 
1
package Koha::Displays;
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
use DateTime;
22
23
use Koha::Database;
24
use Koha::Display;
25
26
use base qw(Koha::Objects);
27
28
=head1 NAME
29
30
Koha::Displays - Koha Display Object set class
31
32
=head1 API
33
34
=head2 Class methods
35
36
=cut
37
38
=head3 enabled
39
40
    my $enabled_displays = $displays->enabled;
41
42
Returns only the enabled displays.
43
44
=cut
45
46
sub enabled {
47
    my ($self) = @_;
48
    return $self->search( { enabled => 1 } );
49
}
50
51
=head3 for_branch
52
53
    my $branch_displays = $displays->for_branch($branchcode);
54
55
Returns displays for a specific branch.
56
57
=cut
58
59
sub for_branch {
60
    my ( $self, $branchcode ) = @_;
61
    return $self->search( { display_branch => $branchcode } );
62
}
63
64
=head3 active
65
66
    my $active_displays = $displays->active;
67
68
Returns displays that are currently active (enabled and within date range if specified).
69
70
=cut
71
72
sub active {
73
    my ($self) = @_;
74
    my $today = DateTime->today->ymd;
75
76
    return $self->search(
77
        {
78
            enabled => 1,
79
            -or     => [
80
                { start_date => undef },
81
                { start_date => { '<=' => $today } }
82
            ],
83
            -and => [
84
                -or => [
85
                    { end_date => undef },
86
                    { end_date => { '>=' => $today } }
87
                ]
88
            ]
89
        }
90
    );
91
}
92
93
=head2 Internal methods
94
95
=head3 _type
96
97
=cut
98
99
sub _type {
100
    return 'Display';
101
}
102
103
=head3 object_class
104
105
=cut
106
107
sub object_class {
108
    return 'Koha::Display';
109
}
110
111
=head1 AUTHOR
112
113
Koha Development Team <http://koha-community.org/>
114
115
=cut
116
117
1;

Return to bug 14962