Bug 16223: (QA follow-up) Remove GetDebarments
[koha.git] / Koha / Patron / MessagePreferences.pm
1 package Koha::Patron::MessagePreferences;
2
3 # Copyright Koha-Suomi Oy 2016
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 use Koha::Database;
23 use Koha::Patron::MessagePreference::Attributes;
24 use Koha::Patron::MessagePreference;
25 use Koha::Patron::MessagePreference::Transports;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::Patron::MessagePreferences - Koha Patron Message Preferences object class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 =head3 find_with_message_name
40
41 Koha::Patron::MessagePreferences->find_with_message_name({
42     borrowernumber => 123,
43     message_name => 'Hold_Filled',
44 });
45
46 Converts C<message_name> into C<message_attribute_id> and continues find.
47
48 =cut
49
50 sub find_with_message_name {
51     my ($self, $id) = @_;
52
53     if (ref($id) eq "HASH" && $id->{'message_name'}) {
54         my $attr = Koha::Patron::MessagePreference::Attributes->find({
55             message_name => $id->{'message_name'},
56         });
57         $id->{'message_attribute_id'} = ($attr) ?
58                     $attr->message_attribute_id : undef;
59         delete $id->{'message_name'};
60     }
61
62     return $self->SUPER::find($id);
63 }
64
65 =head3 get_options
66
67 my $messaging_options = Koha::Patron::MessagePreferences->get_options
68
69 Returns an ARRAYref of HASHrefs on available messaging options.
70
71 =cut
72
73 sub get_options {
74     my ($self) = @_;
75
76     my $transports = Koha::Patron::MessagePreference::Transports->search(undef,
77         {
78             join => ['message_attribute'],
79             '+select' => ['message_attribute.message_name', 'message_attribute.takes_days'],
80             '+as' => ['message_name', 'takes_days'],
81         });
82
83     my $choices;
84     while (my $transport = $transports->next) {
85         my $name = $transport->get_column('message_name');
86         $choices->{$name}->{'message_attribute_id'} = $transport->message_attribute_id;
87         $choices->{$name}->{'message_name'}         = $name;
88         $choices->{$name}->{'takes_days'}           = $transport->get_column('takes_days');
89         $choices->{$name}->{'has_digest'}           ||= 1 if $transport->is_digest;
90         $choices->{$name}->{'has_digest_off'}       ||= 1 if !$transport->is_digest;
91         $choices->{$name}->{'transport_'.$transport->get_column('message_transport_type')} = ' ';
92     }
93
94     my @return = values %$choices;
95     @return = sort { $a->{message_attribute_id} <=> $b->{message_attribute_id} } @return;
96
97     return \@return;
98 }
99
100 =head3 search_with_message_name
101
102 Koha::Patron::MessagePreferences->search_with_message_name({
103     borrowernumber => 123,
104     message_name => 'Hold_Filled',
105 });
106
107 Converts C<message_name> into C<message_attribute_id> and continues search. Use
108 Koha::Patron::MessagePreferences->search with a proper join for more complicated
109 searches.
110
111 =cut
112
113 sub search_with_message_name {
114     my ($self, $params, $attributes) = @_;
115
116     if (ref($params) eq "HASH" && $params->{'message_name'}) {
117         my $attr = Koha::Patron::MessagePreference::Attributes->find({
118             message_name => $params->{'message_name'},
119         });
120         $params->{'message_attribute_id'} = ($attr) ?
121                     $attr->message_attribute_id : undef;
122         delete $params->{'message_name'};
123     }
124
125     return $self->SUPER::search($params, $attributes);
126 }
127
128 =head3 _type
129
130 =cut
131
132 sub _type {
133     return 'BorrowerMessagePreference';
134 }
135
136 =head3 object_class
137
138 =cut
139
140 sub object_class {
141     return 'Koha::Patron::MessagePreference';
142 }
143
144 =head1 AUTHOR
145
146 Lari Taskula <lari.taskula@hypernova.fi>
147
148 =cut
149
150 1;