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