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