Moving to GitHub.
[platal.git] / include / vcard.inc.php
index d00cf3a..2fb2e58 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -25,17 +25,19 @@ class VCard extends PlVCard
     private $count     = 0;
     private $freetext  = null;
     private $photos    = true;
+    private $visibility;
 
     public function __construct($photos = true, $freetext = null)
     {
         PlVCard::$folding = false;
+        $this->visibility = Visibility::defaultForRead(Visibility::VIEW_PRIVATE);
         $this->freetext = $freetext;
         $this->photos   = $photos;
     }
 
     public function addProfile($profile)
     {
-        $profile = Profile::get($profile);
+        $profile = Profile::get($profile, Profile::FETCH_ALL, $this->visibility);
         if ($profile) {
             $this->profile_list[] = $profile;
             $this->count++;
@@ -75,9 +77,10 @@ class VCard extends PlVCard
         // Emails
         if (!is_null($user)) {
             $entry->addMail(null, $user->bestalias, true);
-            $entry->addMail(null, $user->bestalias_alternate);
             if ($user->forlife != $user->bestalias) {
                 $entry->addMail(null, $user->forlife);
+            }
+            if ($user->forlife_alternate != $user->bestalias) {
                 $entry->addMail(null, $user->forlife_alternate);
             }
         }
@@ -90,8 +93,7 @@ class VCard extends PlVCard
                                 null, $adr->administrativeArea, null,
                                 $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
             } else {
-                // TODO : find a way to fetch only the "street" part of the address
-                $group = $entry->addHome($adr->text, null, null, $adr->postalCode,
+                $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'));
             }
@@ -106,23 +108,39 @@ class VCard extends PlVCard
         }
 
         // Pro
-        $adrs = $pf->iterAddresses(Profile::ADDRESS_PRO);
-        while ($adr = $adrs->next()) {
-            if (!$adr->postalCode || !$adr->locality || !$adr->country) {
-                $group = $entry->addWork(null, null, null, null,
-                                         $adr->text, null, null, null,
-                                         null, $adr->administrativeArea, null);
+        $jobs = $pf->getJobs();
+        foreach ($jobs as $job) {
+            $terms_array = array();
+            foreach ($job->terms as $term) {
+               $terms_array[] = $term->full_name;
+            }
+            $terms = implode(', ', $terms_array);
+            if ($job->address) {
+                if (!$job->address->postalCode || !$job->address->locality || !$job->address->country) {
+                    $group = $entry->addWork($job->company->name, null, $job->description, $terms,
+                                             $job->address->text, null, null, null,
+                                             null, $job->address->administrativeArea, null);
+                } else {
+                    $group = $entry->addWork($job->company->name, null, $job->description, $terms,
+                                             trim(Geocoder::getFirstLines($job->address->text, $job->address->postalCode, 4)),
+                                             null, null, $job->address->postalCode,
+                                             $job->address->locality, $job->address->administrativeArea, $job->address->country);
+                }
             } else {
-                // TODO : link address to company
-                $group = $entry->addWork(null, null, null, null,
-                                         $adr->text, null, null, $adr->postalCode,
-                                         $adr->locality, $adr->administrativeArea, $adr->country);
+                $group = $entry->addWork($job->company->name, null, $job->description, $terms,
+                                         null, null, null, null,
+                                         null, null, null);
             }
-            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) {
+            if ($job->user_email) {
+                $entry->addMail($group, $job->user_email);
+            }
+            foreach ($job->phones as $phone) {
+                if ($phone->type == Phone::TYPE_MOBILE) {
+                    $entry->addTel($group, $phone->display, false, true, true, false, true);
+                } else if ($phone->type == Phone::TYPE_FAX) {
                     $entry->addTel($group, $phone->display, true);
+                } else {
+                    $entry->addTel($group, $phone->display, false, true, true);
                 }
             }
         }
@@ -166,8 +184,9 @@ class VCard extends PlVCard
         if ($this->photos) {
             $res = XDB::query(
                     "SELECT  attach, attachmime
-                       FROM  profile_photos AS p
-                      WHERE  p.pid = {?}", $pf->id());
+                       FROM  profile_photos
+                      WHERE  pid = {?} AND pub IN ('public', {?})",
+                    $pf->id(), $this->visibility->level());
             if ($res->numRows()) {
                 list($data, $type) = $res->fetchOneRow();
                 $entry->setPhoto($data, strtoupper($type));
@@ -177,5 +196,5 @@ class VCard extends PlVCard
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>