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