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 editbranchform($branchcode,$template);
121 $template->param( 'heading-branches-add-branch-p' => 1, 'add' => 1, "ERROR$error" => 1 );
123 $template->param( else => 1);
124 default("MESSAGE2",$template);
128 elsif ( $op eq 'delete' ) {
129 # if the user has pressed the "delete branch" button.
131 # check to see if the branchcode is being used in the database somewhere....
132 my $dbh = C4::Context->dbh;
133 my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
134 $sth->execute( $branchcode, $branchcode );
135 my ($total) = $sth->fetchrow_array;
141 $message = "MESSAGE7";
145 $template->param( else => 1 );
146 default($message,$template);
149 $template->param( branchname => $branchname );
150 $template->param( delete_confirm => 1 );
151 $template->param( branchcode => $branchcode );
154 elsif ( $op eq 'delete_confirmed' ) {
156 # actually delete branch and return to the main screen....
157 DelBranch($branchcode);
158 $template->param( else => 1 );
159 default("MESSAGE3",$template);
161 elsif ( $op eq 'editcategory' ) {
163 # If the user has pressed the "add new category" or "modify" buttons.
164 $template->param( 'heading-branches-edit-category-p' => 1 );
165 editcatform($categorycode,$template);
167 elsif ( $op eq 'addcategory_validate' ) {
169 # confirm settings change...
170 my $params = $input->Vars;
171 unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
172 $template->param( else => 1 );
173 default("MESSAGE4",$template);
176 ModBranchCategoryInfo($params);
177 $template->param( else => 1 );
178 default("MESSAGE5",$template);
181 elsif ( $op eq 'delete_category' ) {
183 # if the user has pressed the "delete branch" button.
184 my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
186 $template->param( else => 1 );
187 default($message,$template);
190 $template->param( delete_category => 1 );
191 $template->param( categorycode => $categorycode );
194 elsif ( $op eq 'categorydelete_confirmed' ) {
196 # actually delete branch and return to the main screen....
197 DelBranchCategory($categorycode);
198 $template->param( else => 1 );
199 default("MESSAGE6",$template);
204 # if no operation has been set...
205 default("",$template);
208 ################################################################################
210 # html output functions....
213 my ($message,$innertemplate) = @_;
214 $innertemplate->param( 'heading-branches-p' => 1 );
215 $innertemplate->param( "$message" => 1 );
216 $innertemplate->param( action => $script_name );
217 branchinfotable("",$innertemplate);
221 my ($branchcode,$innertemplate) = @_;
222 # initiate the scrolling-list to select the printers
223 my $printers = GetPrinters();
225 my $printercount = 0;
232 $data = GetBranchInfo($branchcode);
235 # get the old printer of the branch
236 $oldprinter = $data->{'branchprinter'};
239 foreach my $thisprinter ( keys %$printers ) {
242 if $oldprinter and ( $oldprinter eq $printers->{$thisprinter} );
245 value => $thisprinter,
246 selected => $selected,
247 branchprinter => $printers->{$thisprinter}->{'printqueue'},
249 push @printerloop, \%row;
252 $innertemplate->param(
253 printerloop => \@printerloop,
254 branchcode => $data->{'branchcode'},
255 branch_name => $data->{'branchname'},
256 branchaddress1 => $data->{'branchaddress1'},
257 branchaddress2 => $data->{'branchaddress2'},
258 branchaddress3 => $data->{'branchaddress3'},
259 branchphone => $data->{'branchphone'},
260 branchfax => $data->{'branchfax'},
261 branchemail => $data->{'branchemail'},
262 branchip => $data->{'branchip'}
265 else { #case of an add branch select printer
266 foreach my $thisprinter ( keys %$printers ) {
268 value => $thisprinter,
269 branchprinter => $printers->{$thisprinter}->{'printqueue'},
271 push @printerloop, \%row;
273 $innertemplate->param( printerloop => \@printerloop );
276 # make the checkboxs.....
278 # We export a "categoryloop" array to the template, each element of which
279 # contains separate 'categoryname', 'categorycode', 'codedescription', and
280 # 'checked' fields. The $checked field is either '' or 'checked'
283 my $catinfo = GetBranchCategory();
286 # print DEBUG "catinfo=".cvs($catinfo)."\n";
287 my @categoryloop = ();
288 foreach my $cat (@$catinfo) {
290 my $tmp = quotemeta( $cat->{'categorycode'} );
291 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
292 $checked = "checked=\"checked\"";
296 categoryname => $cat->{'categoryname'},
297 categorycode => $cat->{'categorycode'},
298 categorytype => $cat->{'categorytype'},
299 codedescription => $cat->{'codedescription'},
303 $innertemplate->param( categoryloop => \@categoryloop );
305 # {{{ Leave this here until bug 130 is completely resolved in the templates
306 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
307 $innertemplate->param(
308 $obsolete => 'Your template is out of date (bug 130)' );
316 # prepares the edit form...
317 my ($categorycode,$innertemplate) = @_;
318 warn "cat : $categorycode";
321 $innertemplate->param( categorytype => \@cats);
323 $data = GetBranchCategory($categorycode);
325 $innertemplate->param( categorycode => $data->{'categorycode'} ,
326 categoryname => $data->{'categoryname'},
327 codedescription => $data->{'codedescription'} ,
330 for my $ctype (GetCategoryTypes()) {
331 push @cats , { type => $ctype , selected => ($data->{'categorytype'} eq $ctype) };
337 # message to print if the
338 my ($branchcode) = @_;
341 sub branchinfotable {
343 # makes the html for a table of branch info from reference to an array of hashs.
345 my ($branchcode,$innertemplate) = @_;
348 $branchinfo = GetBranchInfo($branchcode);
351 $branchinfo = GetBranchInfo();
356 foreach my $branch (@$branchinfo) {
357 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
360 # We export the following fields to the template. These are not
361 # pre-composed as a single "address" field because the template
362 # might (and should) escape what is exported here. (See bug 180)
365 # - branch_name (Note: not "branchname")
366 # - branch_code (Note: not "branchcode")
367 # - address (containing a static error message)
370 # - branchaddress3 | comprising the old "address" field
374 # - address-empty-p (1 if no address information, 0 otherwise)
375 # - categories (containing a static error message)
376 # - category_list (loop containing "categoryname")
377 # - no-categories-p (1 if no categories set, 0 otherwise)
383 # Handle address fields separately
384 my $address_empty_p = 1;
386 'branchaddress1', 'branchaddress2',
387 'branchaddress3', 'branchphone',
388 'branchfax', 'branchemail',
389 'branchip', 'branchprinter'
392 $row{$field} = $branch->{$field};
393 if ( $branch->{$field} ) {
394 $address_empty_p = 0;
397 $row{'address-empty-p'} = $address_empty_p;
399 # {{{ Leave this here until bug 180 is completely resolved in templates
400 $row{'address'} = 'Your template is out of date (see bug 180)';
405 my $no_categories_p = 1;
407 foreach my $cat ( @{ $branch->{'categories'} } ) {
408 my ($catinfo) = @{ GetBranchCategory($cat) };
409 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
410 $no_categories_p = 0;
413 # {{{ Leave this here until bug 180 is completely resolved in templates
414 $row{'categories'} = 'Your template is out of date (see bug 180)';
417 $row{'category_list'} = \@categories;
418 $row{'no-categories-p'} = $no_categories_p;
420 # Handle all other fields
421 $row{'branch_name'} = $branch->{'branchname'};
422 $row{'branch_code'} = $branch->{'branchcode'};
423 $row{'toggle'} = $toggle;
424 $row{'value'} = $branch->{'branchcode'};
425 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
427 push @loop_data, {%row};
430 my @branchcategories = ();
431 for my $ctype ( GetCategoryTypes() ) {
432 my $catinfo = GetBranchCategories(undef,$ctype);
434 foreach my $cat (@$catinfo) {
437 categoryname => $cat->{'categoryname'},
438 categorycode => $cat->{'categorycode'},
439 codedescription => $cat->{'codedescription'},
440 categorytype => $cat->{'categorytype'},
443 push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
445 $innertemplate->param(
446 branches => \@loop_data,
447 branchcategories => \@branchcategories
452 # FIXME logic seems wrong ## sub is not used.
453 sub branchcategoriestable {
454 my $innertemplate = shift;
455 #Needs to be implemented...
457 my $categoryinfo = GetBranchCategory();
459 foreach my $cat (@$categoryinfo) {
460 $innertemplate->param( categoryname => $cat->{'categoryname'} );
461 $innertemplate->param( categorycode => $cat->{'categorycode'} );
462 $innertemplate->param( codedescription => $cat->{'codedescription'} );
466 output_html_with_http_headers $input, $cookie, $template->output;