PDF + VarStream
authorPierre Habouzit (MadCoder <pierre.habouzit@m4x.org>
Wed, 29 Dec 2004 18:12:45 +0000 (18:12 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 26 Jun 2008 21:27:01 +0000 (23:27 +0200)
* enhance PDF with photos

* VarStream :
    this is a real kludge, it allows to access global variables like file.
    every global var is accessible with 'var://varname' url.
    really cool for the previous since I don't want to use temporary files.

git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-179

ChangeLog
htdocs/carnet/mescontacts_pdf.php
include/contacts.pdf.inc.php
include/xorg.varstream.inc.php [new file with mode: 0644]
templates/carnet/mescontacts.tpl

index 17f27ab..ff56d69 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,7 @@ New :
 
        * Contacts :
                - Brand new PDF of the contact list (using FPDF).                                       -MC
+               - Even include Photos in the PDF (kludgy !).                                            -MC
 
        * Validation :
                - quite a lot of rewrite, simplifications.                                                      -MC
index ba074c3..3dce194 100644 (file)
@@ -40,9 +40,8 @@ $pdf   = new ContactsPDF();
 
 while (list($alias) = $citer->next()) {
     $user = get_user_details($alias);
-    $pdf->addContact($user);
+    $pdf->addContact($user, Env::has('photo'));
 }
-
 $pdf->Output();
 
 ?>
index af06a5f..9a2342c 100644 (file)
 
 define ('FPDF_FONTPATH', dirname(__FILE__).'/fonts/');
 require_once('/usr/share/fpdf/fpdf.php');
+require_once('xorg.varstream.inc.php');
 
 class ContactsPDF extends FPDF
 {
     var $col = 0;
     var $y0;
 
-    var $title = "Mes contacts sur Polytechnique.org";
+    var $title  = "Mes contacts sur Polytechnique.org";
     var $broken = false;
+    var $error  = false;
 
     function ContactsPDF()
     {
@@ -44,6 +46,12 @@ class ContactsPDF extends FPDF
         $this->AddPage();
     }
 
+    function Output()
+    {
+        Header('Pragma: public');
+        parent::Output();
+    }
+
     function Rotate($angle,$x=-1,$y=-1)
     {
         if($x==-1) {
@@ -202,7 +210,12 @@ class ContactsPDF extends FPDF
         }
     }
 
-    function AddContact($x)
+    function Error()
+    {
+        $this->error = true;
+    }
+
+    function AddContact($x, $wp = true)
     {
         global $globals;
         /* infamous hack :
@@ -219,7 +232,36 @@ class ContactsPDF extends FPDF
         $this->SetLineWidth(0.4);
 
         $nom = $x['prenom'].' '.($x['epouse'] ? "{$x['epouse']} - née {$x['nom']}" : $x['nom'])." ({$x['promo']})";
-        $this->Cell(0, 6, $nom, "T", 1, 'C', 1, $globals->baseurl."/fiche.php?user={$x['forlife']}");
+        $ok  = false;
+
+        if ($wp) {
+            $res = $globals->xdb->query("SELECT * FROM photo WHERE attachmime IN ('jpeg','png') AND uid={?}", $x['user_id']);
+            if ($i = $res->numRows()) {
+                $old2  = $this;
+                $photo = $res->fetchOneAssoc();
+                $width = $photo['x'] * 20/$photo['y'];
+                $GLOBALS["p{$x['user_id']}"] = $photo['attach'];
+               
+                $_x = $this->getX();
+                $_y = $this->getY();
+                $this->Cell(0, 20, '', '', 0, 'C', 1);
+                $this->Image("var://p{$x['user_id']}", $_x, $_y, $width, 20, $photo['attachmime']);
+                
+                if ($this->error) {
+                    $this = $old2;
+                } else {
+                    $this->setX($_x);
+                    $this->Cell($width, 20, '', "T");
+                    $h = ( $this->GetStringWidth($nom) + $width > 90 ) ? 10 : 20;
+                    $this->MultiCell(0, $h, $nom, 'T', 'C');
+                    $ok = true;
+                }
+            }
+        }
+        if (!$ok) {
+            $this->MultiCell(0, 6, $nom, "T", 'C', 1);
+        }
+        
         $this->Space();
 
         if ($x['mobile']) {
@@ -242,12 +284,11 @@ class ContactsPDF extends FPDF
             $this->Space();
         }
 
-        $this->Ln();
-        $this->Ln();
+        $this->Ln(10);
 
         if ($this->broken) {
             $old->NextCol();
-            $old->AddContact($x);
+            $old->AddContact($x, $wp);
             $this = $old;
         }
     }
diff --git a/include/xorg.varstream.inc.php b/include/xorg.varstream.inc.php
new file mode 100644 (file)
index 0000000..ef0909d
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2004 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+// {{{ class VarStream
+class VarStream
+{
+    // {{{ properties
+    
+    // Stream handler to read from global variables
+    var $varname;
+    var $position;
+
+    // }}}
+    // {{{ stream_open
+
+    function stream_open($path, $mode, $options, &$opened_path)
+    {
+        $url = parse_url($path);
+        $this->varname = $url['host'];
+        if(!isset($GLOBALS[$this->varname]))
+        {
+            trigger_error('Global variable '.$this->varname.' does not exist', E_USER_WARNING);
+            return false;
+        }
+        $this->position = 0;
+        return true;
+    }
+
+    // }}}
+    // {{{ stream_read
+
+    function stream_read($count)
+    {
+        $ret = substr($GLOBALS[$this->varname], $this->position, $count);
+        $this->position += strlen($ret);
+        return $ret;
+    }
+
+    // }}}
+    // {{{ stream_eof
+
+    function stream_eof()
+    {
+        return $this->position >= strlen($GLOBALS[$this->varname]);
+    }
+
+    // }}}
+}
+
+// }}}
+
+stream_wrapper_register('var','VarStream');
+
+// vim:set et sw=4 sts=4 sws=4:
+?>
index 18ffdb6..2acea6a 100644 (file)
 
 {if $trombi || $citer->total()}
 <p>
-  Pour récupérer ta liste de contacts dans un PDF imprimable :<br />
-  [<a href="mescontacts_pdf.php/mes_contacts.pdf?order=promo" class='popup'><strong>Triée par promo</strong></a>]
-  [<a href="mescontacts_pdf.php/mes_contacts.pdf" class='popup'><strong>Triée par noms</strong></a>]
+Pour récupérer ta liste de contacts dans un PDF imprimable :<br />
+(attention, les photos font beaucoup grossir les fichiers !)
 </p>
+<ul>
+  <li>avec les photos :
+  [<a href="mescontacts_pdf.php/mes_contacts.pdf?order=promo&amp;photo" class='popup'><strong>tri par promo</strong></a>]
+  [<a href="mescontacts_pdf.php/mes_contacts.pdf?photo" class='popup'><strong>tri par noms</strong></a>]
+  </li>
+  <li>sans les photos :
+  [<a href="mescontacts_pdf.php/mes_contacts.pdf?order=promo" class='popup'><strong>tri par promo</strong></a>]
+  [<a href="mescontacts_pdf.php/mes_contacts.pdf" class='popup'><strong>tri par noms</strong></a>]
+  </li>
+</ul>
 
 {if $trombi}