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);
91 $template->param('use-heading-flags-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('use-heading-flags-p' => 1);
99 $template->param(add => 1);
100 editbranchform($branchcode);
101 } elsif ($op eq 'add_validate') {
102 # confirm settings change...
103 my $params = $input->Vars;
104 unless ($params->{'branchcode'} && $params->{'branchname'}) {
105 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
107 setbranchinfo($params);
108 $template->param(else => 1);
109 default ("Branch record changed for branch: $params->{'branchname'}");
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);
126 default("The branch \"$branchname\" ($branchcode) has been deleted.");
127 } elsif ($op eq 'editcategory') {
128 # If the user has pressed the "add new category" or "modify" buttons.
129 heading("Branches: Edit Category");
130 $template->param('heading-branches-edit-category-p' => 1);
131 $template->param('use-heading-flags-p' => 1);
132 editcatform($categorycode);
133 } elsif ($op eq 'addcategory_validate') {
134 # confirm settings change...
135 my $params = $input->Vars;
136 unless ($params->{'categorycode'} && $params->{'categoryname'}) {
137 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
139 setcategoryinfo($params);
140 $template->param(else => 1);
141 default ("Category record changed for category $params->{'categoryname'}");
143 } elsif ($op eq 'delete_category') {
144 # if the user has pressed the "delete branch" button.
145 my $message = checkcategorycode($categorycode);
147 $template->param(else => 1);
150 $template->param(delete_category => 1);
151 $template->param(categorycode => $categorycode);
153 } elsif ($op eq 'categorydelete_confirmed') {
154 # actually delete branch and return to the main screen....
155 deletecategory($categorycode);
156 $template->param(else => 1);
157 default("The category with code $categorycode has been deleted.");
160 # if no operation has been set...
166 ######################################################################################################
168 # html output functions....
173 $template->param('heading-branches-p' => 1);
174 $template->param('use-heading-flags-p' => 1);
175 $template->param(message => $message);
176 $template->param(action => $script_name);
180 # FIXME: this function should not exist; otherwise headings are untranslatable
183 $template->param(head => $head);
187 # prepares the edit form...
188 my ($branchcode) = @_;
191 $data = getbranchinfo($branchcode);
193 $template->param(branchcode => $data->{'branchcode'});
194 $template->param(branchname => $data->{'branchname'});
195 $template->param(branchaddress1 => $data->{'branchaddress1'});
196 $template->param(branchaddress2 => $data->{'branchaddress2'});
197 $template->param(branchaddress3 => $data->{'branchaddress3'});
198 $template->param(branchphone => $data->{'branchphone'});
199 $template->param(branchfax => $data->{'branchfax'});
200 $template->param(branchemail => $data->{'branchemail'});
203 # make the checkboxs.....
205 # We export a "categoryloop" array to the template, each element of which
206 # contains separate 'categoryname', 'categorycode', 'codedescription', and
207 # 'checked' fields. The $checked field is either '' or 'checked'
210 my $catinfo = getcategoryinfo();
212 # print DEBUG "catinfo=".cvs($catinfo)."\n";
213 my @categoryloop = ();
214 foreach my $cat (@$catinfo) {
216 my $tmp = quotemeta($cat->{'categorycode'});
217 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
218 $checked = "checked=\"checked\"";
220 push @categoryloop, {
221 categoryname => $cat->{'categoryname'},
222 categorycode => $cat->{'categorycode'},
223 codedescription => $cat->{'codedescription'},
227 $template->param(categoryloop => \@categoryloop);
229 # {{{ Leave this here until bug 130 is completely resolved in the templates
230 for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
231 $template->param($obsolete => 'Your template is out of date (bug 130)');
237 # prepares the edit form...
238 my ($categorycode) = @_;
239 warn "cat : $categorycode";
242 $data = getcategoryinfo($categorycode);
244 $template->param(categorycode => $data->{'categorycode'});
245 $template->param(categoryname => $data->{'categoryname'});
246 $template->param(codedescription => $data->{'codedescription'});
251 # message to print if the
252 my ($branchcode) = @_;
256 sub branchinfotable {
257 # makes the html for a table of branch info from reference to an array of hashs.
259 my ($branchcode) = @_;
262 $branchinfo = getbranchinfo($branchcode);
264 $branchinfo = getbranchinfo();
268 foreach my $branch (@$branchinfo) {
269 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
271 # We export the following fields to the template. These are not
272 # pre-composed as a single "address" field because the template
273 # might (and should) escape what is exported here. (See bug 180)
276 # - branch_name (Note: not "branchname")
277 # - branch_code (Note: not "branchcode")
278 # - address (containing a static error message)
281 # - branchaddress3 | comprising the old "address" field
285 # - address-empty-p (1 if no address information, 0 otherwise)
286 # - categories (containing a static error message)
287 # - category_list (loop containing "categoryname")
288 # - no-categories-p (1 if no categories set, 0 otherwise)
294 # Handle address fields separately
295 my $address_empty_p = 1;
296 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
297 'branchphone', 'branchfax', 'branchemail') {
298 $row{$field} = $branch->{$field};
299 if ( $branch->{$field} ) {
300 $address_empty_p = 0;
303 $row{'address-empty-p'} = $address_empty_p;
304 # {{{ Leave this here until bug 180 is completely resolved in templates
305 $row{'address'} = 'Your template is out of date (see bug 180)';
309 my $no_categories_p = 1;
311 foreach my $cat (@{$branch->{'categories'}}) {
312 my ($catinfo) = @{getcategoryinfo($cat)};
313 push @categories, {'categoryname' => $catinfo->{'categoryname'}};
314 $no_categories_p = 0;
316 # {{{ Leave this here until bug 180 is completely resolved in templates
317 $row{'categories'} = 'Your template is out of date (see bug 180)';
319 $row{'category_list'} = \@categories;
320 $row{'no-categories-p'} = $no_categories_p;
322 # Handle all other fields
323 $row{'branch_name'} = $branch->{'branchname'};
324 $row{'branch_code'} = $branch->{'branchcode'};
325 $row{'color'} = $color;
326 $row{'value'} = $branch->{'branchcode'};
327 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
329 push @loop_data, { %row };
331 my @branchcategories =();
332 my $catinfo = getcategoryinfo();
333 foreach my $cat (@$catinfo) {
334 push @branchcategories, {
335 categoryname => $cat->{'categoryname'},
336 categorycode => $cat->{'categorycode'},
337 codedescription => $cat->{'codedescription'},
341 $template->param(branches => \@loop_data,
342 branchcategories => \@branchcategories);
346 # FIXME logic seems wrong
347 sub branchcategoriestable {
348 #Needs to be implemented...
350 my $categoryinfo = getcategoryinfo();
352 foreach my $cat (@$categoryinfo) {
353 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
354 $template->param(color => $color);
355 $template->param(categoryname => $cat->{'categoryname'});
356 $template->param(categorycode => $cat->{'categorycode'});
357 $template->param(codedescription => $cat->{'codedescription'});
361 ######################################################################################################
363 # Database functions....
366 # returns a reference to an array of hashes containing branches,
368 my ($branchcode) = @_;
369 my $dbh = C4::Context->dbh;
372 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
373 $sth->execute($branchcode);
375 $sth = $dbh->prepare("Select * from branches order by branchcode");
379 while (my $data = $sth->fetchrow_hashref) {
380 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
381 $nsth->execute($data->{'branchcode'});;
383 while (my ($cat) = $nsth->fetchrow_array) {
387 $data->{'categories'} = \@cats;
388 push(@results, $data);
394 # FIXME This doesn't belong here; it should be moved into a module
395 sub getcategoryinfo {
396 # returns a reference to an array of hashes containing branches,
398 my $dbh = C4::Context->dbh;
400 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
402 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
403 $sth->execute($catcode);
405 $sth = $dbh->prepare("Select * from branchcategories");
409 while (my $data = $sth->fetchrow_hashref) {
410 push(@results, $data);
413 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
417 # FIXME This doesn't belong here; it should be moved into a module
419 # sets the data from the editbranch form, and writes to the database...
421 my $dbh = C4::Context->dbh;
422 my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (?,?,?,?,?,?,?,?)");
423 $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
424 $data->{'branchaddress1'}, $data->{'branchaddress2'},
425 $data->{'branchaddress3'}, $data->{'branchphone'},
426 $data->{'branchfax'}, $data->{'branchemail'});
429 # sort out the categories....
431 my $cats = getcategoryinfo();
432 foreach my $cat (@$cats) {
433 my $code = $cat->{'categorycode'};
434 if ($data->{$code}) {
435 push(@checkedcats, $code);
438 my $branchcode =uc($data->{'branchcode'});
439 my $branch = getbranchinfo($branchcode);
440 $branch = $branch->[0];
441 my $branchcats = $branch->{'categories'};
444 foreach my $bcat (@$branchcats) {
445 unless (grep {/^$bcat$/} @checkedcats) {
446 push(@removecats, $bcat);
449 foreach my $ccat (@checkedcats){
450 unless (grep {/^$ccat$/} @$branchcats) {
451 push(@addcats, $ccat);
454 foreach my $cat (@addcats) {
455 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
456 $sth->execute($branchcode, $cat);
459 foreach my $cat (@removecats) {
460 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
461 $sth->execute($branchcode, $cat);
468 my ($branchcode) = @_;
469 my $dbh = C4::Context->dbh;
470 my $sth=$dbh->prepare("delete from branches where branchcode = ?");
471 $sth->execute($branchcode);
475 sub setcategoryinfo {
476 # sets the data from the editbranch form, and writes to the database...
478 my $dbh = C4::Context->dbh;
479 my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
480 $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
486 my ($categorycode) = @_;
487 my $dbh = C4::Context->dbh;
488 my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
489 $sth->execute($categorycode);
493 sub checkdatabasefor {
494 # check to see if the branchcode is being used in the database somewhere....
495 my ($branchcode) = @_;
496 my $dbh = C4::Context->dbh;
497 my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
498 $sth->execute($branchcode, $branchcode);
499 my ($total) = $sth->fetchrow_array;
503 # FIXME: need to be replaced by an exported boolean parameter
504 $message = "Branch cannot be deleted because there are $total items using that branch.";
509 sub checkcategorycode {
510 # check to see if the branchcode is being used in the database somewhere....
511 my ($categorycode) = @_;
512 my $dbh = C4::Context->dbh;
513 my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
514 $sth->execute($categorycode);
515 my ($total) = $sth->fetchrow_array;
519 # FIXME: need to be replaced by an exported boolean parameter
520 $message = "Category cannot be deleted because there are $total branches using that category.";
525 output_html_with_http_headers $input, $cookie, $template->output;