Fix 'reversed' field handling for AX newsletter.
[platal.git] / include / vcard.inc.php
index 749af3d..2fb2e58 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 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('xorg.misc.inc.php');
-require_once('user.func.inc.php');
-
-class VCard
+class VCard extends PlVCard
 {
-    var $users = array();
+    private $profile_list = array();
+    private $count     = 0;
+    private $freetext  = null;
+    private $photos    = true;
+    private $visibility;
 
-    function VCard($users, $freetext = null)
+    public function __construct($photos = true, $freetext = null)
     {
-        if (is_array($users)) {
-            foreach ($users as $user) {
-                $this->add_user($user, $freetext);
-            }
-        } else {
-            $this->add_user($users, $freetext);
-        }
+        PlVCard::$folding = false;
+        $this->visibility = Visibility::defaultForRead(Visibility::VIEW_PRIVATE);
+        $this->freetext = $freetext;
+        $this->photos   = $photos;
     }
 
-    function escape($text)
+    public function addProfile($profile)
     {
-        return preg_replace('/[,;:]/', '\\\\$0', $text);
+        $profile = Profile::get($profile, Profile::FETCH_ALL, $this->visibility);
+        if ($profile) {
+            $this->profile_list[] = $profile;
+            $this->count++;
+        }
     }
 
-    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 $this->text_encode(';;'
-                . $this->escape($adr) . ';'
-                . $this->escape($city) . ';'
-                . $this->escape($region) . ';'
-                . $this->escape($postcode) . ';'
-                . $this->escape($country), false);
+    public function addProfiles(array $profiles) {
+        foreach ($profiles as $profile) {
+            $this->addProfile($profile);
+        }
     }
 
-    function text_encode($text, $escape = true)
+    protected function fetch()
     {
-        if ($escape) {
-            $text = $this->escape($text);
-        }
-        return preg_replace("/(\r\n|\n|\r)/", '\n', $text);
+        return PlIteratorUtils::fromArray($this->profile_list);
     }
 
-    function add_user($x, $freetext)
+    protected function buildEntry($pf)
     {
         global $globals;
+        $pf = $pf['value'];
+
+        $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames(), null, null, $pf->nickname);
+
+        $user = $pf->owner();
 
-        $login = get_user_forlife($x);
-        $user  = get_user_details($login);
+        // 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);
+        }
 
-        if (strlen(trim($user['freetext']))) {
-            $user['freetext'] = html_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 (!is_null($freetext)) {
-            if (strlen(trim($user['freetext']))) {
-                $user['freetext'] = $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'] = $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}'",
-                S::v('uid'),
-                $user['forlife'].'@'.$globals->mail->domain,
-                $user['forlife'].'@'.$globals->mail->domain2);
-
-        $user['virtualalias'] = $res->fetchOneCell();
-
-        // get photo
-        $res = XDB::query(
-                "SELECT attach, attachmime
-                   FROM photo   AS p
-             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();
+        // 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);
+                }
+            }
         }
-        $this->users[] = $user;
-    }
 
-    function do_page(&$page)
-    {
-        $page->changeTpl('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->users);
-
-        header("Pragma: ");
-        header("Cache-Control: ");
-        header("Content-type: text/x-vcard; charset=iso-8859-15");
-        header("Content-Transfer-Encoding: 8bit");
+        // Melix
+        if (!is_null($user)) {
+            $alias = $user->emailAlias();
+            if (!is_null($alias) && $pf->alias_pub == 'public') {
+                $entry->addMail(null, $alias);
+            }
+        }
+
+        // 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));
+            }
+        }
+
+        $binets = $pf->getBinets();
+
+        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 (!empty($pf->section)) {
+            $entry->set('X-SECTION', $pf->section);
+        }
+
+        // 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 $entry;
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>