Bug 6254: make it possible to set default privacy setting for new patrons
[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
36 # with Koha; if not, write to the Free Software Foundation, Inc.,
37 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38
39 use Modern::Perl;
40
41 use CGI;
42 use C4::Context;
43 use C4::Auth;
44 use C4::Branch;
45 use C4::Output;
46 use C4::Dates;
47 use C4::Form::MessagingPreferences;
48 use Koha::Database;
49
50 sub StringSearch  {
51         my ($searchstring,$type)=@_;
52         my $dbh = C4::Context->dbh;
53         $searchstring //= '';
54         $searchstring=~ s/\'/\\\'/g;
55         my @data=split(' ',$searchstring);
56         push @data,q{} if $#data==-1;
57         my $count=@data;
58         my $sth=$dbh->prepare("Select * from categories where (description like ?) order by category_type,description,categorycode");
59         $sth->execute("$data[0]%");
60         my @results;
61         while (my $data=$sth->fetchrow_hashref){
62         push(@results,$data);
63         }
64         #  $sth->execute;
65         $sth->finish;
66         return (scalar(@results),\@results);
67 }
68
69 my $input = new CGI;
70 my $searchfield=$input->param('description');
71 my $script_name="/cgi-bin/koha/admin/categorie.pl";
72 my $categorycode=$input->param('categorycode');
73 my $op = $input->param('op') // '';
74 my $block_expired = $input->param("block_expired");
75
76 my ($template, $loggedinuser, $cookie)
77     = get_template_and_user({template_name => "admin/categorie.tmpl",
78                              query => $input,
79                              type => "intranet",
80                              authnotrequired => 0,
81                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
82                              debug => 1,
83                              });
84
85
86 $template->param(script_name => $script_name,
87                  categorycode => $categorycode,
88                  searchfield => $searchfield);
89
90
91 ################## ADD_FORM ##################################
92 # called by default. Used to create form to add or  modify a record
93 if ($op eq 'add_form') {
94         $template->param(add_form => 1);
95         
96         #---- if primkey exists, it's a modify action, so read values to modify...
97         my $data;
98     my @selected_branches;
99         if ($categorycode) {
100                 my $dbh = C4::Context->dbh;
101          my $sth=$dbh->prepare("SELECT * FROM categories WHERE categorycode=?");
102                 $sth->execute($categorycode);
103                 $data=$sth->fetchrow_hashref;
104
105         $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM categories_branches AS cb, branches AS b WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?");
106         $sth->execute( $categorycode );
107         while ( my $branch = $sth->fetchrow_hashref ) {
108             push @selected_branches, $branch;
109         }
110         $sth->finish;
111     }
112
113     if ($data->{'enrolmentperioddate'} && $data->{'enrolmentperioddate'} eq '0000-00-00') {
114         $data->{'enrolmentperioddate'} = undef;
115     }
116     $data->{'category_type'} //= '';
117
118     my $branches = GetBranches;
119     my @branches_loop;
120     foreach my $branch (sort keys %$branches) {
121         my $selected = ( grep {$$_{branchcode} eq $branch} @selected_branches ) ? 1 : 0;
122         push @branches_loop, {
123             branchcode => $$branches{$branch}{branchcode},
124             branchname => $$branches{$branch}{branchname},
125             selected => $selected,
126         };
127     }
128
129     $template->param(
130         description         => $data->{'description'},
131         enrolmentperiod     => $data->{'enrolmentperiod'},
132         enrolmentperioddate => $data->{'enrolmentperioddate'},
133         upperagelimit       => $data->{'upperagelimit'},
134         dateofbirthrequired => $data->{'dateofbirthrequired'},
135         enrolmentfee        => sprintf( "%.2f", $data->{'enrolmentfee'} || 0 ),
136         overduenoticerequired => $data->{'overduenoticerequired'},
137         issuelimit            => $data->{'issuelimit'},
138         reservefee            => sprintf( "%.2f", $data->{'reservefee'} || 0 ),
139         hidelostitems         => $data->{'hidelostitems'},
140         category_type         => $data->{'category_type'},
141         SMSSendDriver         => C4::Context->preference("SMSSendDriver"),
142         TalkingTechItivaPhone =>
143           C4::Context->preference("TalkingTechItivaPhoneNotification"),
144         "type_" . $data->{'category_type'} => 1,
145         branches_loop                      => \@branches_loop,
146         BlockExpiredPatronOpacActions =>
147           $data->{'BlockExpiredPatronOpacActions'},
148         default_privacy => $data->{'default_privacy'},
149     );
150
151     if (C4::Context->preference('EnhancedMessagingPreferences')) {
152         C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode } , $template);
153     }
154                                                                                                         # END $OP eq ADD_FORM
155 ################## ADD_VALIDATE ##################################
156 # called by add_form, used to insert/modify data in DB
157 } elsif ($op eq 'add_validate') {
158         $template->param(add_validate => 1);
159         my $is_a_modif = $input->param("is_a_modif");
160         my $dbh = C4::Context->dbh;
161         if($input->param('enrolmentperioddate')){
162             $input->param('enrolmentperioddate' => C4::Dates::format_date_in_iso($input->param('enrolmentperioddate')) );
163         }
164         
165         if ($is_a_modif) {
166             my $sth=$dbh->prepare("
167                 UPDATE categories
168                 SET description=?,
169                     enrolmentperiod=?,
170                     enrolmentperioddate=?,
171                     upperagelimit=?,
172                     dateofbirthrequired=?,
173                     enrolmentfee=?,
174                     reservefee=?,
175                     hidelostitems=?,
176                     overduenoticerequired=?,
177                     category_type=?,
178                     BlockExpiredPatronOpacActions=?,
179                     default_privacy=?
180                 WHERE categorycode=?"
181             );
182             $sth->execute(
183                 map { $input->param($_) } (
184                     'description',
185                     'enrolmentperiod',
186                     'enrolmentperioddate',
187                     'upperagelimit',
188                     'dateofbirthrequired',
189                     'enrolmentfee',
190                     'reservefee',
191                     'hidelostitems',
192                     'overduenoticerequired',
193                     'category_type',
194                     'block_expired',
195                     'default_privacy',
196                     'categorycode'
197                 )
198             );
199             my @branches = $input->param("branches");
200             if ( @branches ) {
201                 $sth = $dbh->prepare("DELETE FROM categories_branches WHERE categorycode = ?");
202                 $sth->execute( $input->param( "categorycode" ) );
203                 $sth = $dbh->prepare(
204                     "INSERT INTO categories_branches
205                                 ( categorycode, branchcode )
206                                 VALUES ( ?, ? )"
207                 );
208                 for my $branchcode ( @branches ) {
209                     next if not $branchcode;
210                     $sth->bind_param( 1, $input->param( "categorycode" ) );
211                     $sth->bind_param( 2, $branchcode );
212                     $sth->execute;
213                 }
214             }
215             $sth->finish;
216     } else {
217         my $sth=$dbh->prepare("
218             INSERT INTO categories (
219                 categorycode,
220                 description,
221                 enrolmentperiod,
222                 enrolmentperioddate,
223                 upperagelimit,
224                 dateofbirthrequired,
225                 enrolmentfee,
226                 reservefee,
227                 hidelostitems,
228                 overduenoticerequired,
229                 category_type,
230                 BlockExpiredPatronOpacActions,
231                 default_privacy
232             )
233             VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
234         $sth->execute(
235             map { $input->param($_) } (
236                 'categorycode',
237                 'description',
238                 'enrolmentperiod',
239                 'enrolmentperioddate',
240                 'upperagelimit',
241                 'dateofbirthrequired',
242                 'enrolmentfee',
243                 'reservefee',
244                 'hidelostitems',
245                 'overduenoticerequired',
246                 'category_type',
247                 'block_expired',
248                 'default_privacy',
249             )
250         );
251         $sth->finish;
252     }
253
254     if (C4::Context->preference('EnhancedMessagingPreferences')) {
255         C4::Form::MessagingPreferences::handle_form_action($input, 
256                                                            { categorycode => $input->param('categorycode') }, $template);
257     }
258         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
259         exit;
260
261                                                                                                         # END $OP eq ADD_VALIDATE
262 ################## DELETE_CONFIRM ##################################
263 # called by default form, used to confirm deletion of data in DB
264 } elsif ($op eq 'delete_confirm') {
265     my $schema = Koha::Database->new()->schema();
266         $template->param(delete_confirm => 1);
267
268     my $count = $schema->resultset('Borrower')->search( { categorycode => $categorycode } )->count();
269     my $category = $schema->resultset('Category')->find($categorycode);
270     $category->enrolmentperioddate( C4::Dates::format_date( $category->enrolmentperioddate() ) );
271     $template->param( category => $category, patrons_in_category => $count );
272 # END $OP eq DELETE_CONFIRM
273 ################## DELETE_CONFIRMED ##################################
274 # called by delete_confirm, used to effectively confirm deletion of data in DB
275 } elsif ($op eq 'delete_confirmed') {
276         $template->param(delete_confirmed => 1);
277         my $dbh = C4::Context->dbh;
278         my $categorycode=uc($input->param('categorycode'));
279         my $sth=$dbh->prepare("delete from categories where categorycode=?");
280         $sth->execute($categorycode);
281         $sth->finish;
282         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=categorie.pl\"></html>";
283         exit;
284
285                                                                                                         # END $OP eq DELETE_CONFIRMED
286 } else { # DEFAULT
287         $template->param(else => 1);
288         my @loop;
289         my ($count,$results)=StringSearch($searchfield,'web');
290     my $dbh = C4::Context->dbh;
291     my $sth = $dbh->prepare("SELECT b.branchcode, b.branchname FROM categories_branches AS cb, branches AS b WHERE cb.branchcode = b.branchcode AND cb.categorycode = ?");
292         for (my $i=0; $i < $count; $i++){
293         $sth->execute( $results->[$i]{'categorycode'} );
294         my @selected_branches;
295         while ( my $branch = $sth->fetchrow_hashref ) {
296             push @selected_branches, $branch;
297         }
298         my $enrolmentperioddate = $results->[$i]{'enrolmentperioddate'};
299     if ($enrolmentperioddate && $enrolmentperioddate eq '0000-00-00') {
300         $enrolmentperioddate = undef;
301     }
302     $results->[$i]{'category_type'} //= '';
303                 my %row = (
304                         categorycode            => $results->[$i]{'categorycode'},
305                                 description             => $results->[$i]{'description'},
306                                 enrolmentperiod         => $results->[$i]{'enrolmentperiod'},
307                         enrolmentperioddate     => $enrolmentperioddate,
308                                 upperagelimit           => $results->[$i]{'upperagelimit'},
309                                 dateofbirthrequired     => $results->[$i]{'dateofbirthrequired'},
310                         enrolmentfee            => sprintf("%.2f",$results->[$i]{'enrolmentfee'} || 0),
311                                 overduenoticerequired   => $results->[$i]{'overduenoticerequired'},
312                                 issuelimit              => $results->[$i]{'issuelimit'},
313                         reservefee              => sprintf("%.2f",$results->[$i]{'reservefee'} || 0),
314                                 hidelostitems           => $results->[$i]{'hidelostitems'},
315                                 category_type           => $results->[$i]{'category_type'},
316             default_privacy       => $results->[$i]{'default_privacy'},
317                 "type_".$results->[$i]{'category_type'} => 1,
318                 branches                => \@selected_branches,
319         );
320         if (C4::Context->preference('EnhancedMessagingPreferences')) {
321             my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
322             $row{messaging_prefs} = $brief_prefs if @$brief_prefs;
323         }
324                 push @loop, \%row;
325         }
326         $template->param(loop => \@loop);
327         # check that I (institution) and C (child) exists. otherwise => warning to the user
328     $sth=$dbh->prepare("select category_type from categories where category_type='C'");
329         $sth->execute;
330         my ($categoryChild) = $sth->fetchrow;
331         $template->param(categoryChild => $categoryChild);
332         $sth=$dbh->prepare("select category_type from categories where category_type='I'");
333         $sth->execute;
334         my ($categoryInstitution) = $sth->fetchrow;
335         $template->param(categoryInstitution => $categoryInstitution);
336         $sth->finish;
337
338
339 } #---- END $OP eq DEFAULT
340 output_html_with_http_headers $input, $cookie, $template->output;
341
342 exit 0;
343
344 sub _get_brief_messaging_prefs {
345     my $categorycode = shift;
346     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
347     my $results = [];
348     PREF: foreach my $option ( @$messaging_options ) {
349         my $pref = C4::Members::Messaging::GetMessagingPreferences( { categorycode => $categorycode,
350                                                                     message_name       => $option->{'message_name'} } );
351         next unless  $pref->{'transports'};
352         my $brief_pref = {
353             message_attribute_id    => $option->{'message_attribute_id'},
354             message_name            => $option->{'message_name'},
355             $option->{'message_name'} => 1
356         };
357         foreach my $transport ( keys %{$pref->{'transports'}} ) {
358             push @{ $brief_pref->{'transports'} }, { transport => $transport };
359         }
360         push @$results, $brief_pref;
361     }
362     return $results;
363 }