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: heading() need to be moved to templates, need to notify translators
14 # FIXME: there are too many TMPL_IF's; the proper way to do it is to have
15 # separate templates for each individual action; need to notify
17 # FIXME: there are lots of error messages exported to the template; a lot
18 # of these should be converted into exported booleans / counters etc
19 # so that the error messages can be localized; need to notify translators
21 # Finlay working on this file from 26-03-2002
22 # Reorganising this branches admin page.....
25 # Copyright 2000-2002 Katipo Communications
27 # This file is part of Koha.
29 # Koha is free software; you can redistribute it and/or modify it under the
30 # terms of the GNU General Public License as published by the Free Software
31 # Foundation; either version 2 of the License, or (at your option) any later
34 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
35 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
36 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
38 # You should have received a copy of the GNU General Public License along with
39 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
40 # Suite 330, Boston, MA 02111-1307 USA
47 use C4::Interface::CGI::Output;
51 my $linecolor1='#ffffcc';
52 my $linecolor2='white';
53 my $backgroundimage="/images/background-mem.gif";
54 my $script_name="/cgi-bin/koha/admin/branches.pl";
58 #######################################################################################
61 my $branchcode=$input->param('branchcode');
62 my $categorycode = $input->param('categorycode');
63 my $op = $input->param('op');
65 my ($template, $borrowernumber, $cookie)
66 = get_template_and_user({template_name => "parameters/branches.tmpl",
70 flagsrequired => {parameters => 1},
74 $template->param(script_name => $script_name,
75 $op => 1); # we show only the TMPL_VAR names $op
77 $template->param(script_name => $script_name,
78 else => 1); # we show only the TMPL_VAR names $op
80 $template->param(action => $script_name);
83 # If the user has pressed the "add new branch" button.
84 heading("Branches: Add Branch");
87 } elsif ($op eq 'edit') {
88 # if the user has pressed the "edit branch settings" button.
89 heading("Branches: Edit Branch");
90 $template->param(add => 1);
91 editbranchform($branchcode);
92 } elsif ($op eq 'add_validate') {
93 # confirm settings change...
94 my $params = $input->Vars;
95 unless ($params->{'branchcode'} && $params->{'branchname'}) {
96 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
98 setbranchinfo($params);
99 $template->param(else => 1);
100 default ("Branch record changed for branch: $params->{'branchname'}");
102 } elsif ($op eq 'delete') {
103 # if the user has pressed the "delete branch" button.
104 my $message = checkdatabasefor($branchcode);
106 $template->param(else => 1);
109 $template->param(delete_confirm => 1);
110 $template->param(branchcode => $branchcode);
112 } elsif ($op eq 'delete_confirmed') {
113 # actually delete branch and return to the main screen....
114 deletebranch($branchcode);
115 $template->param(else => 1);
116 default("The branch with code $branchcode has been deleted.");
117 } elsif ($op eq 'editcategory') {
118 # If the user has pressed the "add new category" button.
119 heading("Branches: Edit Category");
120 editcatform($categorycode);
121 } elsif ($op eq 'addcategory_validate') {
122 # confirm settings change...
123 my $params = $input->Vars;
124 unless ($params->{'categorycode'} && $params->{'categoryname'}) {
125 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
127 setcategoryinfo($params);
128 $template->param(else => 1);
129 default ("Category record changed for category $params->{'categoryname'}");
131 } elsif ($op eq 'delete_category') {
132 # if the user has pressed the "delete branch" button.
133 my $message = checkcategorycode($categorycode);
135 $template->param(else => 1);
138 $template->param(delete_category => 1);
139 $template->param(categorycode => $categorycode);
141 } elsif ($op eq 'categorydelete_confirmed') {
142 # actually delete branch and return to the main screen....
143 deletecategory($categorycode);
144 $template->param(else => 1);
145 default("The category with code $categorycode has been deleted.");
148 # if no operation has been set...
154 ######################################################################################################
156 # html output functions....
161 $template->param(message => $message);
162 $template->param(action => $script_name);
166 # FIXME: this function should not exist; otherwise headings are untranslatable
169 $template->param(head => $head);
173 # prepares the edit form...
174 my ($branchcode) = @_;
177 $data = getbranchinfo($branchcode);
179 $template->param(branchcode => $data->{'branchcode'});
180 $template->param(branchname => $data->{'branchname'});
181 $template->param(branchaddress1 => $data->{'branchaddress1'});
182 $template->param(branchaddress2 => $data->{'branchaddress2'});
183 $template->param(branchaddress3 => $data->{'branchaddress3'});
184 $template->param(branchphone => $data->{'branchphone'});
185 $template->param(branchfax => $data->{'branchfax'});
186 $template->param(branchemail => $data->{'branchemail'});
189 # make the checkboxs.....
191 # We export a "categoryloop" array to the template, each element of which
192 # contains separate 'categoryname', 'categorycode', 'codedescription', and
193 # 'checked' fields. The $checked field is either '' or 'checked'
196 my $catinfo = getcategoryinfo();
198 # print DEBUG "catinfo=".cvs($catinfo)."\n";
199 my @categoryloop = ();
200 foreach my $cat (@$catinfo) {
202 my $tmp = quotemeta($cat->{'categorycode'});
203 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
204 $checked = "CHECKED";
206 push @categoryloop, {
207 categoryname => $cat->{'categoryname'},
208 categorycode => $cat->{'categorycode'},
209 codedescription => $cat->{'codedescription'},
213 $template->param(categoryloop => \@categoryloop);
215 # {{{ Leave this here until bug 130 is completely resolved in the templates
216 for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
217 $template->param($obsolete => 'Your template is out of date (bug 130)');
223 # prepares the edit form...
224 my ($categorycode) = @_;
225 warn "cat : $categorycode";
228 $data = getcategoryinfo($categorycode);
230 $template->param(categorycode => $data->{'categorycode'});
231 $template->param(categoryname => $data->{'categoryname'});
232 $template->param(codedescription => $data->{'codedescription'});
237 # message to print if the
238 my ($branchcode) = @_;
242 sub branchinfotable {
243 # makes the html for a table of branch info from reference to an array of hashs.
245 my ($branchcode) = @_;
248 $branchinfo = getbranchinfo($branchcode);
250 $branchinfo = getbranchinfo();
254 foreach my $branch (@$branchinfo) {
255 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
257 # We export the following fields to the template. These are not
258 # pre-composed as a single "address" field because the template
259 # might (and should) escape what is exported here. (See bug 180)
262 # - branch_name (Note: not "branchname")
263 # - branch_code (Note: not "branchcode")
264 # - address (containing a static error message)
267 # - branchaddress3 | comprising the old "address" field
271 # - address-empty-p (1 if no address information, 0 otherwise)
272 # - categories (containing a static error message)
273 # - category_list (loop containing "categoryname")
274 # - no-categories-p (1 if no categories set, 0 otherwise)
280 # Handle address fields separately
281 my $address_empty_p = 1;
282 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
283 'branchphone', 'branchfax', 'branchemail') {
284 $row{$field} = $branch->{$field};
285 $address_empty_p = 0;
287 $row{'address-empty-p'} = $address_empty_p;
288 # {{{ Leave this here until bug 180 is completely resolved in templates
289 $row{'address'} = 'Your template is out of date (see bug 180)';
293 my $no_categories_p = 1;
295 foreach my $cat (@{$branch->{'categories'}}) {
296 my ($catinfo) = @{getcategoryinfo($cat)};
297 push @categories, {'categoryname' => $catinfo->{'categoryname'}};
298 $no_categories_p = 0;
300 # {{{ Leave this here until bug 180 is completely resolved in templates
301 $row{'categories'} = 'Your template is out of date (see bug 180)';
303 $row{'category_list'} = \@categories;
304 $row{'no-categories-p'} = $no_categories_p;
306 # Handle all other fields
307 $row{'branch_name'} = $branch->{'branchname'};
308 $row{'branch_code'} = $branch->{'branchcode'};
309 $row{'color'} = $color;
310 $row{'value'} = $branch->{'branchcode'};
311 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
313 push @loop_data, { %row };
315 my @branchcategories =();
316 my $catinfo = getcategoryinfo();
317 foreach my $cat (@$catinfo) {
318 push @branchcategories, {
319 categoryname => $cat->{'categoryname'},
320 categorycode => $cat->{'categorycode'},
321 codedescription => $cat->{'codedescription'},
325 $template->param(branches => \@loop_data,
326 branchcategories => \@branchcategories);
330 # FIXME logic seems wrong
331 sub branchcategoriestable {
332 #Needs to be implemented...
334 my $categoryinfo = getcategoryinfo();
336 foreach my $cat (@$categoryinfo) {
337 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
338 $template->param(color => $color);
339 $template->param(categoryname => $cat->{'categoryname'});
340 $template->param(categorycode => $cat->{'categorycode'});
341 $template->param(codedescription => $cat->{'codedescription'});
345 ######################################################################################################
347 # Database functions....
350 # returns a reference to an array of hashes containing branches,
352 my ($branchcode) = @_;
353 my $dbh = C4::Context->dbh;
354 my ($query, @query_args);
356 $query = "Select * from branches where branchcode = ?";
357 @query_args = ($branchcode);
359 $query = "Select * from branches";
361 my $sth = $dbh->prepare($query);
362 $sth->execute(@query_args);
364 while (my $data = $sth->fetchrow_hashref) {
365 $query = "select categorycode from branchrelations where branchcode = ?";
366 my $nsth = $dbh->prepare($query);
367 $nsth->execute($data->{'branchcode'});;
369 while (my ($cat) = $nsth->fetchrow_array) {
373 $data->{'categories'} = \@cats;
374 push(@results, $data);
380 # FIXME This doesn't belong here; it should be moved into a module
381 sub getcategoryinfo {
382 # returns a reference to an array of hashes containing branches,
384 my $dbh = C4::Context->dbh;
385 my ($query, @query_args);
386 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
388 $query = "select * from branchcategories where categorycode = ?";
389 @query_args = ($catcode);
391 $query = "Select * from branchcategories";
393 # print DEBUG "getcategoryinfo: query=".cvs($query)."\n";
394 my $sth = $dbh->prepare($query);
395 $sth->execute(@query_args);
397 while (my $data = $sth->fetchrow_hashref) {
398 push(@results, $data);
401 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
405 # FIXME This doesn't belong here; it should be moved into a module
407 # sets the data from the editbranch form, and writes to the database...
409 my $dbh = C4::Context->dbh;
410 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (?,?,?,?,?,?,?,?)";
411 my $sth=$dbh->prepare($query);
412 $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
413 $data->{'branchaddress1'}, $data->{'branchaddress2'},
414 $data->{'branchaddress3'}, $data->{'branchphone'},
415 $data->{'branchfax'}, $data->{'branchemail'});
418 # sort out the categories....
420 my $cats = getcategoryinfo();
421 foreach my $cat (@$cats) {
422 my $code = $cat->{'categorycode'};
423 if ($data->{$code}) {
424 push(@checkedcats, $code);
427 my $branchcode =uc($data->{'branchcode'});
428 my $branch = getbranchinfo($branchcode);
429 $branch = $branch->[0];
430 my $branchcats = $branch->{'categories'};
433 foreach my $bcat (@$branchcats) {
434 unless (grep {/^$bcat$/} @checkedcats) {
435 push(@removecats, $bcat);
438 foreach my $ccat (@checkedcats){
439 unless (grep {/^$ccat$/} @$branchcats) {
440 push(@addcats, $ccat);
443 # FIXME - There's already a $dbh in this scope.
444 my $dbh = C4::Context->dbh;
445 foreach my $cat (@addcats) {
446 my $query = "insert into branchrelations (branchcode, categorycode) values(?, ?)";
447 my $sth = $dbh->prepare($query);
448 $sth->execute($branchcode, $cat);
451 foreach my $cat (@removecats) {
452 my $query = "delete from branchrelations where branchcode=? and categorycode=?";
453 my $sth = $dbh->prepare($query);
454 $sth->execute($branchcode, $cat);
461 my ($branchcode) = @_;
462 my $query = "delete from branches where branchcode = ?";
463 my $dbh = C4::Context->dbh;
464 my $sth=$dbh->prepare($query);
465 $sth->execute($branchcode);
469 sub setcategoryinfo {
470 # sets the data from the editbranch form, and writes to the database...
472 my $dbh = C4::Context->dbh;
473 my $query = "replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)";
474 my $sth=$dbh->prepare($query);
475 $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
481 my ($categorycode) = @_;
482 my $query = "delete from branchcategories where categorycode = ?";
483 my $dbh = C4::Context->dbh;
484 my $sth=$dbh->prepare($query);
485 $sth->execute($categorycode);
489 sub checkdatabasefor {
490 # check to see if the branchcode is being used in the database somewhere....
491 my ($branchcode) = @_;
492 my $dbh = C4::Context->dbh;
493 my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
494 $sth->execute($branchcode, $branchcode);
495 my ($total) = $sth->fetchrow_array;
499 # FIXME: need to be replaced by an exported boolean parameter
500 $message = "Branch cannot be deleted because there are $total items using that branch.";
505 sub checkcategorycode {
506 # check to see if the branchcode is being used in the database somewhere....
507 my ($categorycode) = @_;
508 my $dbh = C4::Context->dbh;
509 my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
510 $sth->execute($categorycode);
511 my ($total) = $sth->fetchrow_array;
515 # FIXME: need to be replaced by an exported boolean parameter
516 $message = "Category cannot be deleted because there are $total branches using that category.";
521 output_html_with_http_headers $input, $cookie, $template->output;