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