Head & rel_2_2 merged
[koha.git] / admin / branches.pl
1 #!/usr/bin/perl
2 # NOTE: Use standard 8-space tabs for this file (indents are 4 spaces)
3
4 #require '/u/acli/lib/cvs.pl';#DEBUG
5 #open(DEBUG,'>/tmp/koha.debug');
6
7 # FIXME: individual fields in branch address need to be exported to templates,
8 #        in order to fix bug 180; need to notify translators
9 # FIXME: looped html (e.g., list of checkboxes) need to be properly
10 #        TMPL_LOOP'ized; doing this properly will fix bug 130; need to
11 #        notify translators
12 # FIXME: need to implement the branch categories stuff
13 # FIXME: there are too many TMPL_IF's; the proper way to do it is to have
14 #        separate templates for each individual action; need to notify
15 #        translators
16 # FIXME: there are lots of error messages exported to the template; a lot
17 #        of these should be converted into exported booleans / counters etc
18 #        so that the error messages can be localized; need to notify translators
19 #
20 # NOTE:  heading() should now be called like this:
21 #        1. Use heading() as before
22 #        2. $template->param('heading-LISPISHIZED-HEADING-p' => 1);
23 #        3. $template->param('use-heading-flags-p' => 1);
24 #        This ensures that both converted and unconverted templates work
25
26 # Finlay working on this file from 26-03-2002
27 # Reorganising this branches admin page.....
28
29
30 # Copyright 2000-2002 Katipo Communications
31 #
32 # This file is part of Koha.
33 #
34 # Koha is free software; you can redistribute it and/or modify it under the
35 # terms of the GNU General Public License as published by the Free Software
36 # Foundation; either version 2 of the License, or (at your option) any later
37 # version.
38 #
39 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
40 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
41 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
42 #
43 # You should have received a copy of the GNU General Public License along with
44 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
45 # Suite 330, Boston, MA  02111-1307 USA
46
47 use strict;
48 use CGI;
49 use C4::Auth;
50 use C4::Context;
51 use C4::Output;
52 use C4::Interface::CGI::Output;
53 use HTML::Template;
54 use C4::Koha;
55
56 # Fixed variables
57 my $linecolor1='#ffffcc';
58 my $linecolor2='white';
59 my $backgroundimage="/images/background-mem.gif";
60 my $script_name="/cgi-bin/koha/admin/branches.pl";
61 my $pagesize=20;
62
63
64 #######################################################################################
65 # Main loop....
66 my $input = new CGI;
67 my $branchcode=$input->param('branchcode');
68 my $branchname=$input->param('branchname');
69 my $categorycode = $input->param('categorycode');
70 my $op = $input->param('op');
71
72 my ($template, $borrowernumber, $cookie)
73     = get_template_and_user({template_name => "admin/branches.tmpl",
74                              query => $input,
75                              type => "intranet",
76                              authnotrequired => 0,
77                              flagsrequired => {parameters => 1, management => 1},
78                              debug => 1,
79                              });
80 if ($op) {
81         $template->param(script_name => $script_name,
82                                 $op         => 1); # we show only the TMPL_VAR names $op
83 } else {
84         $template->param(script_name => $script_name,
85                                 else        => 1); # we show only the TMPL_VAR names $op
86 }
87 $template->param(action => $script_name);
88 if ($op eq 'add') {
89         # If the user has pressed the "add new branch" button.
90         heading("Branches: Add Branch");
91         $template->param('heading-branches-add-branch-p' => 1);
92         editbranchform();
93
94 } elsif ($op eq 'edit') {
95         # if the user has pressed the "edit branch settings" button.
96         heading("Branches: Edit Branch");
97         $template->param('heading-branches-edit-branch-p' => 1);
98         $template->param(add => 1);
99         editbranchform($branchcode);
100 } elsif ($op eq 'add_validate') {
101         # confirm settings change...
102         my $params = $input->Vars;
103         unless ($params->{'branchcode'} && $params->{'branchname'}) {
104                 $template->param(else => 1);
105                 default ("MESSAGE1");
106         } else {
107                 setbranchinfo($params);
108                 $template->param(else => 1);
109                 default ("MESSAGE2");
110         }
111 } elsif ($op eq 'delete') {
112         # if the user has pressed the "delete branch" button.
113         my $message = checkdatabasefor($branchcode);
114         if ($message) {
115                 $template->param(else => 1);
116                 default($message);
117         } else {
118                 $template->param(branchname => $branchname);
119                 $template->param(delete_confirm => 1);
120                 $template->param(branchcode => $branchcode);
121         }
122 } elsif ($op eq 'delete_confirmed') {
123         # actually delete branch and return to the main screen....
124         deletebranch($branchcode);
125         $template->param(else => 1);
126         default("MESSAGE3");
127 } elsif ($op eq 'editcategory') {
128         # If the user has pressed the "add new category" or "modify" buttons.
129         $template->param('heading-branches-edit-category-p' => 1);
130         editcatform($categorycode);
131 } elsif ($op eq 'addcategory_validate') {
132         # confirm settings change...
133         my $params = $input->Vars;
134         unless ($params->{'categorycode'} && $params->{'categoryname'}) {
135                 $template->param(else => 1);
136                 default ("MESSAGE4");
137         } else {
138                 setcategoryinfo($params);
139                 $template->param(else => 1);
140                 default ("MESSAGE5");
141         }
142 } elsif ($op eq 'delete_category') {
143         # if the user has pressed the "delete branch" button.
144         my $message = checkcategorycode($categorycode);
145         if ($message) {
146                 $template->param(else => 1);
147                 default($message);
148         } else {
149                 $template->param(delete_category => 1);
150                 $template->param(categorycode => $categorycode);
151         }
152 } elsif ($op eq 'categorydelete_confirmed') {
153         # actually delete branch and return to the main screen....
154         deletecategory($categorycode);
155         $template->param(else => 1);
156         default("MESSAGE6");
157
158 } else {
159         # if no operation has been set...
160         default();
161 }
162
163
164
165 ######################################################################################################
166 #
167 # html output functions....
168
169 sub default {
170         my ($message) = @_;
171         heading("Branches");
172         $template->param('heading-branches-p' => 1);
173         $template->param("$message" => 1);
174         $template->param(action => $script_name);
175         branchinfotable();
176 }
177
178 # FIXME: this function should not exist; otherwise headings are untranslatable
179 sub heading {
180         my ($head) = @_;
181         $template->param(head => $head);
182 }
183
184 sub editbranchform {
185         # prepares the edit form...
186
187 # initiate the scrolling-list to select the printers
188         my %env;
189         my $printers=getprinters(\%env);
190         my @printerloop;
191         my $printercount=0;
192         my $oldprinter;
193         my $CGIprinter;
194         my ($branchcode) = @_;
195         my $data;
196         
197         if ($branchcode) {
198                 $data = getbranchinfo($branchcode);
199                 $data = $data->[0];
200         # get the old printer of the branch
201                 $oldprinter = $data->{'branchprinter'}; 
202 #       printer loop
203                 foreach my $thisprinter (keys %$printers) {
204                         my $selected = 1 if $oldprinter eq $printers->{$thisprinter}->{'printqueue'};
205                         my %row =(value => $thisprinter,
206                                         selected => $selected,
207                                         branchprinter => $printers->{$thisprinter}->{'printqueue'},
208                         );
209                 push @printerloop, \%row;
210                 }
211                 
212                 $template->param(printerloop => \@printerloop );
213                 $template->param(branchcode => $data->{'branchcode'});
214                 $template->param(branch_name => $data->{'branchname'});
215                 $template->param(branchaddress1 => $data->{'branchaddress1'});
216                 $template->param(branchaddress2 => $data->{'branchaddress2'});
217                 $template->param(branchaddress3 => $data->{'branchaddress3'});
218                 $template->param(branchphone => $data->{'branchphone'});
219                 $template->param(branchfax => $data->{'branchfax'});
220                 $template->param(branchemail => $data->{'branchemail'});
221                 $template->param(branchip => $data->{'branchip'});
222         }
223         else { #case of an add branch select printer
224                 foreach my $thisprinter (keys %$printers) {
225                         my %row =(value => $thisprinter,
226                                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
227                         );
228                 push @printerloop, \%row;
229                 }
230         $template->param(printerloop => \@printerloop );        
231         }
232
233     # make the checkboxs.....
234     #
235     # We export a "categoryloop" array to the template, each element of which
236     # contains separate 'categoryname', 'categorycode', 'codedescription', and
237     # 'checked' fields. The $checked field is either '' or 'checked'
238     # (see bug 130)
239     #
240     my $catinfo = getcategoryinfo();
241     my $catcheckbox;
242 #    print DEBUG "catinfo=".cvs($catinfo)."\n";
243     my @categoryloop = ();
244     foreach my $cat (@$catinfo) {
245         my $checked = "";
246         my $tmp = quotemeta($cat->{'categorycode'});
247         if (grep {/^$tmp$/} @{$data->{'categories'}}) {
248                 $checked = "checked=\"checked\"";
249         }
250         push @categoryloop, {
251                 categoryname    => $cat->{'categoryname'},
252                 categorycode    => $cat->{'categorycode'},
253                 codedescription => $cat->{'codedescription'},
254                 checked         => $checked,
255             };
256         }
257         $template->param(categoryloop => \@categoryloop);
258
259     # {{{ Leave this here until bug 130 is completely resolved in the templates
260         for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
261                 $template->param($obsolete => 'Your template is out of date (bug 130)');
262         }
263     # }}}
264 }
265
266 sub editcatform {
267         # prepares the edit form...
268         my ($categorycode) = @_;
269         warn "cat : $categorycode";
270         my $data;
271         if ($categorycode) {
272                 $data = getcategoryinfo($categorycode);
273                 $data = $data->[0];
274                 $template->param(categorycode => $data->{'categorycode'});
275                 $template->param(categoryname => $data->{'categoryname'});
276                 $template->param(codedescription => $data->{'codedescription'});
277     }
278 }
279
280 sub deleteconfirm {
281 # message to print if the
282     my ($branchcode) = @_;
283 }
284
285
286 sub branchinfotable {
287 # makes the html for a table of branch info from reference to an array of hashs.
288
289         my ($branchcode) = @_;
290         my $branchinfo;
291         if ($branchcode) {
292                 $branchinfo = getbranchinfo($branchcode);
293         } else {
294                 $branchinfo = getbranchinfo();
295         }
296         my $toggle;
297         my $i;
298         my @loop_data =();
299         foreach my $branch (@$branchinfo) {
300                 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
301                 #
302                 # We export the following fields to the template. These are not
303                 # pre-composed as a single "address" field because the template
304                 # might (and should) escape what is exported here. (See bug 180)
305                 #
306                 # - color
307                 # - branch_name     (Note: not "branchname")
308                 # - branch_code     (Note: not "branchcode")
309                 # - address         (containing a static error message)
310                 # - branchaddress1 \
311                 # - branchaddress2  |
312                 # - branchaddress3  | comprising the old "address" field
313                 # - branchphone     |
314                 # - branchfax       |
315                 # - branchemail    /
316                 # - address-empty-p (1 if no address information, 0 otherwise)
317                 # - categories      (containing a static error message)
318                 # - category_list   (loop containing "categoryname")
319                 # - no-categories-p (1 if no categories set, 0 otherwise)
320                 # - value
321                 # - action
322                 #
323                 my %row = ();
324
325                 # Handle address fields separately
326                 my $address_empty_p = 1;
327                 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
328                         'branchphone', 'branchfax', 'branchemail', 'branchip', 'branchprinter') {
329                         $row{$field} = $branch->{$field};
330                         if ( $branch->{$field} ) {
331                                 $address_empty_p = 0;
332                         }
333                 }
334                 $row{'address-empty-p'} = $address_empty_p;
335                 # {{{ Leave this here until bug 180 is completely resolved in templates
336                 $row{'address'} = 'Your template is out of date (see bug 180)';
337                 # }}}
338
339                 # Handle categories
340                 my $no_categories_p = 1;
341                 my @categories = '';
342                 foreach my $cat (@{$branch->{'categories'}}) {
343                         my ($catinfo) = @{getcategoryinfo($cat)};
344                         push @categories, {'categoryname' => $catinfo->{'categoryname'}};
345                         $no_categories_p = 0;
346                 }
347                 # {{{ Leave this here until bug 180 is completely resolved in templates
348                 $row{'categories'} = 'Your template is out of date (see bug 180)';
349                 # }}}
350                 $row{'category_list'} = \@categories;
351                 $row{'no-categories-p'} = $no_categories_p;
352
353                 # Handle all other fields
354                 $row{'branch_name'} = $branch->{'branchname'};
355                 $row{'branch_code'} = $branch->{'branchcode'};
356                 $row{'toggle'} = $toggle;
357                 $row{'value'} = $branch->{'branchcode'};
358                 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
359
360                 push @loop_data, { %row };
361                 $i++;
362         }
363         my @branchcategories =();
364         my $catinfo = getcategoryinfo();
365         my $toggle;
366         my $i = 0;
367         foreach my $cat (@$catinfo) {
368                 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
369                 push @branchcategories, {
370                         toggle => $toggle,
371                         categoryname    => $cat->{'categoryname'},
372                         categorycode    => $cat->{'categorycode'},
373                         codedescription => $cat->{'codedescription'},
374                 };
375                 $i++;
376         } 
377
378         $template->param(branches => \@loop_data,
379                                                         branchcategories => \@branchcategories);
380
381 }
382
383 # FIXME logic seems wrong
384 sub branchcategoriestable {
385 #Needs to be implemented...
386
387     my $categoryinfo = getcategoryinfo();
388     my $color;
389     foreach my $cat (@$categoryinfo) {
390         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
391         $template->param(color => $color);
392         $template->param(categoryname => $cat->{'categoryname'});
393         $template->param(categorycode => $cat->{'categorycode'});
394         $template->param(codedescription => $cat->{'codedescription'});
395     }
396 }
397
398 ######################################################################################################
399 #
400 # Database functions....
401
402 sub getbranchinfo {
403 # returns a reference to an array of hashes containing branches,
404
405     my ($branchcode) = @_;
406     my $dbh = C4::Context->dbh;
407     my $sth;
408     if ($branchcode) {
409                 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
410                 $sth->execute($branchcode);
411     } else {
412                 $sth = $dbh->prepare("Select * from branches order by branchcode");
413                 $sth->execute();
414     }
415     my @results;
416     while (my $data = $sth->fetchrow_hashref) {
417         my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
418         $nsth->execute($data->{'branchcode'});;
419         my @cats = ();
420         while (my ($cat) = $nsth->fetchrow_array) {
421             push(@cats, $cat);
422         }
423         $nsth->finish;
424         $data->{'categories'} = \@cats;
425         push(@results, $data);
426     }
427     $sth->finish;
428     return \@results;
429 }
430
431 # FIXME This doesn't belong here; it should be moved into a module
432 sub getcategoryinfo {
433 # returns a reference to an array of hashes containing branches,
434         my ($catcode) = @_;
435         my $dbh = C4::Context->dbh;
436         my $sth;
437         #    print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
438         if ($catcode) {
439                 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
440                 $sth->execute($catcode);
441         } else {
442                 $sth = $dbh->prepare("Select * from branchcategories");
443                 $sth->execute();
444         }
445         my @results;
446         while (my $data = $sth->fetchrow_hashref) {
447                 push(@results, $data);
448         }
449         $sth->finish;
450         #    print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
451         return \@results;
452 }
453
454 # FIXME This doesn't belong here; it should be moved into a module
455 sub setbranchinfo {
456 # sets the data from the editbranch form, and writes to the database...
457         my ($data) = @_;
458         my $dbh = C4::Context->dbh;
459         my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail,branchip,branchprinter) values (?,?,?,?,?,?,?,?,?,?)");
460         $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
461                 $data->{'branchaddress1'}, $data->{'branchaddress2'},
462                 $data->{'branchaddress3'}, $data->{'branchphone'},
463                 $data->{'branchfax'}, $data->{'branchemail'}, $data->{'branchip'},$data->{'branchprinter'});
464
465         $sth->finish;
466         # sort out the categories....
467         my @checkedcats;
468         my $cats = getcategoryinfo();
469         foreach my $cat (@$cats) {
470                 my $code = $cat->{'categorycode'};
471                 if ($data->{$code}) {
472                         push(@checkedcats, $code);
473                 }
474         }
475         my $branchcode =uc($data->{'branchcode'});
476         my $branch = getbranchinfo($branchcode);
477         $branch = $branch->[0];
478         my $branchcats = $branch->{'categories'};
479         my @addcats;
480         my @removecats;
481         foreach my $bcat (@$branchcats) {
482                 unless (grep {/^$bcat$/} @checkedcats) {
483                         push(@removecats, $bcat);
484                 }
485         }
486         foreach my $ccat (@checkedcats){
487                 unless (grep {/^$ccat$/} @$branchcats) {
488                         push(@addcats, $ccat);
489                 }
490         }
491         foreach my $cat (@addcats) {
492                 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
493                 $sth->execute($branchcode, $cat);
494                 $sth->finish;
495         }
496         foreach my $cat (@removecats) {
497                 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
498                 $sth->execute($branchcode, $cat);
499                 $sth->finish;
500         }
501 }
502
503 sub deletebranch {
504 # delete branch...
505     my ($branchcode) = @_;
506     my $dbh = C4::Context->dbh;
507     my $sth=$dbh->prepare("delete from branches where branchcode = ?");
508     $sth->execute($branchcode);
509     $sth->finish;
510 }
511
512 sub setcategoryinfo {
513 # sets the data from the editbranch form, and writes to the database...
514         my ($data) = @_;
515         my $dbh = C4::Context->dbh;
516         my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
517         $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
518
519         $sth->finish;
520 }
521 sub deletecategory {
522 # delete branch...
523     my ($categorycode) = @_;
524     my $dbh = C4::Context->dbh;
525     my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
526     $sth->execute($categorycode);
527     $sth->finish;
528 }
529
530 sub checkdatabasefor {
531 # check to see if the branchcode is being used in the database somewhere....
532     my ($branchcode) = @_;
533     my $dbh = C4::Context->dbh;
534     my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
535     $sth->execute($branchcode, $branchcode);
536     my ($total) = $sth->fetchrow_array;
537     $sth->finish;
538     my $message;
539     if ($total) {
540         # FIXME: need to be replaced by an exported boolean parameter
541         $message = "MESSAGE7";
542     }
543     return $message;
544 }
545
546 sub checkcategorycode {
547 # check to see if the branchcode is being used in the database somewhere....
548     my ($categorycode) = @_;
549     my $dbh = C4::Context->dbh;
550     my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
551     $sth->execute($categorycode);
552     my ($total) = $sth->fetchrow_array;
553     $sth->finish;
554     my $message;
555     if ($total) {
556         # FIXME: need to be replaced by an exported boolean parameter
557         $message = "Category cannot be deleted because there are $total branches using that category.";
558     }
559     return $message;
560 }
561 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
562                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
563                 IntranetNav => C4::Context->preference("IntranetNav"),
564                 );
565 output_html_with_http_headers $input, $cookie, $template->output;
566
567 # Local Variables:
568 # tab-width: 8
569 # End: