dolibarr  16.0.5
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/class/vcard.class.php';
31 
32 $contact = new Contact($db);
33 
34 
35 $id = GETPOST('id', 'int');
36 
37 // Security check
38 $result = restrictedArea($user, 'contact', $id, 'socpeople&societe');
39 
40 
41 $result = $contact->fetch($id);
42 if ($result <= 0) {
43  dol_print_error($contact->error);
44  exit;
45 }
46 
47 
48 $company = new Societe($db);
49 if ($contact->socid) {
50  $result = $company->fetch($contact->socid);
51 }
52 
53 // We create VCard
54 $v = new vCard();
55 $v->setProdId('Dolibarr '.DOL_VERSION);
56 
57 $v->setUid('DOLIBARR-CONTACTID-'.$contact->id);
58 $v->setName($contact->lastname, $contact->firstname, "", $contact->civility, "");
59 $v->setFormattedName($contact->getFullName($langs, 1));
60 
61 $v->setPhoneNumber($contact->phone_pro, "TYPE=WORK;VOICE");
62 //$v->setPhoneNumber($contact->phone_perso,"TYPE=HOME;VOICE");
63 $v->setPhoneNumber($contact->phone_mobile, "TYPE=CELL;VOICE");
64 $v->setPhoneNumber($contact->fax, "TYPE=WORK;FAX");
65 
66 $country = $contact->country_code ? $contact->country : '';
67 
68 $v->setAddress("", "", $contact->address, $contact->town, $contact->state, $contact->zip, $country, "TYPE=WORK;POSTAL");
69 $v->setLabel("", "", $contact->address, $contact->town, $contact->state, $contact->zip, $country, "TYPE=WORK");
70 
71 $v->setEmail($contact->email);
72 $v->setNote($contact->note);
73 $v->setTitle($contact->poste);
74 
75 // Data from linked company
76 if ($company->id) {
77  $v->setURL($company->url, "TYPE=WORK");
78  if (!$contact->phone_pro) {
79  $v->setPhoneNumber($company->phone, "TYPE=WORK;VOICE");
80  }
81  if (!$contact->fax) {
82  $v->setPhoneNumber($company->fax, "TYPE=WORK;FAX");
83  }
84  if (!$contact->zip) {
85  $v->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK;POSTAL");
86  }
87 
88  // when company e-mail is empty, use only contact e-mail
89  if (empty(trim($company->email))) {
90  // was set before, don't set twice
91  } elseif (empty(trim($contact->email))) {
92  // when contact e-mail is empty, use only company e-mail
93  $v->setEmail($company->email);
94  } elseif (strtolower(end(explode("@", $contact->email))) == strtolower(end(explode("@", $company->email)))) {
95  // when e-mail domain of contact and company are the same, use contact e-mail at first (and company e-mail at second)
96  $v->setEmail($contact->email);
97 
98  // support by Microsoft Outlook (2019 and possible earlier)
99  $v->setEmail($company->email, 'INTERNET');
100  } else {
101  // when e-mail of contact and company complete different use company e-mail at first (and contact e-mail at second)
102  $v->setEmail($company->email);
103 
104  // support by Microsoft Outlook (2019 and possible earlier)
105  $v->setEmail($contact->email, 'INTERNET');
106  }
107 
108  // Si contact lie a un tiers non de type "particulier"
109  if ($company->typent_code != 'TE_PRIVATE') {
110  $v->setOrg($company->name);
111  }
112 }
113 
114 // Personal informations
115 $v->setPhoneNumber($contact->phone_perso, "TYPE=HOME;VOICE");
116 if ($contact->birthday) {
117  $v->setBirthday($contact->birthday);
118 }
119 
120 $db->close();
121 
122 
123 // Renvoi la VCard au navigateur
124 
125 $output = $v->getVCard();
126 
127 $filename = trim(urldecode($v->getFileName())); // "Nom prenom.vcf"
128 $filenameurlencoded = dol_sanitizeFileName(urlencode($filename));
129 //$filename = dol_sanitizeFileName($filename);
130 
131 
132 header("Content-Disposition: attachment; filename=\"".$filename."\"");
133 header("Content-Length: ".dol_strlen($output));
134 header("Connection: close");
135 header("Content-Type: text/x-vcard; name=\"".$filename."\"");
136 
137 print $output;
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
restrictedArea
restrictedArea($user, $features, $objectid=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.
Definition: security.lib.php:234
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
vCard
Class to buld vCard files.
Definition: vcard.class.php:88
Contact
Class to manage contact/addresses.
Definition: contact.class.php:40
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747