<?php
/***************************************************************************
- * Copyright (C) 2003-2008 Polytechnique.org *
+ * Copyright (C) 2003-2010 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;
- 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 addProfile($profile)
{
- $forlife = get_user_forlife($user, '_silent_user_callback');
- if ($forlife) {
- $this->user_list[] = get_user_forlife($user);
+ $profile = Profile::get($profile);
+ if ($profile) {
+ $this->profile_list[] = $profile;
$this->count++;
}
}
- public function first()
- {
- return count($this->user_list) == $this->count - 1;
- }
-
- public function last()
- {
- return count($this->user_list) == 0;
+ public function addProfiles(array $profiles) {
+ foreach ($profiles as $profile) {
+ $this->addProfile($profile);
+ }
}
- public function total()
+ protected function fetch()
{
- return $this->count;
+ return PlIteratorUtils::fromArray($this->profile_list);
}
- public function next()
+ protected function buildEntry($pf)
{
- if (!$this->user_list) {
- return null;
- }
global $globals;
- $login = array_shift($this->user_list);
- $user = get_user_details($login);
- if (strlen(trim($user['freetext']))) {
- $user['freetext'] = pl_entity_decode($user['freetext']);
- }
+ $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames, null, null, $pf->nickname);
+
+ $user = $pf->owner();
+
+ // Free text
+ $freetext = '(' . $pf->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;
+ }
+ $entry->set('NOTE', $freetext);
+ // Mobile
+ if (!empty($pf->mobile)) {
+ $entry->addTel(null, $pf->mobile, false, true, true, false, true, true);
+ }
+
+ // 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);
+ $entry->addMail(null, $user->forlife_alternate);
}
}
- // 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
- 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();
+ // 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 (!empty($adr['fax_tel'])) {
+ $entry->addTel($group, $adr['fax_tel'], true, false, false, false, false, false);
}
}
- return $user;
- }
-}
-class VCard
-{
- static private $windows = false;
- private $iterator = null;
+ // 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);
+ }
+ /* TODO : fetch email for jobs, too
+ if (!empty($pro['email'])) {
+ $entry->addMail($group, $pro['email']);
+ }
+ */
+ }
- 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)) {
+ $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();
+ 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(', ', $bid));
}
- if ($escape) {
- $text = VCard::escape($text);
+ if (!empty($pf->section)) {
+ $sections = DirEnum::getOptions(DirEnum::SECTIONS);
+ $entry->set('X-SECTION', $sections[$pf->section]);
}
- if (VCard::$windows) {
- $text = utf8_decode($text);
+
+ // Photo
+ if ($this->photos) {
+ $res = XDB::query(
+ "SELECT attach, attachmime
+ FROM photo AS p
+ WHERE p.uid = {?}", $pf->id());
+ 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: