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