5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
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
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.
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
27 use C4::Interface::CGI::Output;
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";
37 #######################################################################################
40 my $branchcode=$input->param('branchcode');
41 my $branchname=$input->param('branchname');
42 my $categorycode = $input->param('categorycode');
43 my $op = $input->param('op');
45 my ($template, $borrowernumber, $cookie)
46 = get_template_and_user({template_name => "admin/branches.tmpl",
50 flagsrequired => {parameters => 1, management => 1},
54 $template->param(script_name => $script_name,
55 $op => 1); # we show only the TMPL_VAR names $op
57 $template->param(script_name => $script_name,
58 else => 1); # we show only the TMPL_VAR names $op
60 $template->param(action => $script_name);
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);
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");
81 setbranchinfo($params);
82 $template->param(else => 1);
83 default ("Branch record changed for branch: $params->{'branchname'}");
85 } elsif ($op eq 'delete') {
86 # if the user has pressed the "delete branch" button.
87 my $message = checkdatabasefor($branchcode);
89 $template->param(else => 1);
92 $template->param(branchname => $branchname);
93 $template->param(delete_confirm => 1);
94 $template->param(branchcode => $branchcode);
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");
113 setcategoryinfo($params);
114 $template->param(else => 1);
115 default ("Category record changed for category $params->{'categoryname'}");
117 } elsif ($op eq 'delete_category') {
118 # if the user has pressed the "delete branch" button.
119 my $message = checkcategorycode($categorycode);
121 $template->param(else => 1);
124 $template->param(delete_category => 1);
125 $template->param(categorycode => $categorycode);
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.");
134 # if no operation has been set...
140 ######################################################################################################
142 # html output functions....
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);
154 # FIXME: this function should not exist; otherwise headings are untranslatable
157 $template->param(head => $head);
161 # prepares the edit form...
162 my ($branchcode) = @_;
165 $data = getbranchinfo($branchcode);
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'});
177 # make the checkboxs.....
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'
184 my $catinfo = getcategoryinfo();
186 # print DEBUG "catinfo=".cvs($catinfo)."\n";
187 my @categoryloop = ();
188 foreach my $cat (@$catinfo) {
190 my $tmp = quotemeta($cat->{'categorycode'});
191 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
192 $checked = "checked=\"checked\"";
194 push @categoryloop, {
195 categoryname => $cat->{'categoryname'},
196 categorycode => $cat->{'categorycode'},
197 codedescription => $cat->{'codedescription'},
201 $template->param(categoryloop => \@categoryloop);
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)');
211 # prepares the edit form...
212 my ($categorycode) = @_;
213 warn "cat : $categorycode";
216 $data = getcategoryinfo($categorycode);
218 $template->param(categorycode => $data->{'categorycode'});
219 $template->param(categoryname => $data->{'categoryname'});
220 $template->param(codedescription => $data->{'codedescription'});
225 # message to print if the
226 my ($branchcode) = @_;
230 sub branchinfotable {
231 # makes the html for a table of branch info from reference to an array of hashs.
233 my ($branchcode) = @_;
236 $branchinfo = getbranchinfo($branchcode);
238 $branchinfo = getbranchinfo();
242 foreach my $branch (@$branchinfo) {
243 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
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)
250 # - branch_name (Note: not "branchname")
251 # - branch_code (Note: not "branchcode")
252 # - address (containing a static error message)
255 # - branchaddress3 | comprising the old "address" field
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)
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;
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)';
283 my $no_categories_p = 1;
285 foreach my $cat (@{$branch->{'categories'}}) {
286 my ($catinfo) = @{getcategoryinfo($cat)};
287 push @categories, {'categoryname' => $catinfo->{'categoryname'}};
288 $no_categories_p = 0;
290 # {{{ Leave this here until bug 180 is completely resolved in templates
291 $row{'categories'} = 'Your template is out of date (see bug 180)';
293 $row{'category_list'} = \@categories;
294 $row{'no-categories-p'} = $no_categories_p;
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';
303 push @loop_data, { %row };
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'},
315 $template->param(branches => \@loop_data,
316 branchcategories => \@branchcategories);
320 # FIXME logic seems wrong
321 sub branchcategoriestable {
322 #Needs to be implemented...
324 my $categoryinfo = getcategoryinfo();
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'});
335 ######################################################################################################
337 # Database functions....
340 # returns a reference to an array of hashes containing branches,
342 my ($branchcode) = @_;
343 my $dbh = C4::Context->dbh;
346 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
347 $sth->execute($branchcode);
349 $sth = $dbh->prepare("Select * from branches order by branchcode");
353 while (my $data = $sth->fetchrow_hashref) {
354 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
355 $nsth->execute($data->{'branchcode'});;
357 while (my ($cat) = $nsth->fetchrow_array) {
361 $data->{'categories'} = \@cats;
362 push(@results, $data);
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,
372 my $dbh = C4::Context->dbh;
374 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
376 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
377 $sth->execute($catcode);
379 $sth = $dbh->prepare("Select * from branchcategories");
383 while (my $data = $sth->fetchrow_hashref) {
384 push(@results, $data);
387 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
391 # FIXME This doesn't belong here; it should be moved into a module
393 # sets the data from the editbranch form, and writes to the database...
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'});
403 # sort out the categories....
405 my $cats = getcategoryinfo();
406 foreach my $cat (@$cats) {
407 my $code = $cat->{'categorycode'};
408 if ($data->{$code}) {
409 push(@checkedcats, $code);
412 my $branchcode =uc($data->{'branchcode'});
413 my $branch = getbranchinfo($branchcode);
414 $branch = $branch->[0];
415 my $branchcats = $branch->{'categories'};
418 foreach my $bcat (@$branchcats) {
419 unless (grep {/^$bcat$/} @checkedcats) {
420 push(@removecats, $bcat);
423 foreach my $ccat (@checkedcats){
424 unless (grep {/^$ccat$/} @$branchcats) {
425 push(@addcats, $ccat);
428 foreach my $cat (@addcats) {
429 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
430 $sth->execute($branchcode, $cat);
433 foreach my $cat (@removecats) {
434 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
435 $sth->execute($branchcode, $cat);
442 my ($branchcode) = @_;
443 my $dbh = C4::Context->dbh;
444 my $sth=$dbh->prepare("delete from branches where branchcode = ?");
445 $sth->execute($branchcode);
449 sub setcategoryinfo {
450 # sets the data from the editbranch form, and writes to the database...
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'});
460 my ($categorycode) = @_;
461 my $dbh = C4::Context->dbh;
462 my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
463 $sth->execute($categorycode);
467 sub checkdatabasefor {
468 # check to see if the branchcode is being used in the database somewhere....
469 my ($branchcode) = @_;
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);
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.";
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;
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.";
505 output_html_with_http_headers $input, $cookie, $template->output;