Bug 20473: Whitespace
[koha.git] / Koha / AudioAlerts.pm
1 package Koha::AudioAlerts;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22
23 use Koha::Database;
24
25 use Koha::AudioAlert;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::AudioAlerts - Koha Audio Alert object set class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =head3 search
38
39 Overrides default search such that
40 the default ordering is by precedence
41
42 =cut
43
44 sub search {
45     my ( $self, $params, $attributes ) = @_;
46
47     $attributes->{order_by} ||= 'precedence';
48
49     return $self->SUPER::search( $params, $attributes );
50 }
51
52 =head3 get_next_precedence
53
54 Gets the next precedence value for audio alerts
55
56 =cut
57
58 sub get_next_precedence {
59     my ($self) = @_;
60
61     return $self->get_last_precedence() + 1;
62 }
63
64 =head3 get_last_precedence
65
66 Gets the last precedence value for audio alerts
67
68 =cut
69
70 sub get_last_precedence {
71     my ($self) = @_;
72
73     return $self->_resultset()->get_column('precedence')->max() || 0;
74 }
75
76 =head3 move
77
78 Koha::AudioAlerts->move( { audio_alert => $audio_alert, where => $where } );
79
80 Moves the given alert precedence 'up', 'down', 'top' or 'bottom'
81
82 =cut
83
84 sub move {
85     my ( $self, $params ) = @_;
86
87     my $alert = $params->{audio_alert};
88     my $where = $params->{where};
89
90     return unless ( $alert && $where );
91
92     if ( $where eq 'up' ) {
93         unless ( $alert->precedence() == 1 ) {
94             my $other = $self->search( { precedence => $alert->precedence() - 1 } )->next;
95             $other->precedence( $alert->precedence() )->store();
96             $alert->precedence( $alert->precedence() - 1 )->store();
97         }
98     }
99     elsif ( $where eq 'down' ) {
100         unless ( $alert->precedence() == $self->get_last_precedence() ) {
101             my $other = $self->search( { precedence => $alert->precedence() + 1 } )->next;
102             $other->precedence( $alert->precedence() )->store();
103             $alert->precedence( $alert->precedence() + 1 )->store();
104         }
105     }
106     elsif ( $where eq 'top' ) {
107         $alert->precedence(0)->store();
108         $self->fix_precedences();
109     }
110     elsif ( $where eq 'bottom' ) {
111         $alert->precedence( $self->get_next_precedence() )->store();
112         $self->fix_precedences();
113     }
114 }
115
116 =head3 fix_precedences
117
118 Koha::AudioAlerts->fix_precedences();
119
120 Updates precedence numbers to start with 1
121 and to have no gaps
122
123 =cut
124
125 sub fix_precedences {
126     my ($self) = @_;
127
128     my @alerts = $self->search->as_list;
129
130     my $i = 1;
131     map { $_->precedence( $i++ )->store() } @alerts;
132 }
133
134 =head3 type
135
136 =cut
137
138 sub _type {
139     return 'AudioAlert';
140 }
141
142 =head3 object_class
143
144 =cut
145
146 sub object_class {
147     return 'Koha::AudioAlert';
148 }
149
150 =head1 AUTHOR
151
152 Kyle M Hall <kyle@bywatersolutions.com>
153
154 =cut
155
156 1;