<?php
/***************************************************************************
- * Copyright (C) 2003-2007 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('xorg.misc.inc.php');
-require_once('user.func.inc.php');
-
-class VCard
+class VCard extends PlVCard
{
- var $users = array();
- var $photos;
+ private $profile_list = array();
+ private $count = 0;
+ private $freetext = null;
+ private $photos = true;
- function VCard($users, $photos = true, $freetext = null)
+ public function __construct($photos = true, $freetext = null)
{
- $this->photos = $photos;
- if (is_array($users)) {
- foreach ($users as $user) {
- $this->add_user($user, $freetext);
- }
- } else {
- $this->add_user($users, $freetext);
- }
+ PlVCard::$folding = false;
+ $this->freetext = $freetext;
+ $this->photos = $photos;
}
- function escape($text)
+ public function addProfile($profile)
{
- return preg_replace('/[,;:]/', '\\\\$0', $text);
+ $profile = Profile::get($profile);
+ 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 (is_array($text)) {
- return implode(',', array_map(array($this, 'text_encode'), $text));
- }
- 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;
- $login = get_user_forlife($x);
- $user = get_user_details($login);
+ $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames, null, null, $pf->nickname);
+
+ $user = $pf->owner();
- if (strlen(trim($user['freetext']))) {
- $user['freetext'] = html_entity_decode($user['freetext']);
+ // Free text
+ $freetext = '(' . $pf->promo . ')';
+ if ($this->freetext) {
+ $freetext .= "\n" . $this->freetext;
}
- if (!is_null($freetext)) {
- if (strlen(trim($user['freetext']))) {
- $user['freetext'] = $freetext . "\n" . $user['freetext'];
- } else {
- $user['freetext'] = $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);
+ }
+ }
+
+ // 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);
}
}
- // 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();
- $user['gpxs_vcardjoin'] = join(',', array_map(array($this, 'text_encode'), $user['gpxs_name']));
- $user['binets_vcardjoin'] = join(',', array_map(array($this, 'text_encode'), $user['binets']));
- // get photo
+ // 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']);
+ }
+ */
+ }
+
+ // Melix
+ if (!is_null($user)) {
+ $alias = $user->emailAlias();
+ if (!is_null($alias)) {
+ $entry->addMail(null, $alias);
+ }
+ }
+
+ // 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));
+ }
+ }
+
+ $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(', ', $bid));
+ }
+ if (!empty($pf->section)) {
+ $sections = DirEnum::getOptions(DirEnum::SECTIONS);
+ $entry->set('X-SECTION', $sections[$pf->section]);
+ }
+
+ // 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);
+ "SELECT attach, attachmime
+ FROM photo AS p
+ WHERE p.uid = {?}", $pf->id());
if ($res->numRows()) {
- $user['photo'] = $res->fetchOneAssoc();
+ list($data, $type) = $res->fetchOneRow();
+ $entry->setPhoto($data, strtoupper($type));
}
}
- $this->users[] = $user;
- }
-
- 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->users);
-
- header("Pragma: ");
- header("Cache-Control: ");
- header("Content-type: text/x-vcard; charset=iso-8859-15");
- header("Content-Transfer-Encoding: 8bit");
+ return $entry;
}
}
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
?>