3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
22 FIXME: individual fields in branch address need to be exported to templates,
23 in order to fix bug 180; need to notify translators
24 FIXME: looped html (e.g., list of checkboxes) need to be properly
25 TMPL_LOOP'ized; doing this properly will fix bug 130; need to
27 FIXME: need to implement the branch categories stuff
28 FIXME: there are too many TMPL_IF's; the proper way to do it is to have
29 separate templates for each individual action; need to notify
31 FIXME: there are lots of error messages exported to the template; a lot
32 of these should be converted into exported booleans / counters etc
33 so that the error messages can be localized; need to notify translators
35 NOTE: heading() should now be called like this:
36 1. Use heading() as before
37 2. $template->param('heading-LISPISHIZED-HEADING-p' => 1);
38 3. $template->param('use-heading-flags-p' => 1);
39 This ensures that both converted and unconverted templates work
41 Finlay working on this file from 26-03-2002
42 Reorganising this branches admin page.....
56 my $script_name = "/cgi-bin/koha/admin/branches.pl";
59 ################################################################################
62 my $branchcode = $input->param('branchcode');
63 my $branchname = $input->param('branchname');
64 my $categorycode = $input->param('categorycode');
65 my $op = $input->param('op');
71 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
73 template_name => "admin/branches.tmpl",
77 flagsrequired => { parameters => 1},
83 script_name => $script_name,
85 ); # we show only the TMPL_VAR names $op
89 script_name => $script_name,
91 ); # we show only the TMPL_VAR names $op
93 $template->param( action => $script_name );
96 # If the user has pressed the "add new branch" button.
97 $template->param( 'heading-branches-add-branch-p' => 1 );
98 editbranchform($branchcode,$template);
101 elsif ( $op eq 'edit' ) {
103 # if the user has pressed the "edit branch settings" button.
104 $template->param( 'heading-branches-add-branch-p' => 0,
106 editbranchform($branchcode,$template);
108 elsif ( $op eq 'add_validate' ) {
110 # confirm settings change...
111 my $params = $input->Vars;
112 unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
113 $template->param( else => 1 );
114 default("MESSAGE1",$template);
117 my $error = ModBranch($params);
118 # if error saving, stay on edit and rise error
120 # copy input parameters back to form
121 # FIXME - doing this doesn't preserve any branch group selections, but good enough for now
122 $template->param(%$params);
123 $template->param(branch_name => $params->{branchname});
124 $template->param( 'heading-branches-add-branch-p' => 1, 'add' => 1, "ERROR$error" => 1 );
126 $template->param( else => 1);
127 default("MESSAGE2",$template);
131 elsif ( $op eq 'delete' ) {
132 # if the user has pressed the "delete branch" button.
134 # check to see if the branchcode is being used in the database somewhere....
135 my $dbh = C4::Context->dbh;
136 my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
137 $sth->execute( $branchcode, $branchcode );
138 my ($total) = $sth->fetchrow_array;
144 $message = "MESSAGE7";
148 $template->param( else => 1 );
149 default($message,$template);
152 $template->param( branchname => $branchname );
153 $template->param( delete_confirm => 1 );
154 $template->param( branchcode => $branchcode );
157 elsif ( $op eq 'delete_confirmed' ) {
159 # actually delete branch and return to the main screen....
160 DelBranch($branchcode);
161 $template->param( else => 1 );
162 default("MESSAGE3",$template);
164 elsif ( $op eq 'editcategory' ) {
166 # If the user has pressed the "add new category" or "modify" buttons.
167 $template->param( 'heading-branches-edit-category-p' => 1 );
168 editcatform($categorycode,$template);
170 elsif ( $op eq 'addcategory_validate' ) {
172 # confirm settings change...
173 my $params = $input->Vars;
174 unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
175 $template->param( else => 1 );
176 default("MESSAGE4",$template);
179 ModBranchCategoryInfo($params);
180 $template->param( else => 1 );
181 default("MESSAGE5",$template);
184 elsif ( $op eq 'delete_category' ) {
186 # if the user has pressed the "delete branch" button.
187 my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
189 $template->param( else => 1 );
190 default($message,$template);
193 $template->param( delete_category => 1 );
194 $template->param( categorycode => $categorycode );
197 elsif ( $op eq 'categorydelete_confirmed' ) {
199 # actually delete branch and return to the main screen....
200 DelBranchCategory($categorycode);
201 $template->param( else => 1 );
202 default("MESSAGE6",$template);
207 # if no operation has been set...
208 default("",$template);
211 ################################################################################
213 # html output functions....
216 my ($message,$innertemplate) = @_;
217 $innertemplate->param( 'heading-branches-p' => 1 );
218 $innertemplate->param( "$message" => 1 );
219 $innertemplate->param( action => $script_name );
220 branchinfotable("",$innertemplate);
224 my ($branchcode,$innertemplate) = @_;
225 # initiate the scrolling-list to select the printers
226 my $printers = GetPrinters();
228 my $printercount = 0;
235 $data = GetBranchInfo($branchcode);
238 # get the old printer of the branch
239 $oldprinter = $data->{'branchprinter'};
242 foreach my $thisprinter ( keys %$printers ) {
245 if $oldprinter and ( $oldprinter eq $printers->{$thisprinter} );
248 value => $thisprinter,
249 selected => $selected,
250 branchprinter => $printers->{$thisprinter}->{'printqueue'},
252 push @printerloop, \%row;
255 $innertemplate->param(
256 printerloop => \@printerloop,
257 branchcode => $data->{'branchcode'},
258 branch_name => $data->{'branchname'},
259 branchaddress1 => $data->{'branchaddress1'},
260 branchaddress2 => $data->{'branchaddress2'},
261 branchaddress3 => $data->{'branchaddress3'},
262 branchphone => $data->{'branchphone'},
263 branchfax => $data->{'branchfax'},
264 branchemail => $data->{'branchemail'},
265 branchip => $data->{'branchip'}
268 else { #case of an add branch select printer
269 foreach my $thisprinter ( keys %$printers ) {
271 value => $thisprinter,
272 branchprinter => $printers->{$thisprinter}->{'printqueue'},
274 push @printerloop, \%row;
276 $innertemplate->param( printerloop => \@printerloop );
279 # make the checkboxs.....
281 # We export a "categoryloop" array to the template, each element of which
282 # contains separate 'categoryname', 'categorycode', 'codedescription', and
283 # 'checked' fields. The $checked field is either '' or 'checked'
286 my $catinfo = GetBranchCategory();
289 # print DEBUG "catinfo=".cvs($catinfo)."\n";
290 my @categoryloop = ();
291 foreach my $cat (@$catinfo) {
293 my $tmp = quotemeta( $cat->{'categorycode'} );
294 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
295 $checked = "checked=\"checked\"";
299 categoryname => $cat->{'categoryname'},
300 categorycode => $cat->{'categorycode'},
301 categorytype => $cat->{'categorytype'},
302 codedescription => $cat->{'codedescription'},
306 $innertemplate->param( categoryloop => \@categoryloop );
308 # {{{ Leave this here until bug 130 is completely resolved in the templates
309 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
310 $innertemplate->param(
311 $obsolete => 'Your template is out of date (bug 130)' );
319 # prepares the edit form...
320 my ($categorycode,$innertemplate) = @_;
321 warn "cat : $categorycode";
324 $innertemplate->param( categorytype => \@cats);
326 $data = GetBranchCategory($categorycode);
328 $innertemplate->param( categorycode => $data->{'categorycode'} ,
329 categoryname => $data->{'categoryname'},
330 codedescription => $data->{'codedescription'} ,
333 for my $ctype (GetCategoryTypes()) {
334 push @cats , { type => $ctype , selected => ($data->{'categorytype'} eq $ctype) };
340 # message to print if the
341 my ($branchcode) = @_;
344 sub branchinfotable {
346 # makes the html for a table of branch info from reference to an array of hashs.
348 my ($branchcode,$innertemplate) = @_;
351 $branchinfo = GetBranchInfo($branchcode);
354 $branchinfo = GetBranchInfo();
359 foreach my $branch (@$branchinfo) {
360 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
363 # We export the following fields to the template. These are not
364 # pre-composed as a single "address" field because the template
365 # might (and should) escape what is exported here. (See bug 180)
368 # - branch_name (Note: not "branchname")
369 # - branch_code (Note: not "branchcode")
370 # - address (containing a static error message)
373 # - branchaddress3 | comprising the old "address" field
377 # - address-empty-p (1 if no address information, 0 otherwise)
378 # - categories (containing a static error message)
379 # - category_list (loop containing "categoryname")
380 # - no-categories-p (1 if no categories set, 0 otherwise)
386 # Handle address fields separately
387 my $address_empty_p = 1;
389 'branchaddress1', 'branchaddress2',
390 'branchaddress3', 'branchphone',
391 'branchfax', 'branchemail',
392 'branchip', 'branchprinter'
395 $row{$field} = $branch->{$field};
396 if ( $branch->{$field} ) {
397 $address_empty_p = 0;
400 $row{'address-empty-p'} = $address_empty_p;
402 # {{{ Leave this here until bug 180 is completely resolved in templates
403 $row{'address'} = 'Your template is out of date (see bug 180)';
408 my $no_categories_p = 1;
410 foreach my $cat ( @{ $branch->{'categories'} } ) {
411 my ($catinfo) = @{ GetBranchCategory($cat) };
412 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
413 $no_categories_p = 0;
416 # {{{ Leave this here until bug 180 is completely resolved in templates
417 $row{'categories'} = 'Your template is out of date (see bug 180)';
420 $row{'category_list'} = \@categories;
421 $row{'no-categories-p'} = $no_categories_p;
423 # Handle all other fields
424 $row{'branch_name'} = $branch->{'branchname'};
425 $row{'branch_code'} = $branch->{'branchcode'};
426 $row{'toggle'} = $toggle;
427 $row{'value'} = $branch->{'branchcode'};
428 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
430 push @loop_data, {%row};
433 my @branchcategories = ();
434 for my $ctype ( GetCategoryTypes() ) {
435 my $catinfo = GetBranchCategories(undef,$ctype);
437 foreach my $cat (@$catinfo) {
440 categoryname => $cat->{'categoryname'},
441 categorycode => $cat->{'categorycode'},
442 codedescription => $cat->{'codedescription'},
443 categorytype => $cat->{'categorytype'},
446 push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
448 $innertemplate->param(
449 branches => \@loop_data,
450 branchcategories => \@branchcategories
455 # FIXME logic seems wrong ## sub is not used.
456 sub branchcategoriestable {
457 my $innertemplate = shift;
458 #Needs to be implemented...
460 my $categoryinfo = GetBranchCategory();
462 foreach my $cat (@$categoryinfo) {
463 $innertemplate->param( categoryname => $cat->{'categoryname'} );
464 $innertemplate->param( categorycode => $cat->{'categorycode'} );
465 $innertemplate->param( codedescription => $cat->{'codedescription'} );
469 output_html_with_http_headers $input, $cookie, $template->output;