Bug 18420: Fix HoldFulfillmentPolicy.t and Passwordrecovery.t
[koha.git] / Koha / AudioAlerts.pm
blobbe4f51a9ee711c92b7f2a9a57d11ebaf7ba40914
1 package Koha::AudioAlerts;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use Carp;
24 use Koha::Database;
26 use Koha::AudioAlert;
28 use base qw(Koha::Objects);
30 =head1 NAME
32 Koha::AudioAlerts - Koha Audio Alert object set class
34 =head1 API
36 =head2 Class Methods
38 =head3 search
40 Overrides default search such that
41 the default ordering is by precedence
43 =cut
45 sub search {
46 my ( $self, $params, $attributes ) = @_;
48 $attributes->{order_by} ||= 'precedence';
50 return $self->SUPER::search( $params, $attributes );
53 =head3 get_next_precedence
55 Gets the next precedence value for audio alerts
57 =cut
59 sub get_next_precedence {
60 my ($self) = @_;
62 return $self->get_last_precedence() + 1;
65 =head3 get_last_precedence
67 Gets the last precedence value for audio alerts
69 =cut
71 sub get_last_precedence {
72 my ($self) = @_;
74 return $self->_resultset()->get_column('precedence')->max() || 0;
77 =head3 move
79 Koha::AudioAlerts->move( { audio_alert => $audio_alert, where => $where } );
81 Moves the given alert precedence 'up', 'down', 'top' or 'bottom'
83 =cut
85 sub move {
86 my ( $self, $params ) = @_;
88 my $alert = $params->{audio_alert};
89 my $where = $params->{where};
91 return unless ( $alert && $where );
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();
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();
107 elsif ( $where eq 'top' ) {
108 $alert->precedence(0)->store();
109 $self->fix_precedences();
111 elsif ( $where eq 'bottom' ) {
112 $alert->precedence( $self->get_next_precedence() )->store();
113 $self->fix_precedences();
117 =head3 fix_precedences
119 Koha::AudioAlerts->fix_precedences();
121 Updates precedence numbers to start with 1
122 and to have no gaps
124 =cut
126 sub fix_precedences {
127 my ($self) = @_;
129 my @alerts = $self->search();
131 my $i = 1;
132 map { $_->precedence( $i++ )->store() } @alerts;
135 =head3 type
137 =cut
139 sub _type {
140 return 'AudioAlert';
143 =head3 object_class
145 =cut
147 sub object_class {
148 return 'Koha::AudioAlert';
151 =head1 AUTHOR
153 Kyle M Hall <kyle@bywatersolutions.com>
155 =cut