Switch to the new VCard.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 28 Aug 2008 22:00:46 +0000 (00:00 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 28 Aug 2008 22:00:46 +0000 (00:00 +0200)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
core
include/user.func.inc.php
include/vcard.inc.php
modules/carnet.php
modules/profile.php
modules/xnetgrp.php
templates/core/vcard.tpl [deleted file]

diff --git a/core b/core
index 3716d92..eda1814 160000 (submodule)
--- a/core
+++ b/core
@@ -1 +1 @@
-Subproject commit 3716d925e42ab021bc3d8b996fd91e4f1ed689e8
+Subproject commit eda18149373d85ebbf9a529fb56bcb6d213e2b84
index 21bcbb5..d9232a3 100644 (file)
@@ -358,6 +358,7 @@ function get_user_details_adr($uid, $view = 'private') {
                      gp.pays AS countrytxt,a.region, a.regiontxt,
                      FIND_IN_SET('active', a.statut) AS active, a.adrid,
                      FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
+                     FIND_IN_SET('courrier', a.statut) AS courier,
                      a.pub, gp.display
                FROM  adresses AS a
           LEFT JOIN  geoloc_pays AS gp ON (gp.a2=a.country)
index 6083b25..0fc553e 100644 (file)
 
 require_once('user.func.inc.php');
 
-class VCardIterator implements PlIterator
+class VCard extends PlVCard
 {
     private $user_list = array();
     private $count     = 0;
     private $freetext  = null;
     private $photos    = true;
 
-    public function __construct($photos, $freetext)
+    public function __construct($photos = true, $freetext = null)
     {
+        PlVCard::$folding = false;
         $this->freetext = $freetext;
         $this->photos   = $photos;
     }
 
-    public function add_user($user)
+    public function addUser($user)
     {
         $forlife = get_user_forlife($user, '_silent_user_callback');
         if ($forlife) {
@@ -43,42 +44,94 @@ class VCardIterator implements PlIterator
         }
     }
 
-    public function first()
-    {
-        return count($this->user_list) == $this->count - 1;
-    }
-
-    public function last()
-    {
-        return count($this->user_list) == 0;
+    public function addUsers(array $users) {
+        foreach ($users as $user) {
+            $this->addUser($user);
+        }
     }
 
-    public function total()
+    protected function fetch()
     {
-        return $this->count;
+        return new PlArrayIterator($this->user_list);
     }
 
-    public function next()
+    protected function buildEntry($entry)
     {
-        if (!$this->user_list) {
-            return null;
-        }
         global $globals;
-        $login = array_shift($this->user_list);
+        $login = $entry['value'];
         $user  = get_user_details($login);
 
-        if (strlen(trim($user['freetext']))) {
-            $user['freetext'] = pl_entity_decode($user['freetext']);
+        if (empty($user['nom_usage'])) {
+            $entry = new PlVCardEntry($user['prenom'], $user['nom'], null, null, @$user['nickname']);
+        } else {
+            $entry = new PlVCardEntry($user['prenom'], array($user['nom'], $user['nom_usage']), null, null, @$user['nickname']);
         }
+
+        // Free text
+        $freetext = '(' . $user['promo'] . ')';
         if ($this->freetext) {
-            if (strlen(trim($user['freetext']))) {
-                $user['freetext'] = $this->freetext . "\n" . $user['freetext'];
-            } else {
-                $user['freetext'] = $this->freetext;
+            $freetext .= "\n" . $this->freetext;
+        }
+        if (strlen(trim($user['freetext']))) {
+            $freetext .= "\n" . MiniWiki::WikiToText($user['freetext']);
+        }
+        $entry->set('NOTE', $freetext);
+
+        // Mobile
+        if (!empty($user['mobile'])) {
+            $entry->addTel(null, $user['mobile'], false, true, true, false, true, true);
+        }
+
+        // Emails
+        $entry->addMail(null, $user['bestalias'] . '@' . $globals->mail->domain, true);
+        $entry->addMail(null, $user['bestalias'] . '@' . $globals->mail->domain2);
+        if ($user['bestalias'] != $user['forlife']) {
+            $entry->addMail(null, $user['forlife'] . '@' . $globals->mail->domain);
+            $entry->addMail(null, $user['forlife'] . '@' . $globals->mail->domain2);
+        }
+
+        // Homes
+        foreach ($user['adr'] as $adr) {
+            $street = array($adr['adr1']);
+            if (!empty($adr['adr2'])) {
+                $street[] = $adr['adr2'];
+            }
+            if (!empty($adr['adr3'])) {
+                $street[] = $adr['adr3'];
+            }
+            $group = $entry->addHome($street, null, null, $adr['postcode'], $adr['city'], $adr['region'], @$adr['country'],
+                                     $adr['active'], $adr['courier'], $adr['courier']);
+            if (!empty($adr['tels'])) {
+                foreach ($adr['tels'] as $tel) {
+                    $fax = $tel['tel_type'] == 'Fax';
+                    $entry->addTel($group, $tel['tel'], $fax, !$fax, !$fax, false, false, !$fax && $adr['active'] && empty($user['mobile']));
+                }
+            }
+        }
+
+        // Pro
+        foreach ($user['adr_pro'] as $pro) {
+            $street = array($adr['adr1']);
+            if (!empty($pro['adr2'])) {
+                $street[] = $pro['adr2'];
+            }
+            if (!empty($pro['adr3'])) {
+                $street[] = $pro['adr3'];
+            }
+            $group = $entry->addWork($pro['entreprise'], null, $pro['poste'], $pro['fonction'],
+                                     $street, null, null, $pro['postcode'], $pro['city'], $pro['region'], @$pro['country']);
+            if (!empty($pro['tel'])) {
+                $entry->addTel($group, $pro['tel']);
+            }
+            if (!empty($pro['fax'])) {
+                $entry->addTel($group, $pro['fax'], true);
+            }
+            if (!empty($pro['email'])) {
+                $entry->addMail($group, $pro['email']);
             }
         }
 
-        // alias virtual
+        // Melix
         $res = XDB::query(
                 "SELECT alias
                    FROM virtual
@@ -89,11 +142,22 @@ class VCardIterator implements PlIterator
                 $user['user_id'],
                 $user['forlife'].'@'.$globals->mail->domain,
                 $user['forlife'].'@'.$globals->mail->domain2);
+        if ($res->numRows()) {
+            $entry->addMail(null, $res->fetchOneCell());
+        }
+
+        // Custom fields
+        if (count($user['gpxs_name'])) {
+            $entry->set('X-GROUPS', join(', ', $user['gpxs_name']));
+        }
+        if (count($user['binets'])) {
+            $entry->set('X-BINETS', join(', ', $user['binets']));
+        }
+        if (!empty($user['section'])) {
+            $entry->set('X-SECTION', $user['section']);
+        }
 
-        $user['virtualalias'] = $res->fetchOneCell();
-        $user['gpxs_vcardjoin'] = join(', ', array_map(array('VCard', 'text_encode'), $user['gpxs_name']));
-        $user['binets_vcardjoin'] = join(', ', array_map(array('VCard', 'text_encode'), $user['binets']));
-        // get photo
+        // Photo
         if ($this->photos) {
             $res = XDB::query(
                     "SELECT attach, attachmime
@@ -101,80 +165,12 @@ class VCardIterator implements PlIterator
                  INNER JOIN aliases AS a ON (a.id = p.uid AND a.type = 'a_vie')
                       WHERE a.alias = {?}", $login);
             if ($res->numRows()) {
-                $user['photo'] = $res->fetchOneAssoc();
-            }
-        }
-        return $user;
-    }
-}
-
-class VCard
-{
-    static private $windows = false;
-    private $iterator = null;
-
-    public function __construct($users, $photos = true, $freetext = null)
-    {
-        $this->iterator = new VCardIterator($photos, $freetext);
-        VCard::$windows  = (strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') !== false);
-        if (is_array($users)) {
-            foreach ($users as $user) {
-                $this->iterator->add_user($user);
+                list($data, $type) = $res->fetchOneRow();
+                $entry->setPhoto($data, strtoupper($type));
             }
-        } else {
-            $this->iterator->add_user($users);
-        }
-    }
-
-    public static function escape($text)
-    {
-        if (VCard::$windows) {
-            return str_replace(';', '\\\\;', $text);
-        } else {
-            return str_replace(array(';', ','), array('\\\\;', '\\\\,'), $text);
-        }
-    }
-
-    public static function format_adr($params, &$smarty)
-    {
-        // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
-        extract($params['adr']);
-        $adr = trim($adr1);
-        $adr = trim("$adr\n$adr2");
-        $adr = trim("$adr\n$adr3");
-        return VCard::text_encode(';;'
-                . (VCard::$windows ? VCard::escape($adr) : $adr) . ';'
-                . (VCard::$windows ? VCard::escape($city) : $city) . ';'
-                . (VCard::$windows ? VCard::escape($region) : $region) . ';'
-                . (VCard::$windows ? VCard::escape($postcode) : $postcode) . ';'
-                . (VCard::$windows ? VCard::escape($country) : $country), false);
-    }
-
-    public static function text_encode($text, $escape = true)
-    {
-        if (is_array($text)) {
-            return implode(',', array_map(array('VCard', 'text_encode'), $text));
-        }
-        if ($escape) {
-            $text = VCard::escape($text);
         }
-        if (VCard::$windows) {
-            $text = utf8_decode($text);
-        }
-        return str_replace(array("\r\n", "\n", "\r"), '\n', $text);
+        return $entry;
     }
-
-    public function do_page(&$page)
-    {
-        $page->changeTpl('core/vcard.tpl', NO_SKIN);
-        $page->register_modifier('vcard_enc',  array($this, 'text_encode'));
-        $page->register_function('format_adr', array($this, 'format_adr'));
-        $page->assign_by_ref('users', $this->iterator);
-
-        header("Pragma: ");
-        header("Cache-Control: ");
-        header("Content-type: text/x-vcard; charset=UTF-8");
-  }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
index 32a71b9..ce4b16c 100644 (file)
@@ -364,8 +364,9 @@ class CarnetModule extends PLModule
         $res = XDB::query('SELECT contact
                              FROM contacts
                             WHERE uid = {?}', S::v('uid'));
-        $vcard = new VCard($res->fetchColumn(), $photos == 'photos');
-        $vcard->do_page(&$page);
+        $vcard = new VCard($photos == 'photos');
+        $vcard->addUsers($res->fetchColumn());
+        $vcard->show();
     }
 }
 
index 68700f7..039f71a 100644 (file)
@@ -718,8 +718,9 @@ class ProfileModule extends PLModule
             $x = substr($x, 0, strlen($x) - 4);
         }
 
-        $vcard = new VCard($x);
-        $vcard->do_page($page);
+        $vcard = new VCard();
+        $vcard->addUser($x);
+        $vcard->show();
     }
 
     function handler_admin_trombino(&$page, $uid = null, $action = null) {
index b7a9eef..37c93d2 100644 (file)
@@ -479,8 +479,9 @@ class XnetGrpModule extends PLModule
         $res = XDB::query('SELECT  uid
                              FROM  groupex.membres
                             WHERE  asso_id = {?}', $globals->asso('id'));
-        $vcard = new VCard($res->fetchColumn(), $photos == 'photos', 'Membre du groupe ' . $globals->asso('nom'));
-        $vcard->do_page($page);
+        $vcard = new VCard($photos == 'photos', 'Membre du groupe ' . $globals->asso('nom'));
+        $vcard->addUsers($res->fetchColumn());
+        $vcard->show();
     }
 
     function handler_csv(&$page, $filename = null)
diff --git a/templates/core/vcard.tpl b/templates/core/vcard.tpl
deleted file mode 100644 (file)
index d0524a1..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-{**************************************************************************}
-{*                                                                        *}
-{*  Copyright (C) 2003-2008 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               *}
-{*                                                                        *}
-{**************************************************************************}
-{iterate from=$users item=vcard}
-BEGIN:VCARD
-VERSION:3.0
-{if $vcard.nom_usage}
-FN:{$vcard.prenom|vcard_enc} {$vcard.nom_usage|vcard_enc} ({$vcard.nom|vcard_enc})
-{else}
-FN:{$vcard.prenom|vcard_enc} {$vcard.nom|vcard_enc}
-{/if}
-N:{$vcard.nom|vcard_enc};{$vcard.prenom|vcard_enc};{$vcard.nom_usage|vcard_enc};;
-{if $vcard.nickname}
-NICKNAME:{$vcard.nickname|vcard_enc}
-{/if}
-EMAIL;TYPE=internet,pref:{$vcard.bestalias}@{#globals.mail.domain#}
-EMAIL;TYPE=internet:{$vcard.bestalias}@{#globals.mail.domain2#}
-{if $vcard.bestalias neq $vcard.forlife}
-EMAIL;TYPE=internet:{$vcard.forlife}@{#globals.mail.domain#}
-EMAIL;TYPE=internet:{$vcard.forlife}@{#globals.mail.domain2#}
-{/if}
-{if $vcard.virtualalias}
-EMAIL;TYPE=internet:{$vcard.virtualalias}
-{/if}
-{if $vcard.mobile}
-TEL;TYPE=cell:{$vcard.mobile|vcard_enc}
-{/if}
-{if $vcard.adr_pro}
-{if $vcard.adr_pro[0].entreprise}
-ORG:{$vcard.adr_pro[0].entreprise|vcard_enc}
-{/if}
-{if $vcard.adr_pro[0].poste}
-TITLE:{$vcard.adr_pro[0].poste|vcard_enc}
-{/if}
-{if $vcard.adr_pro[0].fonction}
-ROLE:{$vcard.adr_pro[0].fonction|vcard_enc}
-{/if}
-{if $vcard.adr_pro[0].tel}
-TEL;TYPE=work:{$vcard.adr_pro[0].tel|vcard_enc}
-{/if}
-{if $vcard.adr_pro[0].fax}
-FAX;TYPE=work:{$vcard.adr_pro[0].fax|vcard_enc}
-{/if}
-ADR;TYPE=work:{format_adr adr=$vcard.adr_pro[0]}
-{/if}
-{foreach item=adr from=$vcard.adr}
-ADR;TYPE=home{if $adr.courier},postal{/if}:{format_adr adr=$adr}
-{foreach item=tel from=$adr.tels}
-{if $tel.tel}
-{if $tel.tel_type neq 'Fax'}TEL{else}FAX{/if};TYPE=home:{$tel.tel}
-{/if}
-{/foreach}
-{/foreach}
-{if $vcard.web}
-URL:{$vcard.web}
-{/if}
-{if strlen(trim($vcard.freetext)) == 0}
-NOTE:(X{$vcard.promo})
-{else}
-NOTE:(X{$vcard.promo})\n{$vcard.freetext|miniwiki:'no_title':'text'|vcard_enc}
-{/if}
-{if $vcard.section}
-X-SECTION:{$vcard.section}
-{/if}
-{if $vcard.binets_vcardjoin}
-X-BINETS:{$vcard.binets_vcardjoin}
-{/if}
-{if $vcard.gpxs_vcardjoin}
-X-GROUPS:{$vcard.gpxs_vcardjoin}
-{/if}
-{if $vcard.photo}
-PHOTO;ENCODING=b;TYPE={$vcard.photo.attachmime}:{$vcard.photo.attach|base64_encode|vcard_enc}
-{/if}
-SORT-STRING:{$vcard.nom|vcard_enc}
-REV:{$vcard.date|date_format:"%Y%m%dT000000Z"}
-END:VCARD{"\n"}
-{/iterate}
-{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}