Bug 25184: (RM follow-up) Make DB update idempotent
[koha.git] / Koha / AudioAlerts.pm
blobdaca3c6aedea9b6c36c3628abfe1a416e0d7badf
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
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.
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.
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>.
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