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