Assigning bug 1835 : change password would never log password change.
[koha.git] / C4 / Barcodes / PrinterConfig.pm
1 package C4::Barcodes::PrinterConfig;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 use vars qw($VERSION @EXPORT);
20
21 use PDF::API2;
22 use PDF::API2::Page;
23
24 BEGIN {
25         # set the version for version checking
26         $VERSION = 0.02;
27         require Exporter;
28         @EXPORT = qw(&labelsPage &getLabelPosition setPositionsForX setPositionsForY);
29 }
30
31 =head1 NAME
32
33 C4::Barcodes::PrinterConfig - Koha module dealing with labels in a PDF.
34
35 =head1 SYNOPSIS
36
37         use C4::Barcodes::PrinterConfig;
38
39 =head1 DESCRIPTION
40
41 This package is used to deal with labels in a pdf file. Giving some parameters,
42 this package contains several functions to handle every label considering the 
43 environment of the pdf file.
44
45 =head1 FUNCTIONS
46
47 =over 2
48
49 =cut
50
51 my @positionsForX; # Takes all the X positions of the pdf file.
52 my @positionsForY; # Takes all the Y positions of the pdf file.
53 my $firstLabel = 1; # Test if the label passed as a parameter is the first label to be printed into the pdf file.
54
55 =item setPositionsForX
56
57         C4::Barcodes::PrinterConfig::setPositionsForX($marginLeft, $labelWidth, $columns, $pageType);
58
59 Calculate and stores all the X positions across the pdf page.
60
61 C<$marginLeft> Indicates how much left margin do you want in your page type.
62
63 C<$labelWidth> Indicates the width of the label that you are going to use.
64
65 C<$columns> Indicates how many columns do you want in your page type.
66
67 C<$pageType> Page type to print (eg: a4, legal, etc).
68
69 =cut
70 #'
71 sub setPositionsForX {
72         my ($marginLeft, $labelWidth, $columns, $pageType) = @_;
73         my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch
74         my $whereToStart = ($marginLeft + ($labelWidth/2));
75         my $firstLabel = $whereToStart*$defaultDpi;
76         my $spaceBetweenLabels = $labelWidth*$defaultDpi;
77         my @positions;
78         for (my $i = 0; $i < $columns ; $i++) {
79                 push @positions, ($firstLabel+($spaceBetweenLabels*$i));
80         }
81         @positionsForX = @positions;
82 }
83
84 =item setPositionsForY
85
86         C4::Barcodes::PrinterConfig::setPositionsForY($marginBottom, $labelHeigth, $rows, $pageType);
87
88 Calculate and stores all tha Y positions across the pdf page.
89
90 C<$marginBottom> Indicates how much bottom margin do you want in your page type.
91
92 C<$labelHeigth> Indicates the height of the label that you are going to use.
93
94 C<$rows> Indicates how many rows do you want in your page type.
95
96 C<$pageType> Page type to print (eg: a4, legal, etc).
97
98 =cut
99 #'
100 sub setPositionsForY {
101         my ($marginBottom, $labelHeigth, $rows, $pageType) = @_;
102         my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch
103         my $whereToStart = ($marginBottom + ($labelHeigth/2));
104         my $firstLabel = $whereToStart*$defaultDpi;
105         my $spaceBetweenLabels = $labelHeigth*$defaultDpi;
106         my @positions;
107         for (my $i = 0; $i < $rows; $i++) {
108                 unshift @positions, ($firstLabel+($spaceBetweenLabels*$i));
109         }
110         @positionsForY = @positions;
111 }
112
113 =item getLabelPosition
114
115         (my $x, my $y, $pdfObject, $pageObject, $gfxObject, $textObject, $coreObject, $labelPosition) = 
116                                         C4::Barcodes::PrinterConfig::getLabelPosition($labelPosition, 
117                                                                                                                                   $pdfObject, 
118                                                                                                                                   $page,
119                                                                                                                                   $gfx,
120                                                                                                                                   $text,
121                                                                                                                                   $fontObject,
122                                                                                                                                   $pageType);   
123
124 Return the (x,y) position of the label that you are going to print considering the environment.
125
126 C<$labelPosition> Indicates which label positions do you want to place by x and y coordinates.
127
128 C<$pdfObject> The PDF object in use.
129
130 C<$page> The page in use.
131
132 C<$gfx> The gfx resource to handle with barcodes objects.
133
134 C<$text> The text resource to handle with text.
135
136 C<$fontObject> The font object
137
138 C<$pageType> Page type to print (eg: a4, legal, etc).
139
140 =cut
141 #'
142 sub getLabelPosition {
143         my ($labelNum, $pdf, $page, $gfxObject, $textObject, $fontObject, $pageType) = @_;
144         my $indexX = $labelNum % @positionsForX;
145         my $indexY = int($labelNum / @positionsForX);
146         # Calculates the next label position and return that label number
147         my $nextIndexX = $labelNum % @positionsForX;
148         my $nextIndexY = $labelNum % @positionsForY;
149         if ($firstLabel) {
150           $page = $pdf->page;
151           $page->mediabox($pageType);
152           $gfxObject = $page->gfx;
153           $textObject = $page->text;
154           $textObject->font($fontObject, 7);
155                   $firstLabel = 0;
156         } elsif (($nextIndexX == 0) && ($nextIndexY == 0)) {
157           $page = $pdf->page;
158           $page->mediabox($pageType);
159           $gfxObject = $page->gfx;
160           $textObject = $page->text;
161           $textObject->font($fontObject, 7);
162         }
163         $labelNum = $labelNum + 1;      
164         if ($labelNum == (@positionsForX*@positionsForY)) {
165                 $labelNum = 0;
166         }
167         return ($positionsForX[$indexX], $positionsForY[$indexY], $pdf, $page, $gfxObject, $textObject, $fontObject, $labelNum);
168 }
169
170 =item labelsPage
171
172         my @labelTable = C4::Barcodes::PrinterConfig::labelsPage($rows, $columns);
173
174 This function will help you to build the labels panel, where you can choose
175 wich label position do you want to start the printer process.
176
177 C<$rows> Indicates how many rows do you want in your page type.
178
179 C<$columns> Indicates how many rows do you want in your page type.
180
181 =cut
182 #'
183 sub labelsPage{
184         my ($rows, $columns) = @_;
185         my @pageType;
186         my $tagname = 0;
187         my $labelname = 1;
188         my $check;
189         for (my $i = 1; $i <= $rows; $i++) {
190                 my @column;
191                 for (my $j = 1; $j <= $columns; $j++) {
192                         my %cell;
193                         if ($tagname == 0) {
194                                 $check = 'checked';
195                         } else {
196                                 $check = '';
197                         }               
198                         %cell = (check => $check,
199                                          tagname => $tagname,
200                                  labelname => $labelname);
201                         $tagname = $tagname + 1;        
202                         $labelname = $labelname + 1;    
203                         push @column, \%cell;
204                 }
205                 my %columns = (columns => \@column);
206                 push @pageType, \%columns;
207         }
208         return @pageType;
209 }
210
211 1;
212
213 __END__
214
215 =back
216
217 =head1 AUTHOR
218
219 Koha Physics Library UNLP <matias_veleda@hotmail.com>
220
221 =cut