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
213 my $printers = GetPrinters();
215 my $printercount = 0;
222 $data = GetBranchInfo($branchcode);
225 # get the old printer of the branch
226 $oldprinter = $data->{'branchprinter'};
229 foreach my $thisprinter ( keys %$printers ) {
231 if $oldprinter eq $printers->{$thisprinter}->{'printqueue'};
233 value => $thisprinter,
234 selected => $selected,
235 branchprinter => $printers->{$thisprinter}->{'printqueue'},
237 push @printerloop, \%row;
240 $innertemplate->param(
241 printerloop => \@printerloop,
242 branchcode => $data->{'branchcode'},
243 branch_name => $data->{'branchname'},
244 branchaddress1 => $data->{'branchaddress1'},
245 branchaddress2 => $data->{'branchaddress2'},
246 branchaddress3 => $data->{'branchaddress3'},
247 branchphone => $data->{'branchphone'},
248 branchfax => $data->{'branchfax'},
249 branchemail => $data->{'branchemail'},
250 branchip => $data->{'branchip'}
253 else { #case of an add branch select printer
254 foreach my $thisprinter ( keys %$printers ) {
256 value => $thisprinter,
257 branchprinter => $printers->{$thisprinter}->{'printqueue'},
259 push @printerloop, \%row;
261 $innertemplate->param( printerloop => \@printerloop );
264 # make the checkboxs.....
266 # We export a "categoryloop" array to the template, each element of which
267 # contains separate 'categoryname', 'categorycode', 'codedescription', and
268 # 'checked' fields. The $checked field is either '' or 'checked'
271 my $catinfo = GetBranchCategory();
274 # print DEBUG "catinfo=".cvs($catinfo)."\n";
275 my @categoryloop = ();
276 foreach my $cat (@$catinfo) {
278 my $tmp = quotemeta( $cat->{'categorycode'} );
279 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
280 $checked = "checked=\"checked\"";
284 categoryname => $cat->{'categoryname'},
285 categorycode => $cat->{'categorycode'},
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 $data = GetBranchCategory($categorycode);
310 $innertemplate->param( categorycode => $data->{'categorycode'} );
311 $innertemplate->param( categoryname => $data->{'categoryname'} );
312 $innertemplate->param( codedescription => $data->{'codedescription'} );
318 # message to print if the
319 my ($branchcode) = @_;
322 sub branchinfotable {
324 # makes the html for a table of branch info from reference to an array of hashs.
326 my ($branchcode,$innertemplate) = @_;
329 $branchinfo = GetBranchInfo($branchcode);
332 $branchinfo = GetBranchInfo();
337 foreach my $branch (@$branchinfo) {
338 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
341 # We export the following fields to the template. These are not
342 # pre-composed as a single "address" field because the template
343 # might (and should) escape what is exported here. (See bug 180)
346 # - branch_name (Note: not "branchname")
347 # - branch_code (Note: not "branchcode")
348 # - address (containing a static error message)
351 # - branchaddress3 | comprising the old "address" field
355 # - address-empty-p (1 if no address information, 0 otherwise)
356 # - categories (containing a static error message)
357 # - category_list (loop containing "categoryname")
358 # - no-categories-p (1 if no categories set, 0 otherwise)
364 # Handle address fields separately
365 my $address_empty_p = 1;
367 'branchaddress1', 'branchaddress2',
368 'branchaddress3', 'branchphone',
369 'branchfax', 'branchemail',
370 'branchip', 'branchprinter'
373 $row{$field} = $branch->{$field};
374 if ( $branch->{$field} ) {
375 $address_empty_p = 0;
378 $row{'address-empty-p'} = $address_empty_p;
380 # {{{ Leave this here until bug 180 is completely resolved in templates
381 $row{'address'} = 'Your template is out of date (see bug 180)';
386 my $no_categories_p = 1;
388 foreach my $cat ( @{ $branch->{'categories'} } ) {
389 my ($catinfo) = @{ GetBranchCategory($cat) };
390 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
391 $no_categories_p = 0;
394 # {{{ Leave this here until bug 180 is completely resolved in templates
395 $row{'categories'} = 'Your template is out of date (see bug 180)';
398 $row{'category_list'} = \@categories;
399 $row{'no-categories-p'} = $no_categories_p;
401 # Handle all other fields
402 $row{'branch_name'} = $branch->{'branchname'};
403 $row{'branch_code'} = $branch->{'branchcode'};
404 $row{'toggle'} = $toggle;
405 $row{'value'} = $branch->{'branchcode'};
406 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
408 push @loop_data, {%row};
411 my @branchcategories = ();
412 my $catinfo = GetBranchCategory();
414 foreach my $cat (@$catinfo) {
415 ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
416 push @branchcategories,
419 categoryname => $cat->{'categoryname'},
420 categorycode => $cat->{'categorycode'},
421 codedescription => $cat->{'codedescription'},
426 $innertemplate->param(
427 branches => \@loop_data,
428 branchcategories => \@branchcategories
433 # FIXME logic seems wrong
434 sub branchcategoriestable {
435 my $innertemplate = shift;
436 #Needs to be implemented...
438 my $categoryinfo = GetBranchCategory();
440 foreach my $cat (@$categoryinfo) {
441 $innertemplate->param( categoryname => $cat->{'categoryname'} );
442 $innertemplate->param( categorycode => $cat->{'categorycode'} );
443 $innertemplate->param( codedescription => $cat->{'codedescription'} );
448 intranetcolorstylesheet =>
449 C4::Context->preference("intranetcolorstylesheet"),
450 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
451 IntranetNav => C4::Context->preference("IntranetNav"),
453 output_html_with_http_headers $input, $cookie, $template->output;