bug 2884: followup patch
[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             # copy input parameters back to form
121             # FIXME - doing this doesn't preserve any branch group selections, but good enough for now
122             $template->param(%$params);
123             $template->param(branch_name => $params->{branchname});
124             $template->param( 'heading-branches-add-branch-p' => 1, 'add' => 1, "ERROR$error" => 1 );
125         } else {
126             $template->param( else => 1);
127             default("MESSAGE2",$template);
128         }
129     }
130 }
131 elsif ( $op eq 'delete' ) {
132     # if the user has pressed the "delete branch" button.
133     
134     # check to see if the branchcode is being used in the database somewhere....
135     my $dbh = C4::Context->dbh;
136     my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
137     $sth->execute( $branchcode, $branchcode );
138     my ($total) = $sth->fetchrow_array;
139     $sth->finish;
140     
141     my $message;
142
143     if ($total) {
144         $message = "MESSAGE7";
145     }
146    
147     if ($message) {
148         $template->param( else => 1 );
149         default($message,$template);
150     }
151     else {
152         $template->param( branchname     => $branchname );
153         $template->param( delete_confirm => 1 );
154         $template->param( branchcode     => $branchcode );
155     }
156 }
157 elsif ( $op eq 'delete_confirmed' ) {
158
159     # actually delete branch and return to the main screen....
160     DelBranch($branchcode);
161     $template->param( else => 1 );
162     default("MESSAGE3",$template);
163 }
164 elsif ( $op eq 'editcategory' ) {
165
166     # If the user has pressed the "add new category" or "modify" buttons.
167     $template->param( 'heading-branches-edit-category-p' => 1 );
168     editcatform($categorycode,$template);
169 }
170 elsif ( $op eq 'addcategory_validate' ) {
171
172     # confirm settings change...
173     my $params = $input->Vars;
174     unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
175         $template->param( else => 1 );
176         default("MESSAGE4",$template);
177     }
178     else {
179         ModBranchCategoryInfo($params);
180         $template->param( else => 1 );
181         default("MESSAGE5",$template);
182     }
183 }
184 elsif ( $op eq 'delete_category' ) {
185
186     # if the user has pressed the "delete branch" button.
187     my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
188     if ($message) {
189         $template->param( else => 1 );
190         default($message,$template);
191     }
192     else {
193         $template->param( delete_category => 1 );
194         $template->param( categorycode    => $categorycode );
195     }
196 }
197 elsif ( $op eq 'categorydelete_confirmed' ) {
198
199     # actually delete branch and return to the main screen....
200     DelBranchCategory($categorycode);
201     $template->param( else => 1 );
202     default("MESSAGE6",$template);
203
204 }
205 else {
206
207     # if no operation has been set...
208     default("",$template);
209 }
210
211 ################################################################################
212 #
213 # html output functions....
214
215 sub default {
216     my ($message,$innertemplate) = @_;
217     $innertemplate->param( 'heading-branches-p' => 1 );
218     $innertemplate->param( "$message"           => 1 );
219     $innertemplate->param( action               => $script_name );
220     branchinfotable("",$innertemplate);
221 }
222
223 sub editbranchform {
224     my ($branchcode,$innertemplate) = @_;
225     # initiate the scrolling-list to select the printers
226     my $printers = GetPrinters();
227     my @printerloop;
228     my $printercount = 0;
229     my $oldprinter;
230     my $CGIprinter;
231     
232     my $data;
233
234     if ($branchcode) {
235         $data = GetBranchInfo($branchcode);
236         $data = $data->[0];
237
238         # get the old printer of the branch
239         $oldprinter = $data->{'branchprinter'};
240
241         #       printer loop
242         foreach my $thisprinter ( keys %$printers ) {
243
244             my $selected = 1
245               if $oldprinter and ( $oldprinter eq $printers->{$thisprinter} );
246
247             my %row = (
248                 value         => $thisprinter,
249                 selected      => $selected,
250                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
251             );
252             push @printerloop, \%row;
253         }
254
255         $innertemplate->param( 
256              printerloop    => \@printerloop,
257              branchcode     => $data->{'branchcode'},
258              branch_name    => $data->{'branchname'},
259              branchaddress1 => $data->{'branchaddress1'},
260              branchaddress2 => $data->{'branchaddress2'},
261              branchaddress3 => $data->{'branchaddress3'},
262              branchphone    => $data->{'branchphone'},
263              branchfax      => $data->{'branchfax'},
264              branchemail    => $data->{'branchemail'},
265              branchip       => $data->{'branchip'} 
266         );
267     }
268     else {    #case of an add branch select printer
269         foreach my $thisprinter ( keys %$printers ) {
270             my %row = (
271                 value         => $thisprinter,
272                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
273             );
274             push @printerloop, \%row;
275         }
276         $innertemplate->param( printerloop => \@printerloop );
277     }
278
279     # make the checkboxs.....
280     #
281     # We export a "categoryloop" array to the template, each element of which
282     # contains separate 'categoryname', 'categorycode', 'codedescription', and
283     # 'checked' fields. The $checked field is either '' or 'checked'
284     # (see bug 130)
285     #
286     my $catinfo = GetBranchCategory();
287     my $catcheckbox;
288
289     #    print DEBUG "catinfo=".cvs($catinfo)."\n";
290     my @categoryloop = ();
291     foreach my $cat (@$catinfo) {
292         my $checked = "";
293         my $tmp     = quotemeta( $cat->{'categorycode'} );
294         if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
295             $checked = "checked=\"checked\"";
296         }
297         push @categoryloop,
298           {
299             categoryname    => $cat->{'categoryname'},
300             categorycode    => $cat->{'categorycode'},
301             categorytype    => $cat->{'categorytype'},
302             codedescription => $cat->{'codedescription'},
303             checked         => $checked,
304           };
305     }
306     $innertemplate->param( categoryloop => \@categoryloop );
307
308     # {{{ Leave this here until bug 130 is completely resolved in the templates
309     for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
310         $innertemplate->param(
311             $obsolete => 'Your template is out of date (bug 130)' );
312     }
313
314     # }}}
315 }
316
317 sub editcatform {
318
319     # prepares the edit form...
320     my ($categorycode,$innertemplate) = @_;
321     warn "cat : $categorycode";
322     my $data;
323         my @cats;
324     $innertemplate->param( categorytype => \@cats);
325         if ($categorycode) {
326         $data = GetBranchCategory($categorycode);
327         $data = $data->[0];
328         $innertemplate->param(  categorycode    => $data->{'categorycode'} ,
329                                         categoryname    => $data->{'categoryname'},
330                                         codedescription => $data->{'codedescription'} ,
331                                                 );
332     }
333         for my $ctype (GetCategoryTypes()) {
334                 push @cats , { type => $ctype , selected => ($data->{'categorytype'} eq $ctype) };
335         }
336 }
337
338 sub deleteconfirm {
339
340     # message to print if the
341     my ($branchcode) = @_;
342 }
343
344 sub branchinfotable {
345
346 # makes the html for a table of branch info from reference to an array of hashs.
347
348     my ($branchcode,$innertemplate) = @_;
349     my $branchinfo;
350     if ($branchcode) {
351         $branchinfo = GetBranchInfo($branchcode);
352     }
353     else {
354         $branchinfo = GetBranchInfo();
355     }
356     my $toggle;
357     my $i = 0;
358     my @loop_data = ();
359     foreach my $branch (@$branchinfo) {
360         ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
361
362         #
363         # We export the following fields to the template. These are not
364         # pre-composed as a single "address" field because the template
365         # might (and should) escape what is exported here. (See bug 180)
366         #
367         # - color
368         # - branch_name     (Note: not "branchname")
369         # - branch_code     (Note: not "branchcode")
370         # - address         (containing a static error message)
371         # - branchaddress1 \
372         # - branchaddress2  |
373         # - branchaddress3  | comprising the old "address" field
374         # - branchphone     |
375         # - branchfax       |
376         # - branchemail    /
377         # - address-empty-p (1 if no address information, 0 otherwise)
378         # - categories      (containing a static error message)
379         # - category_list   (loop containing "categoryname")
380         # - no-categories-p (1 if no categories set, 0 otherwise)
381         # - value
382         # - action
383         #
384         my %row = ();
385
386         # Handle address fields separately
387         my $address_empty_p = 1;
388         for my $field (
389             'branchaddress1', 'branchaddress2',
390             'branchaddress3', 'branchphone',
391             'branchfax',      'branchemail',
392             'branchip',       'branchprinter'
393           )
394         {
395             $row{$field} = $branch->{$field};
396             if ( $branch->{$field} ) {
397                 $address_empty_p = 0;
398             }
399         }
400         $row{'address-empty-p'} = $address_empty_p;
401
402         # {{{ Leave this here until bug 180 is completely resolved in templates
403         $row{'address'} = 'Your template is out of date (see bug 180)';
404
405         # }}}
406
407         # Handle categories
408         my $no_categories_p = 1;
409         my @categories;
410         foreach my $cat ( @{ $branch->{'categories'} } ) {
411             my ($catinfo) = @{ GetBranchCategory($cat) };
412             push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
413             $no_categories_p = 0;
414         }
415
416         # {{{ Leave this here until bug 180 is completely resolved in templates
417         $row{'categories'} = 'Your template is out of date (see bug 180)';
418
419         # }}}
420         $row{'category_list'}   = \@categories;
421         $row{'no-categories-p'} = $no_categories_p;
422
423         # Handle all other fields
424         $row{'branch_name'} = $branch->{'branchname'};
425         $row{'branch_code'} = $branch->{'branchcode'};
426         $row{'toggle'}      = $toggle;
427         $row{'value'}       = $branch->{'branchcode'};
428         $row{'action'}      = '/cgi-bin/koha/admin/branches.pl';
429
430         push @loop_data, {%row};
431         $i++;
432     }
433     my @branchcategories = ();
434         for my $ctype ( GetCategoryTypes() ) {
435         my $catinfo = GetBranchCategories(undef,$ctype);
436         my @categories;
437                 foreach my $cat (@$catinfo) {
438                 push @categories,
439                   {
440                     categoryname    => $cat->{'categoryname'},
441                     categorycode    => $cat->{'categorycode'},
442                     codedescription => $cat->{'codedescription'},
443                     categorytype => $cat->{'categorytype'},
444                   };
445         }
446         push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
447         }
448     $innertemplate->param(
449         branches         => \@loop_data,
450         branchcategories => \@branchcategories
451     );
452
453 }
454
455 # FIXME logic seems wrong   ##  sub is not used.
456 sub branchcategoriestable {
457     my $innertemplate = shift;
458     #Needs to be implemented...
459
460     my $categoryinfo = GetBranchCategory();
461     my $color;
462     foreach my $cat (@$categoryinfo) {
463         $innertemplate->param( categoryname    => $cat->{'categoryname'} );
464         $innertemplate->param( categorycode    => $cat->{'categorycode'} );
465         $innertemplate->param( codedescription => $cat->{'codedescription'} );
466     }
467 }
468
469 output_html_with_http_headers $input, $cookie, $template->output;
470
471 # Local Variables:
472 # tab-width: 8
473 # End: