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