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 ) {
236 if $oldprinter and ( $oldprinter eq $printers->{$thisprinter} );
239 value => $thisprinter,
240 selected => $selected,
241 branchprinter => $printers->{$thisprinter}->{'printqueue'},
243 push @printerloop, \%row;
246 $innertemplate->param(
247 printerloop => \@printerloop,
248 branchcode => $data->{'branchcode'},
249 branch_name => $data->{'branchname'},
250 branchaddress1 => $data->{'branchaddress1'},
251 branchaddress2 => $data->{'branchaddress2'},
252 branchaddress3 => $data->{'branchaddress3'},
253 branchphone => $data->{'branchphone'},
254 branchfax => $data->{'branchfax'},
255 branchemail => $data->{'branchemail'},
256 branchip => $data->{'branchip'}
259 else { #case of an add branch select printer
260 foreach my $thisprinter ( keys %$printers ) {
262 value => $thisprinter,
263 branchprinter => $printers->{$thisprinter}->{'printqueue'},
265 push @printerloop, \%row;
267 $innertemplate->param( printerloop => \@printerloop );
270 # make the checkboxs.....
272 # We export a "categoryloop" array to the template, each element of which
273 # contains separate 'categoryname', 'categorycode', 'codedescription', and
274 # 'checked' fields. The $checked field is either '' or 'checked'
277 my $catinfo = GetBranchCategory();
280 # print DEBUG "catinfo=".cvs($catinfo)."\n";
281 my @categoryloop = ();
282 foreach my $cat (@$catinfo) {
284 my $tmp = quotemeta( $cat->{'categorycode'} );
285 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
286 $checked = "checked=\"checked\"";
290 categoryname => $cat->{'categoryname'},
291 categorycode => $cat->{'categorycode'},
292 categorytype => $cat->{'categorytype'},
293 codedescription => $cat->{'codedescription'},
297 $innertemplate->param( categoryloop => \@categoryloop );
299 # {{{ Leave this here until bug 130 is completely resolved in the templates
300 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
301 $innertemplate->param(
302 $obsolete => 'Your template is out of date (bug 130)' );
310 # prepares the edit form...
311 my ($categorycode,$innertemplate) = @_;
312 warn "cat : $categorycode";
315 $innertemplate->param( categorytype => \@cats);
317 $data = GetBranchCategory($categorycode);
319 $innertemplate->param( categorycode => $data->{'categorycode'} ,
320 categoryname => $data->{'categoryname'},
321 codedescription => $data->{'codedescription'} ,
324 for my $ctype (GetCategoryTypes()) {
325 push @cats , { type => $ctype , selected => ($data->{'categorytype'} eq $ctype) };
331 # message to print if the
332 my ($branchcode) = @_;
335 sub branchinfotable {
337 # makes the html for a table of branch info from reference to an array of hashs.
339 my ($branchcode,$innertemplate) = @_;
342 $branchinfo = GetBranchInfo($branchcode);
345 $branchinfo = GetBranchInfo();
350 foreach my $branch (@$branchinfo) {
351 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
354 # We export the following fields to the template. These are not
355 # pre-composed as a single "address" field because the template
356 # might (and should) escape what is exported here. (See bug 180)
359 # - branch_name (Note: not "branchname")
360 # - branch_code (Note: not "branchcode")
361 # - address (containing a static error message)
364 # - branchaddress3 | comprising the old "address" field
368 # - address-empty-p (1 if no address information, 0 otherwise)
369 # - categories (containing a static error message)
370 # - category_list (loop containing "categoryname")
371 # - no-categories-p (1 if no categories set, 0 otherwise)
377 # Handle address fields separately
378 my $address_empty_p = 1;
380 'branchaddress1', 'branchaddress2',
381 'branchaddress3', 'branchphone',
382 'branchfax', 'branchemail',
383 'branchip', 'branchprinter'
386 $row{$field} = $branch->{$field};
387 if ( $branch->{$field} ) {
388 $address_empty_p = 0;
391 $row{'address-empty-p'} = $address_empty_p;
393 # {{{ Leave this here until bug 180 is completely resolved in templates
394 $row{'address'} = 'Your template is out of date (see bug 180)';
399 my $no_categories_p = 1;
401 foreach my $cat ( @{ $branch->{'categories'} } ) {
402 my ($catinfo) = @{ GetBranchCategory($cat) };
403 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
404 $no_categories_p = 0;
407 # {{{ Leave this here until bug 180 is completely resolved in templates
408 $row{'categories'} = 'Your template is out of date (see bug 180)';
411 $row{'category_list'} = \@categories;
412 $row{'no-categories-p'} = $no_categories_p;
414 # Handle all other fields
415 $row{'branch_name'} = $branch->{'branchname'};
416 $row{'branch_code'} = $branch->{'branchcode'};
417 $row{'toggle'} = $toggle;
418 $row{'value'} = $branch->{'branchcode'};
419 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
421 push @loop_data, {%row};
424 my @branchcategories = ();
425 for my $ctype ( GetCategoryTypes() ) {
426 my $catinfo = GetBranchCategories(undef,$ctype);
428 foreach my $cat (@$catinfo) {
431 categoryname => $cat->{'categoryname'},
432 categorycode => $cat->{'categorycode'},
433 codedescription => $cat->{'codedescription'},
434 categorytype => $cat->{'categorytype'},
437 push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
439 $innertemplate->param(
440 branches => \@loop_data,
441 branchcategories => \@branchcategories
446 # FIXME logic seems wrong ## sub is not used.
447 sub branchcategoriestable {
448 my $innertemplate = shift;
449 #Needs to be implemented...
451 my $categoryinfo = GetBranchCategory();
453 foreach my $cat (@$categoryinfo) {
454 $innertemplate->param( categoryname => $cat->{'categoryname'} );
455 $innertemplate->param( categorycode => $cat->{'categorycode'} );
456 $innertemplate->param( codedescription => $cat->{'codedescription'} );
460 output_html_with_http_headers $input, $cookie, $template->output;