0337d704 |
1 | <?php |
2 | /*************************************************************************** |
3 | * Copyright (C) 2003-2004 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('xorg.varstream.inc.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 | function ContactsPDF() |
36 | { |
37 | parent::FPDF(); |
38 | $this->AddFont('Vera Sans', '', 'Vera.php'); |
39 | $this->AddFont('Vera Sans', 'I', 'VeraIt.php'); |
40 | $this->AddFont('Vera Sans', 'B', 'VeraBd.php'); |
41 | |
42 | $this->AddFont('Vera Mono', '', 'VeraMono.php'); |
43 | |
44 | $this->SetTitle($this->title); |
45 | $this->SetCreator('Site Polytechnique.org'); |
46 | $this->AddPage(); |
47 | } |
48 | |
49 | function Output() |
50 | { |
51 | Header('Pragma: public'); |
52 | parent::Output(); |
53 | } |
54 | |
55 | function Rotate($angle,$x=-1,$y=-1) |
56 | { |
57 | if($x==-1) { |
58 | $x = $this->x; |
59 | } |
60 | if($y==-1) { |
61 | $y=$this->y; |
62 | } |
63 | if($this->angle != 0) { |
64 | $this->_out('Q'); |
65 | } |
66 | $this->angle = $angle; |
67 | if($angle != 0) |
68 | { |
69 | $angle*=M_PI/180; |
70 | $c = cos($angle); |
71 | $s = sin($angle); |
72 | $cx = $x*$this->k; |
73 | $cy = ($this->h-$y)*$this->k; |
74 | $this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy)); |
75 | } |
76 | } |
77 | |
78 | function Header() |
79 | { |
80 | |
81 | $this->SetFont('Vera Sans', 'B', 20); |
82 | $this->SetTextColor(230); |
83 | $this->Rotate(45,55,190); |
84 | $this->Text(55,190,"informations limitées à un usage"); |
85 | $this->Text(40,210,"strictement personnel et non commercial"); |
86 | $this->Rotate(0); |
87 | |
88 | $this->setLeftMargin(5); |
89 | $this->setRightMargin(5); |
90 | $this->SetFont('Vera Sans', 'B', 16); |
91 | $this->SetY(5); |
92 | $this->SetTextColor(51,102,153); |
93 | $this->SetDrawColor(102,153,204); |
94 | $this->SetLineWidth(0.2); |
95 | $this->SetFillColor(245, 248, 252); |
96 | $this->Cell(200, 10, $this->title, 1, 1, 'C', 1); |
97 | $this->Image(dirname(__FILE__).'/../htdocs/images/logo.png', 5, 5, 10, 10, 'png', 'https://www.polytechnique.org/'); |
98 | |
99 | $this->Ln(10); |
100 | $this->y0 = $this->GetY(); |
101 | $this->ColSetup(false); |
102 | } |
103 | |
104 | function Footer() |
105 | { |
106 | $this->setLeftMargin(5); |
107 | $this->setRightMargin(5); |
108 | $this->SetY(-15); |
109 | $this->SetFont('Vera Sans','I',8); |
110 | $this->SetTextColor(128); |
111 | $this->Cell(0, 10, 'Page '.$this->PageNo(), 0, 0, 'C'); |
112 | } |
113 | |
114 | function ColSetup($col) |
115 | { |
116 | $this->col = $col; |
117 | $x = 10 + $this->col * 100; |
118 | $this->SetLeftMargin($x); |
119 | $this->SetRightMargin(120 - $x); |
120 | $this->SetX($x); |
121 | $this->SetY($this->y0); |
122 | } |
123 | |
124 | function NextCol() |
125 | { |
126 | $this->ColSetup(1 - $this->col); |
127 | if ($this->col == 0) { $this->AddPage(); } |
128 | } |
129 | |
130 | function AcceptPageBreak() |
131 | { |
132 | $this->broken = true; |
133 | } |
134 | |
135 | function Space($w=0.1, $h=0.5) |
136 | { |
137 | $x = $this->getX(); |
138 | $y = $this->getY(); |
139 | $this->SetLineWidth($w); |
140 | $this->Line($x, $y, $x+90, $y); |
141 | $this->Ln($h); |
142 | } |
143 | |
144 | function TableRow($l, $r, $font = 'Sans') |
145 | { |
146 | $this->SetFont('Vera Sans', 'B', 8); |
147 | $y = $this->getY(); |
148 | $x = $this->getX(); |
149 | $this->MultiCell(25, 4, $l, '', 1); |
150 | $y1 = $this->getY(); |
151 | |
152 | $this->SetFont('Vera '.$font, '', 8); |
153 | $this->setY($y); |
154 | $first = 1; |
155 | |
156 | $this->setX($x+25); |
157 | $this->MultiCell(65, 4, $r, '', 1); |
158 | |
159 | $this->setY(max($y1, $this->getY())+0.5); |
160 | $this->setX($x); |
161 | } |
162 | |
163 | function Address($a) |
164 | { |
165 | $l = "adresse\n"; |
166 | if ($a['active']) { |
167 | $l .= 'actuelle'; |
168 | } elseif ($a['secondaire']) { |
169 | $l .= 'secondaire'; |
170 | } else { |
171 | $l .= 'principale'; |
172 | } |
173 | |
174 | $r = ''; |
175 | $r = trim("$r\n".$a['adr1']); |
176 | $r = trim("$r\n".$a['adr2']); |
177 | $r = trim("$r\n".$a['adr3']); |
178 | $r = trim("$r\n".trim($a['postcode'].' '.$a['city'])); |
179 | |
180 | $this->TableRow($l, $r); |
181 | |
182 | if ($a['tel']) { |
183 | $this->TableRow('Téléphone', $a['tel'], 'Mono'); |
184 | } |
185 | if ($a['fax']) { |
186 | $this->TableRow('Fax', $a['fax'], 'Mono'); |
187 | } |
188 | } |
189 | |
190 | function AddressPro($a) |
191 | { |
192 | if ($a['entreprise']) { |
193 | $this->TableRow('Entreprise', $a['entreprise']); |
194 | } |
195 | |
196 | if ($a['adr1'] || $a['adr2'] || $a['adr3'] || $a['postcode'] || $a['city']) { |
197 | $r = ''; |
198 | $r = trim("$r\n".$a['adr1']); |
199 | $r = trim("$r\n".$a['adr2']); |
200 | $r = trim("$r\n".$a['adr3']); |
201 | $r = trim("$r\n".trim($a['postcode'].' '.$a['city'])); |
202 | $this->TableRow('adresse pro', $r); |
203 | } |
204 | |
205 | if ($a['tel']) { |
206 | $this->TableRow('Téléphone', $a['tel'], 'Mono'); |
207 | } |
208 | if ($a['fax']) { |
209 | $this->TableRow('Fax', $a['fax'], 'Mono'); |
210 | } |
211 | } |
212 | |
213 | function Error() |
214 | { |
215 | $this->error = true; |
216 | } |
217 | |
218 | function wordwrap($text, $maxwidth = 90) { |
219 | $text = trim($text); |
220 | if ($text==='') { return 0; } |
221 | $space = $this->GetStringWidth(' '); |
222 | $lines = explode("\n", $text); |
223 | $text = ''; |
224 | $count = 0; |
225 | |
226 | foreach ($lines as $line) |
227 | { |
228 | $words = preg_split('/ +/', $line); |
229 | $width = 0; |
230 | |
231 | foreach ($words as $word) |
232 | { |
233 | $wordwidth = $this->GetStringWidth($word); |
234 | if ($width + $wordwidth <= $maxwidth) |
235 | { |
236 | $width += $wordwidth + $space; |
237 | $text .= $word.' '; |
238 | } |
239 | else |
240 | { |
241 | $width = $wordwidth + $space; |
242 | $text = rtrim($text)."\n".$word.' |
243 | '; |
244 | $count++; |
245 | } |
246 | } |
247 | $text = rtrim($text)."\n"; |
248 | $count++; |
249 | } |
250 | $text = rtrim($text); |
251 | return $count; |
252 | } |
253 | |
254 | function AddContact($x, $wp = true) |
255 | { |
256 | global $globals; |
257 | /* infamous hack : |
258 | 1- we store the current state. |
259 | 2- at the end, we find out if we triggered the page break, |
260 | -> no ? ok |
261 | -> yes ? then we have to create a col, and add the contact again. |
262 | */ |
263 | $old = $this; |
264 | |
265 | $this->SetFont('Vera Sans', '', 10); |
266 | $this->SetDrawColor(0); |
267 | $this->SetFillColor(245, 248, 252); |
268 | $this->SetLineWidth(0.4); |
269 | |
270 | $nom = $x['prenom'].' '.($x['nom_usage'] ? "{$x['nom_usage']} ({$x['nom']})" : $x['nom'])." ({$x['promo']})"; |
271 | $ok = false; |
272 | |
273 | if ($wp) { |
274 | $res = $globals->xdb->query("SELECT * FROM photo WHERE attachmime IN ('jpeg','png') AND uid={?}", $x['user_id']); |
275 | if ($i = $res->numRows()) { |
276 | $old2 = $this; |
277 | $photo = $res->fetchOneAssoc(); |
278 | $width = $photo['x'] * 20/$photo['y']; |
279 | $GLOBALS["p{$x['user_id']}"] = $photo['attach']; |
280 | |
281 | $_x = $this->getX(); |
282 | $_y = $this->getY(); |
283 | $this->Cell(0, 20, '', '', 0, '', 1); |
284 | $this->Image("var://p{$x['user_id']}", $_x, $_y, $width, 20, $photo['attachmime']); |
285 | |
286 | if ($this->error) { |
287 | $this = $old2; |
288 | } else { |
289 | $this->setX($_x); |
290 | $this->Cell($width, 20, '', "T"); |
291 | $h = 20 / $this->wordwrap($nom, 90-$width); |
292 | $this->MultiCell(0, $h, $nom, 'T', 'C'); |
293 | $ok = true; |
294 | } |
295 | } |
296 | } |
297 | if (!$ok) { |
298 | $this->MultiCell(0, 6, $nom, "T", 'C', 1); |
299 | } |
300 | |
301 | if ($x['mobile']) { |
302 | $this->Space(); |
303 | $this->TableRow('mobile', $x['mobile'], 'Mono'); |
304 | } |
305 | |
306 | foreach ($x['adr'] as $a) { |
307 | $this->Space(); |
308 | $this->Address($a); |
309 | } |
310 | |
311 | foreach ($x['adr_pro'] as $a) { |
312 | if ( ! ($a['entreprise'] || $a['tel'] || $a['fax'] |
313 | || $a['adr1'] || $a['adr2'] || $a['adr3'] || $a['postcode'] || $a['city']) ) |
314 | { |
315 | continue; |
316 | } |
317 | $this->Space(); |
318 | $this->AddressPro($a); |
319 | } |
320 | |
321 | $this->Space(0.4, 5); |
322 | |
323 | if ($this->broken) { |
324 | $old->NextCol(); |
325 | $old->AddContact($x, $wp); |
326 | $this = $old; |
327 | } |
328 | } |
329 | |
330 | } |
331 | |
332 | ?> |