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;
55 my $linecolor1='#ffffcc';
56 my $linecolor2='white';
57 my $backgroundimage="/images/background-mem.gif";
58 my $script_name="/cgi-bin/koha/admin/branches.pl";
62 #######################################################################################
65 my $branchcode=$input->param('branchcode');
66 my $branchname=$input->param('branchname');
67 my $categorycode = $input->param('categorycode');
68 my $op = $input->param('op');
70 my ($template, $borrowernumber, $cookie)
71 = get_template_and_user({template_name => "admin/branches.tmpl",
75 flagsrequired => {parameters => 1, management => 1},
79 $template->param(script_name => $script_name,
80 $op => 1); # we show only the TMPL_VAR names $op
82 $template->param(script_name => $script_name,
83 else => 1); # we show only the TMPL_VAR names $op
85 $template->param(action => $script_name);
87 # If the user has pressed the "add new branch" button.
88 heading("Branches: Add Branch");
89 $template->param('heading-branches-add-branch-p' => 1);
90 $template->param('use-heading-flags-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('use-heading-flags-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 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
106 setbranchinfo($params);
107 $template->param(else => 1);
108 default ("Branch record changed for branch: $params->{'branchname'}");
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);
125 default("The branch \"$branchname\" ($branchcode) has been deleted.");
126 } elsif ($op eq 'editcategory') {
127 # If the user has pressed the "add new category" or "modify" buttons.
128 heading("Branches: Edit Category");
129 $template->param('heading-branches-edit-category-p' => 1);
130 $template->param('use-heading-flags-p' => 1);
131 editcatform($categorycode);
132 } elsif ($op eq 'addcategory_validate') {
133 # confirm settings change...
134 my $params = $input->Vars;
135 unless ($params->{'categorycode'} && $params->{'categoryname'}) {
136 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
138 setcategoryinfo($params);
139 $template->param(else => 1);
140 default ("Category record changed for category $params->{'categoryname'}");
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);
156 default("The category with code $categorycode has been deleted.");
159 # if no operation has been set...
165 ######################################################################################################
167 # html output functions....
172 $template->param('heading-branches-p' => 1);
173 $template->param('use-heading-flags-p' => 1);
174 $template->param(message => $message);
175 $template->param(action => $script_name);
179 # FIXME: this function should not exist; otherwise headings are untranslatable
182 $template->param(head => $head);
186 # prepares the edit form...
187 my ($branchcode) = @_;
190 $data = getbranchinfo($branchcode);
192 $template->param(branchcode => $data->{'branchcode'});
193 $template->param(branchname => $data->{'branchname'});
194 $template->param(branchaddress1 => $data->{'branchaddress1'});
195 $template->param(branchaddress2 => $data->{'branchaddress2'});
196 $template->param(branchaddress3 => $data->{'branchaddress3'});
197 $template->param(branchphone => $data->{'branchphone'});
198 $template->param(branchfax => $data->{'branchfax'});
199 $template->param(branchemail => $data->{'branchemail'});
202 # make the checkboxs.....
204 # We export a "categoryloop" array to the template, each element of which
205 # contains separate 'categoryname', 'categorycode', 'codedescription', and
206 # 'checked' fields. The $checked field is either '' or 'checked'
209 my $catinfo = getcategoryinfo();
211 # print DEBUG "catinfo=".cvs($catinfo)."\n";
212 my @categoryloop = ();
213 foreach my $cat (@$catinfo) {
215 my $tmp = quotemeta($cat->{'categorycode'});
216 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
217 $checked = "checked=\"checked\"";
219 push @categoryloop, {
220 categoryname => $cat->{'categoryname'},
221 categorycode => $cat->{'categorycode'},
222 codedescription => $cat->{'codedescription'},
226 $template->param(categoryloop => \@categoryloop);
228 # {{{ Leave this here until bug 130 is completely resolved in the templates
229 for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
230 $template->param($obsolete => 'Your template is out of date (bug 130)');
236 # prepares the edit form...
237 my ($categorycode) = @_;
238 warn "cat : $categorycode";
241 $data = getcategoryinfo($categorycode);
243 $template->param(categorycode => $data->{'categorycode'});
244 $template->param(categoryname => $data->{'categoryname'});
245 $template->param(codedescription => $data->{'codedescription'});
250 # message to print if the
251 my ($branchcode) = @_;
255 sub branchinfotable {
256 # makes the html for a table of branch info from reference to an array of hashs.
258 my ($branchcode) = @_;
261 $branchinfo = getbranchinfo($branchcode);
263 $branchinfo = getbranchinfo();
267 foreach my $branch (@$branchinfo) {
268 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
270 # We export the following fields to the template. These are not
271 # pre-composed as a single "address" field because the template
272 # might (and should) escape what is exported here. (See bug 180)
275 # - branch_name (Note: not "branchname")
276 # - branch_code (Note: not "branchcode")
277 # - address (containing a static error message)
280 # - branchaddress3 | comprising the old "address" field
284 # - address-empty-p (1 if no address information, 0 otherwise)
285 # - categories (containing a static error message)
286 # - category_list (loop containing "categoryname")
287 # - no-categories-p (1 if no categories set, 0 otherwise)
293 # Handle address fields separately
294 my $address_empty_p = 1;
295 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
296 'branchphone', 'branchfax', 'branchemail') {
297 $row{$field} = $branch->{$field};
298 if ( $branch->{$field} ) {
299 $address_empty_p = 0;
302 $row{'address-empty-p'} = $address_empty_p;
303 # {{{ Leave this here until bug 180 is completely resolved in templates
304 $row{'address'} = 'Your template is out of date (see bug 180)';
308 my $no_categories_p = 1;
310 foreach my $cat (@{$branch->{'categories'}}) {
311 my ($catinfo) = @{getcategoryinfo($cat)};
312 push @categories, {'categoryname' => $catinfo->{'categoryname'}};
313 $no_categories_p = 0;
315 # {{{ Leave this here until bug 180 is completely resolved in templates
316 $row{'categories'} = 'Your template is out of date (see bug 180)';
318 $row{'category_list'} = \@categories;
319 $row{'no-categories-p'} = $no_categories_p;
321 # Handle all other fields
322 $row{'branch_name'} = $branch->{'branchname'};
323 $row{'branch_code'} = $branch->{'branchcode'};
324 $row{'color'} = $color;
325 $row{'value'} = $branch->{'branchcode'};
326 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
328 push @loop_data, { %row };
330 my @branchcategories =();
331 my $catinfo = getcategoryinfo();
332 foreach my $cat (@$catinfo) {
333 push @branchcategories, {
334 categoryname => $cat->{'categoryname'},
335 categorycode => $cat->{'categorycode'},
336 codedescription => $cat->{'codedescription'},
340 $template->param(branches => \@loop_data,
341 branchcategories => \@branchcategories);
345 # FIXME logic seems wrong
346 sub branchcategoriestable {
347 #Needs to be implemented...
349 my $categoryinfo = getcategoryinfo();
351 foreach my $cat (@$categoryinfo) {
352 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
353 $template->param(color => $color);
354 $template->param(categoryname => $cat->{'categoryname'});
355 $template->param(categorycode => $cat->{'categorycode'});
356 $template->param(codedescription => $cat->{'codedescription'});
360 ######################################################################################################
362 # Database functions....
365 # returns a reference to an array of hashes containing branches,
367 my ($branchcode) = @_;
368 my $dbh = C4::Context->dbh;
371 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
372 $sth->execute($branchcode);
374 $sth = $dbh->prepare("Select * from branches order by branchcode");
378 while (my $data = $sth->fetchrow_hashref) {
379 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
380 $nsth->execute($data->{'branchcode'});;
382 while (my ($cat) = $nsth->fetchrow_array) {
386 $data->{'categories'} = \@cats;
387 push(@results, $data);
393 # FIXME This doesn't belong here; it should be moved into a module
394 sub getcategoryinfo {
395 # returns a reference to an array of hashes containing branches,
397 my $dbh = C4::Context->dbh;
399 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
401 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
402 $sth->execute($catcode);
404 $sth = $dbh->prepare("Select * from branchcategories");
408 while (my $data = $sth->fetchrow_hashref) {
409 push(@results, $data);
412 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
416 # FIXME This doesn't belong here; it should be moved into a module
418 # sets the data from the editbranch form, and writes to the database...
420 my $dbh = C4::Context->dbh;
421 my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (?,?,?,?,?,?,?,?)");
422 $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
423 $data->{'branchaddress1'}, $data->{'branchaddress2'},
424 $data->{'branchaddress3'}, $data->{'branchphone'},
425 $data->{'branchfax'}, $data->{'branchemail'});
428 # sort out the categories....
430 my $cats = getcategoryinfo();
431 foreach my $cat (@$cats) {
432 my $code = $cat->{'categorycode'};
433 if ($data->{$code}) {
434 push(@checkedcats, $code);
437 my $branchcode =uc($data->{'branchcode'});
438 my $branch = getbranchinfo($branchcode);
439 $branch = $branch->[0];
440 my $branchcats = $branch->{'categories'};
443 foreach my $bcat (@$branchcats) {
444 unless (grep {/^$bcat$/} @checkedcats) {
445 push(@removecats, $bcat);
448 foreach my $ccat (@checkedcats){
449 unless (grep {/^$ccat$/} @$branchcats) {
450 push(@addcats, $ccat);
453 foreach my $cat (@addcats) {
454 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
455 $sth->execute($branchcode, $cat);
458 foreach my $cat (@removecats) {
459 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
460 $sth->execute($branchcode, $cat);
467 my ($branchcode) = @_;
468 my $dbh = C4::Context->dbh;
469 my $sth=$dbh->prepare("delete from branches where branchcode = ?");
470 $sth->execute($branchcode);
474 sub setcategoryinfo {
475 # sets the data from the editbranch form, and writes to the database...
477 my $dbh = C4::Context->dbh;
478 my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
479 $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
485 my ($categorycode) = @_;
486 my $dbh = C4::Context->dbh;
487 my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
488 $sth->execute($categorycode);
492 sub checkdatabasefor {
493 # check to see if the branchcode is being used in the database somewhere....
494 my ($branchcode) = @_;
495 my $dbh = C4::Context->dbh;
496 my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
497 $sth->execute($branchcode, $branchcode);
498 my ($total) = $sth->fetchrow_array;
502 # FIXME: need to be replaced by an exported boolean parameter
503 $message = "Branch cannot be deleted because there are $total items using that branch.";
508 sub checkcategorycode {
509 # check to see if the branchcode is being used in the database somewhere....
510 my ($categorycode) = @_;
511 my $dbh = C4::Context->dbh;
512 my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
513 $sth->execute($categorycode);
514 my ($total) = $sth->fetchrow_array;
518 # FIXME: need to be replaced by an exported boolean parameter
519 $message = "Category cannot be deleted because there are $total branches using that category.";
524 output_html_with_http_headers $input, $cookie, $template->output;