fix for #2884: Add error management to branches
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 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 => 1},
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 $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);
109         }
110     }
111 }
112 elsif ( $op eq 'delete' ) {
113     # if the user has pressed the "delete branch" button.
114     
115     # check to see if the branchcode is being used in the database somewhere....
116     my $dbh = C4::Context->dbh;
117     my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
118     $sth->execute( $branchcode, $branchcode );
119     my ($total) = $sth->fetchrow_array;
120     if ($total) {
121         $template->param( else => 1 );
122         default("MESSAGE7", $template);
123     }
124     else {
125         $template->param( delete_confirm => 1 );
126         $template->param( branchname     => $branchname );
127         $template->param( branchcode     => $branchcode );
128     }
129 }
130 elsif ( $op eq 'delete_confirmed' ) {
131
132     # actually delete branch and return to the main screen....
133     DelBranch($branchcode);
134     $template->param( else => 1 );
135     default("MESSAGE3",$template);
136 }
137 elsif ( $op eq 'editcategory' ) {
138
139     # If the user has pressed the "add new category" or "modify" buttons.
140     $template->param( 'heading-branches-edit-category-p' => 1 );
141     editcatform($categorycode,$template);
142 }
143 elsif ( $op eq 'addcategory_validate' ) {
144
145     $template->param( else => 1 );
146     # confirm settings change...
147     my $params = $input->Vars;
148     unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
149         default("MESSAGE4",$template);
150     }
151     else {
152         ModBranchCategoryInfo($params);
153         default("MESSAGE5",$template);
154     }
155 }
156 elsif ( $op eq 'delete_category' ) {
157
158     # if the user has pressed the "delete branch" button.
159     my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
160     if ($message) {
161         $template->param( else => 1 );
162         default($message,$template);
163     }
164     else {
165         $template->param( delete_category => 1 );
166         $template->param( categorycode    => $categorycode );
167     }
168 }
169 elsif ( $op eq 'categorydelete_confirmed' ) {
170
171     # actually delete branch and return to the main screen....
172     DelBranchCategory($categorycode);
173     $template->param( else => 1 );
174     default("MESSAGE6",$template);
175
176 }
177 else {
178     # if no operation has been set...
179     default("",$template);
180 }
181
182 ################################################################################
183 #
184 # html output functions....
185
186 sub default {
187     my $message       = shift || '';
188     my $innertemplate = shift or return;
189     $innertemplate->param($message => 1) if $message;
190     $innertemplate->param(
191         'heading-branches-p' => 1,
192     );
193     branchinfotable("",$innertemplate);
194 }
195
196 sub editbranchform {
197     my ($branchcode,$innertemplate) = @_;
198     # initiate the scrolling-list to select the printers
199     my $printers = GetPrinters();
200     my @printerloop;
201     my $data;
202     my $oldprinter = "";
203
204     if ($branchcode) {
205         $data = GetBranchInfo($branchcode);
206         $data = $data->[0];
207
208         # get the old printer of the branch
209         $oldprinter = $data->{'branchprinter'} || '';
210         $innertemplate->param( 
211              branchcode     => $data->{'branchcode'},
212              branch_name    => $data->{'branchname'},
213              branchaddress1 => $data->{'branchaddress1'},
214              branchaddress2 => $data->{'branchaddress2'},
215              branchaddress3 => $data->{'branchaddress3'},
216              branchzip      => $data->{'branchzip'},
217              branchcity     => $data->{'branchcity'},
218              branchcountry  => $data->{'branchcountry'},
219              branchphone    => $data->{'branchphone'},
220              branchfax      => $data->{'branchfax'},
221              branchemail    => $data->{'branchemail'},
222              branchurl      => $data->{'branchurl'},
223              branchip       => $data->{'branchip'},
224              branchnotes    => $data->{'branchnotes'}, 
225         );
226     }
227
228     foreach my $thisprinter ( keys %$printers ) {
229         push @printerloop, {
230             value         => $thisprinter,
231             selected      => ( $oldprinter eq $printers->{$thisprinter} ),
232             branchprinter => $printers->{$thisprinter}->{'printqueue'},
233         };
234     }
235
236     $innertemplate->param( printerloop => \@printerloop );
237     # make the checkboxes.....
238     #
239     # We export a "categoryloop" array to the template, each element of which
240     # contains separate 'categoryname', 'categorycode', 'codedescription', and
241     # 'checked' fields. The $checked field is either '' or 'checked="checked"'
242
243     my $catinfo = GetBranchCategory();
244     my @categoryloop = ();
245     foreach my $cat (@$catinfo) {
246         my $checked = "";
247         my $tmp     = quotemeta( $cat->{'categorycode'} );
248         if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
249             $checked = "checked=\"checked\"";
250         }
251         push @categoryloop, {
252             categoryname    => $cat->{'categoryname'},
253             categorycode    => $cat->{'categorycode'},
254             categorytype    => $cat->{'categorytype'},
255             codedescription => $cat->{'codedescription'},
256             checked         => $checked,
257         };
258     }
259     $innertemplate->param( categoryloop => \@categoryloop );
260
261     for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
262         $innertemplate->param(
263             $obsolete => 'Your template is out of date (bug 130)' );
264     }
265 }
266
267 sub editcatform {
268
269     # prepares the edit form...
270     my ($categorycode,$innertemplate) = @_;
271     # warn "cat : $categorycode";
272         my @cats;
273     my $data;
274         if ($categorycode) {
275         my $data = GetBranchCategory($categorycode);
276         $data = $data->[0];
277         $innertemplate->param(
278             categorycode    => $data->{'categorycode'},
279             categoryname    => $data->{'categoryname'},
280             codedescription => $data->{'codedescription'},
281                 );
282     }
283         for my $ctype (GetCategoryTypes()) {
284                 push @cats , { type => $ctype , selected => ($data->{'categorytype'} and $data->{'categorytype'} eq $ctype) };
285         }
286     $innertemplate->param(categorytype => \@cats);
287 }
288
289 sub branchinfotable {
290
291 # makes the html for a table of branch info from reference to an array of hashs.
292
293     my ($branchcode,$innertemplate) = @_;
294     my $branchinfo = $branchcode ? GetBranchInfo($branchcode) : GetBranchInfo();
295     my @loop_data = ();
296     foreach my $branch (@$branchinfo) {
297         #
298         # We export the following fields to the template. These are not
299         # pre-composed as a single "address" field because the template
300         # might (and should) escape what is exported here. (See bug 180)
301         #
302         # - branch_name     (Note: not "branchname")
303         # - branch_code     (Note: not "branchcode")
304         # - address         (containing a static error message)
305         # - branchaddress1 \
306         # - branchaddress2  |
307         # - branchaddress3  | comprising the old "address" field
308         # - branchzip       |
309         # - branchcity      |
310         # - branchcountry   |
311         # - branchphone     |
312         # - branchfax       |
313         # - branchemail    /
314         # - branchurl      /
315         # - address-empty-p (1 if no address information, 0 otherwise)
316         # - categories      (containing a static error message)
317         # - category_list   (loop containing "categoryname")
318         # - no-categories-p (1 if no categories set, 0 otherwise)
319         # - value
320         #
321         my %row = ();
322
323         # Handle address fields separately
324         my $address_empty_p = 1;
325         for my $field (
326             'branchaddress1', 'branchaddress2',
327             'branchaddress3', 'branchzip',
328             'branchcity', 'branchcountry',
329             'branchphone', 'branchfax',
330             'branchemail', 'branchurl',
331             'branchip',       'branchprinter', 'branchnotes'
332           )
333         {
334             $row{$field} = $branch->{$field};
335             $address_empty_p = 0 if ( $branch->{$field} );
336         }
337         $row{'address-empty-p'} = $address_empty_p;
338
339         # Handle categories
340         my $no_categories_p = 1;
341         my @categories;
342         foreach my $cat ( @{ $branch->{'categories'} } ) {
343             my ($catinfo) = @{ GetBranchCategory($cat) };
344             push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
345             $no_categories_p = 0;
346         }
347
348         $row{'category_list'}   = \@categories;
349         $row{'no-categories-p'} = $no_categories_p;
350         $row{'branch_name'} = $branch->{'branchname'};
351         $row{'branch_code'} = $branch->{'branchcode'};
352         $row{'value'}       = $branch->{'branchcode'};
353
354         push @loop_data, \%row;
355     }
356     my @branchcategories = ();
357         for my $ctype ( GetCategoryTypes() ) {
358         my $catinfo = GetBranchCategories(undef,$ctype);
359         my @categories;
360                 foreach my $cat (@$catinfo) {
361             push @categories, {
362                 categoryname    => $cat->{'categoryname'},
363                 categorycode    => $cat->{'categorycode'},
364                 codedescription => $cat->{'codedescription'},
365                 categorytype    => $cat->{'categorytype'},
366             };
367         }
368         push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
369         }
370     $innertemplate->param(
371         branches         => \@loop_data,
372         branchcategories => \@branchcategories
373     );
374
375 }
376
377 output_html_with_http_headers $input, $cookie, $template->output;
378
379 # Local Variables:
380 # tab-width: 8
381 # End: