Fix 'reversed' field handling for AX newsletter.
[platal.git] / include / vcard.inc.php
index 0671343..2fb2e58 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-require_once('user.func.inc.php');
-
-class VCardIterator implements PlIterator
+class VCard extends PlVCard
 {
-    private $user_list = array();
+    private $profile_list = array();
     private $count     = 0;
     private $freetext  = null;
     private $photos    = true;
+    private $visibility;
 
-    public function __construct($photos, $freetext)
+    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 add_user($user)
+    public function addProfile($profile)
     {
-        $user = User::getSilent($user);
-        if ($user) {
-            $this->user_list[] = $user;
+        $profile = Profile::get($profile, Profile::FETCH_ALL, $this->visibility);
+        if ($profile) {
+            $this->profile_list[] = $profile;
             $this->count++;
         }
     }
 
-    public function first()
-    {
-        return count($this->user_list) == $this->count - 1;
+    public function addProfiles(array $profiles) {
+        foreach ($profiles as $profile) {
+            $this->addProfile($profile);
+        }
     }
 
-    public function last()
+    protected function fetch()
     {
-        return count($this->user_list) == 0;
+        return PlIteratorUtils::fromArray($this->profile_list);
     }
 
-    public function total()
+    protected function buildEntry($pf)
     {
-        return $this->count;
-    }
+        global $globals;
+        $pf = $pf['value'];
 
-    public function next()
-    {
-        if (!$this->user_list) {
-            return null;
+        $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames(), null, null, $pf->nickname);
+
+        $user = $pf->owner();
+
+        // Free text
+        $freetext = '(' . $pf->promo . ')';
+        if ($this->freetext) {
+            $freetext .= "\n" . $this->freetext;
+        }
+        $entry->set('NOTE', $freetext);
+        if ($pf->mobile) {
+            $entry->addTel(null, $pf->mobile, false, true, true, false, true, true);
         }
-        global $globals;
-        $login = array_shift($this->user_list);
-        $user  = get_user_details($login->login());
 
-        if (strlen(trim($user['freetext']))) {
-            $user['freetext'] = pl_entity_decode($user['freetext']);
+        // Emails
+        if (!is_null($user)) {
+            $entry->addMail(null, $user->bestalias, true);
+            if ($user->forlife != $user->bestalias) {
+                $entry->addMail(null, $user->forlife);
+            }
+            if ($user->forlife_alternate != $user->bestalias) {
+                $entry->addMail(null, $user->forlife_alternate);
+            }
         }
-        if ($this->freetext) {
-            if (strlen(trim($user['freetext']))) {
-                $user['freetext'] = $this->freetext . "\n" . $user['freetext'];
+
+        // Homes
+        $adrs = $pf->iterAddresses(Profile::ADDRESS_PERSO);
+        while ($adr = $adrs->next()) {
+            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 {
-                $user['freetext'] = $this->freetext;
+                $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'));
+            }
+            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);
+                }
             }
         }
 
-        // alias virtual
-        $res = XDB::query(
-                "SELECT alias
-                   FROM virtual
-             INNER JOIN virtual_redirect USING(vid)
-             INNER JOIN auth_user_quick  ON ( user_id = {?} AND emails_alias_pub = 'public' )
-                  WHERE ( redirect={?} OR redirect={?} )
-                        AND alias LIKE '%@{$globals->mail->alias_dom}'",
-                $user['user_id'],
-                $user['forlife'].'@'.$globals->mail->domain,
-                $user['forlife'].'@'.$globals->mail->domain2);
-
-        $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
-        if ($this->photos) {
-            $res = XDB::query(
-                    "SELECT  attach, attachmime
-                       FROM  photo AS p
-                      WHERE  u.user_id = {?}", $login->id());
-            if ($res->numRows()) {
-                $user['photo'] = $res->fetchOneAssoc();
+        // Pro
+        $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 {
+                $group = $entry->addWork($job->company->name, null, $job->description, $terms,
+                                         null, null, null, null,
+                                         null, null, null);
+            }
+            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);
+                }
             }
         }
-        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);
+        // Melix
+        if (!is_null($user)) {
+            $alias = $user->emailAlias();
+            if (!is_null($alias) && $pf->alias_pub == 'public') {
+                $entry->addMail(null, $alias);
             }
-        } 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);
+        // Custom fields
+        if (!is_null($user)) {
+            $groups = $user->groups(true, true);
+            if (count($groups)) {
+                $gn = DirEnum::getOptions(DirEnum::GROUPESX);
+                $gns = array();
+                foreach (array_keys($groups) as $gid) {
+                    $gns[$gid] = $gn[$gid];
+                }
+                $entry->set('X-GROUPS', join(', ', $gns));
+            }
         }
-    }
 
-    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);
-    }
+        $binets = $pf->getBinets();
 
-    public static function text_encode($text, $escape = true)
-    {
-        if (is_array($text)) {
-            return implode(',', array_map(array('VCard', 'text_encode'), $text));
+        if (count($binets)) {
+            $bn = DirEnum::getOptions(DirEnum::BINETS);
+            $bns = array();
+            foreach ($binets as $bid) {
+                $bns[$bid] = $bn[$bid];
+            }
+            $entry->set('X-BINETS', join(', ', $bns));
         }
-        if ($escape) {
-            $text = VCard::escape($text);
+        if (!empty($pf->section)) {
+            $entry->set('X-SECTION', $pf->section);
         }
-        if (VCard::$windows) {
-            $text = utf8_decode($text);
+
+        // Photo
+        if ($this->photos) {
+            $res = XDB::query(
+                    "SELECT  attach, attachmime
+                       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));
+            }
         }
-        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:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>