Turn off foreign key check for the installation
[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 CGI;
48 use C4::Auth;
49 use C4::Context;
50 use C4::Output;
51 use C4::Koha;
52 use C4::Branch;
53
54 # Fixed variables
55 my $script_name = "/cgi-bin/koha/admin/branches.pl";
56 my $pagesize    = 20;
57
58 ################################################################################
59 # Main loop....
60 my $input        = new CGI;
61 my $branchcode   = $input->param('branchcode');
62 my $branchname   = $input->param('branchname');
63 my $categorycode = $input->param('categorycode');
64 my $op           = $input->param('op');
65
66 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
67     {
68         template_name   => "admin/branches.tmpl",
69         query           => $input,
70         type            => "intranet",
71         authnotrequired => 0,
72         flagsrequired   => { parameters => 1},
73         debug           => 1,
74     }
75 );
76 if ($op) {
77     $template->param(
78         script_name => $script_name,
79         $op         => 1
80     );    # we show only the TMPL_VAR names $op
81 }
82 else {
83     $template->param(
84         script_name => $script_name,
85         else        => 1
86     );    # we show only the TMPL_VAR names $op
87 }
88 $template->param( action => $script_name );
89 if ( $op eq 'add' ) {
90
91     # If the user has pressed the "add new branch" button.
92     $template->param( 'heading-branches-add-branch-p' => 1 );
93     editbranchform($branchcode,$template);
94
95 }
96 elsif ( $op eq 'edit' ) {
97
98     # if the user has pressed the "edit branch settings" button.
99     $template->param( 'heading-branches-add-branch-p' => 0,
100                         'add' => 1, );
101     editbranchform($branchcode,$template);
102 }
103 elsif ( $op eq 'add_validate' ) {
104
105     # confirm settings change...
106     my $params = $input->Vars;
107     unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
108         $template->param( else => 1 );
109         default("MESSAGE1",$template);
110     }
111     else {
112         ModBranch($params);
113         $template->param( else => 1 );
114         default("MESSAGE2",$template);
115     }
116 }
117 elsif ( $op eq 'delete' ) {
118     # if the user has pressed the "delete branch" button.
119     
120     # check to see if the branchcode is being used in the database somewhere....
121     my $dbh = C4::Context->dbh;
122     my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
123     $sth->execute( $branchcode, $branchcode );
124     my ($total) = $sth->fetchrow_array;
125     $sth->finish;
126     
127     my $message;
128
129     if ($total) {
130         $message = "MESSAGE7";
131     }
132    
133     if ($message) {
134         $template->param( else => 1 );
135         default($message,$template);
136     }
137     else {
138         $template->param( branchname     => $branchname );
139         $template->param( delete_confirm => 1 );
140         $template->param( branchcode     => $branchcode );
141     }
142 }
143 elsif ( $op eq 'delete_confirmed' ) {
144
145     # actually delete branch and return to the main screen....
146     DelBranch($branchcode);
147     $template->param( else => 1 );
148     default("MESSAGE3",$template);
149 }
150 elsif ( $op eq 'editcategory' ) {
151
152     # If the user has pressed the "add new category" or "modify" buttons.
153     $template->param( 'heading-branches-edit-category-p' => 1 );
154     editcatform($categorycode,$template);
155 }
156 elsif ( $op eq 'addcategory_validate' ) {
157
158     # confirm settings change...
159     my $params = $input->Vars;
160     unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
161         $template->param( else => 1 );
162         default("MESSAGE4",$template);
163     }
164     else {
165         ModBranchCategoryInfo($params);
166         $template->param( else => 1 );
167         default("MESSAGE5",$template);
168     }
169 }
170 elsif ( $op eq 'delete_category' ) {
171
172     # if the user has pressed the "delete branch" button.
173     my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
174     if ($message) {
175         $template->param( else => 1 );
176         default($message,$template);
177     }
178     else {
179         $template->param( delete_category => 1 );
180         $template->param( categorycode    => $categorycode );
181     }
182 }
183 elsif ( $op eq 'categorydelete_confirmed' ) {
184
185     # actually delete branch and return to the main screen....
186     DelBranchCategory($categorycode);
187     $template->param( else => 1 );
188     default("MESSAGE6",$template);
189
190 }
191 else {
192
193     # if no operation has been set...
194     default("",$template);
195 }
196
197 ################################################################################
198 #
199 # html output functions....
200
201 sub default {
202     my ($message,$innertemplate) = @_;
203     $innertemplate->param( 'heading-branches-p' => 1 );
204     $innertemplate->param( "$message"           => 1 );
205     $innertemplate->param( action               => $script_name );
206     branchinfotable("",$innertemplate);
207 }
208
209 sub editbranchform {
210     my ($branchcode,$innertemplate) = @_;
211     # initiate the scrolling-list to select the printers
212     my $printers = GetPrinters();
213     my @printerloop;
214     my $printercount = 0;
215     my $oldprinter;
216     my $CGIprinter;
217     
218     my $data;
219
220     if ($branchcode) {
221         $data = GetBranchInfo($branchcode);
222         $data = $data->[0];
223
224         # get the old printer of the branch
225         $oldprinter = $data->{'branchprinter'};
226
227         #       printer loop
228         foreach my $thisprinter ( keys %$printers ) {
229             my $selected = 1
230               if $oldprinter eq $printers->{$thisprinter}->{'printqueue'};
231             my %row = (
232                 value         => $thisprinter,
233                 selected      => $selected,
234                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
235             );
236             push @printerloop, \%row;
237         }
238
239         $innertemplate->param( 
240              printerloop    => \@printerloop,
241              branchcode     => $data->{'branchcode'},
242              branch_name    => $data->{'branchname'},
243              branchaddress1 => $data->{'branchaddress1'},
244              branchaddress2 => $data->{'branchaddress2'},
245              branchaddress3 => $data->{'branchaddress3'},
246              branchphone    => $data->{'branchphone'},
247              branchfax      => $data->{'branchfax'},
248              branchemail    => $data->{'branchemail'},
249              branchip       => $data->{'branchip'} 
250         );
251     }
252     else {    #case of an add branch select printer
253         foreach my $thisprinter ( keys %$printers ) {
254             my %row = (
255                 value         => $thisprinter,
256                 branchprinter => $printers->{$thisprinter}->{'printqueue'},
257             );
258             push @printerloop, \%row;
259         }
260         $innertemplate->param( printerloop => \@printerloop );
261     }
262
263     # make the checkboxs.....
264     #
265     # We export a "categoryloop" array to the template, each element of which
266     # contains separate 'categoryname', 'categorycode', 'codedescription', and
267     # 'checked' fields. The $checked field is either '' or 'checked'
268     # (see bug 130)
269     #
270     my $catinfo = GetBranchCategory();
271     my $catcheckbox;
272
273     #    print DEBUG "catinfo=".cvs($catinfo)."\n";
274     my @categoryloop = ();
275     foreach my $cat (@$catinfo) {
276         my $checked = "";
277         my $tmp     = quotemeta( $cat->{'categorycode'} );
278         if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
279             $checked = "checked=\"checked\"";
280         }
281         push @categoryloop,
282           {
283             categoryname    => $cat->{'categoryname'},
284             categorycode    => $cat->{'categorycode'},
285             codedescription => $cat->{'codedescription'},
286             checked         => $checked,
287           };
288     }
289     $innertemplate->param( categoryloop => \@categoryloop );
290
291     # {{{ Leave this here until bug 130 is completely resolved in the templates
292     for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
293         $innertemplate->param(
294             $obsolete => 'Your template is out of date (bug 130)' );
295     }
296
297     # }}}
298 }
299
300 sub editcatform {
301
302     # prepares the edit form...
303     my ($categorycode,$innertemplate) = @_;
304     warn "cat : $categorycode";
305     my $data;
306     if ($categorycode) {
307         $data = GetBranchCategory($categorycode);
308         $data = $data->[0];
309         $innertemplate->param( categorycode    => $data->{'categorycode'} );
310         $innertemplate->param( categoryname    => $data->{'categoryname'} );
311         $innertemplate->param( codedescription => $data->{'codedescription'} );
312     }
313 }
314
315 sub deleteconfirm {
316
317     # message to print if the
318     my ($branchcode) = @_;
319 }
320
321 sub branchinfotable {
322
323 # makes the html for a table of branch info from reference to an array of hashs.
324
325     my ($branchcode,$innertemplate) = @_;
326     my $branchinfo;
327     if ($branchcode) {
328         $branchinfo = GetBranchInfo($branchcode);
329     }
330     else {
331         $branchinfo = GetBranchInfo();
332     }
333     my $toggle;
334     my $i;
335     my @loop_data = ();
336     foreach my $branch (@$branchinfo) {
337         ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
338
339         #
340         # We export the following fields to the template. These are not
341         # pre-composed as a single "address" field because the template
342         # might (and should) escape what is exported here. (See bug 180)
343         #
344         # - color
345         # - branch_name     (Note: not "branchname")
346         # - branch_code     (Note: not "branchcode")
347         # - address         (containing a static error message)
348         # - branchaddress1 \
349         # - branchaddress2  |
350         # - branchaddress3  | comprising the old "address" field
351         # - branchphone     |
352         # - branchfax       |
353         # - branchemail    /
354         # - address-empty-p (1 if no address information, 0 otherwise)
355         # - categories      (containing a static error message)
356         # - category_list   (loop containing "categoryname")
357         # - no-categories-p (1 if no categories set, 0 otherwise)
358         # - value
359         # - action
360         #
361         my %row = ();
362
363         # Handle address fields separately
364         my $address_empty_p = 1;
365         for my $field (
366             'branchaddress1', 'branchaddress2',
367             'branchaddress3', 'branchphone',
368             'branchfax',      'branchemail',
369             'branchip',       'branchprinter'
370           )
371         {
372             $row{$field} = $branch->{$field};
373             if ( $branch->{$field} ) {
374                 $address_empty_p = 0;
375             }
376         }
377         $row{'address-empty-p'} = $address_empty_p;
378
379         # {{{ Leave this here until bug 180 is completely resolved in templates
380         $row{'address'} = 'Your template is out of date (see bug 180)';
381
382         # }}}
383
384         # Handle categories
385         my $no_categories_p = 1;
386         my @categories      = '';
387         foreach my $cat ( @{ $branch->{'categories'} } ) {
388             my ($catinfo) = @{ GetBranchCategory($cat) };
389             push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
390             $no_categories_p = 0;
391         }
392
393         # {{{ Leave this here until bug 180 is completely resolved in templates
394         $row{'categories'} = 'Your template is out of date (see bug 180)';
395
396         # }}}
397         $row{'category_list'}   = \@categories;
398         $row{'no-categories-p'} = $no_categories_p;
399
400         # Handle all other fields
401         $row{'branch_name'} = $branch->{'branchname'};
402         $row{'branch_code'} = $branch->{'branchcode'};
403         $row{'toggle'}      = $toggle;
404         $row{'value'}       = $branch->{'branchcode'};
405         $row{'action'}      = '/cgi-bin/koha/admin/branches.pl';
406
407         push @loop_data, {%row};
408         $i++;
409     }
410     my @branchcategories = ();
411     my $catinfo          = GetBranchCategory();
412     $i = 0;
413     foreach my $cat (@$catinfo) {
414         ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
415         push @branchcategories,
416           {
417             toggle          => $toggle,
418             categoryname    => $cat->{'categoryname'},
419             categorycode    => $cat->{'categorycode'},
420             codedescription => $cat->{'codedescription'},
421           };
422         $i++;
423     }
424
425     $innertemplate->param(
426         branches         => \@loop_data,
427         branchcategories => \@branchcategories
428     );
429
430 }
431
432 # FIXME logic seems wrong
433 sub branchcategoriestable {
434     my $innertemplate = shift;
435     #Needs to be implemented...
436
437     my $categoryinfo = GetBranchCategory();
438     my $color;
439     foreach my $cat (@$categoryinfo) {
440         $innertemplate->param( categoryname    => $cat->{'categoryname'} );
441         $innertemplate->param( categorycode    => $cat->{'categorycode'} );
442         $innertemplate->param( codedescription => $cat->{'codedescription'} );
443     }
444 }
445
446 $template->param(
447     intranetcolorstylesheet =>
448       C4::Context->preference("intranetcolorstylesheet"),
449     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
450     IntranetNav        => C4::Context->preference("IntranetNav"),
451 );
452 output_html_with_http_headers $input, $cookie, $template->output;
453
454 # Local Variables:
455 # tab-width: 8
456 # End: