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