Merge remote branch 'origin/xorg/maint' into xorg/master
[platal.git] / include / vcard.inc.php
index 2a1402a..758714e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -56,8 +56,9 @@ class VCard extends PlVCard
     protected function buildEntry($pf)
     {
         global $globals;
+        $pf = $pf['value'];
 
-        $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames, null, null, $pf->nickname);
+        $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames(), null, null, $pf->nickname);
 
         $user = $pf->owner();
 
@@ -67,8 +68,7 @@ class VCard extends PlVCard
             $freetext .= "\n" . $this->freetext;
         }
         $entry->set('NOTE', $freetext);
-        // Mobile
-        if (!empty($pf->mobile)) {
+        if ($pf->mobile) {
             $entry->addTel(null, $pf->mobile, false, true, true, false, true, true);
         }
 
@@ -85,49 +85,58 @@ class VCard extends PlVCard
         // Homes
         $adrs = $pf->iterAddresses(Profile::ADDRESS_PERSO);
         while ($adr = $adrs->next()) {
-            // TODO : find a way to fetch only the "street" part of the address
-            $group = $entry->addHome($adr['text'], null, null, $adr['postalCode'],
-                            $adr['locality'], $adr['administrativeArea'], $adr['country'],
-                            $adr['current'], $adr['mail'], $adr['mail']);
-            if (!empty($adr['fixed_tel'])) {
-                $entry->addTel($group, $adr['fixed_tel'], false, true, true, false, false, $adr['current'] && empty($pf->mobile));
+            if (!$adr->postalCode || !$adr->locality || !$adr->country) {
+                $group = $entry->addHome($adr->text, null, null, null,
+                                null, $adr->administrativeArea, null,
+                                $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
+            } else {
+                $group = $entry->addHome(trim(Geocoder::getFirstLines($adr->text, $adr->postalCode, 4)), null, null, $adr->postalCode,
+                                $adr->locality, $adr->administrativeArea, $adr->country,
+                                $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
             }
-            if (!empty($adr['fax_tel'])) {
-                $entry->addTel($group, $adr['fax_tel'], true, false, false, false, false, false);
+            foreach ($adr->phones() as $phone) {
+                if ($phone->link_type == Phone::TYPE_FIXED) {
+                    $entry->addTel($group, $phone->display, false, true, true, false, false,
+                                   $adr->hasFlag('current') && empty($pf->mobile));
+                } else if ($phone->link_type == Phone::TYPE_FAX) {
+                    $entry->addTel($group, $phone->display, true, false, false, false, false, false);
+                }
             }
         }
 
         // Pro
         $adrs = $pf->iterAddresses(Profile::ADDRESS_PRO);
         while ($adr = $adrs->next()) {
-            // TODO : link address to company
-            $group = $entry->addWork(null, null, null, null,
-                            $adr['text'], null, null, $adr['postalCode'],
-                            $adr['locality'], $adr['administrativeArea'], $adr['country']);
-            if (!empty($adr['fixed_tel'])) {
-                $entry->addTel($group, $adr['fixed_tel']);
-            }
-            if (!empty($adr['fax_tel'])) {
-                $entry->addTel($group, $adr['display_tel'], true);
+            if (!$adr->postalCode || !$adr->locality || !$adr->country) {
+                $group = $entry->addWork(null, null, null, null,
+                                         $adr->text, null, null, null,
+                                         null, $adr->administrativeArea, null);
+            } else {
+                // TODO : link address to company
+                $group = $entry->addWork(null, null, null, null,
+                                         trim(Geocoder::getFirstLines($adr->text, $adr->postalCode, 4)), null, null, $adr->postalCode,
+                                         $adr->locality, $adr->administrativeArea, $adr->country);
             }
-            /* TODO : fetch email for jobs, too
-                if (!empty($pro['email'])) {
-                    $entry->addMail($group, $pro['email']);
+            foreach ($adr->phones() as $phone) {
+                if ($phone->link_type == Phone::TYPE_FIXED) {
+                    $entry->addTel($group, $phone->display);
+                } else if ($phone->link_type == Phone::TYPE_FAX) {
+                    $entry->addTel($group, $phone->display, true);
                 }
-             */
+            }
         }
 
         // Melix
         if (!is_null($user)) {
             $alias = $user->emailAlias();
-            if (!is_null($alias)) {
+            if (!is_null($alias) && $pf->alias_pub == 'public') {
                 $entry->addMail(null, $alias);
             }
         }
 
         // Custom fields
         if (!is_null($user)) {
-            $groups = $user->groups();
+            $groups = $user->groups(true, true);
             if (count($groups)) {
                 $gn = DirEnum::getOptions(DirEnum::GROUPESX);
                 $gns = array();
@@ -146,19 +155,18 @@ class VCard extends PlVCard
             foreach ($binets as $bid) {
                 $bns[$bid] = $bn[$bid];
             }
-            $entry->set('X-BINETS', join(', ', $bid));
+            $entry->set('X-BINETS', join(', ', $bns));
         }
         if (!empty($pf->section)) {
-            $sections = DirEnum::getOptions(DirEnum::SECTIONS);
-            $entry->set('X-SECTION', $sections[$pf->section]);
+            $entry->set('X-SECTION', $pf->section);
         }
 
         // Photo
         if ($this->photos) {
             $res = XDB::query(
                     "SELECT  attach, attachmime
-                       FROM  photo AS p
-                      WHERE  p.uid = {?}", $pf->id());
+                       FROM  profile_photos AS p
+                      WHERE  p.pid = {?}", $pf->id());
             if ($res->numRows()) {
                 list($data, $type) = $res->fetchOneRow();
                 $entry->setPhoto($data, strtoupper($type));