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