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;
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";
63 #######################################################################################
66 my $branchcode=$input->param('branchcode');
67 my $branchname=$input->param('branchname');
68 my $categorycode = $input->param('categorycode');
69 my $op = $input->param('op') || '';
71 my ($template, $borrowernumber, $cookie)
72 = get_template_and_user({template_name => "parameters/branches.tmpl",
76 flagsrequired => {parameters => 1, management => 1},
80 $template->param(script_name => $script_name,
81 $op => 1); # we show only the TMPL_VAR names $op
83 $template->param(script_name => $script_name,
84 else => 1); # we show only the TMPL_VAR names $op
86 $template->param(action => $script_name);
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);
93 } elsif ($op eq 'edit') {
94 # if the user has pressed the "edit branch settings" button.
95 heading("Branches: Edit Branch");
96 $template->param('heading-branches-edit-branch-p' => 1);
97 $template->param(add => 1);
98 editbranchform($branchcode);
99 } elsif ($op eq 'add_validate') {
100 # confirm settings change...
101 my $params = $input->Vars;
102 unless ($params->{'branchcode'} && $params->{'branchname'}) {
103 $template->param(else => 1);
104 default ("MESSAGE1");
106 setbranchinfo($params);
107 $template->param(else => 1);
108 default ("MESSAGE2");
110 } elsif ($op eq 'delete') {
111 # if the user has pressed the "delete branch" button.
112 my $message = checkdatabasefor($branchcode);
114 $template->param(else => 1);
117 $template->param(branchname => $branchname);
118 $template->param(delete_confirm => 1);
119 $template->param(branchcode => $branchcode);
121 } elsif ($op eq 'delete_confirmed') {
122 # actually delete branch and return to the main screen....
123 deletebranch($branchcode);
124 $template->param(else => 1);
126 } elsif ($op eq 'editcategory') {
127 # If the user has pressed the "add new category" or "modify" buttons.
128 $template->param('heading-branches-edit-category-p' => 1);
129 editcatform($categorycode);
130 } elsif ($op eq 'addcategory_validate') {
131 # confirm settings change...
132 my $params = $input->Vars;
133 unless ($params->{'categorycode'} && $params->{'categoryname'}) {
134 $template->param(else => 1);
135 default ("MESSAGE4");
137 setcategoryinfo($params);
138 $template->param(else => 1);
139 default ("MESSAGE5");
141 } elsif ($op eq 'delete_category') {
142 # if the user has pressed the "delete branch" button.
143 my $message = checkcategorycode($categorycode);
145 $template->param(else => 1);
148 $template->param(delete_category => 1);
149 $template->param(categorycode => $categorycode);
151 } elsif ($op eq 'categorydelete_confirmed') {
152 # actually delete branch and return to the main screen....
153 deletecategory($categorycode);
154 $template->param(else => 1);
158 # if no operation has been set...
164 ######################################################################################################
166 # html output functions....
169 my ($message) = @_ || "";
171 $template->param('heading-branches-p' => 1);
172 $template->param("$message" => 1);
173 $template->param(action => $script_name);
177 # FIXME: this function should not exist; otherwise headings are untranslatable
180 $template->param(head => $head);
184 # prepares the edit form...
185 my ($branchcode) = @_;
188 $data = getbranchinfo($branchcode);
190 $template->param(branchcode => $data->{'branchcode'});
191 $template->param(branchname => $data->{'branchname'});
192 $template->param(branchaddress1 => $data->{'branchaddress1'});
193 $template->param(branchaddress2 => $data->{'branchaddress2'});
194 $template->param(branchaddress3 => $data->{'branchaddress3'});
195 $template->param(branchphone => $data->{'branchphone'});
196 $template->param(branchfax => $data->{'branchfax'});
197 $template->param(branchemail => $data->{'branchemail'});
200 # make the checkboxs.....
202 # We export a "categoryloop" array to the template, each element of which
203 # contains separate 'categoryname', 'categorycode', 'codedescription', and
204 # 'checked' fields. The $checked field is either '' or 'checked'
207 my $catinfo = getcategoryinfo();
209 # print DEBUG "catinfo=".cvs($catinfo)."\n";
210 my @categoryloop = ();
211 foreach my $cat (@$catinfo) {
213 my $tmp = quotemeta($cat->{'categorycode'});
214 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
215 $checked = "checked=\"checked\"";
217 push @categoryloop, {
218 categoryname => $cat->{'categoryname'},
219 categorycode => $cat->{'categorycode'},
220 codedescription => $cat->{'codedescription'},
224 $template->param(categoryloop => \@categoryloop);
226 # {{{ Leave this here until bug 130 is completely resolved in the templates
227 for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
228 $template->param($obsolete => 'Your template is out of date (bug 130)');
234 # prepares the edit form...
235 my ($categorycode) = @_;
236 warn "cat : $categorycode";
239 $data = getcategoryinfo($categorycode);
241 $template->param(categorycode => $data->{'categorycode'});
242 $template->param(categoryname => $data->{'categoryname'});
243 $template->param(codedescription => $data->{'codedescription'});
248 # message to print if the
249 my ($branchcode) = @_;
253 sub branchinfotable {
254 # makes the html for a table of branch info from reference to an array of hashs.
256 my ($branchcode) = @_;
259 $branchinfo = getbranchinfo($branchcode);
261 $branchinfo = getbranchinfo();
266 foreach my $branch (@$branchinfo) {
267 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
269 # We export the following fields to the template. These are not
270 # pre-composed as a single "address" field because the template
271 # might (and should) escape what is exported here. (See bug 180)
274 # - branch_name (Note: not "branchname")
275 # - branch_code (Note: not "branchcode")
276 # - address (containing a static error message)
279 # - branchaddress3 | comprising the old "address" field
283 # - address-empty-p (1 if no address information, 0 otherwise)
284 # - categories (containing a static error message)
285 # - category_list (loop containing "categoryname")
286 # - no-categories-p (1 if no categories set, 0 otherwise)
292 # Handle address fields separately
293 my $address_empty_p = 1;
294 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
295 'branchphone', 'branchfax', 'branchemail') {
296 $row{$field} = $branch->{$field};
297 if ( $branch->{$field} ) {
298 $address_empty_p = 0;
301 $row{'address-empty-p'} = $address_empty_p;
302 # {{{ Leave this here until bug 180 is completely resolved in templates
303 $row{'address'} = 'Your template is out of date (see bug 180)';
307 my $no_categories_p = 1;
309 foreach my $cat (@{$branch->{'categories'}}) {
310 my ($catinfo) = @{getcategoryinfo($cat)};
311 push @categories, {'categoryname' => $catinfo->{'categoryname'}};
312 $no_categories_p = 0;
314 # {{{ Leave this here until bug 180 is completely resolved in templates
315 $row{'categories'} = 'Your template is out of date (see bug 180)';
317 $row{'category_list'} = \@categories;
318 $row{'no-categories-p'} = $no_categories_p;
320 # Handle all other fields
321 $row{'branch_name'} = $branch->{'branchname'};
322 $row{'branch_code'} = $branch->{'branchcode'};
323 $row{'toggle'} = $toggle;
324 $row{'value'} = $branch->{'branchcode'};
325 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
327 push @loop_data, { %row };
330 my @branchcategories =();
331 my $catinfo = getcategoryinfo();
332 foreach my $cat (@$catinfo) {
333 ($i % 2) ? ($toggle = 1) : ($toggle = 0);
334 push @branchcategories, {
336 categoryname => $cat->{'categoryname'},
337 categorycode => $cat->{'categorycode'},
338 codedescription => $cat->{'codedescription'},
343 $template->param(branches => \@loop_data,
344 branchcategories => \@branchcategories);
348 # FIXME logic seems wrong
349 sub branchcategoriestable {
350 #Needs to be implemented...
352 my $categoryinfo = getcategoryinfo();
354 foreach my $cat (@$categoryinfo) {
355 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
356 $template->param(color => $color);
357 $template->param(categoryname => $cat->{'categoryname'});
358 $template->param(categorycode => $cat->{'categorycode'});
359 $template->param(codedescription => $cat->{'codedescription'});
363 ######################################################################################################
365 # Database functions....
368 # returns a reference to an array of hashes containing branches,
370 my ($branchcode) = @_;
371 my $dbh = C4::Context->dbh;
374 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
375 $sth->execute($branchcode);
377 $sth = $dbh->prepare("Select * from branches order by branchcode");
381 while (my $data = $sth->fetchrow_hashref) {
382 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
383 $nsth->execute($data->{'branchcode'});;
385 while (my ($cat) = $nsth->fetchrow_array) {
389 $data->{'categories'} = \@cats;
390 push(@results, $data);
396 # FIXME This doesn't belong here; it should be moved into a module
397 sub getcategoryinfo {
398 # returns a reference to an array of hashes containing branches,
400 my $dbh = C4::Context->dbh;
402 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
404 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
405 $sth->execute($catcode);
407 $sth = $dbh->prepare("Select * from branchcategories");
411 while (my $data = $sth->fetchrow_hashref) {
412 push(@results, $data);
415 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
419 # FIXME This doesn't belong here; it should be moved into a module
421 # sets the data from the editbranch form, and writes to the database...
423 my $dbh = C4::Context->dbh;
424 my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (?,?,?,?,?,?,?,?)");
425 $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
426 $data->{'branchaddress1'}, $data->{'branchaddress2'},
427 $data->{'branchaddress3'}, $data->{'branchphone'},
428 $data->{'branchfax'}, $data->{'branchemail'});
431 # sort out the categories....
433 my $cats = getcategoryinfo();
434 foreach my $cat (@$cats) {
435 my $code = $cat->{'categorycode'};
436 if ($data->{$code}) {
437 push(@checkedcats, $code);
440 my $branchcode =uc($data->{'branchcode'});
441 my $branch = getbranchinfo($branchcode);
442 $branch = $branch->[0];
443 my $branchcats = $branch->{'categories'};
446 foreach my $bcat (@$branchcats) {
447 unless (grep {/^$bcat$/} @checkedcats) {
448 push(@removecats, $bcat);
451 foreach my $ccat (@checkedcats){
452 unless (grep {/^$ccat$/} @$branchcats) {
453 push(@addcats, $ccat);
456 foreach my $cat (@addcats) {
457 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
458 $sth->execute($branchcode, $cat);
461 foreach my $cat (@removecats) {
462 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
463 $sth->execute($branchcode, $cat);
470 my ($branchcode) = @_;
471 my $dbh = C4::Context->dbh;
472 my $sth=$dbh->prepare("delete from branches where branchcode = ?");
473 $sth->execute($branchcode);
477 sub setcategoryinfo {
478 # sets the data from the editbranch form, and writes to the database...
480 my $dbh = C4::Context->dbh;
481 my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
482 $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
488 my ($categorycode) = @_;
489 my $dbh = C4::Context->dbh;
490 my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
491 $sth->execute($categorycode);
495 sub checkdatabasefor {
496 # check to see if the branchcode is being used in the database somewhere....
497 my ($branchcode) = @_;
498 my $dbh = C4::Context->dbh;
499 my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
500 $sth->execute($branchcode, $branchcode);
501 my ($total) = $sth->fetchrow_array;
505 # FIXME: need to be replaced by an exported boolean parameter
506 $message = "MESSAGE7";
511 sub checkcategorycode {
512 # check to see if the branchcode is being used in the database somewhere....
513 my ($categorycode) = @_;
514 my $dbh = C4::Context->dbh;
515 my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
516 $sth->execute($categorycode);
517 my ($total) = $sth->fetchrow_array;
521 # FIXME: need to be replaced by an exported boolean parameter
522 $message = "Category cannot be deleted because there are $total branches using that category.";
527 output_html_with_http_headers $input, $cookie, $template->output;