X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fvcard.inc.php;h=2fb2e58a48198df2b39ec07b0f422c2d673fea9c;hb=2981f248b2db1adfdfbf4c294625bc382efe53fe;hp=ccb85262499b9caabbd314734c23c44afd3180e3;hpb=3ce06e37f07bbda02cb3ee30577333801a6d1b9c;p=platal.git diff --git a/include/vcard.inc.php b/include/vcard.inc.php index ccb8526..2fb2e58 100644 --- a/include/vcard.inc.php +++ b/include/vcard.inc.php @@ -1,6 +1,6 @@ visibility = Visibility::defaultForRead(Visibility::VIEW_PRIVATE); $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, 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); - 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 - 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); + } } } - 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: ?>