Bug 6554: Followup for preferences.pl
[koha.git] / admin / branches.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 branches.pl
21
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
34
35  Finlay working on this file from 26-03-2002
36  Reorganising this branches admin page.....
37
38 =cut
39
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;
48
49 # Fixed variables
50 my $script_name = "/cgi-bin/koha/admin/branches.pl";
51
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') || '';
59
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61     {
62         template_name   => "admin/branches.tmpl",
63         query           => $input,
64         type            => "intranet",
65         authnotrequired => 0,
66         flagsrequired   => { parameters => 'parameters_remaining_permissions'},
67         debug           => 1,
68     }
69 );
70 $template->param(
71      script_name => $script_name,
72      action      => $script_name,
73 );
74 $template->param( ($op || 'else') => 1 );
75
76 if ( $op eq 'add' ) {
77
78     # If the user has pressed the "add new branch" button.
79     $template->param( 'heading_branches_add_branch_p' => 1 );
80     editbranchform($branchcode,$template);
81
82 }
83 elsif ( $op eq 'edit' ) {
84
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);
89 }
90 elsif ( $op eq 'add_validate' ) {
91
92     # confirm settings change...
93     my $params = $input->Vars;
94     unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
95         $template->param( else => 1 );
96         default("MESSAGE1",$template);
97     }
98     else {
99         my $mod_branch = 1;
100         if ($params->{add}) {
101             my ($existing) =
102                 C4::Context->dbh->selectrow_array("SELECT count(*) FROM branches WHERE branchcode = ?", {}, $branchcode);
103             if ($existing > 0) {
104                 $mod_branch = 0;
105                 _branch_to_template($params, $template); # preserve most (FIXME) of user's input
106                 $template->param( 'heading_branches_add_branch_p' => 1, 'add' => 1, 'ERROR1' => 1 );
107             }
108         }
109         if ($mod_branch) {
110             my $error = ModBranch($params); # FIXME: causes warnings to log on duplicate branchcode
111             # if error saving, stay on edit and rise error
112             if ($error) {
113                 # copy input parameters back to form
114                 # FIXME - doing this doesn't preserve any branch group selections, but good enough for now
115                 editbranchform($branchcode,$template);
116                 $template->param( 'heading_branches_add_branch_p' => 1, 'add' => 1, "ERROR$error" => 1 );
117             } else {
118                 $template->param( else => 1);
119                 default("MESSAGE2",$template);
120             }
121         }
122     }
123 }
124 elsif ( $op eq 'delete' ) {
125     # if the user has pressed the "delete branch" button.
126     
127     # check to see if the branchcode is being used in the database somewhere....
128     my $dbh = C4::Context->dbh;
129     my $sthitems     = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
130     my $sthborrowers = $dbh->prepare("select count(*) from borrowers where branchcode=?");
131     $sthitems->execute( $branchcode, $branchcode );
132     $sthborrowers->execute( $branchcode );
133     my ($totalitems)     = $sthitems->fetchrow_array;
134     my ($totalborrowers) = $sthborrowers->fetchrow_array;
135     if ($totalitems && !$totalborrowers) {
136         $template->param( else => 1 );
137         default("MESSAGE10", $template);
138     }
139     elsif (!$totalitems && $totalborrowers){
140         $template->param( else => 1 );
141         default("MESSAGE11", $template);
142     }
143     elsif ($totalitems && $totalborrowers){
144         $template->param( else => 1 );
145         default("MESSAGE7", $template);
146     }
147     else {
148         $template->param( delete_confirm => 1 );
149         $template->param( branchname     => $branchname );
150         $template->param( branchcode     => $branchcode );
151     }
152 }
153 elsif ( $op eq 'delete_confirmed' ) {
154
155     # actually delete branch and return to the main screen....
156     DelBranch($branchcode);
157     $template->param( else => 1 );
158     default("MESSAGE3",$template);
159 }
160 elsif ( $op eq 'editcategory' ) {
161
162     # If the user has pressed the "add new category" or "modify" buttons.
163     $template->param( 'heading_branches_edit_category_p' => 1 );
164     editcatform($categorycode,$template);
165 }
166 elsif ( $op eq 'addcategory_validate' ) {
167
168     $template->param( else => 1 );
169     # confirm settings change...
170     my $params = $input->Vars;
171     $params->{'show_in_pulldown'} = ( $params->{'show_in_pulldown'} eq 'on' ) ? 1 : 0;
172
173     unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
174         default("MESSAGE4",$template);
175     }
176     elsif ($input->param('add')){
177         # doing an add must check the code is unique
178         if (CheckCategoryUnique($input->param('categorycode'))){
179             ModBranchCategoryInfo($params);
180         default("MESSAGE5",$template);
181         }
182         else {
183             default("MESSAGE9",$template);
184         }
185     }
186     else {
187         ModBranchCategoryInfo($params);
188         default("MESSAGE5",$template);
189     }
190 }
191 elsif ( $op eq 'delete_category' ) {
192
193     # if the user has pressed the "delete branch" button.
194     if ( CheckBranchCategorycode($categorycode) ) {
195         $template->param( else => 1 );
196         default( 'MESSAGE8', $template );
197     } else {
198         $template->param( delete_category => 1 );
199         $template->param( categorycode    => $categorycode );
200     }
201 }
202 elsif ( $op eq 'categorydelete_confirmed' ) {
203
204     # actually delete branch and return to the main screen....
205     DelBranchCategory($categorycode);
206     $template->param( else => 1 );
207     default("MESSAGE6",$template);
208
209 }
210 else {
211     # if no operation has been set...
212     default("",$template);
213 }
214
215 ################################################################################
216 #
217 # html output functions....
218
219 sub default {
220     my $message       = shift || '';
221     my $innertemplate = shift or return;
222     $innertemplate->param($message => 1) if $message;
223     $innertemplate->param(
224         'heading_branches_p' => 1,
225     );
226     branchinfotable("",$innertemplate);
227 }
228
229 sub editbranchform {
230     my ($branchcode,$innertemplate) = @_;
231     # initiate the scrolling-list to select the printers
232     my $printers = GetPrinters();
233     my @printerloop;
234     my $data;
235     my $oldprinter = "";
236
237     if ($branchcode) {
238         $data = GetBranchInfo($branchcode);
239         $data = $data->[0];
240
241         # get the old printer of the branch
242         $oldprinter = $data->{'branchprinter'} || '';
243         _branch_to_template($data, $innertemplate);
244     }
245
246     foreach my $thisprinter ( keys %$printers ) {
247         push @printerloop, {
248             value         => $thisprinter,
249             selected      => ( $oldprinter eq $printers->{$thisprinter} ),
250             branchprinter => $printers->{$thisprinter}->{'printqueue'},
251         };
252     }
253
254     $innertemplate->param( printerloop => \@printerloop );
255     # make the checkboxes.....
256     #
257     # We export a "categoryloop" array to the template, each element of which
258     # contains separate 'categoryname', 'categorycode', 'codedescription', and
259     # 'checked' fields. The $checked field is either empty or 1'
260
261     my $catinfo = GetBranchCategory();
262     my @categoryloop = ();
263     foreach my $cat (@$catinfo) {
264         my $checked;
265         my $tmp     = quotemeta( $cat->{'categorycode'} );
266         if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
267             $checked = 1;
268         }
269         push @categoryloop, {
270             categoryname    => $cat->{'categoryname'},
271             categorycode    => $cat->{'categorycode'},
272             categorytype    => $cat->{'categorytype'},
273             codedescription => $cat->{'codedescription'},
274             checked         => $checked,
275         };
276     }
277     $innertemplate->param( categoryloop => \@categoryloop );
278
279     for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
280         $innertemplate->param(
281             $obsolete => 'Your template is out of date (bug 130)' );
282     }
283 }
284
285 sub editcatform {
286
287     # prepares the edit form...
288     my ($categorycode,$innertemplate) = @_;
289     # warn "cat : $categorycode";
290         my @cats;
291     my $data;
292         if ($categorycode) {
293         my $data = GetBranchCategory($categorycode);
294         $data = $data->[0];
295         $innertemplate->param(
296             categorycode    => $data->{'categorycode'},
297             categoryname    => $data->{'categoryname'},
298             codedescription => $data->{'codedescription'},
299             show_in_pulldown => $data->{'show_in_pulldown'},
300                 );
301     }
302         for my $ctype (GetCategoryTypes()) {
303                 push @cats , { type => $ctype , selected => ($data->{'categorytype'} and $data->{'categorytype'} eq $ctype) };
304         }
305     $innertemplate->param(categorytype => \@cats);
306 }
307
308 sub branchinfotable {
309
310 # makes the html for a table of branch info from reference to an array of hashs.
311
312     my ($branchcode,$innertemplate) = @_;
313     my $branchinfo = $branchcode ? GetBranchInfo($branchcode) : GetBranchInfo();
314     my @loop_data = ();
315     foreach my $branch (@$branchinfo) {
316         #
317         # We export the following fields to the template. These are not
318         # pre-composed as a single "address" field because the template
319         # might (and should) escape what is exported here. (See bug 180)
320         #
321         # - branch_name     (Note: not "branchname")
322         # - branch_code     (Note: not "branchcode")
323         # - address         (containing a static error message)
324         # - branchaddress1 \
325         # - branchaddress2  |
326         # - branchaddress3  | comprising the old "address" field
327         # - branchzip       |
328         # - branchcity      |
329         # - branchcountry   |
330         # - branchphone     |
331         # - branchfax       |
332         # - branchemail    /
333         # - branchurl      /
334         # - opac_info (can contain HTML)
335         # - address-empty-p (1 if no address information, 0 otherwise)
336         # - categories      (containing a static error message)
337         # - category_list   (loop containing "categoryname")
338         # - no-categories-p (1 if no categories set, 0 otherwise)
339         # - value
340         #
341         my %row = ();
342
343         # Handle address fields separately
344         my $address_empty_p = 1;
345         for my $field (
346             'branchaddress1', 'branchaddress2',
347             'branchaddress3', 'branchzip',
348             'branchcity', 'branchstate', 'branchcountry',
349             'branchphone', 'branchfax',
350             'branchemail', 'branchurl', 'opac_info',
351             'branchip',       'branchprinter', 'branchnotes'
352           )
353         {
354             $row{$field} = $branch->{$field};
355             $address_empty_p = 0 if ( $branch->{$field} );
356         }
357         $row{'address-empty-p'} = $address_empty_p;
358
359         # Handle categories
360         my $no_categories_p = 1;
361         my @categories;
362         foreach my $cat ( @{ $branch->{'categories'} } ) {
363             my ($catinfo) = @{ GetBranchCategory($cat) };
364             push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
365             $no_categories_p = 0;
366         }
367
368         $row{'category_list'}   = \@categories;
369         $row{'no-categories-p'} = $no_categories_p;
370         $row{'branch_name'} = $branch->{'branchname'};
371         $row{'branch_code'} = $branch->{'branchcode'};
372         $row{'value'}       = $branch->{'branchcode'};
373
374         push @loop_data, \%row;
375     }
376     my @branchcategories = ();
377         for my $ctype ( GetCategoryTypes() ) {
378         my $catinfo = GetBranchCategories(undef,$ctype);
379         my @categories;
380                 foreach my $cat (@$catinfo) {
381             push @categories, {
382                 categoryname    => $cat->{'categoryname'},
383                 categorycode    => $cat->{'categorycode'},
384                 codedescription => $cat->{'codedescription'},
385                 categorytype    => $cat->{'categorytype'},
386             };
387         }
388         push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => ( @categories ? \@categories : undef) };
389         }
390     $innertemplate->param(
391         branches         => \@loop_data,
392         branchcategories => \@branchcategories
393     );
394
395 }
396
397 sub _branch_to_template {
398     my ($data, $template) = @_;
399     $template->param( 
400          branchcode     => $data->{'branchcode'},
401          branch_name    => $data->{'branchname'},
402          branchaddress1 => $data->{'branchaddress1'},
403          branchaddress2 => $data->{'branchaddress2'},
404          branchaddress3 => $data->{'branchaddress3'},
405          branchzip      => $data->{'branchzip'},
406          branchcity     => $data->{'branchcity'},
407          branchstate    => $data->{'branchstate'},
408          branchcountry  => $data->{'branchcountry'},
409          branchphone    => $data->{'branchphone'},
410          branchfax      => $data->{'branchfax'},
411          branchemail    => $data->{'branchemail'},
412          branchurl      => $data->{'branchurl'},
413          opac_info      => $data->{'opac_info'},
414          branchip       => $data->{'branchip'},
415          branchnotes    => $data->{'branchnotes'}, 
416     );
417 }
418
419 output_html_with_http_headers $input, $cookie, $template->output;
420
421 # Local Variables:
422 # tab-width: 8
423 # End: