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);
118 $template->param( else => 1 );
119 default("MESSAGE2",$template);
122 elsif ( $op eq 'delete' ) {
123 # if the user has pressed the "delete branch" button.
125 # check to see if the branchcode is being used in the database somewhere....
126 my $dbh = C4::Context->dbh;
127 my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
128 $sth->execute( $branchcode, $branchcode );
129 my ($total) = $sth->fetchrow_array;
135 $message = "MESSAGE7";
139 $template->param( else => 1 );
140 default($message,$template);
143 $template->param( branchname => $branchname );
144 $template->param( delete_confirm => 1 );
145 $template->param( branchcode => $branchcode );
148 elsif ( $op eq 'delete_confirmed' ) {
150 # actually delete branch and return to the main screen....
151 DelBranch($branchcode);
152 $template->param( else => 1 );
153 default("MESSAGE3",$template);
155 elsif ( $op eq 'editcategory' ) {
157 # If the user has pressed the "add new category" or "modify" buttons.
158 $template->param( 'heading-branches-edit-category-p' => 1 );
159 editcatform($categorycode,$template);
161 elsif ( $op eq 'addcategory_validate' ) {
163 # confirm settings change...
164 my $params = $input->Vars;
165 unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
166 $template->param( else => 1 );
167 default("MESSAGE4",$template);
170 ModBranchCategoryInfo($params);
171 $template->param( else => 1 );
172 default("MESSAGE5",$template);
175 elsif ( $op eq 'delete_category' ) {
177 # if the user has pressed the "delete branch" button.
178 my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
180 $template->param( else => 1 );
181 default($message,$template);
184 $template->param( delete_category => 1 );
185 $template->param( categorycode => $categorycode );
188 elsif ( $op eq 'categorydelete_confirmed' ) {
190 # actually delete branch and return to the main screen....
191 DelBranchCategory($categorycode);
192 $template->param( else => 1 );
193 default("MESSAGE6",$template);
198 # if no operation has been set...
199 default("",$template);
202 ################################################################################
204 # html output functions....
207 my ($message,$innertemplate) = @_;
208 $innertemplate->param( 'heading-branches-p' => 1 );
209 $innertemplate->param( "$message" => 1 );
210 $innertemplate->param( action => $script_name );
211 branchinfotable("",$innertemplate);
215 my ($branchcode,$innertemplate) = @_;
216 # initiate the scrolling-list to select the printers
217 my $printers = GetPrinters();
219 my $printercount = 0;
226 $data = GetBranchInfo($branchcode);
229 # get the old printer of the branch
230 $oldprinter = $data->{'branchprinter'};
233 foreach my $thisprinter ( keys %$printers ) {
235 if $oldprinter eq $printers->{$thisprinter}->{'printqueue'};
237 value => $thisprinter,
238 selected => $selected,
239 branchprinter => $printers->{$thisprinter}->{'printqueue'},
241 push @printerloop, \%row;
244 $innertemplate->param(
245 printerloop => \@printerloop,
246 branchcode => $data->{'branchcode'},
247 branch_name => $data->{'branchname'},
248 branchaddress1 => $data->{'branchaddress1'},
249 branchaddress2 => $data->{'branchaddress2'},
250 branchaddress3 => $data->{'branchaddress3'},
251 branchphone => $data->{'branchphone'},
252 branchfax => $data->{'branchfax'},
253 branchemail => $data->{'branchemail'},
254 branchip => $data->{'branchip'}
257 else { #case of an add branch select printer
258 foreach my $thisprinter ( keys %$printers ) {
260 value => $thisprinter,
261 branchprinter => $printers->{$thisprinter}->{'printqueue'},
263 push @printerloop, \%row;
265 $innertemplate->param( printerloop => \@printerloop );
268 # make the checkboxs.....
270 # We export a "categoryloop" array to the template, each element of which
271 # contains separate 'categoryname', 'categorycode', 'codedescription', and
272 # 'checked' fields. The $checked field is either '' or 'checked'
275 my $catinfo = GetBranchCategory();
278 # print DEBUG "catinfo=".cvs($catinfo)."\n";
279 my @categoryloop = ();
280 foreach my $cat (@$catinfo) {
282 my $tmp = quotemeta( $cat->{'categorycode'} );
283 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
284 $checked = "checked=\"checked\"";
288 categoryname => $cat->{'categoryname'},
289 categorycode => $cat->{'categorycode'},
290 categorytype => $cat->{'categorytype'},
291 codedescription => $cat->{'codedescription'},
295 $innertemplate->param( categoryloop => \@categoryloop );
297 # {{{ Leave this here until bug 130 is completely resolved in the templates
298 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
299 $innertemplate->param(
300 $obsolete => 'Your template is out of date (bug 130)' );
308 # prepares the edit form...
309 my ($categorycode,$innertemplate) = @_;
310 warn "cat : $categorycode";
313 $innertemplate->param( categorytype => \@cats);
315 $data = GetBranchCategory($categorycode);
317 $innertemplate->param( categorycode => $data->{'categorycode'} ,
318 categoryname => $data->{'categoryname'},
319 codedescription => $data->{'codedescription'} ,
322 for my $ctype (GetCategoryTypes()) {
323 push @cats , { type => $ctype , selected => ($data->{'categorytype'} eq $ctype) };
329 # message to print if the
330 my ($branchcode) = @_;
333 sub branchinfotable {
335 # makes the html for a table of branch info from reference to an array of hashs.
337 my ($branchcode,$innertemplate) = @_;
340 $branchinfo = GetBranchInfo($branchcode);
343 $branchinfo = GetBranchInfo(undef,'properties');
348 foreach my $branch (@$branchinfo) {
349 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
352 # We export the following fields to the template. These are not
353 # pre-composed as a single "address" field because the template
354 # might (and should) escape what is exported here. (See bug 180)
357 # - branch_name (Note: not "branchname")
358 # - branch_code (Note: not "branchcode")
359 # - address (containing a static error message)
362 # - branchaddress3 | comprising the old "address" field
366 # - address-empty-p (1 if no address information, 0 otherwise)
367 # - categories (containing a static error message)
368 # - category_list (loop containing "categoryname")
369 # - no-categories-p (1 if no categories set, 0 otherwise)
375 # Handle address fields separately
376 my $address_empty_p = 1;
378 'branchaddress1', 'branchaddress2',
379 'branchaddress3', 'branchphone',
380 'branchfax', 'branchemail',
381 'branchip', 'branchprinter'
384 $row{$field} = $branch->{$field};
385 if ( $branch->{$field} ) {
386 $address_empty_p = 0;
389 $row{'address-empty-p'} = $address_empty_p;
391 # {{{ Leave this here until bug 180 is completely resolved in templates
392 $row{'address'} = 'Your template is out of date (see bug 180)';
397 my $no_categories_p = 1;
399 foreach my $cat ( @{ $branch->{'categories'} } ) {
400 my ($catinfo) = @{ GetBranchCategory($cat) };
401 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
402 $no_categories_p = 0;
405 # {{{ Leave this here until bug 180 is completely resolved in templates
406 $row{'categories'} = 'Your template is out of date (see bug 180)';
409 $row{'category_list'} = \@categories;
410 $row{'no-categories-p'} = $no_categories_p;
412 # Handle all other fields
413 $row{'branch_name'} = $branch->{'branchname'};
414 $row{'branch_code'} = $branch->{'branchcode'};
415 $row{'toggle'} = $toggle;
416 $row{'value'} = $branch->{'branchcode'};
417 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
419 push @loop_data, {%row};
422 my @branchcategories = ();
423 for my $ctype ( GetCategoryTypes() ) {
424 my $catinfo = GetBranchCategories(undef,$ctype);
426 foreach my $cat (@$catinfo) {
429 categoryname => $cat->{'categoryname'},
430 categorycode => $cat->{'categorycode'},
431 codedescription => $cat->{'codedescription'},
432 categorytype => $cat->{'categorytype'},
435 push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
437 $innertemplate->param(
438 branches => \@loop_data,
439 branchcategories => \@branchcategories
444 # FIXME logic seems wrong ## sub is not used.
445 sub branchcategoriestable {
446 my $innertemplate = shift;
447 #Needs to be implemented...
449 my $categoryinfo = GetBranchCategory();
451 foreach my $cat (@$categoryinfo) {
452 $innertemplate->param( categoryname => $cat->{'categoryname'} );
453 $innertemplate->param( categorycode => $cat->{'categorycode'} );
454 $innertemplate->param( codedescription => $cat->{'codedescription'} );
458 output_html_with_http_headers $input, $cookie, $template->output;