fix for #2884: Add error management to branches
[koha.git] / admin / branches.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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 2 of the License, or (at your option) any later
10 # version.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 branches.pl
21
22  FIXME: individual fields in branch address need to be exported to templates,
23         in order to fix bug 180; need to notify translators
24 FIXME: looped html (e.g., list of checkboxes) need to be properly
25         TMPL_LOOP'ized; doing this properly will fix bug 130; need to
26         notify translators
27  FIXME: need to implement the branch categories stuff
28  FIXME: there are too many TMPL_IF's; the proper way to do it is to have
29         separate templates for each individual action; need to notify
30         translators
31  FIXME: there are lots of error messages exported to the template; a lot
32         of these should be converted into exported booleans / counters etc
33         so that the error messages can be localized; need to notify translators
34
35  NOTE:  heading() should now be called like this:
36         1. Use heading() as before
37         2. $template->param('heading-LISPISHIZED-HEADING-p' => 1);
38         3. $template->param('use-heading-flags-p' => 1);
39         This ensures that both converted and unconverted templates work
40
41  Finlay working on this file from 26-03-2002
42  Reorganising this branches admin page.....
43  
44 =cut
45
46 use strict;
47 use warnings;
48 use CGI;
49 use C4::Auth;
50 use C4::Context;
51 use C4::Output;
52 use C4::Koha;
53 use C4::Branch;
54
55 # Fixed variables
56 my $script_name = "/cgi-bin/koha/admin/branches.pl";
57 my $pagesize    = 20;
58
59 ################################################################################
60 # Main loop....
61 my $input        = new CGI;
62 my $branchcode   = $input->param('branchcode');
63 my $branchname   = $input->param('branchname');
64 my $categorycode = $input->param('categorycode');
65 my $op           = $input->param('op');
66
67 if(!defined($op)){
68   $op = '';
69 }
70
71 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
72     {
73         template_name   => "admin/branches.tmpl",
74         query           => $input,
75         type            => "intranet",
76         authnotrequired => 0,
77         flagsrequired   => { parameters => 1},
78         debug           => 1,
79     }
80 );
81 if ($op) {
82     $template->param(
83         script_name => $script_name,
84         $op         => 1
85     );    # we show only the TMPL_VAR names $op
86 }
87 else {
88     $template->param(
89         script_name => $script_name,
90         else        => 1
91     );    # we show only the TMPL_VAR names $op
92 }
93 $template->param( action => $script_name );
94 if ( $op eq 'add' ) {
95
96     # If the user has pressed the "add new branch" button.
97     $template->param( 'heading-branches-add-branch-p' => 1 );
98     editbranchform($branchcode,$template);
99
100 }
101 elsif ( $op eq 'edit' ) {
102
103     # if the user has pressed the "edit branch settings" button.
104     $template->param( 'heading-branches-add-branch-p' => 0,
105                         'add' => 1, );
106     editbranchform($branchcode,$template);
107 }
108 elsif ( $op eq 'add_validate' ) {
109
110     # confirm settings change...
111     my $params = $input->Vars;
112     unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
113         $template->param( else => 1 );
114         default("MESSAGE1",$template);
115     }
116     else {
117         my $error = ModBranch($params);
118         # if error saving, stay on edit and rise error
119         if ($error) {
120             editbranchform($branchcode,$template);
121             $template->param( 'heading-branches-add-branch-p' => 1, 'add' => 1, "ERROR$error" => 1 );
122         } else {
123             $template->param( else => 1);
124             default("MESSAGE2",$template);
125         }
126     }
127 }
128 elsif ( $op eq 'delete' ) {
129     # if the user has pressed the "delete branch" button.
130     
131     # check to see if the branchcode is being used in the database somewhere....
132     my $dbh = C4::Context->dbh;
133     my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
134     $sth->execute( $branchcode, $branchcode );
135     my ($total) = $sth->fetchrow_array;
136     $sth->finish;
137     
138     my $message;
139
140     if ($total) {
141         $message = "MESSAGE7";
142     }
143    
144     if ($message) {
145         $template->param( else => 1 );
146         default($message,$template);
147     }
148     else {
149         $template->param( branchname     => $branchname );
150         $template->param( delete_confirm => 1 );
151         $template->param( branchcode     => $branchcode );
152     }
153 }
154 elsif ( $op eq 'delete_confirmed' ) {
155
156     # actually delete branch and return to the main screen....
157     DelBranch($branchcode);
158     $template->param( else => 1 );
159     default("MESSAGE3",$template);
160 }
161 elsif ( $op eq 'editcategory' ) {
162
163     # If the user has pressed the "add new category" or "modify" buttons.
164     $template->param( 'heading-branches-edit-category-p' => 1 );
165     editcatform($categorycode,$template);
166 }
167 elsif ( $op eq 'addcategory_validate' ) {
168
169     # confirm settings change...
170     my $params = $input->Vars;
171     unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
172         $template->param( else => 1 );
173         default("MESSAGE4",$template);
174     }
175     else {
176         ModBranchCategoryInfo($params);
177         $template->param( else => 1 );
178         default("MESSAGE5",$template);
179     }
180 }
181 elsif ( $op eq 'delete_category' ) {
182
183     # if the user has pressed the "delete branch" button.
184     my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
185     if ($message) {
186         $template->param( else => 1 );
187         default($message,$template);
188     }
189     else {
190         $template->param( delete_category => 1 );
191         $template->param( categorycode    => $categorycode );
192     }
193 }
194 elsif ( $op eq 'categorydelete_confirmed' ) {
195
196     # actually delete branch and return to the main screen....
197     DelBranchCategory($categorycode);
198     $template->param( else => 1 );
199     default("MESSAGE6",$template);
200
201 }
202 else {
203
204     # if no operation has been set...
205     default("",$template);
206 }
207
208 ################################################################################
209 #
210 # html output functions....
211
212 sub default {
213     my ($message,$innertemplate) = @_;
214     $innertemplate->param( 'heading-branches-p' => 1 );
215     $innertemplate->param( "$message"           => 1 );
216     $innertemplate->param( action               => $script_name );
217     branchinfotable("",$innertemplate);
218 }
219
220 sub editbranchform {
221     my ($branchcode,$innertemplate) = @_;
222     # initiate the scrolling-list to select the printers
223     my $printers = GetPrinters();
224     my @printerloop;
225     my $printercount = 0;
226     my $oldprinter;
227     my $CGIprinter;
228     
229     my $data;
230
231     if ($branchcode) {
232         $data = GetBranchInfo($branchcode);
233         $data = $data->[0];
234
235         # get the old printer of the branch
236         $oldprinter = $data->{'branchprinter'};
237
238         #       printer loop
239         foreach my $thisprinter ( keys %$printers ) {
240
241             my $selected = 1
242               if $oldprinter and ( $oldprinter eq $printers->{$thisprinter} );
243
244             my %row = (
245                 value         => $thisprinter,
246                 selected      => $selected,
247                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
248             );
249             push @printerloop, \%row;
250         }
251
252         $innertemplate->param( 
253              printerloop    => \@printerloop,
254              branchcode     => $data->{'branchcode'},
255              branch_name    => $data->{'branchname'},
256              branchaddress1 => $data->{'branchaddress1'},
257              branchaddress2 => $data->{'branchaddress2'},
258              branchaddress3 => $data->{'branchaddress3'},
259              branchphone    => $data->{'branchphone'},
260              branchfax      => $data->{'branchfax'},
261              branchemail    => $data->{'branchemail'},
262              branchip       => $data->{'branchip'} 
263         );
264     }
265     else {    #case of an add branch select printer
266         foreach my $thisprinter ( keys %$printers ) {
267             my %row = (
268                 value         => $thisprinter,
269                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
270             );
271             push @printerloop, \%row;
272         }
273         $innertemplate->param( printerloop => \@printerloop );
274     }
275
276     # make the checkboxs.....
277     #
278     # We export a "categoryloop" array to the template, each element of which
279     # contains separate 'categoryname', 'categorycode', 'codedescription', and
280     # 'checked' fields. The $checked field is either '' or 'checked'
281     # (see bug 130)
282     #
283     my $catinfo = GetBranchCategory();
284     my $catcheckbox;
285
286     #    print DEBUG "catinfo=".cvs($catinfo)."\n";
287     my @categoryloop = ();
288     foreach my $cat (@$catinfo) {
289         my $checked = "";
290         my $tmp     = quotemeta( $cat->{'categorycode'} );
291         if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
292             $checked = "checked=\"checked\"";
293         }
294         push @categoryloop,
295           {
296             categoryname    => $cat->{'categoryname'},
297             categorycode    => $cat->{'categorycode'},
298             categorytype    => $cat->{'categorytype'},
299             codedescription => $cat->{'codedescription'},
300             checked         => $checked,
301           };
302     }
303     $innertemplate->param( categoryloop => \@categoryloop );
304
305     # {{{ Leave this here until bug 130 is completely resolved in the templates
306     for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
307         $innertemplate->param(
308             $obsolete => 'Your template is out of date (bug 130)' );
309     }
310
311     # }}}
312 }
313
314 sub editcatform {
315
316     # prepares the edit form...
317     my ($categorycode,$innertemplate) = @_;
318     warn "cat : $categorycode";
319     my $data;
320         my @cats;
321     $innertemplate->param( categorytype => \@cats);
322         if ($categorycode) {
323         $data = GetBranchCategory($categorycode);
324         $data = $data->[0];
325         $innertemplate->param(  categorycode    => $data->{'categorycode'} ,
326                                         categoryname    => $data->{'categoryname'},
327                                         codedescription => $data->{'codedescription'} ,
328                                                 );
329     }
330         for my $ctype (GetCategoryTypes()) {
331                 push @cats , { type => $ctype , selected => ($data->{'categorytype'} eq $ctype) };
332         }
333 }
334
335 sub deleteconfirm {
336
337     # message to print if the
338     my ($branchcode) = @_;
339 }
340
341 sub branchinfotable {
342
343 # makes the html for a table of branch info from reference to an array of hashs.
344
345     my ($branchcode,$innertemplate) = @_;
346     my $branchinfo;
347     if ($branchcode) {
348         $branchinfo = GetBranchInfo($branchcode);
349     }
350     else {
351         $branchinfo = GetBranchInfo();
352     }
353     my $toggle;
354     my $i = 0;
355     my @loop_data = ();
356     foreach my $branch (@$branchinfo) {
357         ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
358
359         #
360         # We export the following fields to the template. These are not
361         # pre-composed as a single "address" field because the template
362         # might (and should) escape what is exported here. (See bug 180)
363         #
364         # - color
365         # - branch_name     (Note: not "branchname")
366         # - branch_code     (Note: not "branchcode")
367         # - address         (containing a static error message)
368         # - branchaddress1 \
369         # - branchaddress2  |
370         # - branchaddress3  | comprising the old "address" field
371         # - branchphone     |
372         # - branchfax       |
373         # - branchemail    /
374         # - address-empty-p (1 if no address information, 0 otherwise)
375         # - categories      (containing a static error message)
376         # - category_list   (loop containing "categoryname")
377         # - no-categories-p (1 if no categories set, 0 otherwise)
378         # - value
379         # - action
380         #
381         my %row = ();
382
383         # Handle address fields separately
384         my $address_empty_p = 1;
385         for my $field (
386             'branchaddress1', 'branchaddress2',
387             'branchaddress3', 'branchphone',
388             'branchfax',      'branchemail',
389             'branchip',       'branchprinter'
390           )
391         {
392             $row{$field} = $branch->{$field};
393             if ( $branch->{$field} ) {
394                 $address_empty_p = 0;
395             }
396         }
397         $row{'address-empty-p'} = $address_empty_p;
398
399         # {{{ Leave this here until bug 180 is completely resolved in templates
400         $row{'address'} = 'Your template is out of date (see bug 180)';
401
402         # }}}
403
404         # Handle categories
405         my $no_categories_p = 1;
406         my @categories;
407         foreach my $cat ( @{ $branch->{'categories'} } ) {
408             my ($catinfo) = @{ GetBranchCategory($cat) };
409             push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
410             $no_categories_p = 0;
411         }
412
413         # {{{ Leave this here until bug 180 is completely resolved in templates
414         $row{'categories'} = 'Your template is out of date (see bug 180)';
415
416         # }}}
417         $row{'category_list'}   = \@categories;
418         $row{'no-categories-p'} = $no_categories_p;
419
420         # Handle all other fields
421         $row{'branch_name'} = $branch->{'branchname'};
422         $row{'branch_code'} = $branch->{'branchcode'};
423         $row{'toggle'}      = $toggle;
424         $row{'value'}       = $branch->{'branchcode'};
425         $row{'action'}      = '/cgi-bin/koha/admin/branches.pl';
426
427         push @loop_data, {%row};
428         $i++;
429     }
430     my @branchcategories = ();
431         for my $ctype ( GetCategoryTypes() ) {
432         my $catinfo = GetBranchCategories(undef,$ctype);
433         my @categories;
434                 foreach my $cat (@$catinfo) {
435                 push @categories,
436                   {
437                     categoryname    => $cat->{'categoryname'},
438                     categorycode    => $cat->{'categorycode'},
439                     codedescription => $cat->{'codedescription'},
440                     categorytype => $cat->{'categorytype'},
441                   };
442         }
443         push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
444         }
445     $innertemplate->param(
446         branches         => \@loop_data,
447         branchcategories => \@branchcategories
448     );
449
450 }
451
452 # FIXME logic seems wrong   ##  sub is not used.
453 sub branchcategoriestable {
454     my $innertemplate = shift;
455     #Needs to be implemented...
456
457     my $categoryinfo = GetBranchCategory();
458     my $color;
459     foreach my $cat (@$categoryinfo) {
460         $innertemplate->param( categoryname    => $cat->{'categoryname'} );
461         $innertemplate->param( categorycode    => $cat->{'categorycode'} );
462         $innertemplate->param( codedescription => $cat->{'codedescription'} );
463     }
464 }
465
466 output_html_with_http_headers $input, $cookie, $template->output;
467
468 # Local Variables:
469 # tab-width: 8
470 # End: