dolibarr 21.0.0-beta
vcard.php
Go to the documentation of this file.
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) 2024 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29// Load Dolibarr environment
30require '../main.inc.php';
31require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
32require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/vcard.class.php';
34
43$contact = new Contact($db);
44
45
46$id = GETPOSTINT('id');
47
48// Security check
49$result = restrictedArea($user, 'contact', $id, 'socpeople&societe');
50
51
52$result = $contact->fetch($id);
53if ($result <= 0) {
54 dol_print_error($db, $contact->error);
55 exit;
56}
57
58
59$company = new Societe($db);
60if ($contact->socid) {
61 $result = $company->fetch($contact->socid);
62}
63
64// We create VCard
65$v = new vCard();
66$v->setProdId('Dolibarr '.DOL_VERSION);
67
68$v->setUid('DOLIBARR-CONTACTID-'.$contact->id);
69$v->setName($contact->lastname, $contact->firstname, "", $contact->civility, "");
70$v->setFormattedName($contact->getFullName($langs, 1));
71
72$v->setPhoneNumber($contact->phone_pro, "TYPE=WORK;VOICE");
73//$v->setPhoneNumber($contact->phone_perso,"TYPE=HOME;VOICE");
74$v->setPhoneNumber($contact->phone_mobile, "TYPE=CELL;VOICE");
75$v->setPhoneNumber($contact->fax, "TYPE=WORK;FAX");
76
77$country = $contact->country_code ? $contact->country : '';
78
79$v->setAddress("", "", $contact->address, $contact->town, $contact->state, $contact->zip, $country, "TYPE=WORK;POSTAL");
80// @phan-suppress-next-line PhanDeprecatedFunction setLabel applies the old method, setAddress is the new method.
81$v->setLabel("", "", $contact->address, $contact->town, $contact->state, $contact->zip, $country, "TYPE=WORK");
82
83$v->setEmail($contact->email);
84$v->setNote($contact->note);
85$v->setTitle($contact->poste);
86
87// Data from linked company
88if ($company->id) {
89 $v->setURL($company->url, "TYPE=WORK");
90 if (!$contact->phone_pro) {
91 $v->setPhoneNumber($company->phone, "TYPE=WORK;VOICE");
92 }
93 if (!$contact->fax) {
94 $v->setPhoneNumber($company->fax, "TYPE=WORK;FAX");
95 }
96 if (!$contact->zip) {
97 $v->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK;POSTAL");
98 }
99
100 // when company e-mail is empty, use only contact e-mail
101 if (empty(trim($company->email))) {
102 // was set before, don't set twice
103 } elseif (empty(trim($contact->email))) {
104 // when contact e-mail is empty, use only company e-mail
105 $v->setEmail($company->email);
106 } else {
107 $tmpcontact = explode("@", trim($contact->email));
108 $tmpcompany = explode("@", trim($company->email));
109
110 if (strtolower(end($tmpcontact)) == strtolower(end($tmpcompany))) {
111 // when e-mail domain of contact and company are the same, use contact e-mail at first (and company e-mail at second)
112 $v->setEmail($contact->email);
113
114 // support by Microsoft Outlook (2019 and possible earlier)
115 $v->setEmail($company->email, 'INTERNET');
116 } else {
117 // when e-mail of contact and company complete different use company e-mail at first (and contact e-mail at second)
118 $v->setEmail($company->email);
119
120 // support by Microsoft Outlook (2019 and possible earlier)
121 $v->setEmail($contact->email, 'INTERNET');
122 }
123 }
124
125 // Si contact lie a un tiers non de type "particulier"
126 if ($company->typent_code != 'TE_PRIVATE') {
127 $v->setOrg($company->name);
128 }
129}
130
131// Personal information
132$v->setPhoneNumber($contact->phone_perso, "TYPE=HOME;VOICE");
133if ($contact->birthday) {
134 $v->setBirthday($contact->birthday);
135}
136
137$db->close();
138
139
140// Renvoi la VCard au navigateur
141
142$output = $v->getVCard();
143
144$filename = trim(urldecode($v->getFileName())); // "Nom prenom.vcf"
145$filenameurlencoded = dol_sanitizeFileName(urlencode($filename));
146//$filename = dol_sanitizeFileName($filename);
147
148
149header("Content-Disposition: attachment; filename=\"".$filename."\"");
150header("Content-Length: ".dol_strlen($output));
151header("Connection: close");
152header("Content-Type: text/x-vcard; name=\"".$filename."\"");
153
154print $output;
$id
Definition account.php:48
Class to manage contact/addresses.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to build vCard files.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.