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.....
55 my $script_name = "/cgi-bin/koha/admin/branches.pl";
58 ################################################################################
61 my $branchcode = $input->param('branchcode');
62 my $branchname = $input->param('branchname');
63 my $categorycode = $input->param('categorycode');
64 my $op = $input->param('op');
66 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
68 template_name => "admin/branches.tmpl",
72 flagsrequired => { parameters => 1},
78 script_name => $script_name,
80 ); # we show only the TMPL_VAR names $op
84 script_name => $script_name,
86 ); # we show only the TMPL_VAR names $op
88 $template->param( action => $script_name );
91 # If the user has pressed the "add new branch" button.
92 $template->param( 'heading-branches-add-branch-p' => 1 );
93 editbranchform($branchcode,$template);
96 elsif ( $op eq 'edit' ) {
98 # if the user has pressed the "edit branch settings" button.
99 $template->param( 'heading-branches-add-branch-p' => 0,
101 editbranchform($branchcode,$template);
103 elsif ( $op eq 'add_validate' ) {
105 # confirm settings change...
106 my $params = $input->Vars;
107 unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
108 $template->param( else => 1 );
109 default("MESSAGE1",$template);
113 $template->param( else => 1 );
114 default("MESSAGE2",$template);
117 elsif ( $op eq 'delete' ) {
118 # if the user has pressed the "delete branch" button.
120 # check to see if the branchcode is being used in the database somewhere....
121 my $dbh = C4::Context->dbh;
122 my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
123 $sth->execute( $branchcode, $branchcode );
124 my ($total) = $sth->fetchrow_array;
130 $message = "MESSAGE7";
134 $template->param( else => 1 );
135 default($message,$template);
138 $template->param( branchname => $branchname );
139 $template->param( delete_confirm => 1 );
140 $template->param( branchcode => $branchcode );
143 elsif ( $op eq 'delete_confirmed' ) {
145 # actually delete branch and return to the main screen....
146 DelBranch($branchcode);
147 $template->param( else => 1 );
148 default("MESSAGE3",$template);
150 elsif ( $op eq 'editcategory' ) {
152 # If the user has pressed the "add new category" or "modify" buttons.
153 $template->param( 'heading-branches-edit-category-p' => 1 );
154 editcatform($categorycode,$template);
156 elsif ( $op eq 'addcategory_validate' ) {
158 # confirm settings change...
159 my $params = $input->Vars;
160 unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
161 $template->param( else => 1 );
162 default("MESSAGE4",$template);
165 ModBranchCategoryInfo($params);
166 $template->param( else => 1 );
167 default("MESSAGE5",$template);
170 elsif ( $op eq 'delete_category' ) {
172 # if the user has pressed the "delete branch" button.
173 my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
175 $template->param( else => 1 );
176 default($message,$template);
179 $template->param( delete_category => 1 );
180 $template->param( categorycode => $categorycode );
183 elsif ( $op eq 'categorydelete_confirmed' ) {
185 # actually delete branch and return to the main screen....
186 DelBranchCategory($categorycode);
187 $template->param( else => 1 );
188 default("MESSAGE6",$template);
193 # if no operation has been set...
194 default("",$template);
197 ################################################################################
199 # html output functions....
202 my ($message,$innertemplate) = @_;
203 $innertemplate->param( 'heading-branches-p' => 1 );
204 $innertemplate->param( "$message" => 1 );
205 $innertemplate->param( action => $script_name );
206 branchinfotable("",$innertemplate);
210 my ($branchcode,$innertemplate) = @_;
211 # initiate the scrolling-list to select the printers
212 my $printers = GetPrinters();
214 my $printercount = 0;
221 $data = GetBranchInfo($branchcode);
224 # get the old printer of the branch
225 $oldprinter = $data->{'branchprinter'};
228 foreach my $thisprinter ( keys %$printers ) {
230 if $oldprinter eq $printers->{$thisprinter}->{'printqueue'};
232 value => $thisprinter,
233 selected => $selected,
234 branchprinter => $printers->{$thisprinter}->{'printqueue'},
236 push @printerloop, \%row;
239 $innertemplate->param(
240 printerloop => \@printerloop,
241 branchcode => $data->{'branchcode'},
242 branch_name => $data->{'branchname'},
243 branchaddress1 => $data->{'branchaddress1'},
244 branchaddress2 => $data->{'branchaddress2'},
245 branchaddress3 => $data->{'branchaddress3'},
246 branchphone => $data->{'branchphone'},
247 branchfax => $data->{'branchfax'},
248 branchemail => $data->{'branchemail'},
249 branchip => $data->{'branchip'}
252 else { #case of an add branch select printer
253 foreach my $thisprinter ( keys %$printers ) {
255 value => $thisprinter,
256 branchprinter => $printers->{$thisprinter}->{'printqueue'},
258 push @printerloop, \%row;
260 $innertemplate->param( printerloop => \@printerloop );
263 # make the checkboxs.....
265 # We export a "categoryloop" array to the template, each element of which
266 # contains separate 'categoryname', 'categorycode', 'codedescription', and
267 # 'checked' fields. The $checked field is either '' or 'checked'
270 my $catinfo = GetBranchCategory();
273 # print DEBUG "catinfo=".cvs($catinfo)."\n";
274 my @categoryloop = ();
275 foreach my $cat (@$catinfo) {
277 my $tmp = quotemeta( $cat->{'categorycode'} );
278 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
279 $checked = "checked=\"checked\"";
283 categoryname => $cat->{'categoryname'},
284 categorycode => $cat->{'categorycode'},
285 codedescription => $cat->{'codedescription'},
289 $innertemplate->param( categoryloop => \@categoryloop );
291 # {{{ Leave this here until bug 130 is completely resolved in the templates
292 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
293 $innertemplate->param(
294 $obsolete => 'Your template is out of date (bug 130)' );
302 # prepares the edit form...
303 my ($categorycode,$innertemplate) = @_;
304 warn "cat : $categorycode";
307 $data = GetBranchCategory($categorycode);
309 $innertemplate->param( categorycode => $data->{'categorycode'} );
310 $innertemplate->param( categoryname => $data->{'categoryname'} );
311 $innertemplate->param( codedescription => $data->{'codedescription'} );
317 # message to print if the
318 my ($branchcode) = @_;
321 sub branchinfotable {
323 # makes the html for a table of branch info from reference to an array of hashs.
325 my ($branchcode,$innertemplate) = @_;
328 $branchinfo = GetBranchInfo($branchcode);
331 $branchinfo = GetBranchInfo();
336 foreach my $branch (@$branchinfo) {
337 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
340 # We export the following fields to the template. These are not
341 # pre-composed as a single "address" field because the template
342 # might (and should) escape what is exported here. (See bug 180)
345 # - branch_name (Note: not "branchname")
346 # - branch_code (Note: not "branchcode")
347 # - address (containing a static error message)
350 # - branchaddress3 | comprising the old "address" field
354 # - address-empty-p (1 if no address information, 0 otherwise)
355 # - categories (containing a static error message)
356 # - category_list (loop containing "categoryname")
357 # - no-categories-p (1 if no categories set, 0 otherwise)
363 # Handle address fields separately
364 my $address_empty_p = 1;
366 'branchaddress1', 'branchaddress2',
367 'branchaddress3', 'branchphone',
368 'branchfax', 'branchemail',
369 'branchip', 'branchprinter'
372 $row{$field} = $branch->{$field};
373 if ( $branch->{$field} ) {
374 $address_empty_p = 0;
377 $row{'address-empty-p'} = $address_empty_p;
379 # {{{ Leave this here until bug 180 is completely resolved in templates
380 $row{'address'} = 'Your template is out of date (see bug 180)';
385 my $no_categories_p = 1;
387 foreach my $cat ( @{ $branch->{'categories'} } ) {
388 my ($catinfo) = @{ GetBranchCategory($cat) };
389 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
390 $no_categories_p = 0;
393 # {{{ Leave this here until bug 180 is completely resolved in templates
394 $row{'categories'} = 'Your template is out of date (see bug 180)';
397 $row{'category_list'} = \@categories;
398 $row{'no-categories-p'} = $no_categories_p;
400 # Handle all other fields
401 $row{'branch_name'} = $branch->{'branchname'};
402 $row{'branch_code'} = $branch->{'branchcode'};
403 $row{'toggle'} = $toggle;
404 $row{'value'} = $branch->{'branchcode'};
405 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
407 push @loop_data, {%row};
410 my @branchcategories = ();
411 my $catinfo = GetBranchCategory();
413 foreach my $cat (@$catinfo) {
414 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
415 push @branchcategories,
418 categoryname => $cat->{'categoryname'},
419 categorycode => $cat->{'categorycode'},
420 codedescription => $cat->{'codedescription'},
425 $innertemplate->param(
426 branches => \@loop_data,
427 branchcategories => \@branchcategories
432 # FIXME logic seems wrong
433 sub branchcategoriestable {
434 my $innertemplate = shift;
435 #Needs to be implemented...
437 my $categoryinfo = GetBranchCategory();
439 foreach my $cat (@$categoryinfo) {
440 $innertemplate->param( categoryname => $cat->{'categoryname'} );
441 $innertemplate->param( categorycode => $cat->{'categorycode'} );
442 $innertemplate->param( codedescription => $cat->{'codedescription'} );
447 intranetcolorstylesheet =>
448 C4::Context->preference("intranetcolorstylesheet"),
449 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
450 IntranetNav => C4::Context->preference("IntranetNav"),
452 output_html_with_http_headers $input, $cookie, $template->output;