bug 3222: messaging preferences for patron categories
[koha.git] / admin / categorie.pl
1 #!/usr/bin/perl
2
3 #script to administer the categories table
4 #written 20/02/2002 by paul.poulain@free.fr
5
6 # ALGO :
7 # this script use an $op to know what to do.
8 # if $op is empty or none of the above values,
9 #       - the default screen is build (with all records, or filtered datas).
10 #       - the   user can clic on add, modify or delete record.
11 # if $op=add_form
12 #       - if primkey exists, this is a modification,so we read the $primkey record
13 #       - builds the add/modify form
14 # if $op=add_validate
15 #       - the user has just send datas, so we create/modify the record
16 # if $op=delete_form
17 #       - we show the record having primkey=$primkey and ask for deletion validation form
18 # if $op=delete_confirm
19 #       - we delete the record having primkey=$primkey
20
21
22 # Copyright 2000-2002 Katipo Communications
23 #
24 # This file is part of Koha.
25 #
26 # Koha is free software; you can redistribute it and/or modify it under the
27 # terms of the GNU General Public License as published by the Free Software
28 # Foundation; either version 2 of the License, or (at your option) any later
29 # version.
30 #
31 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
32 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
33 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
34 #
35 # You should have received a copy of the GNU General Public License along with
36 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
37 # Suite 330, Boston, MA  02111-1307 USA
38
39 use strict;
40 use CGI;
41 use C4::Context;
42 use C4::Auth;
43 use C4::Output;
44 use C4::Form::MessagingPreferences;
45
46 sub StringSearch  {
47         my ($searchstring,$type)=@_;
48         my $dbh = C4::Context->dbh;
49         $searchstring=~ s/\'/\\\'/g;
50         my @data=split(' ',$searchstring);
51         my $count=@data;
52         my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
53         $sth->execute("$data[0]%");
54         my @results;
55         while (my $data=$sth->fetchrow_hashref){
56         push(@results,$data);
57         }
58         #  $sth->execute;
59         $sth->finish;
60         return (scalar(@results),\@results);
61 }
62
63 my $input = new CGI;
64 my $searchfield=$input->param('description');
65 my $script_name="/cgi-bin/koha/admin/categorie.pl";
66 my $categorycode=$input->param('categorycode');
67 my $op = $input->param('op');
68
69 my ($template, $loggedinuser, $cookie)
70     = get_template_and_user({template_name => "admin/categorie.tmpl",
71                              query => $input,
72                              type => "intranet",
73                              authnotrequired => 0,
74                              flagsrequired => {parameters => 1},
75                              debug => 1,
76                              });
77
78
79 $template->param(script_name => $script_name,
80                  categorycode => $categorycode,
81                  searchfield => $searchfield);
82
83
84 ################## ADD_FORM ##################################
85 # called by default. Used to create form to add or  modify a record
86 if ($op eq 'add_form') {
87         $template->param(add_form => 1);
88         
89         #---- if primkey exists, it's a modify action, so read values to modify...
90         my $data;
91         if ($categorycode) {
92                 my $dbh = C4::Context->dbh;
93                 my $sth=$dbh->prepare("select categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired,category_type from categories where categorycode=?");
94                 $sth->execute($categorycode);
95                 $data=$sth->fetchrow_hashref;
96                 $sth->finish;
97         }
98
99         $template->param(description        => $data->{'description'},
100                                 enrolmentperiod         => $data->{'enrolmentperiod'},
101                                 upperagelimit           => $data->{'upperagelimit'},
102                                 dateofbirthrequired     => $data->{'dateofbirthrequired'},
103                                 enrolmentfee            => sprintf("%.2f",$data->{'enrolmentfee'}),
104                                 overduenoticerequired   => $data->{'overduenoticerequired'},
105                                 issuelimit              => $data->{'issuelimit'},
106                                 reservefee              => sprintf("%.2f",$data->{'reservefee'}),
107                                 category_type           => $data->{'category_type'},
108                                 "type_".$data->{'category_type'} => 1,
109                                 );
110     if (C4::Context->preference('EnhancedMessagingPreferences')) {
111         C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
112     }
113                                                                                                         # END $OP eq ADD_FORM
114 ################## ADD_VALIDATE ##################################
115 # called by add_form, used to insert/modify data in DB
116 } elsif ($op eq 'add_validate') {
117         $template->param(add_validate => 1);
118         my $is_a_modif = $input->param("is_a_modif");
119         my $dbh = C4::Context->dbh;
120         if ($is_a_modif) {
121             my $sth=$dbh->prepare("UPDATE categories SET description=?,enrolmentperiod=?,upperagelimit=?,dateofbirthrequired=?,enrolmentfee=?,reservefee=?,overduenoticerequired=?,category_type=? WHERE categorycode=?");
122             $sth->execute(map { $input->param($_) } ('description','enrolmentperiod','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','overduenoticerequired','category_type','categorycode'));
123             $sth->finish;
124         } else {
125             my $sth=$dbh->prepare("INSERT INTO categories  (categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,reservefee,overduenoticerequired,category_type) values (?,?,?,?,?,?,?,?,?)");
126             $sth->execute(map { $input->param($_) } ('categorycode','description','enrolmentperiod','upperagelimit','dateofbirthrequired','enrolmentfee','reservefee','overduenoticerequired','category_type'));
127             $sth->finish;
128         }
129     if (C4::Context->preference('EnhancedMessagingPreferences')) {
130         C4::Form::MessagingPreferences::handle_form_action($input, 
131                                                            { categorycode => $input->param('categorycode') }, $template);
132     }
133         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
134         exit;
135
136                                                                                                         # END $OP eq ADD_VALIDATE
137 ################## DELETE_CONFIRM ##################################
138 # called by default form, used to confirm deletion of data in DB
139 } elsif ($op eq 'delete_confirm') {
140         $template->param(delete_confirm => 1);
141
142         my $dbh = C4::Context->dbh;
143         my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
144         $sth->execute($categorycode);
145         my $total = $sth->fetchrow_hashref;
146         $sth->finish;
147         $template->param(total => $total->{'total'});
148         
149         my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,overduenoticerequired,category_type from categories where categorycode=?");
150         $sth2->execute($categorycode);
151         my $data=$sth2->fetchrow_hashref;
152         $sth2->finish;
153         if ($total->{'total'} >0) {
154                 $template->param(totalgtzero => 1);
155         }
156
157         $template->param(description             => $data->{'description'},
158                                 enrolmentperiod         => $data->{'enrolmentperiod'},
159                                 upperagelimit           => $data->{'upperagelimit'},
160                                 dateofbirthrequired     => $data->{'dateofbirthrequired'},
161                                 enrolmentfee            =>  sprintf("%.2f",$data->{'enrolmentfee'}),
162                                 overduenoticerequired   => $data->{'overduenoticerequired'},
163                                 issuelimit              => $data->{'issuelimit'},
164                                 reservefee              =>  sprintf("%.2f",$data->{'reservefee'}),
165                                 category_type           => $data->{'category_type'},
166                                 );
167                                                                                                         # END $OP eq DELETE_CONFIRM
168 ################## DELETE_CONFIRMED ##################################
169 # called by delete_confirm, used to effectively confirm deletion of data in DB
170 } elsif ($op eq 'delete_confirmed') {
171         $template->param(delete_confirmed => 1);
172         my $dbh = C4::Context->dbh;
173         my $categorycode=uc($input->param('categorycode'));
174         my $sth=$dbh->prepare("delete from categories where categorycode=?");
175         $sth->execute($categorycode);
176         $sth->finish;
177         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
178         exit;
179
180                                                                                                         # END $OP eq DELETE_CONFIRMED
181 } else { # DEFAULT
182         $template->param(else => 1);
183         my @loop;
184         my ($count,$results)=StringSearch($searchfield,'web');
185         my $toggle = 0;
186         for (my $i=0; $i < $count; $i++){
187                 my %row = (categorycode => $results->[$i]{'categorycode'},
188                                 description => $results->[$i]{'description'},
189                                 enrolmentperiod => $results->[$i]{'enrolmentperiod'},
190                                 upperagelimit => $results->[$i]{'upperagelimit'},
191                                 dateofbirthrequired => $results->[$i]{'dateofbirthrequired'},
192                                 enrolmentfee => sprintf("%.2f",$results->[$i]{'enrolmentfee'}),
193                                 overduenoticerequired => $results->[$i]{'overduenoticerequired'},
194                                 issuelimit => $results->[$i]{'issuelimit'},
195                                 reservefee => sprintf("%.2f",$results->[$i]{'reservefee'}),
196                                 category_type => $results->[$i]{'category_type'},
197                                 "type_".$results->[$i]{'category_type'} => 1,
198                                 toggle => $toggle );
199         if (C4::Context->preference('EnhancedMessagingPreferences')) {
200             my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
201             $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
202         }
203                 push @loop, \%row;
204                 if ( $toggle eq 0 )
205                 {
206                         $toggle = 1;
207                 }
208                 else
209                 {
210                         $toggle = 0;
211                 }
212         }
213         $template->param(loop => \@loop);
214         # check that I (institution) and C (child) exists. otherwise => warning to the user
215         my $dbh = C4::Context->dbh;
216         my $sth=$dbh->prepare("select category_type from categories where category_type='C'");
217         $sth->execute;
218         my ($categoryChild) = $sth->fetchrow;
219         $template->param(categoryChild => $categoryChild);
220         $sth=$dbh->prepare("select category_type from categories where category_type='I'");
221         $sth->execute;
222         my ($categoryInstitution) = $sth->fetchrow;
223         $template->param(categoryInstitution => $categoryInstitution);
224         $sth->finish;
225
226
227 } #---- END $OP eq DEFAULT
228 output_html_with_http_headers $input, $cookie, $template->output;
229
230 exit 0;
231
232 sub _get_brief_messaging_prefs {
233     my $categorycode = shift;
234     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
235     my $results = [];
236     PREF: foreach my $option ( @$messaging_options ) {
237         my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
238                                                                     message_name       => $option->{'message_name'} } );
239         next unless  @{$pref->{'transports'}};
240         my $brief_pref = { message_attribute_id => $option->{'message_attribute_id'},
241                            message_name => $option->{'message_name'},
242                          };
243         foreach my $transport ( @{$pref->{'transports'}} ) {
244             push @{ $brief_pref->{'transports'} }, { transport => $transport };
245         }
246         push @$results, $brief_pref;
247     }
248     return $results;
249 }