1<?php
2/* Copyright (C) 2004      Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2010 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012 Regis Houssin        <regis.houssin@inodbox.com>
5 * Copyright (C) 2020		Tobias Sekan		<tobias.sekan@startmail.com>
6 * Copyright (C) 2020		Frédéric France		<frederic.france@netlogic.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22/**
23 *	    \file       htdocs/adherent/vcard.php
24 *      \ingroup    societe
25 *		\brief      Onglet vcard d'un adherent
26 */
27
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
30require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/vcard.class.php';
32
33$adherent = new adherent($db);
34
35
36$id = GETPOST('id', 'int');
37
38// Security check
39$result = restrictedArea($user, 'adherent', $id, '', '', 'socid', 'rowid', $objcanvas);
40
41
42$result = $adherent->fetch($id);
43if ($result <= 0) {
44	dol_print_error($adherent->error);
45	exit;
46}
47
48$physicalperson = 1;
49
50$company = new Societe($db);
51if ($adherent->socid) {
52	$result = $company->fetch($adherent->socid);
53}
54
55// We create VCard
56$v = new vCard();
57$v->setProdId('Dolibarr '.DOL_VERSION);
58
59$v->setUid('DOLIBARR-ADHERENTID-'.$adherent->id);
60$v->setName($adherent->lastname, $adherent->firstname, "", $adherent->civility, "");
61$v->setFormattedName($adherent->getFullName($langs, 1));
62
63$v->setPhoneNumber($adherent->phone_pro, "TYPE=WORK;VOICE");
64//$v->setPhoneNumber($adherent->phone_perso,"TYPE=HOME;VOICE");
65$v->setPhoneNumber($adherent->phone_mobile, "TYPE=CELL;VOICE");
66$v->setPhoneNumber($adherent->fax, "TYPE=WORK;FAX");
67
68$country = $adherent->country_code ? $adherent->country : '';
69
70$v->setAddress("", "", $adherent->address, $adherent->town, $adherent->state, $adherent->zip, $country, "TYPE=WORK;POSTAL");
71$v->setLabel("", "", $adherent->address, $adherent->town, $adherent->state, $adherent->zip, $country, "TYPE=WORK");
72
73$v->setEmail($adherent->email);
74$v->setNote($adherent->note_public);
75$v->setTitle($adherent->poste);
76
77// Data from linked company
78if ($company->id) {
79	$v->setURL($company->url, "TYPE=WORK");
80	if (!$adherent->phone_pro) {
81		$v->setPhoneNumber($company->phone, "TYPE=WORK;VOICE");
82	}
83	if (!$adherent->fax) {
84		$v->setPhoneNumber($company->fax, "TYPE=WORK;FAX");
85	}
86	if (!$adherent->zip) {
87		$v->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK;POSTAL");
88	}
89	// when company e-mail is empty, use only adherent e-mail
90	if (empty(trim($company->email))) {
91		// was set before, don't set twice
92	} elseif (empty(trim($adherent->email))) {
93		// when adherent e-mail is empty, use only company e-mail
94		$v->setEmail($company->email);
95	} elseif (strtolower(end(explode("@", $adherent->email))) == strtolower(end(explode("@", $company->email)))) {
96		// when e-mail domain of adherent and company are the same, use adherent e-mail at first (and company e-mail at second)
97		$v->setEmail($adherent->email);
98
99		// support by Microsoft Outlook (2019 and possible earlier)
100		$v->setEmail($company->email, 'INTERNET');
101	} else {
102		// when e-mail of adherent and company complete different use company e-mail at first (and adherent e-mail at second)
103		$v->setEmail($company->email);
104
105		// support by Microsoft Outlook (2019 and possible earlier)
106		$v->setEmail($adherent->email, 'INTERNET');
107	}
108
109	// Si adherent lie a un tiers non de type "particulier"
110	if ($company->typent_code != 'TE_PRIVATE') {
111		$v->setOrg($company->name);
112	}
113}
114
115// Personal informations
116$v->setPhoneNumber($adherent->phone_perso, "TYPE=HOME;VOICE");
117if ($adherent->birth) {
118	$v->setBirthday($adherent->birth);
119}
120
121$db->close();
122
123
124// Renvoi la VCard au navigateur
125
126$output = $v->getVCard();
127
128$filename = trim(urldecode($v->getFileName())); // "Nom prenom.vcf"
129$filenameurlencoded = dol_sanitizeFileName(urlencode($filename));
130//$filename = dol_sanitizeFileName($filename);
131
132
133header("Content-Disposition: attachment; filename=\"".$filename."\"");
134header("Content-Length: ".dol_strlen($output));
135header("Connection: close");
136header("Content-Type: text/x-vcard; name=\"".$filename."\"");
137
138print $output;
139