Alter auth_subfield_structure table as well incase somebody uses thisprior to updatin...
[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}->{'printername'},
208                         );
209                 push @printerloop, \%row;
210                 }
211                 
212                 $template->param(printerloop => \@printerloop );
213                 $template->param(branchcode => $data->{'branchcode'});
214                 $template->param(branchname => $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 {
224 # on add new branch mode, simple scrolling list
225                 $CGIprinter=CGI::scrolling_list( -name     => 'branchprinter',
226                 -id => 'branchprinter',
227                 -values   => \@printerloop,
228                 -size     => 1,
229                 -multiple => 0 );       
230
231         }
232 #  sending the cgiprinter to the template
233 #               $template->param(printerloop => $CGIprinter);
234
235     # make the checkboxs.....
236     #
237     # We export a "categoryloop" array to the template, each element of which
238     # contains separate 'categoryname', 'categorycode', 'codedescription', and
239     # 'checked' fields. The $checked field is either '' or 'checked'
240     # (see bug 130)
241     #
242     my $catinfo = getcategoryinfo();
243     my $catcheckbox;
244 #    print DEBUG "catinfo=".cvs($catinfo)."\n";
245     my @categoryloop = ();
246     foreach my $cat (@$catinfo) {
247         my $checked = "";
248         my $tmp = quotemeta($cat->{'categorycode'});
249         if (grep {/^$tmp$/} @{$data->{'categories'}}) {
250                 $checked = "checked=\"checked\"";
251         }
252         push @categoryloop, {
253                 categoryname    => $cat->{'categoryname'},
254                 categorycode    => $cat->{'categorycode'},
255                 codedescription => $cat->{'codedescription'},
256                 checked         => $checked,
257             };
258         }
259         $template->param(categoryloop => \@categoryloop);
260
261     # {{{ Leave this here until bug 130 is completely resolved in the templates
262         for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
263                 $template->param($obsolete => 'Your template is out of date (bug 130)');
264         }
265     # }}}
266 }
267
268 sub editcatform {
269         # prepares the edit form...
270         my ($categorycode) = @_;
271         warn "cat : $categorycode";
272         my $data;
273         if ($categorycode) {
274                 $data = getcategoryinfo($categorycode);
275                 $data = $data->[0];
276                 $template->param(categorycode => $data->{'categorycode'});
277                 $template->param(categoryname => $data->{'categoryname'});
278                 $template->param(codedescription => $data->{'codedescription'});
279     }
280 }
281
282 sub deleteconfirm {
283 # message to print if the
284     my ($branchcode) = @_;
285 }
286
287
288 sub branchinfotable {
289 # makes the html for a table of branch info from reference to an array of hashs.
290
291         my ($branchcode) = @_;
292         my $branchinfo;
293         if ($branchcode) {
294                 $branchinfo = getbranchinfo($branchcode);
295         } else {
296                 $branchinfo = getbranchinfo();
297         }
298         my $toggle;
299         my $i;
300         my @loop_data =();
301         foreach my $branch (@$branchinfo) {
302                 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
303                 #
304                 # We export the following fields to the template. These are not
305                 # pre-composed as a single "address" field because the template
306                 # might (and should) escape what is exported here. (See bug 180)
307                 #
308                 # - color
309                 # - branch_name     (Note: not "branchname")
310                 # - branch_code     (Note: not "branchcode")
311                 # - address         (containing a static error message)
312                 # - branchaddress1 \
313                 # - branchaddress2  |
314                 # - branchaddress3  | comprising the old "address" field
315                 # - branchphone     |
316                 # - branchfax       |
317                 # - branchemail    /
318                 # - address-empty-p (1 if no address information, 0 otherwise)
319                 # - categories      (containing a static error message)
320                 # - category_list   (loop containing "categoryname")
321                 # - no-categories-p (1 if no categories set, 0 otherwise)
322                 # - value
323                 # - action
324                 #
325                 my %row = ();
326
327                 # Handle address fields separately
328                 my $address_empty_p = 1;
329                 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
330                         'branchphone', 'branchfax', 'branchemail', 'branchip', 'branchprinter') {
331                         $row{$field} = $branch->{$field};
332                         if ( $branch->{$field} ) {
333                                 $address_empty_p = 0;
334                         }
335                 }
336                 $row{'address-empty-p'} = $address_empty_p;
337                 # {{{ Leave this here until bug 180 is completely resolved in templates
338                 $row{'address'} = 'Your template is out of date (see bug 180)';
339                 # }}}
340
341                 # Handle categories
342                 my $no_categories_p = 1;
343                 my @categories = '';
344                 foreach my $cat (@{$branch->{'categories'}}) {
345                         my ($catinfo) = @{getcategoryinfo($cat)};
346                         push @categories, {'categoryname' => $catinfo->{'categoryname'}};
347                         $no_categories_p = 0;
348                 }
349                 # {{{ Leave this here until bug 180 is completely resolved in templates
350                 $row{'categories'} = 'Your template is out of date (see bug 180)';
351                 # }}}
352                 $row{'category_list'} = \@categories;
353                 $row{'no-categories-p'} = $no_categories_p;
354
355                 # Handle all other fields
356                 $row{'branch_name'} = $branch->{'branchname'};
357                 $row{'branch_code'} = $branch->{'branchcode'};
358                 $row{'toggle'} = $toggle;
359                 $row{'value'} = $branch->{'branchcode'};
360                 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
361
362                 push @loop_data, { %row };
363                 $i++;
364         }
365         my @branchcategories =();
366         my $catinfo = getcategoryinfo();
367         my $toggle;
368         my $i = 0;
369         foreach my $cat (@$catinfo) {
370                 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
371                 push @branchcategories, {
372                         toggle => $toggle,
373                         categoryname    => $cat->{'categoryname'},
374                         categorycode    => $cat->{'categorycode'},
375                         codedescription => $cat->{'codedescription'},
376                 };
377                 $i++;
378         } 
379
380         $template->param(branches => \@loop_data,
381                                                         branchcategories => \@branchcategories);
382
383 }
384
385 # FIXME logic seems wrong
386 sub branchcategoriestable {
387 #Needs to be implemented...
388
389     my $categoryinfo = getcategoryinfo();
390     my $color;
391     foreach my $cat (@$categoryinfo) {
392         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
393         $template->param(color => $color);
394         $template->param(categoryname => $cat->{'categoryname'});
395         $template->param(categorycode => $cat->{'categorycode'});
396         $template->param(codedescription => $cat->{'codedescription'});
397     }
398 }
399
400 ######################################################################################################
401 #
402 # Database functions....
403
404 sub getbranchinfo {
405 # returns a reference to an array of hashes containing branches,
406
407     my ($branchcode) = @_;
408     my $dbh = C4::Context->dbh;
409     my $sth;
410     if ($branchcode) {
411                 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
412                 $sth->execute($branchcode);
413     } else {
414                 $sth = $dbh->prepare("Select * from branches order by branchcode");
415                 $sth->execute();
416     }
417     my @results;
418     while (my $data = $sth->fetchrow_hashref) {
419         my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
420         $nsth->execute($data->{'branchcode'});;
421         my @cats = ();
422         while (my ($cat) = $nsth->fetchrow_array) {
423             push(@cats, $cat);
424         }
425         $nsth->finish;
426         $data->{'categories'} = \@cats;
427         push(@results, $data);
428     }
429     $sth->finish;
430     return \@results;
431 }
432
433 # FIXME This doesn't belong here; it should be moved into a module
434 sub getcategoryinfo {
435 # returns a reference to an array of hashes containing branches,
436         my ($catcode) = @_;
437         my $dbh = C4::Context->dbh;
438         my $sth;
439         #    print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
440         if ($catcode) {
441                 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
442                 $sth->execute($catcode);
443         } else {
444                 $sth = $dbh->prepare("Select * from branchcategories");
445                 $sth->execute();
446         }
447         my @results;
448         while (my $data = $sth->fetchrow_hashref) {
449                 push(@results, $data);
450         }
451         $sth->finish;
452         #    print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
453         return \@results;
454 }
455
456 # FIXME This doesn't belong here; it should be moved into a module
457 sub setbranchinfo {
458 # sets the data from the editbranch form, and writes to the database...
459         my ($data) = @_;
460         my $dbh = C4::Context->dbh;
461         my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail,branchip,branchprinter) values (?,?,?,?,?,?,?,?,?,?)");
462         $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
463                 $data->{'branchaddress1'}, $data->{'branchaddress2'},
464                 $data->{'branchaddress3'}, $data->{'branchphone'},
465                 $data->{'branchfax'}, $data->{'branchemail'}, $data->{'branchip'},$data->{'branchprinter'});
466
467         $sth->finish;
468         # sort out the categories....
469         my @checkedcats;
470         my $cats = getcategoryinfo();
471         foreach my $cat (@$cats) {
472                 my $code = $cat->{'categorycode'};
473                 if ($data->{$code}) {
474                         push(@checkedcats, $code);
475                 }
476         }
477         my $branchcode =uc($data->{'branchcode'});
478         my $branch = getbranchinfo($branchcode);
479         $branch = $branch->[0];
480         my $branchcats = $branch->{'categories'};
481         my @addcats;
482         my @removecats;
483         foreach my $bcat (@$branchcats) {
484                 unless (grep {/^$bcat$/} @checkedcats) {
485                         push(@removecats, $bcat);
486                 }
487         }
488         foreach my $ccat (@checkedcats){
489                 unless (grep {/^$ccat$/} @$branchcats) {
490                         push(@addcats, $ccat);
491                 }
492         }
493         foreach my $cat (@addcats) {
494                 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
495                 $sth->execute($branchcode, $cat);
496                 $sth->finish;
497         }
498         foreach my $cat (@removecats) {
499                 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
500                 $sth->execute($branchcode, $cat);
501                 $sth->finish;
502         }
503 }
504
505 sub deletebranch {
506 # delete branch...
507     my ($branchcode) = @_;
508     my $dbh = C4::Context->dbh;
509     my $sth=$dbh->prepare("delete from branches where branchcode = ?");
510     $sth->execute($branchcode);
511     $sth->finish;
512 }
513
514 sub setcategoryinfo {
515 # sets the data from the editbranch form, and writes to the database...
516         my ($data) = @_;
517         my $dbh = C4::Context->dbh;
518         my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
519         $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
520
521         $sth->finish;
522 }
523 sub deletecategory {
524 # delete branch...
525     my ($categorycode) = @_;
526     my $dbh = C4::Context->dbh;
527     my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
528     $sth->execute($categorycode);
529     $sth->finish;
530 }
531
532 sub checkdatabasefor {
533 # check to see if the branchcode is being used in the database somewhere....
534     my ($branchcode) = @_;
535     my $dbh = C4::Context->dbh;
536     my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
537     $sth->execute($branchcode, $branchcode);
538     my ($total) = $sth->fetchrow_array;
539     $sth->finish;
540     my $message;
541     if ($total) {
542         # FIXME: need to be replaced by an exported boolean parameter
543         $message = "MESSAGE7";
544     }
545     return $message;
546 }
547
548 sub checkcategorycode {
549 # check to see if the branchcode is being used in the database somewhere....
550     my ($categorycode) = @_;
551     my $dbh = C4::Context->dbh;
552     my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
553     $sth->execute($categorycode);
554     my ($total) = $sth->fetchrow_array;
555     $sth->finish;
556     my $message;
557     if ($total) {
558         # FIXME: need to be replaced by an exported boolean parameter
559         $message = "Category cannot be deleted because there are $total branches using that category.";
560     }
561     return $message;
562 }
563
564 output_html_with_http_headers $input, $cookie, $template->output;
565
566 # Local Variables:
567 # tab-width: 8
568 # End: