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