followup da49fc77df9 enh 3736
[koha.git] / admin / branches.pl
blob744ccfbd0c708ebe654b444399f7f447ee315b8e
1 #!/usr/bin/perl
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
10 # version.
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
20 =head1 branches.pl
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
26 notify translators
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
30 translators
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 Finlay working on this file from 26-03-2002
36 Reorganising this branches admin page.....
38 =cut
40 use strict;
41 use warnings;
42 use CGI;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46 use C4::Koha;
47 use C4::Branch;
49 # Fixed variables
50 my $script_name = "/cgi-bin/koha/admin/branches.pl";
52 ################################################################################
53 # Main loop....
54 my $input = new CGI;
55 my $branchcode = $input->param('branchcode');
56 my $branchname = $input->param('branchname');
57 my $categorycode = $input->param('categorycode');
58 my $op = $input->param('op') || '';
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62 template_name => "admin/branches.tmpl",
63 query => $input,
64 type => "intranet",
65 authnotrequired => 0,
66 flagsrequired => { parameters => 1},
67 debug => 1,
70 $template->param(
71 script_name => $script_name,
72 action => $script_name,
74 $template->param( ($op || 'else') => 1 );
76 if ( $op eq 'add' ) {
78 # If the user has pressed the "add new branch" button.
79 $template->param( 'heading-branches-add-branch-p' => 1 );
80 editbranchform($branchcode,$template);
83 elsif ( $op eq 'edit' ) {
85 # if the user has pressed the "edit branch settings" button.
86 $template->param( 'heading-branches-add-branch-p' => 0,
87 'add' => 1, );
88 editbranchform($branchcode,$template);
90 elsif ( $op eq 'add_validate' ) {
92 # confirm settings change...
93 my $params = $input->Vars;
94 unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
95 $template->param( else => 1 );
96 default("MESSAGE1",$template);
98 else {
99 my $error = ModBranch($params); # FIXME: causes warnings to log on duplicate branchcode
100 # if error saving, stay on edit and rise error
101 if ($error) {
102 # copy input parameters back to form
103 # FIXME - doing this doesn't preserve any branch group selections, but good enough for now
104 editbranchform($branchcode,$template);
105 $template->param( 'heading-branches-add-branch-p' => 1, 'add' => 1, "ERROR$error" => 1 );
106 } else {
107 $template->param( else => 1);
108 default("MESSAGE2",$template);
112 elsif ( $op eq 'delete' ) {
113 # if the user has pressed the "delete branch" button.
115 # check to see if the branchcode is being used in the database somewhere....
116 my $dbh = C4::Context->dbh;
117 my $sthitems = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
118 my $sthborrowers = $dbh->prepare("select count(*) from borrowers where branchcode=?");
119 $sthitems->execute( $branchcode, $branchcode );
120 $sthborrowers->execute( $branchcode );
121 my ($totalitems) = $sthitems->fetchrow_array;
122 my ($totalborrowers) = $sthitems->fetchrow_array;
123 if ($totalitems or $totalborrowers) {
124 $template->param( else => 1 );
125 default("MESSAGE7", $template);
127 else {
128 $template->param( delete_confirm => 1 );
129 $template->param( branchname => $branchname );
130 $template->param( branchcode => $branchcode );
133 elsif ( $op eq 'delete_confirmed' ) {
135 # actually delete branch and return to the main screen....
136 DelBranch($branchcode);
137 $template->param( else => 1 );
138 default("MESSAGE3",$template);
140 elsif ( $op eq 'editcategory' ) {
142 # If the user has pressed the "add new category" or "modify" buttons.
143 $template->param( 'heading-branches-edit-category-p' => 1 );
144 editcatform($categorycode,$template);
146 elsif ( $op eq 'addcategory_validate' ) {
148 $template->param( else => 1 );
149 # confirm settings change...
150 my $params = $input->Vars;
151 unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
152 default("MESSAGE4",$template);
154 else {
155 ModBranchCategoryInfo($params);
156 default("MESSAGE5",$template);
159 elsif ( $op eq 'delete_category' ) {
161 # if the user has pressed the "delete branch" button.
162 my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
163 if ($message) {
164 $template->param( else => 1 );
165 default($message,$template);
167 else {
168 $template->param( delete_category => 1 );
169 $template->param( categorycode => $categorycode );
172 elsif ( $op eq 'categorydelete_confirmed' ) {
174 # actually delete branch and return to the main screen....
175 DelBranchCategory($categorycode);
176 $template->param( else => 1 );
177 default("MESSAGE6",$template);
180 else {
181 # if no operation has been set...
182 default("",$template);
185 ################################################################################
187 # html output functions....
189 sub default {
190 my $message = shift || '';
191 my $innertemplate = shift or return;
192 $innertemplate->param($message => 1) if $message;
193 $innertemplate->param(
194 'heading-branches-p' => 1,
196 branchinfotable("",$innertemplate);
199 sub editbranchform {
200 my ($branchcode,$innertemplate) = @_;
201 # initiate the scrolling-list to select the printers
202 my $printers = GetPrinters();
203 my @printerloop;
204 my $data;
205 my $oldprinter = "";
207 if ($branchcode) {
208 $data = GetBranchInfo($branchcode);
209 $data = $data->[0];
211 # get the old printer of the branch
212 $oldprinter = $data->{'branchprinter'} || '';
213 $innertemplate->param(
214 branchcode => $data->{'branchcode'},
215 branch_name => $data->{'branchname'},
216 branchaddress1 => $data->{'branchaddress1'},
217 branchaddress2 => $data->{'branchaddress2'},
218 branchaddress3 => $data->{'branchaddress3'},
219 branchzip => $data->{'branchzip'},
220 branchcity => $data->{'branchcity'},
221 branchcountry => $data->{'branchcountry'},
222 branchphone => $data->{'branchphone'},
223 branchfax => $data->{'branchfax'},
224 branchemail => $data->{'branchemail'},
225 branchurl => $data->{'branchurl'},
226 branchip => $data->{'branchip'},
227 branchnotes => $data->{'branchnotes'},
231 foreach my $thisprinter ( keys %$printers ) {
232 push @printerloop, {
233 value => $thisprinter,
234 selected => ( $oldprinter eq $printers->{$thisprinter} ),
235 branchprinter => $printers->{$thisprinter}->{'printqueue'},
239 $innertemplate->param( printerloop => \@printerloop );
240 # make the checkboxes.....
242 # We export a "categoryloop" array to the template, each element of which
243 # contains separate 'categoryname', 'categorycode', 'codedescription', and
244 # 'checked' fields. The $checked field is either '' or 'checked="checked"'
246 my $catinfo = GetBranchCategory();
247 my @categoryloop = ();
248 foreach my $cat (@$catinfo) {
249 my $checked = "";
250 my $tmp = quotemeta( $cat->{'categorycode'} );
251 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
252 $checked = "checked=\"checked\"";
254 push @categoryloop, {
255 categoryname => $cat->{'categoryname'},
256 categorycode => $cat->{'categorycode'},
257 categorytype => $cat->{'categorytype'},
258 codedescription => $cat->{'codedescription'},
259 checked => $checked,
262 $innertemplate->param( categoryloop => \@categoryloop );
264 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
265 $innertemplate->param(
266 $obsolete => 'Your template is out of date (bug 130)' );
270 sub editcatform {
272 # prepares the edit form...
273 my ($categorycode,$innertemplate) = @_;
274 # warn "cat : $categorycode";
275 my @cats;
276 my $data;
277 if ($categorycode) {
278 my $data = GetBranchCategory($categorycode);
279 $data = $data->[0];
280 $innertemplate->param(
281 categorycode => $data->{'categorycode'},
282 categoryname => $data->{'categoryname'},
283 codedescription => $data->{'codedescription'},
286 for my $ctype (GetCategoryTypes()) {
287 push @cats , { type => $ctype , selected => ($data->{'categorytype'} and $data->{'categorytype'} eq $ctype) };
289 $innertemplate->param(categorytype => \@cats);
292 sub branchinfotable {
294 # makes the html for a table of branch info from reference to an array of hashs.
296 my ($branchcode,$innertemplate) = @_;
297 my $branchinfo = $branchcode ? GetBranchInfo($branchcode) : GetBranchInfo();
298 my @loop_data = ();
299 foreach my $branch (@$branchinfo) {
301 # We export the following fields to the template. These are not
302 # pre-composed as a single "address" field because the template
303 # might (and should) escape what is exported here. (See bug 180)
305 # - branch_name (Note: not "branchname")
306 # - branch_code (Note: not "branchcode")
307 # - address (containing a static error message)
308 # - branchaddress1 \
309 # - branchaddress2 |
310 # - branchaddress3 | comprising the old "address" field
311 # - branchzip |
312 # - branchcity |
313 # - branchcountry |
314 # - branchphone |
315 # - branchfax |
316 # - branchemail /
317 # - branchurl /
318 # - address-empty-p (1 if no address information, 0 otherwise)
319 # - categories (containing a static error message)
320 # - category_list (loop containing "categoryname")
321 # - no-categories-p (1 if no categories set, 0 otherwise)
322 # - value
324 my %row = ();
326 # Handle address fields separately
327 my $address_empty_p = 1;
328 for my $field (
329 'branchaddress1', 'branchaddress2',
330 'branchaddress3', 'branchzip',
331 'branchcity', 'branchcountry',
332 'branchphone', 'branchfax',
333 'branchemail', 'branchurl',
334 'branchip', 'branchprinter', 'branchnotes'
337 $row{$field} = $branch->{$field};
338 $address_empty_p = 0 if ( $branch->{$field} );
340 $row{'address-empty-p'} = $address_empty_p;
342 # Handle categories
343 my $no_categories_p = 1;
344 my @categories;
345 foreach my $cat ( @{ $branch->{'categories'} } ) {
346 my ($catinfo) = @{ GetBranchCategory($cat) };
347 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
348 $no_categories_p = 0;
351 $row{'category_list'} = \@categories;
352 $row{'no-categories-p'} = $no_categories_p;
353 $row{'branch_name'} = $branch->{'branchname'};
354 $row{'branch_code'} = $branch->{'branchcode'};
355 $row{'value'} = $branch->{'branchcode'};
357 push @loop_data, \%row;
359 my @branchcategories = ();
360 for my $ctype ( GetCategoryTypes() ) {
361 my $catinfo = GetBranchCategories(undef,$ctype);
362 my @categories;
363 foreach my $cat (@$catinfo) {
364 push @categories, {
365 categoryname => $cat->{'categoryname'},
366 categorycode => $cat->{'categorycode'},
367 codedescription => $cat->{'codedescription'},
368 categorytype => $cat->{'categorytype'},
371 push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
373 $innertemplate->param(
374 branches => \@loop_data,
375 branchcategories => \@branchcategories
380 output_html_with_http_headers $input, $cookie, $template->output;
382 # Local Variables:
383 # tab-width: 8
384 # End: