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 categorytype
=> $cat->{'categorytype'},
286 codedescription
=> $cat->{'codedescription'},
290 $innertemplate->param( categoryloop
=> \
@categoryloop );
292 # {{{ Leave this here until bug 130 is completely resolved in the templates
293 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
294 $innertemplate->param(
295 $obsolete => 'Your template is out of date (bug 130)' );
303 # prepares the edit form...
304 my ($categorycode,$innertemplate) = @_;
305 warn "cat : $categorycode";
308 $innertemplate->param( categorytype
=> \
@cats);
310 $data = GetBranchCategory
($categorycode);
312 $innertemplate->param( categorycode
=> $data->{'categorycode'} ,
313 categoryname
=> $data->{'categoryname'},
314 codedescription
=> $data->{'codedescription'} ,
317 for my $ctype (GetCategoryTypes
()) {
318 push @cats , { type
=> $ctype , selected
=> ($data->{'categorytype'} eq $ctype) };
324 # message to print if the
325 my ($branchcode) = @_;
328 sub branchinfotable
{
330 # makes the html for a table of branch info from reference to an array of hashs.
332 my ($branchcode,$innertemplate) = @_;
335 $branchinfo = GetBranchInfo
($branchcode);
338 $branchinfo = GetBranchInfo
(undef,'properties');
343 foreach my $branch (@
$branchinfo) {
344 ( $i % 2 ) ?
( $toggle = 1 ) : ( $toggle = 0 );
347 # We export the following fields to the template. These are not
348 # pre-composed as a single "address" field because the template
349 # might (and should) escape what is exported here. (See bug 180)
352 # - branch_name (Note: not "branchname")
353 # - branch_code (Note: not "branchcode")
354 # - address (containing a static error message)
357 # - branchaddress3 | comprising the old "address" field
361 # - address-empty-p (1 if no address information, 0 otherwise)
362 # - categories (containing a static error message)
363 # - category_list (loop containing "categoryname")
364 # - no-categories-p (1 if no categories set, 0 otherwise)
370 # Handle address fields separately
371 my $address_empty_p = 1;
373 'branchaddress1', 'branchaddress2',
374 'branchaddress3', 'branchphone',
375 'branchfax', 'branchemail',
376 'branchip', 'branchprinter'
379 $row{$field} = $branch->{$field};
380 if ( $branch->{$field} ) {
381 $address_empty_p = 0;
384 $row{'address-empty-p'} = $address_empty_p;
386 # {{{ Leave this here until bug 180 is completely resolved in templates
387 $row{'address'} = 'Your template is out of date (see bug 180)';
392 my $no_categories_p = 1;
394 foreach my $cat ( @
{ $branch->{'categories'} } ) {
395 my ($catinfo) = @
{ GetBranchCategory
($cat) };
396 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
397 $no_categories_p = 0;
400 # {{{ Leave this here until bug 180 is completely resolved in templates
401 $row{'categories'} = 'Your template is out of date (see bug 180)';
404 $row{'category_list'} = \
@categories;
405 $row{'no-categories-p'} = $no_categories_p;
407 # Handle all other fields
408 $row{'branch_name'} = $branch->{'branchname'};
409 $row{'branch_code'} = $branch->{'branchcode'};
410 $row{'toggle'} = $toggle;
411 $row{'value'} = $branch->{'branchcode'};
412 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
414 push @loop_data, {%row};
417 my @branchcategories = ();
418 for my $ctype ( GetCategoryTypes
() ) {
419 my $catinfo = GetBranchCategories
(undef,$ctype);
421 foreach my $cat (@
$catinfo) {
424 categoryname
=> $cat->{'categoryname'},
425 categorycode
=> $cat->{'categorycode'},
426 codedescription
=> $cat->{'codedescription'},
427 categorytype
=> $cat->{'categorytype'},
430 push @branchcategories, { categorytype
=> $ctype , $ctype => 1 , catloop
=> \
@categories};
432 $innertemplate->param(
433 branches
=> \
@loop_data,
434 branchcategories
=> \
@branchcategories
439 # FIXME logic seems wrong ## sub is not used.
440 sub branchcategoriestable
{
441 my $innertemplate = shift;
442 #Needs to be implemented...
444 my $categoryinfo = GetBranchCategory
();
446 foreach my $cat (@
$categoryinfo) {
447 $innertemplate->param( categoryname
=> $cat->{'categoryname'} );
448 $innertemplate->param( categorycode
=> $cat->{'categorycode'} );
449 $innertemplate->param( codedescription
=> $cat->{'codedescription'} );
453 output_html_with_http_headers
$input, $cookie, $template->output;