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