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