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