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