Code cleaning :
[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 use C4::Interface::CGI::Output;
28
29
30 use C4::Context;
31 use C4::Barcodes::PrinterConfig;
32
33
34
35 # This function returns the path to deal with the correct files, considering
36 # templates set and language.
37 sub getPath {
38         my $type = shift @_;
39         my $templatesSet = C4::Context->preference('template');
40         my $lang = C4::Context->preference('opaclanguages');
41         if ($type eq "intranet") {
42                 return "$ENV{'DOCUMENT_ROOT'}/intranet-tmpl/$templatesSet/$lang";
43         } else {
44                 return "$ENV{'DOCUMENT_ROOT'}/opac-tmpl/$templatesSet/$lang";
45         }
46 }
47
48 # Load a configuration file. Before use this function, check if that file exists.
49 sub loadConfFromFile {
50   my $fileName = shift @_;
51         my %keyValues;
52         open FILE, "<$fileName";
53         while (<FILE>) {
54                 chomp;
55                 if (/\s*([\w_]*)\s*=\s*([\[\]\<\>\w_\s:@,\.-]*)\s*/) {
56                         $keyValues{$1} = $2;
57                 }
58         }
59         close FILE;
60         return %keyValues;
61 }
62
63 # Save settings to a configuration file. It delete previous configuration settings.
64 sub saveConfToFile {
65         my $fileName = shift @_;
66         my %keyValues = %{shift @_};
67         my $i;
68         open FILE, ">$fileName";                        
69         my $i;
70         foreach $i (keys(%keyValues)) {
71     print FILE $i." = ".$keyValues{$i}."\n";
72         }
73         close FILE;
74 }
75
76 # Load the config file.
77 my $filenameConf = &getPath("intranet")."/includes/labelConfig/itemsLabelConfig.conf";
78 my %labelConfig = &loadConfFromFile($filenameConf);
79
80 my $input = new CGI;
81 # Defines type of page to use in the printer process
82 my @labelTable = C4::Barcodes::PrinterConfig::labelsPage($labelConfig{'rows'}, $labelConfig{'columns'});
83
84 # It creates a list of posible intervals to choose codes to generate
85 my %list = ('continuous' => 'Continuous Range of items', 'individuals' => 'Individual Codes');
86 my @listValues = keys(%list);
87 my $rangeType = CGI::scrolling_list(-name => 'rangeType',
88                                         -values => \@listValues,
89                                                 -labels => \%list,
90                                                 -size => 1,
91                                                                         -default => ['continuous'],
92                                                 -multiple => 0,
93                                                                         -id => "rangeType",
94                                                                         -onChange => "changeRange(this)");
95 # It creates a list of posible standard codifications. First checks if the user has just added a new code.
96 if ($input->param('addCode')) {
97         my $newCountryName = $input->param('countryName');
98         my $newCountryCode = $input->param('countryCode'); 
99
100         my $countryCodesFilename = &getPath("intranet")."/includes/countryCodes/countryCodes.dat";
101         open COUNTRY_CODES, ">>$countryCodesFilename";                  
102     print COUNTRY_CODES $newCountryCode." = ".$newCountryName."\n";
103         close COUNTRY_CODES;
104 }
105
106 # Takes the country codes from a file and use them to set the country list.
107 my $countryCodes = &getPath("intranet")."/includes/countryCodes/countryCodes.dat";
108 my %list = &loadConfFromFile($countryCodes);
109 @listValues = keys(%list);
110 my $number_system = CGI::scrolling_list(-name => 'numbersystem',
111                                             -values => \@listValues,
112                                                     -labels   => \%list,
113                                                     -size     => 1,
114                                                     -multiple => 0);
115
116 # Set the script name
117 my $script_name = "/cgi-bin/koha/tools/barcodesGenerator.pl";
118
119
120 # Get the template to use
121 my ($template, $loggedinuser, $cookie)
122     = get_template_and_user({template_name => "barcodes/barcodes.tmpl",
123                                          type => "intranet",
124                                          query => $input,
125                                          authnotrequired => 0,
126                                          flagsrequired => {tools => 1},
127                                                  debug => 1,
128                                        });
129
130 # Replace the template values with the real ones
131 $template->param(SCRIPT_NAME => $script_name);
132 $template->param(NUMBER_SYSTEM => $number_system);
133 $template->param(PAGES => $labelConfig{'pageType'});
134 $template->param(RANGE_TYPE => $rangeType);
135 $template->param(LABEL_TABLE => \@labelTable);
136 $template->param(COL_SPAN => $labelConfig{'columns'});
137 if ($input->param('error')) {
138         $template->param(ERROR => 1);
139 } else {
140         $template->param(ERROR => 0);
141 }
142 # Shows the template with the real values replaced
143 output_html_with_http_headers $input, $cookie, $template->output;