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.....
51 use C4::Interface::CGI::Output;
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');
67 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
69 template_name => "admin/branches.tmpl",
73 flagsrequired => { parameters => 1},
79 script_name => $script_name,
81 ); # we show only the TMPL_VAR names $op
85 script_name => $script_name,
87 ); # we show only the TMPL_VAR names $op
89 $template->param( action => $script_name );
92 # If the user has pressed the "add new branch" button.
93 $template->param( 'heading-branches-add-branch-p' => 1 );
94 editbranchform($branchcode,$template);
97 elsif ( $op eq 'edit' ) {
99 # if the user has pressed the "edit branch settings" button.
100 $template->param( 'heading-branches-add-branch-p' => 0,
102 editbranchform($branchcode,$template);
104 elsif ( $op eq 'add_validate' ) {
106 # confirm settings change...
107 my $params = $input->Vars;
108 unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
109 $template->param( else => 1 );
110 default("MESSAGE1",$template);
114 $template->param( else => 1 );
115 default("MESSAGE2",$template);
118 elsif ( $op eq 'delete' ) {
119 # if the user has pressed the "delete branch" button.
121 # check to see if the branchcode is being used in the database somewhere....
122 my $dbh = C4::Context->dbh;
123 my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
124 $sth->execute( $branchcode, $branchcode );
125 my ($total) = $sth->fetchrow_array;
131 $message = "MESSAGE7";
135 $template->param( else => 1 );
136 default($message,$template);
139 $template->param( branchname => $branchname );
140 $template->param( delete_confirm => 1 );
141 $template->param( branchcode => $branchcode );
144 elsif ( $op eq 'delete_confirmed' ) {
146 # actually delete branch and return to the main screen....
147 DelBranch($branchcode);
148 $template->param( else => 1 );
149 default("MESSAGE3",$template);
151 elsif ( $op eq 'editcategory' ) {
153 # If the user has pressed the "add new category" or "modify" buttons.
154 $template->param( 'heading-branches-edit-category-p' => 1 );
155 editcatform($categorycode,$template);
157 elsif ( $op eq 'addcategory_validate' ) {
159 # confirm settings change...
160 my $params = $input->Vars;
161 unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
162 $template->param( else => 1 );
163 default("MESSAGE4",$template);
166 ModBranchCategoryInfo($params);
167 $template->param( else => 1 );
168 default("MESSAGE5",$template);
171 elsif ( $op eq 'delete_category' ) {
173 # if the user has pressed the "delete branch" button.
174 my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
176 $template->param( else => 1 );
177 default($message,$template);
180 $template->param( delete_category => 1 );
181 $template->param( categorycode => $categorycode );
184 elsif ( $op eq 'categorydelete_confirmed' ) {
186 # actually delete branch and return to the main screen....
187 DeleteBranchCategory($categorycode);
188 $template->param( else => 1 );
189 default("MESSAGE6",$template);
194 # if no operation has been set...
195 default("",$template);
198 ################################################################################
200 # html output functions....
203 my ($message,$innertemplate) = @_;
204 $innertemplate->param( 'heading-branches-p' => 1 );
205 $innertemplate->param( "$message" => 1 );
206 $innertemplate->param( action => $script_name );
207 branchinfotable("",$innertemplate);
211 my ($branchcode,$innertemplate) = @_;
212 # initiate the scrolling-list to select the printers
214 my $printers = GetPrinters( \%env );
216 my $printercount = 0;
223 $data = GetBranchInfo($branchcode);
226 # get the old printer of the branch
227 $oldprinter = $data->{'branchprinter'};
230 foreach my $thisprinter ( keys %$printers ) {
232 if $oldprinter eq $printers->{$thisprinter}->{'printqueue'};
234 value => $thisprinter,
235 selected => $selected,
236 branchprinter => $printers->{$thisprinter}->{'printqueue'},
238 push @printerloop, \%row;
241 $innertemplate->param(
242 printerloop => \@printerloop,
243 branchcode => $data->{'branchcode'},
244 branch_name => $data->{'branchname'},
245 branchaddress1 => $data->{'branchaddress1'},
246 branchaddress2 => $data->{'branchaddress2'},
247 branchaddress3 => $data->{'branchaddress3'},
248 branchphone => $data->{'branchphone'},
249 branchfax => $data->{'branchfax'},
250 branchemail => $data->{'branchemail'},
251 branchip => $data->{'branchip'}
254 else { #case of an add branch select printer
255 foreach my $thisprinter ( keys %$printers ) {
257 value => $thisprinter,
258 branchprinter => $printers->{$thisprinter}->{'printqueue'},
260 push @printerloop, \%row;
262 $innertemplate->param( printerloop => \@printerloop );
265 # make the checkboxs.....
267 # We export a "categoryloop" array to the template, each element of which
268 # contains separate 'categoryname', 'categorycode', 'codedescription', and
269 # 'checked' fields. The $checked field is either '' or 'checked'
272 my $catinfo = GetBranchCategory();
275 # print DEBUG "catinfo=".cvs($catinfo)."\n";
276 my @categoryloop = ();
277 foreach my $cat (@$catinfo) {
279 my $tmp = quotemeta( $cat->{'categorycode'} );
280 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
281 $checked = "checked=\"checked\"";
285 categoryname => $cat->{'categoryname'},
286 categorycode => $cat->{'categorycode'},
287 codedescription => $cat->{'codedescription'},
291 $innertemplate->param( categoryloop => \@categoryloop );
293 # {{{ Leave this here until bug 130 is completely resolved in the templates
294 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
295 $innertemplate->param(
296 $obsolete => 'Your template is out of date (bug 130)' );
304 # prepares the edit form...
305 my ($categorycode,$innertemplate) = @_;
306 warn "cat : $categorycode";
309 $data = GetBranchCategory($categorycode);
311 $innertemplate->param( categorycode => $data->{'categorycode'} );
312 $innertemplate->param( categoryname => $data->{'categoryname'} );
313 $innertemplate->param( codedescription => $data->{'codedescription'} );
319 # message to print if the
320 my ($branchcode) = @_;
323 sub branchinfotable {
325 # makes the html for a table of branch info from reference to an array of hashs.
327 my ($branchcode,$innertemplate) = @_;
330 $branchinfo = GetBranchInfo($branchcode);
333 $branchinfo = GetBranchInfo();
338 foreach my $branch (@$branchinfo) {
339 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
342 # We export the following fields to the template. These are not
343 # pre-composed as a single "address" field because the template
344 # might (and should) escape what is exported here. (See bug 180)
347 # - branch_name (Note: not "branchname")
348 # - branch_code (Note: not "branchcode")
349 # - address (containing a static error message)
352 # - branchaddress3 | comprising the old "address" field
356 # - address-empty-p (1 if no address information, 0 otherwise)
357 # - categories (containing a static error message)
358 # - category_list (loop containing "categoryname")
359 # - no-categories-p (1 if no categories set, 0 otherwise)
365 # Handle address fields separately
366 my $address_empty_p = 1;
368 'branchaddress1', 'branchaddress2',
369 'branchaddress3', 'branchphone',
370 'branchfax', 'branchemail',
371 'branchip', 'branchprinter'
374 $row{$field} = $branch->{$field};
375 if ( $branch->{$field} ) {
376 $address_empty_p = 0;
379 $row{'address-empty-p'} = $address_empty_p;
381 # {{{ Leave this here until bug 180 is completely resolved in templates
382 $row{'address'} = 'Your template is out of date (see bug 180)';
387 my $no_categories_p = 1;
389 foreach my $cat ( @{ $branch->{'categories'} } ) {
390 my ($catinfo) = @{ GetBranchCategory($cat) };
391 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
392 $no_categories_p = 0;
395 # {{{ Leave this here until bug 180 is completely resolved in templates
396 $row{'categories'} = 'Your template is out of date (see bug 180)';
399 $row{'category_list'} = \@categories;
400 $row{'no-categories-p'} = $no_categories_p;
402 # Handle all other fields
403 $row{'branch_name'} = $branch->{'branchname'};
404 $row{'branch_code'} = $branch->{'branchcode'};
405 $row{'toggle'} = $toggle;
406 $row{'value'} = $branch->{'branchcode'};
407 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
409 push @loop_data, {%row};
412 my @branchcategories = ();
413 my $catinfo = GetBranchCategory();
415 foreach my $cat (@$catinfo) {
416 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
417 push @branchcategories,
420 categoryname => $cat->{'categoryname'},
421 categorycode => $cat->{'categorycode'},
422 codedescription => $cat->{'codedescription'},
427 $innertemplate->param(
428 branches => \@loop_data,
429 branchcategories => \@branchcategories
434 # FIXME logic seems wrong
435 sub branchcategoriestable {
436 my $innertemplate = shift;
437 #Needs to be implemented...
439 my $categoryinfo = GetBranchCategory();
441 foreach my $cat (@$categoryinfo) {
442 $innertemplate->param( categoryname => $cat->{'categoryname'} );
443 $innertemplate->param( categorycode => $cat->{'categorycode'} );
444 $innertemplate->param( codedescription => $cat->{'codedescription'} );
449 intranetcolorstylesheet =>
450 C4::Context->preference("intranetcolorstylesheet"),
451 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
452 IntranetNav => C4::Context->preference("IntranetNav"),
454 output_html_with_http_headers $input, $cookie, $template->output;