Partial port of PDF export of contact list.
[platal.git] / modules / carnet / contacts.pdf.inc.php
index 3529835..616030e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
 
 define ('FPDF_FONTPATH', dirname(__FILE__).'/fonts/');
 require_once '/usr/share/fpdf/fpdf.php';
-require_once dirname(__FILE__).'/../../classes/VarStream.php';
+
+VarStream::init();
 
 class ContactsPDF extends FPDF
 {
-    var $col = 0;
-    var $y0;
+    public $title  = "Mes contacts sur Polytechnique.org";
+
+    private $col = 0;
+    private $y0;
 
-    var $title  = "Mes contacts sur Polytechnique.org";
-    var $broken = false;
-    var $error  = false;
+    private $broken = false;
+    private $error  = false;
 
-    function ContactsPDF()
+    private $report = 0;
+
+    public function __construct()
     {
+        $this->report = error_reporting(0);
         parent::FPDF();
+        error_reporting($this->report);
+
         $this->AddFont('Vera Sans', '',  'Vera.php');
         $this->AddFont('Vera Sans', 'I', 'VeraIt.php');
         $this->AddFont('Vera Sans', 'B', 'VeraBd.php');
@@ -46,13 +53,15 @@ class ContactsPDF extends FPDF
         $this->AddPage();
     }
 
-    function Output()
+    public function Output($name='mescontacts.pdf', $dest='I')
     {
         Header('Pragma: public');
-        parent::Output();
+        error_reporting(0);
+        parent::Output($name, $dest);
+        error_reporting($this->report);
     }
 
-    function Rotate($angle,$x=-1,$y=-1)
+    private function Rotate($angle, $x=-1, $y=-1)
     {
         if ($x==-1) {
             $x = $this->x;
@@ -60,59 +69,60 @@ class ContactsPDF extends FPDF
         if ($y==-1) {
             $y=$this->y;
         }
-        if ($this->angle != 0) {
+        if (!empty($this->angle)) {
             $this->_out('Q');
         }
         $this->angle = $angle;
-        if ($angle != 0)
-        {
+        if ($angle != 0) {
             $angle*=M_PI/180;
             $c  = cos($angle);
             $s  = sin($angle);
             $cx = $x*$this->k;
             $cy = ($this->h-$y)*$this->k;
-            $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));
+            $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));
         }
     }
 
-    function Header()
+    public function Header()
     {
 
         $this->SetFont('Vera Sans', 'B', 20);
         $this->SetTextColor(230);
-        $this->Rotate(45,55,190);
-        $this->Text(55,190,"informations limitées à un usage");
-        $this->Text(40,210,"strictement personnel et non commercial");
+        $this->Rotate(45, 55, 190);
+        $this->Text(55, 190, utf8_decode("informations limitées à un usage"));
+        $this->Text(40, 210, utf8_decode("strictement personnel et non commercial"));
         $this->Rotate(0);
 
         $this->setLeftMargin(5);
         $this->setRightMargin(5);
         $this->SetFont('Vera Sans', 'B', 16);
         $this->SetY(5);
-        $this->SetTextColor(51,102,153);
-        $this->SetDrawColor(102,153,204);
+        $this->SetTextColor(51, 102, 153);
+        $this->SetDrawColor(102, 153, 204);
         $this->SetLineWidth(0.2);
         $this->SetFillColor(245, 248, 252);
         $this->Cell(200, 10, $this->title, 1, 1, 'C', 1);
-        $this->Image(dirname(__FILE__).'/../../htdocs/images/logo.png', 5, 5, 10, 10, 'png', 'https://www.polytechnique.org/');
+        $this->Image(dirname(__FILE__).'/../../htdocs/images/logo.png',
+                     5, 5, 10, 10, 'png', 'https://www.polytechnique.org/');
 
         $this->Ln(10);
         $this->y0 = $this->GetY();
         $this->ColSetup(false);
     }
 
-    function Footer()
+    public function Footer()
     {
         $this->setLeftMargin(5);
         $this->setRightMargin(5);
         $this->SetY(-15);
-        $this->SetFont('Vera Sans','I',8);
+        $this->SetFont('Vera Sans', 'I', 8);
         $this->SetTextColor(128);
         $this->Cell(0, 10, 'Page '.$this->PageNo(), 0, 0, 'C');
         $this->Cell(0, 10, '(en date du '.strftime('%d %B %Y').')', 0, 0, 'R');
     }
 
-    function ColSetup($col)
+    private function ColSetup($col)
     {
         $this->col = $col;
         $x = 10 + $this->col * 100;
@@ -122,7 +132,7 @@ class ContactsPDF extends FPDF
         $this->SetY($this->y0);
     }
 
-    function NextCol()
+    private function NextCol()
     {
         $this->ColSetup(1 - $this->col);
         if ($this->col == 0) {
@@ -130,12 +140,12 @@ class ContactsPDF extends FPDF
         }
     }
 
-    function AcceptPageBreak()
+    public function AcceptPageBreak()
     {
         $this->broken = true;
     }
 
-    function Space($w=0.1, $h=0.5)
+    private function Space($w=0.1, $h=0.5)
     {
         $x = $this->getX();
         $y = $this->getY();
@@ -144,7 +154,7 @@ class ContactsPDF extends FPDF
         $this->Ln($h);
     }
 
-    function TableRow($l, $r, $font = 'Sans')
+    private function TableRow($l, $r, $font = 'Sans')
     {
         $this->SetFont('Vera Sans', 'B', 8);
         $y = $this->getY();
@@ -163,33 +173,41 @@ class ContactsPDF extends FPDF
         $this->setX($x);
     }
 
-    function Address($a)
+    private function Address($a)
     {
+        if (!$a['text']) {
+            return;
+        }
         $l = "adresse\n";
-        if ($a['active']) {
+        if ($a['current']) {
             $l .= 'actuelle';
-        } elseif ($a['secondaire']) {
+        } elseif ($a['secondary']) {
             $l .= 'secondaire';
         } else {
             $l .= 'principale';
         }
 
-        $r = '';
-        $r = trim("$r\n".$a['adr1']);
+        $r = utf8_decode($a['text']);
+/*        $r = trim("$r\n".$a['adr1']);
         $r = trim("$r\n".$a['adr2']);
         $r = trim("$r\n".$a['adr3']);
         $r = trim("$r\n".trim($a['postcode'].' '.$a['city']));
-
+*/
         $this->TableRow($l, $r);
-
-        foreach ($a['tels'] as $tel)
-            if ($tel['tel']) {
-                $this->TableRow($tel['tel_type'], $tel['tel'], 'Mono');
+/*
+        if (!empty($a['tels'])) {
+            foreach ($a['tels'] as $tel) {
+                if (!empty($tel['tel'])) {
+                    $this->TableRow(utf8_decode($tel['tel_type']), $tel['tel'], 'Mono');
+                }
             }
+        }*/
     }
 
-    function AddressPro($a)
+    private function AddressPro($a)
     {
+        return;
+
         if ($a['entreprise']) {
             $this->TableRow('Entreprise', $a['entreprise']);
         }
@@ -204,19 +222,19 @@ class ContactsPDF extends FPDF
         }
 
         if ($a['tel']) {
-            $this->TableRow('Téléphone', $a['tel'], 'Mono');
+            $this->TableRow(utf8_decode('Téléphone'), $a['tel'], 'Mono');
         }
         if ($a['fax']) {
             $this->TableRow('Fax', $a['fax'], 'Mono');
         }
     }
 
-    function Error()
+    public function Error($msg)
     {
         $this->error = true;
     }
 
-    function wordwrap($text, $maxwidth = 90) {
+    private function wordwrap($text, $maxwidth = 90) {
         $text = trim($text);
         if ($text==='') { return 0; }
         $space = $this->GetStringWidth(' ');
@@ -246,7 +264,7 @@ class ContactsPDF extends FPDF
         return $count;
     }
 
-    function AddContact($x, $wp = true)
+    public static function AddContact(ContactsPDF $self, Profile &$profile, $wp = true)
     {
         /* infamous hack :
            1- we store the current state.
@@ -254,73 +272,79 @@ class ContactsPDF extends FPDF
               -> no ? ok
               -> yes ? then we have to create a col, and add the contact again.
         */
-        $old = $this;
+        $old = clone $self;
 
-        $this->SetFont('Vera Sans', '', 10);
-        $this->SetDrawColor(0);
-        $this->SetFillColor(245, 248, 252);
-        $this->SetLineWidth(0.4);
+        $self->SetFont('Vera Sans', '', 10);
+        $self->SetDrawColor(0);
+        $self->SetFillColor(245, 248, 252);
+        $self->SetLineWidth(0.4);
 
-        $nom = $x['prenom'].' '.($x['nom_usage'] ? "{$x['nom_usage']} ({$x['nom']})" : $x['nom'])." ({$x['promo']})";
+        $nom = utf8_decode($profile->full_name);
         $ok  = false;
 
         if ($wp) {
-            $res = XDB::query("SELECT * FROM photo WHERE attachmime IN ('jpeg','png') AND uid={?}", $x['user_id']);
+            $res = XDB::query("SELECT * FROM photo WHERE attachmime IN ('jpeg', 'png') AND uid={?}",
+                              $profile->pid);
             if ($i = $res->numRows()) {
-                $old2  = $this;
+                $old2  = clone $self;
                 $photo = $res->fetchOneAssoc();
                 $width = $photo['x'] * 20/$photo['y'];
-                $GLOBALS["p{$x['user_id']}"] = $photo['attach'];
+                $GLOBALS['p' . $profile->pid] = $photo['attach'];
 
-                $_x = $this->getX();
-                $_y = $this->getY();
-                $this->Cell(0, 20, '', '', 0, '', 1);
-                $this->Image("var://p{$x['user_id']}", $_x, $_y, $width, 20, $photo['attachmime']);
+                $_x = $self->getX();
+                $_y = $self->getY();
+                $self->Cell(0, 20, '', '', 0, '', 1);
+                error_reporting(0);
+                $self->Image("var://p" . $profile->pid, $_x, $_y, $width, 20, $photo['attachmime']);
+                error_reporting($self->report);
 
-                if ($this->error) {
-                    $this = $old2;
+                if ($self->error) {
+                    $self = clone $old2;
                 } else {
-                    $this->setX($_x);
-                    $this->Cell($width, 20, '', "T");
-                    $h = 20 / $this->wordwrap($nom, 90-$width);
-                    $this->MultiCell(0, $h, $nom, 'T', 'C');
+                    $self->setX($_x);
+                    $self->Cell($width, 20, '', "T");
+                    $h = 20 / $self->wordwrap($nom, 90-$width);
+                    $self->MultiCell(0, $h, $nom, 'T', 'C');
                     $ok = true;
                 }
             }
         }
         if (!$ok) {
-            $this->MultiCell(0, 6, $nom, "T", 'C', 1);
+            $self->MultiCell(0, 6, $nom, "T", 'C', 1);
         }
 
-        if ($x['mobile']) {
-            $this->Space();
-            $this->TableRow('mobile', $x['mobile'], 'Mono');
+        if ($profile->mobile) {
+            $self->Space();
+            $self->TableRow('mobile', utf8_decode($profile->mobile), 'Mono');
         }
 
-        foreach ($x['adr'] as $a) {
-            $this->Space();
-            $this->Address($a);
+        $it = $profile->getAddresses(Profile::ADDRESS_ALL);
+        while ($a = $it->next()) {
+            foreach ($a as &$value) {
+                $value = utf8_decode($value);
+            }
+            $self->Space();
+            $self->Address($a);
         }
-
-        foreach ($x['adr_pro'] as $a) {
-            if ( ! ($a['entreprise'] || $a['tel'] || $a['fax']
-                    || $a['adr1'] || $a['adr2'] || $a['adr3'] || $a['postcode'] || $a['city']) )
-            {
-                continue;
+        $it = $profile->getAddresses(Profile::ADDRESS_PRO);
+        while ($a = $it->next()) {
+            foreach ($a as &$value) {
+                $value = utf8_decode($value);
             }
-            $this->Space();
-            $this->AddressPro($a);
+            $self->Space();
+            $self->AddressPro($a);
         }
 
-        $this->Space(0.4, 5);
+        $self->Space(0.4, 5);
 
-        if ($this->broken) {
+        if ($self->broken) {
             $old->NextCol();
-            $old->AddContact($x, $wp);
-            $this = $old;
+            $self = ContactsPDF::AddContact($old, $profile, $wp);
         }
-    }
 
+        return $self;
+    }
 }
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>