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 $categorycode = $input->param('categorycode');
68 my $op = $input->param('op');
70 my ($template, $borrowernumber, $cookie)
71 = get_template_and_user({template_name => "parameters/branches.tmpl",
75 flagsrequired => {parameters => 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(delete_confirm => 1);
118 $template->param(branchcode => $branchcode);
120 } elsif ($op eq 'delete_confirmed') {
121 # actually delete branch and return to the main screen....
122 deletebranch($branchcode);
123 $template->param(else => 1);
124 default("The branch with code $branchcode has been deleted.");
125 } elsif ($op eq 'editcategory') {
126 # If the user has pressed the "add new category" or "modify" buttons.
127 heading("Branches: Edit Category");
128 $template->param('heading-branches-edit-category-p' => 1);
129 $template->param('use-heading-flags-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 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
137 setcategoryinfo($params);
138 $template->param(else => 1);
139 default ("Category record changed for category $params->{'categoryname'}");
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);
155 default("The category with code $categorycode has been deleted.");
158 # if no operation has been set...
164 ######################################################################################################
166 # html output functions....
171 $template->param('heading-branches-p' => 1);
172 $template->param('use-heading-flags-p' => 1);
173 $template->param(message => $message);
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...
186 my ($branchcode) = @_;
189 $data = getbranchinfo($branchcode);
191 $template->param(branchcode => $data->{'branchcode'});
192 $template->param(branchname => $data->{'branchname'});
193 $template->param(branchaddress1 => $data->{'branchaddress1'});
194 $template->param(branchaddress2 => $data->{'branchaddress2'});
195 $template->param(branchaddress3 => $data->{'branchaddress3'});
196 $template->param(branchphone => $data->{'branchphone'});
197 $template->param(branchfax => $data->{'branchfax'});
198 $template->param(branchemail => $data->{'branchemail'});
201 # make the checkboxs.....
203 # We export a "categoryloop" array to the template, each element of which
204 # contains separate 'categoryname', 'categorycode', 'codedescription', and
205 # 'checked' fields. The $checked field is either '' or 'checked'
208 my $catinfo = getcategoryinfo();
210 # print DEBUG "catinfo=".cvs($catinfo)."\n";
211 my @categoryloop = ();
212 foreach my $cat (@$catinfo) {
214 my $tmp = quotemeta($cat->{'categorycode'});
215 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
216 $checked = "checked=\"checked\"";
218 push @categoryloop, {
219 categoryname => $cat->{'categoryname'},
220 categorycode => $cat->{'categorycode'},
221 codedescription => $cat->{'codedescription'},
225 $template->param(categoryloop => \@categoryloop);
227 # {{{ Leave this here until bug 130 is completely resolved in the templates
228 for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
229 $template->param($obsolete => 'Your template is out of date (bug 130)');
235 # prepares the edit form...
236 my ($categorycode) = @_;
237 warn "cat : $categorycode";
240 $data = getcategoryinfo($categorycode);
242 $template->param(categorycode => $data->{'categorycode'});
243 $template->param(categoryname => $data->{'categoryname'});
244 $template->param(codedescription => $data->{'codedescription'});
249 # message to print if the
250 my ($branchcode) = @_;
254 sub branchinfotable {
255 # makes the html for a table of branch info from reference to an array of hashs.
257 my ($branchcode) = @_;
260 $branchinfo = getbranchinfo($branchcode);
262 $branchinfo = getbranchinfo();
266 foreach my $branch (@$branchinfo) {
267 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
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{'color'} = $color;
324 $row{'value'} = $branch->{'branchcode'};
325 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
327 push @loop_data, { %row };
329 my @branchcategories =();
330 my $catinfo = getcategoryinfo();
331 foreach my $cat (@$catinfo) {
332 push @branchcategories, {
333 categoryname => $cat->{'categoryname'},
334 categorycode => $cat->{'categorycode'},
335 codedescription => $cat->{'codedescription'},
339 $template->param(branches => \@loop_data,
340 branchcategories => \@branchcategories);
344 # FIXME logic seems wrong
345 sub branchcategoriestable {
346 #Needs to be implemented...
348 my $categoryinfo = getcategoryinfo();
350 foreach my $cat (@$categoryinfo) {
351 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
352 $template->param(color => $color);
353 $template->param(categoryname => $cat->{'categoryname'});
354 $template->param(categorycode => $cat->{'categorycode'});
355 $template->param(codedescription => $cat->{'codedescription'});
359 ######################################################################################################
361 # Database functions....
364 # returns a reference to an array of hashes containing branches,
366 my ($branchcode) = @_;
367 my $dbh = C4::Context->dbh;
370 $sth = $dbh->prepare("Select * from branches where branchcode = ? order by branchcode");
371 $sth->execute($branchcode);
373 $sth = $dbh->prepare("Select * from branches order by branchcode");
377 while (my $data = $sth->fetchrow_hashref) {
378 my $nsth = $dbh->prepare("select categorycode from branchrelations where branchcode = ?");
379 $nsth->execute($data->{'branchcode'});;
381 while (my ($cat) = $nsth->fetchrow_array) {
385 $data->{'categories'} = \@cats;
386 push(@results, $data);
392 # FIXME This doesn't belong here; it should be moved into a module
393 sub getcategoryinfo {
394 # returns a reference to an array of hashes containing branches,
396 my $dbh = C4::Context->dbh;
398 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
400 $sth = $dbh->prepare("select * from branchcategories where categorycode = ?");
401 $sth->execute($catcode);
403 $sth = $dbh->prepare("Select * from branchcategories");
407 while (my $data = $sth->fetchrow_hashref) {
408 push(@results, $data);
411 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
415 # FIXME This doesn't belong here; it should be moved into a module
417 # sets the data from the editbranch form, and writes to the database...
419 my $dbh = C4::Context->dbh;
420 my $sth=$dbh->prepare("replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (?,?,?,?,?,?,?,?)");
421 $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
422 $data->{'branchaddress1'}, $data->{'branchaddress2'},
423 $data->{'branchaddress3'}, $data->{'branchphone'},
424 $data->{'branchfax'}, $data->{'branchemail'});
427 # sort out the categories....
429 my $cats = getcategoryinfo();
430 foreach my $cat (@$cats) {
431 my $code = $cat->{'categorycode'};
432 if ($data->{$code}) {
433 push(@checkedcats, $code);
436 my $branchcode =uc($data->{'branchcode'});
437 my $branch = getbranchinfo($branchcode);
438 $branch = $branch->[0];
439 my $branchcats = $branch->{'categories'};
442 foreach my $bcat (@$branchcats) {
443 unless (grep {/^$bcat$/} @checkedcats) {
444 push(@removecats, $bcat);
447 foreach my $ccat (@checkedcats){
448 unless (grep {/^$ccat$/} @$branchcats) {
449 push(@addcats, $ccat);
452 foreach my $cat (@addcats) {
453 my $sth = $dbh->prepare("insert into branchrelations (branchcode, categorycode) values(?, ?)");
454 $sth->execute($branchcode, $cat);
457 foreach my $cat (@removecats) {
458 my $sth = $dbh->prepare("delete from branchrelations where branchcode=? and categorycode=?");
459 $sth->execute($branchcode, $cat);
466 my ($branchcode) = @_;
467 my $dbh = C4::Context->dbh;
468 my $sth=$dbh->prepare("delete from branches where branchcode = ?");
469 $sth->execute($branchcode);
473 sub setcategoryinfo {
474 # sets the data from the editbranch form, and writes to the database...
476 my $dbh = C4::Context->dbh;
477 my $sth=$dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
478 $sth->execute(uc($data->{'categorycode'}), $data->{'categoryname'},$data->{'codedescription'});
484 my ($categorycode) = @_;
485 my $dbh = C4::Context->dbh;
486 my $sth=$dbh->prepare("delete from branchcategories where categorycode = ?");
487 $sth->execute($categorycode);
491 sub checkdatabasefor {
492 # check to see if the branchcode is being used in the database somewhere....
493 my ($branchcode) = @_;
494 my $dbh = C4::Context->dbh;
495 my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
496 $sth->execute($branchcode, $branchcode);
497 my ($total) = $sth->fetchrow_array;
501 # FIXME: need to be replaced by an exported boolean parameter
502 $message = "Branch cannot be deleted because there are $total items using that branch.";
507 sub checkcategorycode {
508 # check to see if the branchcode is being used in the database somewhere....
509 my ($categorycode) = @_;
510 my $dbh = C4::Context->dbh;
511 my $sth=$dbh->prepare("select count(*) from branchrelations where categorycode=?");
512 $sth->execute($categorycode);
513 my ($total) = $sth->fetchrow_array;
517 # FIXME: need to be replaced by an exported boolean parameter
518 $message = "Category cannot be deleted because there are $total branches using that category.";
523 output_html_with_http_headers $input, $cookie, $template->output;