German and Turkish updates
[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
235             my $selected = 1
236               if $oldprinter and ( $oldprinter eq $printers->{$thisprinter} );
237
238             my %row = (
239                 value         => $thisprinter,
240                 selected      => $selected,
241                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
242             );
243             push @printerloop, \%row;
244         }
245
246         $innertemplate->param( 
247              printerloop    => \@printerloop,
248              branchcode     => $data->{'branchcode'},
249              branch_name    => $data->{'branchname'},
250              branchaddress1 => $data->{'branchaddress1'},
251              branchaddress2 => $data->{'branchaddress2'},
252              branchaddress3 => $data->{'branchaddress3'},
253              branchphone    => $data->{'branchphone'},
254              branchfax      => $data->{'branchfax'},
255              branchemail    => $data->{'branchemail'},
256              branchip       => $data->{'branchip'} 
257         );
258     }
259     else {    #case of an add branch select printer
260         foreach my $thisprinter ( keys %$printers ) {
261             my %row = (
262                 value         => $thisprinter,
263                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
264             );
265             push @printerloop, \%row;
266         }
267         $innertemplate->param( printerloop => \@printerloop );
268     }
269
270     # make the checkboxs.....
271     #
272     # We export a "categoryloop" array to the template, each element of which
273     # contains separate 'categoryname', 'categorycode', 'codedescription', and
274     # 'checked' fields. The $checked field is either '' or 'checked'
275     # (see bug 130)
276     #
277     my $catinfo = GetBranchCategory();
278     my $catcheckbox;
279
280     #    print DEBUG "catinfo=".cvs($catinfo)."\n";
281     my @categoryloop = ();
282     foreach my $cat (@$catinfo) {
283         my $checked = "";
284         my $tmp     = quotemeta( $cat->{'categorycode'} );
285         if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
286             $checked = "checked=\"checked\"";
287         }
288         push @categoryloop,
289           {
290             categoryname    => $cat->{'categoryname'},
291             categorycode    => $cat->{'categorycode'},
292             categorytype    => $cat->{'categorytype'},
293             codedescription => $cat->{'codedescription'},
294             checked         => $checked,
295           };
296     }
297     $innertemplate->param( categoryloop => \@categoryloop );
298
299     # {{{ Leave this here until bug 130 is completely resolved in the templates
300     for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
301         $innertemplate->param(
302             $obsolete => 'Your template is out of date (bug 130)' );
303     }
304
305     # }}}
306 }
307
308 sub editcatform {
309
310     # prepares the edit form...
311     my ($categorycode,$innertemplate) = @_;
312     warn "cat : $categorycode";
313     my $data;
314         my @cats;
315     $innertemplate->param( categorytype => \@cats);
316         if ($categorycode) {
317         $data = GetBranchCategory($categorycode);
318         $data = $data->[0];
319         $innertemplate->param(  categorycode    => $data->{'categorycode'} ,
320                                         categoryname    => $data->{'categoryname'},
321                                         codedescription => $data->{'codedescription'} ,
322                                                 );
323     }
324         for my $ctype (GetCategoryTypes()) {
325                 push @cats , { type => $ctype , selected => ($data->{'categorytype'} eq $ctype) };
326         }
327 }
328
329 sub deleteconfirm {
330
331     # message to print if the
332     my ($branchcode) = @_;
333 }
334
335 sub branchinfotable {
336
337 # makes the html for a table of branch info from reference to an array of hashs.
338
339     my ($branchcode,$innertemplate) = @_;
340     my $branchinfo;
341     if ($branchcode) {
342         $branchinfo = GetBranchInfo($branchcode);
343     }
344     else {
345         $branchinfo = GetBranchInfo();
346     }
347     my $toggle;
348     my $i = 0;
349     my @loop_data = ();
350     foreach my $branch (@$branchinfo) {
351         ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
352
353         #
354         # We export the following fields to the template. These are not
355         # pre-composed as a single "address" field because the template
356         # might (and should) escape what is exported here. (See bug 180)
357         #
358         # - color
359         # - branch_name     (Note: not "branchname")
360         # - branch_code     (Note: not "branchcode")
361         # - address         (containing a static error message)
362         # - branchaddress1 \
363         # - branchaddress2  |
364         # - branchaddress3  | comprising the old "address" field
365         # - branchphone     |
366         # - branchfax       |
367         # - branchemail    /
368         # - address-empty-p (1 if no address information, 0 otherwise)
369         # - categories      (containing a static error message)
370         # - category_list   (loop containing "categoryname")
371         # - no-categories-p (1 if no categories set, 0 otherwise)
372         # - value
373         # - action
374         #
375         my %row = ();
376
377         # Handle address fields separately
378         my $address_empty_p = 1;
379         for my $field (
380             'branchaddress1', 'branchaddress2',
381             'branchaddress3', 'branchphone',
382             'branchfax',      'branchemail',
383             'branchip',       'branchprinter'
384           )
385         {
386             $row{$field} = $branch->{$field};
387             if ( $branch->{$field} ) {
388                 $address_empty_p = 0;
389             }
390         }
391         $row{'address-empty-p'} = $address_empty_p;
392
393         # {{{ Leave this here until bug 180 is completely resolved in templates
394         $row{'address'} = 'Your template is out of date (see bug 180)';
395
396         # }}}
397
398         # Handle categories
399         my $no_categories_p = 1;
400         my @categories;
401         foreach my $cat ( @{ $branch->{'categories'} } ) {
402             my ($catinfo) = @{ GetBranchCategory($cat) };
403             push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
404             $no_categories_p = 0;
405         }
406
407         # {{{ Leave this here until bug 180 is completely resolved in templates
408         $row{'categories'} = 'Your template is out of date (see bug 180)';
409
410         # }}}
411         $row{'category_list'}   = \@categories;
412         $row{'no-categories-p'} = $no_categories_p;
413
414         # Handle all other fields
415         $row{'branch_name'} = $branch->{'branchname'};
416         $row{'branch_code'} = $branch->{'branchcode'};
417         $row{'toggle'}      = $toggle;
418         $row{'value'}       = $branch->{'branchcode'};
419         $row{'action'}      = '/cgi-bin/koha/admin/branches.pl';
420
421         push @loop_data, {%row};
422         $i++;
423     }
424     my @branchcategories = ();
425         for my $ctype ( GetCategoryTypes() ) {
426         my $catinfo = GetBranchCategories(undef,$ctype);
427         my @categories;
428                 foreach my $cat (@$catinfo) {
429                 push @categories,
430                   {
431                     categoryname    => $cat->{'categoryname'},
432                     categorycode    => $cat->{'categorycode'},
433                     codedescription => $cat->{'codedescription'},
434                     categorytype => $cat->{'categorytype'},
435                   };
436         }
437         push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
438         }
439     $innertemplate->param(
440         branches         => \@loop_data,
441         branchcategories => \@branchcategories
442     );
443
444 }
445
446 # FIXME logic seems wrong   ##  sub is not used.
447 sub branchcategoriestable {
448     my $innertemplate = shift;
449     #Needs to be implemented...
450
451     my $categoryinfo = GetBranchCategory();
452     my $color;
453     foreach my $cat (@$categoryinfo) {
454         $innertemplate->param( categoryname    => $cat->{'categoryname'} );
455         $innertemplate->param( categorycode    => $cat->{'categorycode'} );
456         $innertemplate->param( codedescription => $cat->{'codedescription'} );
457     }
458 }
459
460 output_html_with_http_headers $input, $cookie, $template->output;
461
462 # Local Variables:
463 # tab-width: 8
464 # End: