fix the pdf code with a twisted way to do that.
[platal.git] / modules / carnet / contacts.pdf.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 define ('FPDF_FONTPATH', dirname(__FILE__).'/fonts/');
23 require_once '/usr/share/fpdf/fpdf.php';
24 require_once dirname(__FILE__).'/../../classes/varstream.php';
25
26 class ContactsPDF extends FPDF
27 {
28 var $col = 0;
29 var $y0;
30
31 var $title = "Mes contacts sur Polytechnique.org";
32 var $broken = false;
33 var $error = false;
34
35 var $report = 0;
36
37 function ContactsPDF()
38 {
39 $this->report = error_reporting(0);
40 parent::FPDF();
41 error_reporting($this->report);
42
43 $this->AddFont('Vera Sans', '', 'Vera.php');
44 $this->AddFont('Vera Sans', 'I', 'VeraIt.php');
45 $this->AddFont('Vera Sans', 'B', 'VeraBd.php');
46
47 $this->AddFont('Vera Mono', '', 'VeraMono.php');
48
49 $this->SetTitle($this->title);
50 $this->SetCreator('Site Polytechnique.org');
51 $this->AddPage();
52 }
53
54 function Output($name='mescontacts.pdf', $dest='I')
55 {
56 Header('Pragma: public');
57 error_reporting(0);
58 parent::Output($name, $dest);
59 error_reporting($this->report);
60 }
61
62 function Rotate($angle, $x=-1, $y=-1)
63 {
64 if ($x==-1) {
65 $x = $this->x;
66 }
67 if ($y==-1) {
68 $y=$this->y;
69 }
70 if (!empty($this->angle)) {
71 $this->_out('Q');
72 }
73 $this->angle = $angle;
74 if ($angle != 0) {
75 $angle*=M_PI/180;
76 $c = cos($angle);
77 $s = sin($angle);
78 $cx = $x*$this->k;
79 $cy = ($this->h-$y)*$this->k;
80 $this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',
81 $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
82 }
83 }
84
85 function Header()
86 {
87
88 $this->SetFont('Vera Sans', 'B', 20);
89 $this->SetTextColor(230);
90 $this->Rotate(45, 55, 190);
91 $this->Text(55, 190, "informations limitées à un usage");
92 $this->Text(40, 210, "strictement personnel et non commercial");
93 $this->Rotate(0);
94
95 $this->setLeftMargin(5);
96 $this->setRightMargin(5);
97 $this->SetFont('Vera Sans', 'B', 16);
98 $this->SetY(5);
99 $this->SetTextColor(51, 102, 153);
100 $this->SetDrawColor(102, 153, 204);
101 $this->SetLineWidth(0.2);
102 $this->SetFillColor(245, 248, 252);
103 $this->Cell(200, 10, $this->title, 1, 1, 'C', 1);
104 $this->Image(dirname(__FILE__).'/../../htdocs/images/logo.png',
105 5, 5, 10, 10, 'png', 'https://www.polytechnique.org/');
106
107 $this->Ln(10);
108 $this->y0 = $this->GetY();
109 $this->ColSetup(false);
110 }
111
112 function Footer()
113 {
114 $this->setLeftMargin(5);
115 $this->setRightMargin(5);
116 $this->SetY(-15);
117 $this->SetFont('Vera Sans', 'I', 8);
118 $this->SetTextColor(128);
119 $this->Cell(0, 10, 'Page '.$this->PageNo(), 0, 0, 'C');
120 $this->Cell(0, 10, '(en date du '.strftime('%d %B %Y').')', 0, 0, 'R');
121 }
122
123 function ColSetup($col)
124 {
125 $this->col = $col;
126 $x = 10 + $this->col * 100;
127 $this->SetLeftMargin($x);
128 $this->SetRightMargin(120 - $x);
129 $this->SetX($x);
130 $this->SetY($this->y0);
131 }
132
133 function NextCol()
134 {
135 $this->ColSetup(1 - $this->col);
136 if ($this->col == 0) {
137 $this->AddPage();
138 }
139 }
140
141 function AcceptPageBreak()
142 {
143 $this->broken = true;
144 }
145
146 function Space($w=0.1, $h=0.5)
147 {
148 $x = $this->getX();
149 $y = $this->getY();
150 $this->SetLineWidth($w);
151 $this->Line($x, $y, $x+90, $y);
152 $this->Ln($h);
153 }
154
155 function TableRow($l, $r, $font = 'Sans')
156 {
157 $this->SetFont('Vera Sans', 'B', 8);
158 $y = $this->getY();
159 $x = $this->getX();
160 $this->MultiCell(25, 4, $l, '', 1);
161 $y1 = $this->getY();
162
163 $this->SetFont('Vera '.$font, '', 8);
164 $this->setY($y);
165 $first = 1;
166
167 $this->setX($x+25);
168 $this->MultiCell(65, 4, $r, '', 1);
169
170 $this->setY(max($y1, $this->getY())+0.5);
171 $this->setX($x);
172 }
173
174 function Address($a)
175 {
176 $l = "adresse\n";
177 if ($a['active']) {
178 $l .= 'actuelle';
179 } elseif ($a['secondaire']) {
180 $l .= 'secondaire';
181 } else {
182 $l .= 'principale';
183 }
184
185 $r = '';
186 $r = trim("$r\n".$a['adr1']);
187 $r = trim("$r\n".$a['adr2']);
188 $r = trim("$r\n".$a['adr3']);
189 $r = trim("$r\n".trim($a['postcode'].' '.$a['city']));
190
191 $this->TableRow($l, $r);
192
193 if (!empty($a['tels'])) {
194 foreach ($a['tels'] as $tel) {
195 if (!empty($tel['tel'])) {
196 $this->TableRow($tel['tel_type'], $tel['tel'], 'Mono');
197 }
198 }
199 }
200 }
201
202 function AddressPro($a)
203 {
204 if ($a['entreprise']) {
205 $this->TableRow('Entreprise', $a['entreprise']);
206 }
207
208 if ($a['adr1'] || $a['adr2'] || $a['adr3'] || $a['postcode'] || $a['city']) {
209 $r = '';
210 $r = trim("$r\n".$a['adr1']);
211 $r = trim("$r\n".$a['adr2']);
212 $r = trim("$r\n".$a['adr3']);
213 $r = trim("$r\n".trim($a['postcode'].' '.$a['city']));
214 $this->TableRow('adresse pro', $r);
215 }
216
217 if ($a['tel']) {
218 $this->TableRow('Téléphone', $a['tel'], 'Mono');
219 }
220 if ($a['fax']) {
221 $this->TableRow('Fax', $a['fax'], 'Mono');
222 }
223 }
224
225 function Error($msg)
226 {
227 $this->error = true;
228 }
229
230 function wordwrap($text, $maxwidth = 90) {
231 $text = trim($text);
232 if ($text==='') { return 0; }
233 $space = $this->GetStringWidth(' ');
234 $lines = explode("\n", $text);
235 $text = '';
236 $count = 0;
237
238 foreach ($lines as $line) {
239 $words = preg_split('/ +/', $line);
240 $width = 0;
241
242 foreach ($words as $word) {
243 $wordwidth = $this->GetStringWidth($word);
244 if ($width + $wordwidth <= $maxwidth) {
245 $width += $wordwidth + $space;
246 $text .= $word.' ';
247 } else {
248 $width = $wordwidth + $space;
249 $text = rtrim($text)."\n".$word.' ';
250 $count++;
251 }
252 }
253 $text = rtrim($text)."\n";
254 $count++;
255 }
256 $text = rtrim($text);
257 return $count;
258 }
259
260 static function AddContact($self, $x, $wp = true)
261 {
262 /* infamous hack :
263 1- we store the current state.
264 2- at the end, we find out if we triggered the page break,
265 -> no ? ok
266 -> yes ? then we have to create a col, and add the contact again.
267 */
268 $old = clone $self;
269
270 $self->SetFont('Vera Sans', '', 10);
271 $self->SetDrawColor(0);
272 $self->SetFillColor(245, 248, 252);
273 $self->SetLineWidth(0.4);
274
275 $nom = $x['prenom'].' '
276 .($x['nom_usage'] ? "{$x['nom_usage']} ({$x['nom']})" : $x['nom'])
277 ." ({$x['promo']})";
278 $ok = false;
279
280 if ($wp) {
281 $res = XDB::query("SELECT * FROM photo WHERE attachmime IN ('jpeg', 'png') AND uid={?}",
282 $x['user_id']);
283 if ($i = $res->numRows()) {
284 $old2 = clone $self;
285 $photo = $res->fetchOneAssoc();
286 $width = $photo['x'] * 20/$photo['y'];
287 $GLOBALS["p{$x['user_id']}"] = $photo['attach'];
288
289 $_x = $self->getX();
290 $_y = $self->getY();
291 $self->Cell(0, 20, '', '', 0, '', 1);
292 error_reporting(0);
293 $self->Image("var://p{$x['user_id']}", $_x, $_y, $width, 20, $photo['attachmime']);
294 error_reporting($self->report);
295
296 if ($self->error) {
297 $self = clone $old2;
298 } else {
299 $self->setX($_x);
300 $self->Cell($width, 20, '', "T");
301 $h = 20 / $self->wordwrap($nom, 90-$width);
302 $self->MultiCell(0, $h, $nom, 'T', 'C');
303 $ok = true;
304 }
305 }
306 }
307 if (!$ok) {
308 $self->MultiCell(0, 6, $nom, "T", 'C', 1);
309 }
310
311 if ($x['mobile']) {
312 $self->Space();
313 $self->TableRow('mobile', $x['mobile'], 'Mono');
314 }
315
316 foreach ($x['adr'] as $a) {
317 $self->Space();
318 $self->Address($a);
319 }
320
321 if (!empty($x['adr_pro'])) {
322 foreach ($x['adr_pro'] as $a) {
323 if ( ! ($a['entreprise'] || $a['tel'] || $a['fax']
324 || $a['adr1'] || $a['adr2'] || $a['adr3'] || $a['postcode'] || $a['city']) )
325 {
326 continue;
327 }
328 $self->Space();
329 $self->AddressPro($a);
330 }
331 }
332
333 $self->Space(0.4, 5);
334
335 if ($self->broken) {
336 $old->NextCol();
337 $self = ContactsPDF::AddContact($old, $x, $wp);
338 }
339
340 return $self;
341 }
342
343 }
344
345 ?>