2 # NOTE: Use standard 8-space tabs for this file (indents are 4 spaces)
4 #require '/u/acli/lib/cvs.pl';#DEBUG
5 #open(DEBUG,'>/tmp/koha.debug');
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
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
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
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
26 # Finlay working on this file from 26-03-2002
27 # Reorganising this branches admin page.....
30 # Copyright 2000-2002 Katipo Communications
32 # This file is part of Koha.
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
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.
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
52 use C4::Interface::CGI::Output;
57 my $linecolor1='#ffffcc';
58 my $linecolor2='white';
59 my $backgroundimage="/images/background-mem.gif";
60 my $script_name="/cgi-bin/koha/admin/branches.pl";
64 #######################################################################################
67 my $branchcode=$input->param('branchcode');
68 my $branchname=$input->param('branchname');
69 my $categorycode = $input->param('categorycode');
70 my $op = $input->param('op');
72 my ($template, $borrowernumber, $cookie)
73 = get_template_and_user({template_name => "admin/branches.tmpl",
77 flagsrequired => {parameters => 1, management => 1},
81 $template->param(script_name => $script_name,
82 $op => 1); # we show only the TMPL_VAR names $op
84 $template->param(script_name => $script_name,
85 else => 1); # we show only the TMPL_VAR names $op
87 $template->param(action => $script_name);
89 # If the user has pressed the "add new branch" button.
90 heading("Branches: Add Branch");
91 $template->param('heading-branches-add-branch-p' => 1);
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(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 $template->param(else => 1);
105 default ("MESSAGE1");
107 setbranchinfo($params);
108 $template->param(else => 1);
109 default ("MESSAGE2");
111 } elsif ($op eq 'delete') {
112 # if the user has pressed the "delete branch" button.
113 my $message = checkdatabasefor($branchcode);
115 $template->param(else => 1);
118 $template->param(branchname => $branchname);
119 $template->param(delete_confirm => 1);
120 $template->param(branchcode => $branchcode);
122 } elsif ($op eq 'delete_confirmed') {
123 # actually delete branch and return to the main screen....
124 deletebranch($branchcode);
125 $template->param(else => 1);
127 } elsif ($op eq 'editcategory') {
128 # If the user has pressed the "add new category" or "modify" buttons.
129 $template->param('heading-branches-edit-category-p' => 1);
130 editcatform($categorycode);
131 } elsif ($op eq 'addcategory_validate') {
132 # confirm settings change...
133 my $params = $input->Vars;
134 unless ($params->{'categorycode'} && $params->{'categoryname'}) {
135 $template->param(else => 1);
136 default ("MESSAGE4");
138 setcategoryinfo($params);
139 $template->param(else => 1);
140 default ("MESSAGE5");
142 } elsif ($op eq 'delete_category') {
143 # if the user has pressed the "delete branch" button.
144 my $message = checkcategorycode($categorycode);
146 $template->param(else => 1);
149 $template->param(delete_category => 1);
150 $template->param(categorycode => $categorycode);
152 } elsif ($op eq 'categorydelete_confirmed') {
153 # actually delete branch and return to the main screen....
154 deletecategory($categorycode);
155 $template->param(else => 1);
159 # if no operation has been set...
165 ######################################################################################################
167 # html output functions....
172 $template->param('heading-branches-p' => 1);
173 $template->param("$message" => 1);
174 $template->param(action => $script_name);
178 # FIXME: this function should not exist; otherwise headings are untranslatable
181 $template->param(head => $head);
185 # prepares the edit form...
187 # initiate the scrolling-list to select the printers
189 my $printers=getprinters(\%env);
194 my ($branchcode) = @_;
198 $data = getbranchinfo($branchcode);
200 # get the old printer of the branch
201 $oldprinter = $data->{'branchprinter'};
203 foreach my $thisprinter (keys %$printers) {
204 my $selected = 1 if $oldprinter eq $printers->{$thisprinter}->{'printqueue'};
205 my %row =(value => $thisprinter,
206 selected => $selected,
207 branchprinter => $printers->{$thisprinter}->{'printername'},
209 push @printerloop, \%row;
212 $template->param(printerloop => \@printerloop );
213 $template->param(branchcode => $data->{'branchcode'});
214 $template->param(branchname => $data->{'branchname'});
215 $template->param(branchaddress1 => $data->{'branchaddress1'});
216 $template->param(branchaddress2 => $data->{'branchaddress2'});
217 $template->param(branchaddress3 => $data->{'branchaddress3'});
218 $template->param(branchphone => $data->{'branchphone'});
219 $template->param(branchfax => $data->{'branchfax'});
220 $template->param(branchemail => $data->{'branchemail'});
221 $template->param(branchip => $data->{'branchip'});
224 # on add new branch mode, simple scrolling list
225 $CGIprinter=CGI::scrolling_list( -name => 'branchprinter',
226 -id => 'branchprinter',
227 -values => \@printerloop,
232 # sending the cgiprinter to the template
233 # $template->param(printerloop => $CGIprinter);
235 # make the checkboxs.....
237 # We export a "categoryloop" array to the template, each element of which
238 # contains separate 'categoryname', 'categorycode', 'codedescription', and
239 # 'checked' fields. The $checked field is either '' or 'checked'
242 my $catinfo = getcategoryinfo();
244 # print DEBUG "catinfo=".cvs($catinfo)."\n";
245 my @categoryloop = ();
246 foreach my $cat (@$catinfo) {
248 my $tmp = quotemeta($cat->{'categorycode'});
249 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
250 $checked = "checked=\"checked\"";
252 push @categoryloop, {
253 categoryname => $cat->{'categoryname'},
254 categorycode => $cat->{'categorycode'},
255 codedescription => $cat->{'codedescription'},
259 $template->param(categoryloop => \@categoryloop);
261 # {{{ Leave this here until bug 130 is completely resolved in the templates
262 for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
263 $template->param($obsolete => 'Your template is out of date (bug 130)');
269 # prepares the edit form...
270 my ($categorycode) = @_;
271 warn "cat : $categorycode";
274 $data = getcategoryinfo($categorycode);
276 $template->param(categorycode => $data->{'categorycode'});
277 $template->param(categoryname => $data->{'categoryname'});
278 $template->param(codedescription => $data->{'codedescription'});
283 # message to print if the
284 my ($branchcode) = @_;
288 sub branchinfotable {
289 # makes the html for a table of branch info from reference to an array of hashs.
291 my ($branchcode) = @_;
294 $branchinfo = getbranchinfo($branchcode);
296 $branchinfo = getbranchinfo();
301 foreach my $branch (@$branchinfo) {
302 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
304 # We export the following fields to the template. These are not
305 # pre-composed as a single "address" field because the template
306 # might (and should) escape what is exported here. (See bug 180)
309 # - branch_name (Note: not "branchname")
310 # - branch_code (Note: not "branchcode")
311 # - address (containing a static error message)
314 # - branchaddress3 | comprising the old "address" field
318 # - address-empty-p (1 if no address information, 0 otherwise)
319 # - categories (containing a static error message)
320 # - category_list (loop containing "categoryname")
321 # - no-categories-p (1 if no categories set, 0 otherwise)
327 # Handle address fields separately
328 my $address_empty_p = 1;
329 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
330 'branchphone', 'branchfax', 'branchemail', 'branchip', 'branchprinter') {
331 $row{$field} = $branch->{$field};
332 if ( $branch->{$field} ) {
333 $address_empty_p = 0;
336 $row{'address-empty-p'} = $address_empty_p;
337 # {{{ Leave this here until bug 180 is completely resolved in templates
338 $row{'address'} = 'Your template is out of date (see bug 180)';
342 my $no_categories_p = 1;
344 foreach my $cat (@{$branch->{'categories'}}) {
345 my ($catinfo) = @{getcategoryinfo($cat)};
346 push @categories, {'categoryname' => $catinfo->{'categoryname'}};
347 $no_categories_p = 0;
349 # {{{ Leave this here until bug 180 is completely resolved in templates
350 $row{'categories'} = 'Your template is out of date (see bug 180)';
352 $row{'category_list'} = \@categories;
353 $row{'no-categories-p'} = $no_categories_p;
355 # Handle all other fields
356 $row{'branch_name'} = $branch->{'branchname'};
357 $row{'branch_code'} = $branch->{'branchcode'};
358 $row{'toggle'} = $toggle;
359 $row{'value'} = $branch->{'branchcode'};
360 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
362 push @loop_data, { %row };
365 my @branchcategories =();
366 my $catinfo = getcategoryinfo();
369 foreach my $cat (@$catinfo) {
370 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
371 push @branchcategories, {
373 categoryname => $cat->{'categoryname'},
374 categorycode => $cat->{'categorycode'},
375 codedescription => $cat->{'codedescription'},
380 $template->param(branches => \@loop_data,
381 branchcategories => \@branchcategories);
385 # FIXME logic seems wrong
386 sub branchcategoriestable {
387 #Needs to be implemented...
389 my $categoryinfo = getcategoryinfo();
391 foreach my $cat (@$categoryinfo) {
392 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
393 $template->param(color => $color);
394 $template->param(categoryname => $cat->{'categoryname'});
395 $template->param(categorycode => $cat->{'categorycode'});
396 $template->param(codedescription => $cat->{'codedescription'});
400 ######################################################################################################
402 # Database functions....
405 # returns a reference to an array of hashes containing branches,
407 my ($branchcode) = @_;
408 my $dbh = C4::Context->dbh;
411 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
412 $sth->execute($branchcode);
414 $sth = $dbh->prepare("Select * from branches order by branchcode");
418 while (my $data = $sth->fetchrow_hashref) {
419 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
420 $nsth->execute($data->{'branchcode'});;
422 while (my ($cat) = $nsth->fetchrow_array) {
426 $data->{'categories'} = \@cats;
427 push(@results, $data);
433 # FIXME This doesn't belong here; it should be moved into a module
434 sub getcategoryinfo {
435 # returns a reference to an array of hashes containing branches,
437 my $dbh = C4::Context->dbh;
439 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
441 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
442 $sth->execute($catcode);
444 $sth = $dbh->prepare("Select * from branchcategories");
448 while (my $data = $sth->fetchrow_hashref) {
449 push(@results, $data);
452 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
456 # FIXME This doesn't belong here; it should be moved into a module
458 # sets the data from the editbranch form, and writes to the database...
460 my $dbh = C4::Context->dbh;
461 my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail,branchip,branchprinter) values (?,?,?,?,?,?,?,?,?,?)");
462 $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
463 $data->{'branchaddress1'}, $data->{'branchaddress2'},
464 $data->{'branchaddress3'}, $data->{'branchphone'},
465 $data->{'branchfax'}, $data->{'branchemail'}, $data->{'branchip'},$data->{'branchprinter'});
468 # sort out the categories....
470 my $cats = getcategoryinfo();
471 foreach my $cat (@$cats) {
472 my $code = $cat->{'categorycode'};
473 if ($data->{$code}) {
474 push(@checkedcats, $code);
477 my $branchcode =uc($data->{'branchcode'});
478 my $branch = getbranchinfo($branchcode);
479 $branch = $branch->[0];
480 my $branchcats = $branch->{'categories'};
483 foreach my $bcat (@$branchcats) {
484 unless (grep {/^$bcat$/} @checkedcats) {
485 push(@removecats, $bcat);
488 foreach my $ccat (@checkedcats){
489 unless (grep {/^$ccat$/} @$branchcats) {
490 push(@addcats, $ccat);
493 foreach my $cat (@addcats) {
494 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
495 $sth->execute($branchcode, $cat);
498 foreach my $cat (@removecats) {
499 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
500 $sth->execute($branchcode, $cat);
507 my ($branchcode) = @_;
508 my $dbh = C4::Context->dbh;
509 my $sth=$dbh->prepare("delete from branches where branchcode = ?");
510 $sth->execute($branchcode);
514 sub setcategoryinfo {
515 # sets the data from the editbranch form, and writes to the database...
517 my $dbh = C4::Context->dbh;
518 my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
519 $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
525 my ($categorycode) = @_;
526 my $dbh = C4::Context->dbh;
527 my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
528 $sth->execute($categorycode);
532 sub checkdatabasefor {
533 # check to see if the branchcode is being used in the database somewhere....
534 my ($branchcode) = @_;
535 my $dbh = C4::Context->dbh;
536 my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
537 $sth->execute($branchcode, $branchcode);
538 my ($total) = $sth->fetchrow_array;
542 # FIXME: need to be replaced by an exported boolean parameter
543 $message = "MESSAGE7";
548 sub checkcategorycode {
549 # check to see if the branchcode is being used in the database somewhere....
550 my ($categorycode) = @_;
551 my $dbh = C4::Context->dbh;
552 my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
553 $sth->execute($categorycode);
554 my ($total) = $sth->fetchrow_array;
558 # FIXME: need to be replaced by an exported boolean parameter
559 $message = "Category cannot be deleted because there are $total branches using that category.";
564 output_html_with_http_headers $input, $cookie, $template->output;