fixes for notforloan management
[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
55 # Fixed variables
56 my $linecolor1='#ffffcc';
57 my $linecolor2='white';
58 my $backgroundimage="/images/background-mem.gif";
59 my $script_name="/cgi-bin/koha/admin/branches.pl";
60 my $pagesize=20;
61
62
63 #######################################################################################
64 # Main loop....
65 my $input = new CGI;
66 my $branchcode=$input->param('branchcode');
67 my $categorycode = $input->param('categorycode');
68 my $op = $input->param('op');
69
70 my ($template, $borrowernumber, $cookie)
71     = get_template_and_user({template_name => "parameters/branches.tmpl",
72                              query => $input,
73                              type => "intranet",
74                              authnotrequired => 0,
75                              flagsrequired => {parameters => 1},
76                              debug => 1,
77                              });
78 if ($op) {
79         $template->param(script_name => $script_name,
80                                 $op         => 1); # we show only the TMPL_VAR names $op
81 } else {
82         $template->param(script_name => $script_name,
83                                 else        => 1); # we show only the TMPL_VAR names $op
84 }
85 $template->param(action => $script_name);
86 if ($op eq 'add') {
87         # If the user has pressed the "add new branch" button.
88         heading("Branches: Add Branch");
89         $template->param('heading-branches-add-branch-p' => 1);
90         $template->param('use-heading-flags-p' => 1);
91         editbranchform();
92
93 } elsif ($op eq 'edit') {
94         # if the user has pressed the "edit branch settings" button.
95         heading("Branches: Edit Branch");
96         $template->param('heading-branches-edit-branch-p' => 1);
97         $template->param('use-heading-flags-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                 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
105         } else {
106                 setbranchinfo($params);
107                 $template->param(else => 1);
108                 default ("Branch record changed for branch: $params->{'branchname'}");
109         }
110 } elsif ($op eq 'delete') {
111         # if the user has pressed the "delete branch" button.
112         my $message = checkdatabasefor($branchcode);
113         if ($message) {
114                 $template->param(else => 1);
115                 default($message);
116         } else {
117                 $template->param(delete_confirm => 1);
118                 $template->param(branchcode => $branchcode);
119         }
120 } elsif ($op eq 'delete_confirmed') {
121         # actually delete branch and return to the main screen....
122         deletebranch($branchcode);
123         $template->param(else => 1);
124         default("The branch with code $branchcode has been deleted.");
125 } elsif ($op eq 'editcategory') {
126         # If the user has pressed the "add new category" or "modify" buttons.
127         heading("Branches: Edit Category");
128         $template->param('heading-branches-edit-category-p' => 1);
129         $template->param('use-heading-flags-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                 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
136         } else {
137                 setcategoryinfo($params);
138                 $template->param(else => 1);
139                 default ("Category record changed for category $params->{'categoryname'}");
140         }
141 } elsif ($op eq 'delete_category') {
142         # if the user has pressed the "delete branch" button.
143         my $message = checkcategorycode($categorycode);
144         if ($message) {
145                 $template->param(else => 1);
146                 default($message);
147         } else {
148                 $template->param(delete_category => 1);
149                 $template->param(categorycode => $categorycode);
150         }
151 } elsif ($op eq 'categorydelete_confirmed') {
152         # actually delete branch and return to the main screen....
153         deletecategory($categorycode);
154         $template->param(else => 1);
155         default("The category with code $categorycode has been deleted.");
156
157 } else {
158         # if no operation has been set...
159         default();
160 }
161
162
163
164 ######################################################################################################
165 #
166 # html output functions....
167
168 sub default {
169         my ($message) = @_;
170         heading("Branches");
171         $template->param('heading-branches-p' => 1);
172         $template->param('use-heading-flags-p' => 1);
173         $template->param(message => $message);
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         my ($branchcode) = @_;
187         my $data;
188         if ($branchcode) {
189                 $data = getbranchinfo($branchcode);
190                 $data = $data->[0];
191                 $template->param(branchcode => $data->{'branchcode'});
192                 $template->param(branchname => $data->{'branchname'});
193                 $template->param(branchaddress1 => $data->{'branchaddress1'});
194                 $template->param(branchaddress2 => $data->{'branchaddress2'});
195                 $template->param(branchaddress3 => $data->{'branchaddress3'});
196                 $template->param(branchphone => $data->{'branchphone'});
197                 $template->param(branchfax => $data->{'branchfax'});
198                 $template->param(branchemail => $data->{'branchemail'});
199     }
200
201     # make the checkboxs.....
202     #
203     # We export a "categoryloop" array to the template, each element of which
204     # contains separate 'categoryname', 'categorycode', 'codedescription', and
205     # 'checked' fields. The $checked field is either '' or 'checked'
206     # (see bug 130)
207     #
208     my $catinfo = getcategoryinfo();
209     my $catcheckbox;
210 #    print DEBUG "catinfo=".cvs($catinfo)."\n";
211     my @categoryloop = ();
212     foreach my $cat (@$catinfo) {
213         my $checked = "";
214         my $tmp = quotemeta($cat->{'categorycode'});
215         if (grep {/^$tmp$/} @{$data->{'categories'}}) {
216                 $checked = "checked=\"checked\"";
217         }
218         push @categoryloop, {
219                 categoryname    => $cat->{'categoryname'},
220                 categorycode    => $cat->{'categorycode'},
221                 codedescription => $cat->{'codedescription'},
222                 checked         => $checked,
223             };
224         }
225         $template->param(categoryloop => \@categoryloop);
226
227     # {{{ Leave this here until bug 130 is completely resolved in the templates
228         for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
229                 $template->param($obsolete => 'Your template is out of date (bug 130)');
230         }
231     # }}}
232 }
233
234 sub editcatform {
235         # prepares the edit form...
236         my ($categorycode) = @_;
237         warn "cat : $categorycode";
238         my $data;
239         if ($categorycode) {
240                 $data = getcategoryinfo($categorycode);
241                 $data = $data->[0];
242                 $template->param(categorycode => $data->{'categorycode'});
243                 $template->param(categoryname => $data->{'categoryname'});
244                 $template->param(codedescription => $data->{'codedescription'});
245     }
246 }
247
248 sub deleteconfirm {
249 # message to print if the
250     my ($branchcode) = @_;
251 }
252
253
254 sub branchinfotable {
255 # makes the html for a table of branch info from reference to an array of hashs.
256
257         my ($branchcode) = @_;
258         my $branchinfo;
259         if ($branchcode) {
260                 $branchinfo = getbranchinfo($branchcode);
261         } else {
262                 $branchinfo = getbranchinfo();
263         }
264         my $color;
265         my @loop_data =();
266         foreach my $branch (@$branchinfo) {
267                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
268                 #
269                 # We export the following fields to the template. These are not
270                 # pre-composed as a single "address" field because the template
271                 # might (and should) escape what is exported here. (See bug 180)
272                 #
273                 # - color
274                 # - branch_name     (Note: not "branchname")
275                 # - branch_code     (Note: not "branchcode")
276                 # - address         (containing a static error message)
277                 # - branchaddress1 \
278                 # - branchaddress2  |
279                 # - branchaddress3  | comprising the old "address" field
280                 # - branchphone     |
281                 # - branchfax       |
282                 # - branchemail    /
283                 # - address-empty-p (1 if no address information, 0 otherwise)
284                 # - categories      (containing a static error message)
285                 # - category_list   (loop containing "categoryname")
286                 # - no-categories-p (1 if no categories set, 0 otherwise)
287                 # - value
288                 # - action
289                 #
290                 my %row = ();
291
292                 # Handle address fields separately
293                 my $address_empty_p = 1;
294                 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
295                         'branchphone', 'branchfax', 'branchemail') {
296                         $row{$field} = $branch->{$field};
297                         if ( $branch->{$field} ) {
298                                 $address_empty_p = 0;
299                         }
300                 }
301                 $row{'address-empty-p'} = $address_empty_p;
302                 # {{{ Leave this here until bug 180 is completely resolved in templates
303                 $row{'address'} = 'Your template is out of date (see bug 180)';
304                 # }}}
305
306                 # Handle categories
307                 my $no_categories_p = 1;
308                 my @categories = '';
309                 foreach my $cat (@{$branch->{'categories'}}) {
310                         my ($catinfo) = @{getcategoryinfo($cat)};
311                         push @categories, {'categoryname' => $catinfo->{'categoryname'}};
312                         $no_categories_p = 0;
313                 }
314                 # {{{ Leave this here until bug 180 is completely resolved in templates
315                 $row{'categories'} = 'Your template is out of date (see bug 180)';
316                 # }}}
317                 $row{'category_list'} = \@categories;
318                 $row{'no-categories-p'} = $no_categories_p;
319
320                 # Handle all other fields
321                 $row{'branch_name'} = $branch->{'branchname'};
322                 $row{'branch_code'} = $branch->{'branchcode'};
323                 $row{'color'} = $color;
324                 $row{'value'} = $branch->{'branchcode'};
325                 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
326
327                 push @loop_data, { %row };
328         }
329         my @branchcategories =();
330         my $catinfo = getcategoryinfo();
331         foreach my $cat (@$catinfo) {
332                 push @branchcategories, {
333                         categoryname    => $cat->{'categoryname'},
334                         categorycode    => $cat->{'categorycode'},
335                         codedescription => $cat->{'codedescription'},
336                 };
337         }
338
339         $template->param(branches => \@loop_data,
340                                                         branchcategories => \@branchcategories);
341
342 }
343
344 # FIXME logic seems wrong
345 sub branchcategoriestable {
346 #Needs to be implemented...
347
348     my $categoryinfo = getcategoryinfo();
349     my $color;
350     foreach my $cat (@$categoryinfo) {
351         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
352         $template->param(color => $color);
353         $template->param(categoryname => $cat->{'categoryname'});
354         $template->param(categorycode => $cat->{'categorycode'});
355         $template->param(codedescription => $cat->{'codedescription'});
356     }
357 }
358
359 ######################################################################################################
360 #
361 # Database functions....
362
363 sub getbranchinfo {
364 # returns a reference to an array of hashes containing branches,
365
366     my ($branchcode) = @_;
367     my $dbh = C4::Context->dbh;
368     my $sth;
369     if ($branchcode) {
370                 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
371                 $sth->execute($branchcode);
372     } else {
373                 $sth = $dbh->prepare("Select * from branches order by branchcode");
374                 $sth->execute();
375     }
376     my @results;
377     while (my $data = $sth->fetchrow_hashref) {
378         my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
379         $nsth->execute($data->{'branchcode'});;
380         my @cats = ();
381         while (my ($cat) = $nsth->fetchrow_array) {
382             push(@cats, $cat);
383         }
384         $nsth->finish;
385         $data->{'categories'} = \@cats;
386         push(@results, $data);
387     }
388     $sth->finish;
389     return \@results;
390 }
391
392 # FIXME This doesn't belong here; it should be moved into a module
393 sub getcategoryinfo {
394 # returns a reference to an array of hashes containing branches,
395         my ($catcode) = @_;
396         my $dbh = C4::Context->dbh;
397         my $sth;
398         #    print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
399         if ($catcode) {
400                 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
401                 $sth->execute($catcode);
402         } else {
403                 $sth = $dbh->prepare("Select * from branchcategories");
404                 $sth->execute();
405         }
406         my @results;
407         while (my $data = $sth->fetchrow_hashref) {
408                 push(@results, $data);
409         }
410         $sth->finish;
411         #    print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
412         return \@results;
413 }
414
415 # FIXME This doesn't belong here; it should be moved into a module
416 sub setbranchinfo {
417 # sets the data from the editbranch form, and writes to the database...
418         my ($data) = @_;
419         my $dbh = C4::Context->dbh;
420         my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (?,?,?,?,?,?,?,?)");
421         $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
422                 $data->{'branchaddress1'}, $data->{'branchaddress2'},
423                 $data->{'branchaddress3'}, $data->{'branchphone'},
424                 $data->{'branchfax'}, $data->{'branchemail'});
425
426         $sth->finish;
427         # sort out the categories....
428         my @checkedcats;
429         my $cats = getcategoryinfo();
430         foreach my $cat (@$cats) {
431                 my $code = $cat->{'categorycode'};
432                 if ($data->{$code}) {
433                         push(@checkedcats, $code);
434                 }
435         }
436         my $branchcode =uc($data->{'branchcode'});
437         my $branch = getbranchinfo($branchcode);
438         $branch = $branch->[0];
439         my $branchcats = $branch->{'categories'};
440         my @addcats;
441         my @removecats;
442         foreach my $bcat (@$branchcats) {
443                 unless (grep {/^$bcat$/} @checkedcats) {
444                         push(@removecats, $bcat);
445                 }
446         }
447         foreach my $ccat (@checkedcats){
448                 unless (grep {/^$ccat$/} @$branchcats) {
449                         push(@addcats, $ccat);
450                 }
451         }
452         foreach my $cat (@addcats) {
453                 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
454                 $sth->execute($branchcode, $cat);
455                 $sth->finish;
456         }
457         foreach my $cat (@removecats) {
458                 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
459                 $sth->execute($branchcode, $cat);
460                 $sth->finish;
461         }
462 }
463
464 sub deletebranch {
465 # delete branch...
466     my ($branchcode) = @_;
467     my $dbh = C4::Context->dbh;
468     my $sth=$dbh->prepare("delete from branches where branchcode = ?");
469     $sth->execute($branchcode);
470     $sth->finish;
471 }
472
473 sub setcategoryinfo {
474 # sets the data from the editbranch form, and writes to the database...
475         my ($data) = @_;
476         my $dbh = C4::Context->dbh;
477         my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
478         $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
479
480         $sth->finish;
481 }
482 sub deletecategory {
483 # delete branch...
484     my ($categorycode) = @_;
485     my $dbh = C4::Context->dbh;
486     my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
487     $sth->execute($categorycode);
488     $sth->finish;
489 }
490
491 sub checkdatabasefor {
492 # check to see if the branchcode is being used in the database somewhere....
493     my ($branchcode) = @_;
494     my $dbh = C4::Context->dbh;
495     my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
496     $sth->execute($branchcode, $branchcode);
497     my ($total) = $sth->fetchrow_array;
498     $sth->finish;
499     my $message;
500     if ($total) {
501         # FIXME: need to be replaced by an exported boolean parameter
502         $message = "Branch cannot be deleted because there are $total items using that branch.";
503     }
504     return $message;
505 }
506
507 sub checkcategorycode {
508 # check to see if the branchcode is being used in the database somewhere....
509     my ($categorycode) = @_;
510     my $dbh = C4::Context->dbh;
511     my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
512     $sth->execute($categorycode);
513     my ($total) = $sth->fetchrow_array;
514     $sth->finish;
515     my $message;
516     if ($total) {
517         # FIXME: need to be replaced by an exported boolean parameter
518         $message = "Category cannot be deleted because there are $total branches using that category.";
519     }
520     return $message;
521 }
522
523 output_html_with_http_headers $input, $cookie, $template->output;
524
525 # Local Variables:
526 # tab-width: 8
527 # End: