Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
0337d704 | 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/'); | |
63528107 | 23 | require_once '/usr/share/fpdf/fpdf.php'; |
c83168b1 | 24 | |
25 | VarStream::init(); | |
0337d704 | 26 | |
27 | class ContactsPDF extends FPDF | |
28 | { | |
c83168b1 | 29 | public $title = "Mes contacts sur Polytechnique.org"; |
30 | ||
31 | private $col = 0; | |
32 | private $y0; | |
0337d704 | 33 | |
c83168b1 | 34 | private $broken = false; |
35 | private $error = false; | |
0337d704 | 36 | |
c83168b1 | 37 | private $report = 0; |
93c099e1 | 38 | |
714f5437 | 39 | public function __construct() |
0337d704 | 40 | { |
93c099e1 | 41 | $this->report = error_reporting(0); |
0337d704 | 42 | parent::FPDF(); |
93c099e1 | 43 | error_reporting($this->report); |
44 | ||
0337d704 | 45 | $this->AddFont('Vera Sans', '', 'Vera.php'); |
46 | $this->AddFont('Vera Sans', 'I', 'VeraIt.php'); | |
47 | $this->AddFont('Vera Sans', 'B', 'VeraBd.php'); | |
b48a0758 | 48 | |
0337d704 | 49 | $this->AddFont('Vera Mono', '', 'VeraMono.php'); |
b48a0758 | 50 | |
0337d704 | 51 | $this->SetTitle($this->title); |
52 | $this->SetCreator('Site Polytechnique.org'); | |
53 | $this->AddPage(); | |
54 | } | |
55 | ||
714f5437 | 56 | public function Output($name='mescontacts.pdf', $dest='I') |
0337d704 | 57 | { |
58 | Header('Pragma: public'); | |
93c099e1 | 59 | error_reporting(0); |
60 | parent::Output($name, $dest); | |
61 | error_reporting($this->report); | |
0337d704 | 62 | } |
63 | ||
714f5437 | 64 | private function Rotate($angle, $x=-1, $y=-1) |
0337d704 | 65 | { |
e54098b8 | 66 | if ($x==-1) { |
0337d704 | 67 | $x = $this->x; |
68 | } | |
e54098b8 | 69 | if ($y==-1) { |
0337d704 | 70 | $y=$this->y; |
71 | } | |
93c099e1 | 72 | if (!empty($this->angle)) { |
0337d704 | 73 | $this->_out('Q'); |
74 | } | |
75 | $this->angle = $angle; | |
93c099e1 | 76 | if ($angle != 0) { |
0337d704 | 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; | |
93c099e1 | 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)); | |
0337d704 | 84 | } |
85 | } | |
b48a0758 | 86 | |
714f5437 | 87 | public function Header() |
0337d704 | 88 | { |
89 | ||
90 | $this->SetFont('Vera Sans', 'B', 20); | |
91 | $this->SetTextColor(230); | |
93c099e1 | 92 | $this->Rotate(45, 55, 190); |
ac73e294 | 93 | $this->Text(55, 190, utf8_decode("informations limitées à un usage")); |
94 | $this->Text(40, 210, utf8_decode("strictement personnel et non commercial")); | |
0337d704 | 95 | $this->Rotate(0); |
b48a0758 | 96 | |
0337d704 | 97 | $this->setLeftMargin(5); |
98 | $this->setRightMargin(5); | |
99 | $this->SetFont('Vera Sans', 'B', 16); | |
100 | $this->SetY(5); | |
93c099e1 | 101 | $this->SetTextColor(51, 102, 153); |
102 | $this->SetDrawColor(102, 153, 204); | |
0337d704 | 103 | $this->SetLineWidth(0.2); |
104 | $this->SetFillColor(245, 248, 252); | |
105 | $this->Cell(200, 10, $this->title, 1, 1, 'C', 1); | |
93c099e1 | 106 | $this->Image(dirname(__FILE__).'/../../htdocs/images/logo.png', |
107 | 5, 5, 10, 10, 'png', 'https://www.polytechnique.org/'); | |
b48a0758 | 108 | |
0337d704 | 109 | $this->Ln(10); |
110 | $this->y0 = $this->GetY(); | |
111 | $this->ColSetup(false); | |
112 | } | |
113 | ||
714f5437 | 114 | public function Footer() |
0337d704 | 115 | { |
116 | $this->setLeftMargin(5); | |
117 | $this->setRightMargin(5); | |
118 | $this->SetY(-15); | |
93c099e1 | 119 | $this->SetFont('Vera Sans', 'I', 8); |
0337d704 | 120 | $this->SetTextColor(128); |
121 | $this->Cell(0, 10, 'Page '.$this->PageNo(), 0, 0, 'C'); | |
7d804f13 | 122 | $this->Cell(0, 10, '(en date du '.strftime('%d %B %Y').')', 0, 0, 'R'); |
0337d704 | 123 | } |
124 | ||
714f5437 | 125 | private function ColSetup($col) |
0337d704 | 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 | ||
714f5437 | 135 | private function NextCol() |
0337d704 | 136 | { |
137 | $this->ColSetup(1 - $this->col); | |
e54098b8 | 138 | if ($this->col == 0) { |
139 | $this->AddPage(); | |
140 | } | |
0337d704 | 141 | } |
142 | ||
714f5437 | 143 | public function AcceptPageBreak() |
0337d704 | 144 | { |
145 | $this->broken = true; | |
146 | } | |
147 | ||
714f5437 | 148 | private function Space($w=0.1, $h=0.5) |
0337d704 | 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 | ||
714f5437 | 157 | private function TableRow($l, $r, $font = 'Sans') |
0337d704 | 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; | |
b48a0758 | 168 | |
0337d704 | 169 | $this->setX($x+25); |
170 | $this->MultiCell(65, 4, $r, '', 1); | |
b48a0758 | 171 | |
0337d704 | 172 | $this->setY(max($y1, $this->getY())+0.5); |
173 | $this->setX($x); | |
174 | } | |
175 | ||
714f5437 | 176 | private function Address($a) |
0337d704 | 177 | { |
da4dc2a9 | 178 | if (!$a->text) { |
714f5437 FB |
179 | return; |
180 | } | |
0337d704 | 181 | $l = "adresse\n"; |
da4dc2a9 | 182 | if ($a->hasFlag('current')) { |
0337d704 | 183 | $l .= 'actuelle'; |
da4dc2a9 | 184 | } elseif ($a->hasFlag('secondary')) { |
0337d704 | 185 | $l .= 'secondaire'; |
186 | } else { | |
187 | $l .= 'principale'; | |
188 | } | |
189 | ||
ca3f3ae9 FB |
190 | $this->TableRow($l, utf8_decode($a->text)); |
191 | ||
192 | foreach ($a->phones() as $phone) { | |
89d17c5a | 193 | $this->TableRow(utf8_decode($phone->displayType()), |
ca3f3ae9 FB |
194 | utf8_decode($phone->display), 'Mono'); |
195 | } | |
0337d704 | 196 | } |
197 | ||
714f5437 | 198 | private function AddressPro($a) |
0337d704 | 199 | { |
ca3f3ae9 FB |
200 | if ($a->company) { |
201 | $this->TableRow('Entreprise', utf8_decode($a->company->name)); | |
0337d704 | 202 | } |
ca3f3ae9 FB |
203 | if ($a->address()) { |
204 | $this->TableRow('adresse pro', utf8_decode($a->address()->text)); | |
0337d704 | 205 | } |
ca3f3ae9 | 206 | foreach ($a->phones() as $phone) { |
89d17c5a | 207 | $this->TableRow(utf8_decode($phone->displayType()), |
ca3f3ae9 | 208 | utf8_decode($phone->display), 'Mono'); |
0337d704 | 209 | } |
210 | } | |
211 | ||
714f5437 | 212 | public function Error($msg) |
0337d704 | 213 | { |
214 | $this->error = true; | |
215 | } | |
216 | ||
714f5437 | 217 | private function wordwrap($text, $maxwidth = 90) { |
0337d704 | 218 | $text = trim($text); |
219 | if ($text==='') { return 0; } | |
220 | $space = $this->GetStringWidth(' '); | |
221 | $lines = explode("\n", $text); | |
222 | $text = ''; | |
223 | $count = 0; | |
224 | ||
e54098b8 | 225 | foreach ($lines as $line) { |
0337d704 | 226 | $words = preg_split('/ +/', $line); |
227 | $width = 0; | |
228 | ||
e54098b8 | 229 | foreach ($words as $word) { |
0337d704 | 230 | $wordwidth = $this->GetStringWidth($word); |
e54098b8 | 231 | if ($width + $wordwidth <= $maxwidth) { |
0337d704 | 232 | $width += $wordwidth + $space; |
233 | $text .= $word.' '; | |
e54098b8 | 234 | } else { |
0337d704 | 235 | $width = $wordwidth + $space; |
e54098b8 | 236 | $text = rtrim($text)."\n".$word.' '; |
0337d704 | 237 | $count++; |
238 | } | |
239 | } | |
240 | $text = rtrim($text)."\n"; | |
241 | $count++; | |
242 | } | |
243 | $text = rtrim($text); | |
244 | return $count; | |
245 | } | |
246 | ||
26ba053e | 247 | public static function AddContact(ContactsPDF $self, Profile $profile, $wp = true) |
0337d704 | 248 | { |
0337d704 | 249 | /* infamous hack : |
250 | 1- we store the current state. | |
251 | 2- at the end, we find out if we triggered the page break, | |
252 | -> no ? ok | |
253 | -> yes ? then we have to create a col, and add the contact again. | |
254 | */ | |
93c099e1 | 255 | $old = clone $self; |
0337d704 | 256 | |
93c099e1 | 257 | $self->SetFont('Vera Sans', '', 10); |
258 | $self->SetDrawColor(0); | |
259 | $self->SetFillColor(245, 248, 252); | |
260 | $self->SetLineWidth(0.4); | |
0337d704 | 261 | |
714f5437 | 262 | $nom = utf8_decode($profile->full_name); |
0337d704 | 263 | $ok = false; |
264 | ||
265 | if ($wp) { | |
da4dc2a9 | 266 | $photo = $profile->getPhoto(false, true); |
833a6e86 | 267 | if ($photo) { |
1415e9ca SJ |
268 | list(, $type) = explode('/', $photo->mimeType()); |
269 | $type = ($type == 'jpeg') ? 'jpg' : $type; | |
270 | if (method_exists($self, '_parse' . $type)) { | |
271 | $old2 = clone $self; | |
272 | $width = $photo->width() * 20 / $photo->height(); | |
273 | $_x = $self->getX(); | |
274 | $_y = $self->getY(); | |
275 | $self->Cell(0, 20, '', '', 0, '', 1); | |
276 | error_reporting(0); | |
277 | $self->Image($photo->path(), $_x, $_y, $width, 20, $type); | |
278 | error_reporting($self->report); | |
279 | ||
280 | if ($self->error) { | |
281 | $self = clone $old2; | |
282 | } else { | |
283 | $self->setX($_x); | |
284 | $self->Cell($width, 20, '', "T"); | |
285 | $h = 20 / $self->wordwrap($nom, 90 - $width); | |
286 | $self->MultiCell(0, $h, $nom, 'T', 'C'); | |
287 | $ok = true; | |
288 | } | |
0337d704 | 289 | } |
290 | } | |
291 | } | |
292 | if (!$ok) { | |
93c099e1 | 293 | $self->MultiCell(0, 6, $nom, "T", 'C', 1); |
0337d704 | 294 | } |
b48a0758 | 295 | |
714f5437 | 296 | if ($profile->mobile) { |
93c099e1 | 297 | $self->Space(); |
714f5437 | 298 | $self->TableRow('mobile', utf8_decode($profile->mobile), 'Mono'); |
0337d704 | 299 | } |
300 | ||
598c57f6 | 301 | $it = $profile->iterAddresses(Profile::ADDRESS_ALL); |
714f5437 | 302 | while ($a = $it->next()) { |
714f5437 | 303 | $self->Space(); |
93c099e1 | 304 | $self->Address($a); |
0337d704 | 305 | } |
ca3f3ae9 FB |
306 | $it = $profile->getJobs(Profile::JOBS_CURRENT); |
307 | foreach ($it as $a) { | |
714f5437 FB |
308 | $self->Space(); |
309 | $self->AddressPro($a); | |
0337d704 | 310 | } |
311 | ||
93c099e1 | 312 | $self->Space(0.4, 5); |
0337d704 | 313 | |
93c099e1 | 314 | if ($self->broken) { |
0337d704 | 315 | $old->NextCol(); |
714f5437 | 316 | $self = ContactsPDF::AddContact($old, $profile, $wp); |
0337d704 | 317 | } |
93c099e1 | 318 | |
319 | return $self; | |
0337d704 | 320 | } |
0337d704 | 321 | } |
322 | ||
a7de4ef7 | 323 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 324 | ?> |