re indenting
[koha.git] / tools / barcodes.pl
1 #!/usr/bin/perl
2
3 # script to generate items barcodes
4 # written 07/04
5 # by Veleda Matias - matias_veleda@hotmail.com - Physics Library UNLP Argentina and
6 #    Casta�eda Sebastian - seba3c@yahoo.com.ar - Physics Library UNLP Argentina and
7
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 use CGI;
25 use C4::Auth;
26 use C4::Output;
27
28
29 use C4::Context;
30 use C4::Barcodes::PrinterConfig;
31
32
33
34 # This function returns the path to deal with the correct files, considering
35 # templates set and language.
36 sub getPath {
37         my $type = shift @_;
38         my $templatesSet = C4::Context->preference('template');
39         my $lang = C4::Context->preference('opaclanguages');
40         if ($type eq "intranet") {
41                 return "$ENV{'DOCUMENT_ROOT'}/intranet-tmpl/$templatesSet/$lang";
42         } else {
43                 return "$ENV{'DOCUMENT_ROOT'}/opac-tmpl/$templatesSet/$lang";
44         }
45 }
46
47 # Load a configuration file. Before use this function, check if that file exists.
48 sub loadConfFromFile {
49   my $fileName = shift @_;
50         my %keyValues;
51         open FILE, "<$fileName";
52         while (<FILE>) {
53                 chomp;
54                 if (/\s*([\w_]*)\s*=\s*([\[\]\<\>\w_\s:@,\.-]*)\s*/) {
55                         $keyValues{$1} = $2;
56                 }
57         }
58         close FILE;
59         return %keyValues;
60 }
61
62 # Save settings to a configuration file. It delete previous configuration settings.
63 sub saveConfToFile {
64         my $fileName = shift @_;
65         my %keyValues = %{shift @_};
66         my $i;
67         open FILE, ">$fileName";                        
68         my $i;
69         foreach $i (keys(%keyValues)) {
70     print FILE $i." = ".$keyValues{$i}."\n";
71         }
72         close FILE;
73 }
74
75 # Load the config file.
76 my $filenameConf = &getPath("intranet")."/includes/labelConfig/itemsLabelConfig.conf";
77 my %labelConfig = &loadConfFromFile($filenameConf);
78
79 my $input = new CGI;
80 # Defines type of page to use in the printer process
81 my @labelTable = C4::Barcodes::PrinterConfig::labelsPage($labelConfig{'rows'}, $labelConfig{'columns'});
82
83 # It creates a list of posible intervals to choose codes to generate
84 my %list = ('continuous' => 'Continuous Range of items', 'individuals' => 'Individual Codes');
85 my @listValues = keys(%list);
86 my $rangeType = CGI::scrolling_list(-name => 'rangeType',
87                                         -values => \@listValues,
88                                                 -labels => \%list,
89                                                 -size => 1,
90                                                                         -default => ['continuous'],
91                                                 -multiple => 0,
92                                                                         -id => "rangeType",
93                                                                         -onChange => "changeRange(this)");
94 # It creates a list of posible standard codifications. First checks if the user has just added a new code.
95 if ($input->param('addCode')) {
96         my $newCountryName = $input->param('countryName');
97         my $newCountryCode = $input->param('countryCode'); 
98
99         my $countryCodesFilename = &getPath("intranet")."/includes/countryCodes/countryCodes.dat";
100         open COUNTRY_CODES, ">>$countryCodesFilename";                  
101     print COUNTRY_CODES $newCountryCode." = ".$newCountryName."\n";
102         close COUNTRY_CODES;
103 }
104
105 # Takes the country codes from a file and use them to set the country list.
106 my $countryCodes = &getPath("intranet")."/includes/countryCodes/countryCodes.dat";
107 my %list = &loadConfFromFile($countryCodes);
108 @listValues = keys(%list);
109 my $number_system = CGI::scrolling_list(-name => 'numbersystem',
110                                             -values => \@listValues,
111                                                     -labels   => \%list,
112                                                     -size     => 1,
113                                                     -multiple => 0);
114
115 # Set the script name
116 my $script_name = "/cgi-bin/koha/tools/barcodesGenerator.pl";
117
118
119 # Get the template to use
120 my ($template, $loggedinuser, $cookie)
121     = get_template_and_user({template_name => "barcodes/barcodes.tmpl",
122                                          type => "intranet",
123                                          query => $input,
124                                          authnotrequired => 0,
125                                          flagsrequired => {tools => 1},
126                                                  debug => 1,
127                                        });
128
129 # Replace the template values with the real ones
130 $template->param(SCRIPT_NAME => $script_name);
131 $template->param(NUMBER_SYSTEM => $number_system);
132 $template->param(PAGES => $labelConfig{'pageType'});
133 $template->param(RANGE_TYPE => $rangeType);
134 $template->param(LABEL_TABLE => \@labelTable);
135 $template->param(COL_SPAN => $labelConfig{'columns'});
136 if ($input->param('error')) {
137         $template->param(ERROR => 1);
138 } else {
139         $template->param(ERROR => 0);
140 }
141 # Shows the template with the real values replaced
142 output_html_with_http_headers $input, $cookie, $template->output;