$bibid, now correctly $biblionumber.
[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 $branchname=$input->param('branchname');
68 my $categorycode = $input->param('categorycode');
69 my $op = $input->param('op') || '';
70
71 my ($template, $borrowernumber, $cookie)
72     = get_template_and_user({template_name => "admin/branches.tmpl",
73                              query => $input,
74                              type => "intranet",
75                              authnotrequired => 0,
76                              flagsrequired => {parameters => 1, management => 1},
77                              debug => 1,
78                              });
79 if ($op) {
80         $template->param(script_name => $script_name,
81                                 $op         => 1); # we show only the TMPL_VAR names $op
82 } else {
83         $template->param(script_name => $script_name,
84                                 else        => 1); # we show only the TMPL_VAR names $op
85 }
86 $template->param(action => $script_name);
87 if ($op eq 'add') {
88         # If the user has pressed the "add new branch" button.
89         heading("Branches: Add Branch");
90         $template->param('heading-branches-add-branch-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(add => 1);
98         editbranchform($branchcode);
99 } elsif ($op eq 'add_validate') {
100         # confirm settings change...
101         my $params = $input->Vars;
102         unless ($params->{'branchcode'} && $params->{'branchname'}) {
103                 $template->param(else => 1);
104                 default ("MESSAGE1");
105         } else {
106                 setbranchinfo($params);
107                 $template->param(else => 1);
108                 default ("MESSAGE2");
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(branchname => $branchname);
118                 $template->param(delete_confirm => 1);
119                 $template->param(branchcode => $branchcode);
120         }
121 } elsif ($op eq 'delete_confirmed') {
122         # actually delete branch and return to the main screen....
123         deletebranch($branchcode);
124         $template->param(else => 1);
125         default("MESSAGE3");
126 } elsif ($op eq 'editcategory') {
127         # If the user has pressed the "add new category" or "modify" buttons.
128         $template->param('heading-branches-edit-category-p' => 1);
129         editcatform($categorycode);
130 } elsif ($op eq 'addcategory_validate') {
131         # confirm settings change...
132         my $params = $input->Vars;
133         unless ($params->{'categorycode'} && $params->{'categoryname'}) {
134                 $template->param(else => 1);
135                 default ("MESSAGE4");
136         } else {
137                 setcategoryinfo($params);
138                 $template->param(else => 1);
139                 default ("MESSAGE5");
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("MESSAGE6");
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("$message" => 1);
173         $template->param(action => $script_name);
174         branchinfotable();
175 }
176
177 # FIXME: this function should not exist; otherwise headings are untranslatable
178 sub heading {
179         my ($head) = @_;
180         $template->param(head => $head);
181 }
182
183 sub editbranchform {
184         # prepares the edit form...
185         my ($branchcode) = @_;
186         my $data;
187         if ($branchcode) {
188                 $data = getbranchinfo($branchcode);
189                 $data = $data->[0];
190                 $template->param(branchcode => $data->{'branchcode'});
191                 $template->param(branchname => $data->{'branchname'});
192                 $template->param(branchaddress1 => $data->{'branchaddress1'});
193                 $template->param(branchaddress2 => $data->{'branchaddress2'});
194                 $template->param(branchaddress3 => $data->{'branchaddress3'});
195                 $template->param(branchphone => $data->{'branchphone'});
196                 $template->param(branchfax => $data->{'branchfax'});
197                 $template->param(branchemail => $data->{'branchemail'});
198     }
199
200     # make the checkboxs.....
201     #
202     # We export a "categoryloop" array to the template, each element of which
203     # contains separate 'categoryname', 'categorycode', 'codedescription', and
204     # 'checked' fields. The $checked field is either '' or 'checked'
205     # (see bug 130)
206     #
207     my $catinfo = getcategoryinfo();
208     my $catcheckbox;
209 #    print DEBUG "catinfo=".cvs($catinfo)."\n";
210     my @categoryloop = ();
211     foreach my $cat (@$catinfo) {
212         my $checked = "";
213         my $tmp = quotemeta($cat->{'categorycode'});
214         if (grep {/^$tmp$/} @{$data->{'categories'}}) {
215                 $checked = "checked=\"checked\"";
216         }
217         push @categoryloop, {
218                 categoryname    => $cat->{'categoryname'},
219                 categorycode    => $cat->{'categorycode'},
220                 codedescription => $cat->{'codedescription'},
221                 checked         => $checked,
222             };
223         }
224         $template->param(categoryloop => \@categoryloop);
225
226     # {{{ Leave this here until bug 130 is completely resolved in the templates
227         for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
228                 $template->param($obsolete => 'Your template is out of date (bug 130)');
229         }
230     # }}}
231 }
232
233 sub editcatform {
234         # prepares the edit form...
235         my ($categorycode) = @_;
236         warn "cat : $categorycode";
237         my $data;
238         if ($categorycode) {
239                 $data = getcategoryinfo($categorycode);
240                 $data = $data->[0];
241                 $template->param(categorycode => $data->{'categorycode'});
242                 $template->param(categoryname => $data->{'categoryname'});
243                 $template->param(codedescription => $data->{'codedescription'});
244     }
245 }
246
247 sub deleteconfirm {
248 # message to print if the
249     my ($branchcode) = @_;
250 }
251
252
253 sub branchinfotable {
254 # makes the html for a table of branch info from reference to an array of hashs.
255
256         my ($branchcode) = @_;
257         my $branchinfo;
258         if ($branchcode) {
259                 $branchinfo = getbranchinfo($branchcode);
260         } else {
261                 $branchinfo = getbranchinfo();
262         }
263         my $toggle;
264         my $i=0;
265         my @loop_data =();
266         foreach my $branch (@$branchinfo) {
267                 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
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{'toggle'} = $toggle;
324                 $row{'value'} = $branch->{'branchcode'};
325                 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
326
327                 push @loop_data, { %row };
328                 $i++;
329         }
330         my @branchcategories =();
331         my $catinfo = getcategoryinfo();
332         foreach my $cat (@$catinfo) {
333                 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
334                 push @branchcategories, {
335                         toggle => $toggle,
336                         categoryname    => $cat->{'categoryname'},
337                         categorycode    => $cat->{'categorycode'},
338                         codedescription => $cat->{'codedescription'},
339                 };
340                 $i++;
341         } 
342
343         $template->param(branches => \@loop_data,
344                                                         branchcategories => \@branchcategories);
345
346 }
347
348 # FIXME logic seems wrong
349 sub branchcategoriestable {
350 #Needs to be implemented...
351
352     my $categoryinfo = getcategoryinfo();
353     my $color;
354     foreach my $cat (@$categoryinfo) {
355         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
356         $template->param(color => $color);
357         $template->param(categoryname => $cat->{'categoryname'});
358         $template->param(categorycode => $cat->{'categorycode'});
359         $template->param(codedescription => $cat->{'codedescription'});
360     }
361 }
362
363 ######################################################################################################
364 #
365 # Database functions....
366
367 sub getbranchinfo {
368 # returns a reference to an array of hashes containing branches,
369
370     my ($branchcode) = @_;
371     my $dbh = C4::Context->dbh;
372     my $sth;
373     if ($branchcode) {
374                 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
375                 $sth->execute($branchcode);
376     } else {
377                 $sth = $dbh->prepare("Select * from branches order by branchcode");
378                 $sth->execute();
379     }
380     my @results;
381     while (my $data = $sth->fetchrow_hashref) {
382         my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
383         $nsth->execute($data->{'branchcode'});;
384         my @cats = ();
385         while (my ($cat) = $nsth->fetchrow_array) {
386             push(@cats, $cat);
387         }
388         $nsth->finish;
389         $data->{'categories'} = \@cats;
390         push(@results, $data);
391     }
392     $sth->finish;
393     return \@results;
394 }
395
396 # FIXME This doesn't belong here; it should be moved into a module
397 sub getcategoryinfo {
398 # returns a reference to an array of hashes containing branches,
399         my ($catcode) = @_;
400         my $dbh = C4::Context->dbh;
401         my $sth;
402         #    print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
403         if ($catcode) {
404                 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
405                 $sth->execute($catcode);
406         } else {
407                 $sth = $dbh->prepare("Select * from branchcategories");
408                 $sth->execute();
409         }
410         my @results;
411         while (my $data = $sth->fetchrow_hashref) {
412                 push(@results, $data);
413         }
414         $sth->finish;
415         #    print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
416         return \@results;
417 }
418
419 # FIXME This doesn't belong here; it should be moved into a module
420 sub setbranchinfo {
421 # sets the data from the editbranch form, and writes to the database...
422         my ($data) = @_;
423         my $dbh = C4::Context->dbh;
424         my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (?,?,?,?,?,?,?,?)");
425         $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
426                 $data->{'branchaddress1'}, $data->{'branchaddress2'},
427                 $data->{'branchaddress3'}, $data->{'branchphone'},
428                 $data->{'branchfax'}, $data->{'branchemail'});
429
430         $sth->finish;
431         # sort out the categories....
432         my @checkedcats;
433         my $cats = getcategoryinfo();
434         foreach my $cat (@$cats) {
435                 my $code = $cat->{'categorycode'};
436                 if ($data->{$code}) {
437                         push(@checkedcats, $code);
438                 }
439         }
440         my $branchcode =uc($data->{'branchcode'});
441         my $branch = getbranchinfo($branchcode);
442         $branch = $branch->[0];
443         my $branchcats = $branch->{'categories'};
444         my @addcats;
445         my @removecats;
446         foreach my $bcat (@$branchcats) {
447                 unless (grep {/^$bcat$/} @checkedcats) {
448                         push(@removecats, $bcat);
449                 }
450         }
451         foreach my $ccat (@checkedcats){
452                 unless (grep {/^$ccat$/} @$branchcats) {
453                         push(@addcats, $ccat);
454                 }
455         }
456         foreach my $cat (@addcats) {
457                 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
458                 $sth->execute($branchcode, $cat);
459                 $sth->finish;
460         }
461         foreach my $cat (@removecats) {
462                 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
463                 $sth->execute($branchcode, $cat);
464                 $sth->finish;
465         }
466 }
467
468 sub deletebranch {
469 # delete branch...
470     my ($branchcode) = @_;
471     my $dbh = C4::Context->dbh;
472     my $sth=$dbh->prepare("delete from branches where branchcode = ?");
473     $sth->execute($branchcode);
474     $sth->finish;
475 }
476
477 sub setcategoryinfo {
478 # sets the data from the editbranch form, and writes to the database...
479         my ($data) = @_;
480         my $dbh = C4::Context->dbh;
481         my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
482         $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
483
484         $sth->finish;
485 }
486 sub deletecategory {
487 # delete branch...
488     my ($categorycode) = @_;
489     my $dbh = C4::Context->dbh;
490     my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
491     $sth->execute($categorycode);
492     $sth->finish;
493 }
494
495 sub checkdatabasefor {
496 # check to see if the branchcode is being used in the database somewhere....
497     my ($branchcode) = @_;
498     my $dbh = C4::Context->dbh;
499     my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
500     $sth->execute($branchcode, $branchcode);
501     my ($total) = $sth->fetchrow_array;
502     $sth->finish;
503     my $message;
504     if ($total) {
505         # FIXME: need to be replaced by an exported boolean parameter
506         $message = "MESSAGE7";
507     }
508     return $message;
509 }
510
511 sub checkcategorycode {
512 # check to see if the branchcode is being used in the database somewhere....
513     my ($categorycode) = @_;
514     my $dbh = C4::Context->dbh;
515     my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
516     $sth->execute($categorycode);
517     my ($total) = $sth->fetchrow_array;
518     $sth->finish;
519     my $message;
520     if ($total) {
521         # FIXME: need to be replaced by an exported boolean parameter
522         $message = "Category cannot be deleted because there are $total branches using that category.";
523     }
524     return $message;
525 }
526
527 output_html_with_http_headers $input, $cookie, $template->output;
528
529 # Local Variables:
530 # tab-width: 8
531 # End: