3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 require_once 'Contact/Vcard/Build.php';
23 class CRM_Contact_Page_View_Vcard
extends CRM_Contact_Page_View
{
26 * Heart of the vCard data assignment process.
28 * The runner gets all the metadata for the contact and calls the writeVcard method to output the vCard
31 public function run() {
38 $params['id'] = $params['contact_id'] = $this->_contactId
;
39 $contact = CRM_Contact_BAO_Contact
::retrieve($params, $defaults, $ids);
41 // now that we have the contact's data - let's build the vCard
42 // TODO: non-US-ASCII support (requires changes to the Contact_Vcard_Build class)
43 $vcardNames = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', ['labelColumn' => 'vcard_name']);
44 $vcard = new Contact_Vcard_Build('2.1');
46 if ($defaults['contact_type'] == 'Individual') {
47 $vcard->setName(CRM_Utils_Array
::value('last_name', $defaults),
48 CRM_Utils_Array
::value('first_name', $defaults),
49 CRM_Utils_Array
::value('middle_name', $defaults),
50 CRM_Utils_Array
::value('prefix', $defaults),
51 CRM_Utils_Array
::value('suffix', $defaults)
53 $organizationName = $defaults['organization_name'] ??
NULL;
54 if ($organizationName !== NULL) {
55 $vcard->addOrganization($organizationName);
58 elseif ($defaults['contact_type'] == 'Organization') {
59 $vcard->setName($defaults['organization_name'], '', '', '', '');
61 elseif ($defaults['contact_type'] == 'Household') {
62 $vcard->setName($defaults['household_name'], '', '', '', '');
64 $vcard->setFormattedName($defaults['display_name']);
65 $vcard->setSortString($defaults['sort_name']);
67 if (!empty($defaults['nick_name'])) {
68 $vcard->addNickname($defaults['nick_name']);
71 if (!empty($defaults['job_title'])) {
72 $vcard->setTitle($defaults['job_title']);
75 if (!empty($defaults['birth_date'])) {
76 $vcard->setBirthday($defaults['birth_date']);
79 if (!empty($defaults['home_URL'])) {
80 $vcard->setURL($defaults['home_URL']);
83 // TODO: $vcard->setGeo($lat, $lon);
84 if (!empty($defaults['address'])) {
85 $stateProvices = CRM_Core_PseudoConstant
::stateProvince();
86 $countries = CRM_Core_PseudoConstant
::country();
87 foreach ($defaults['address'] as $location) {
88 // we don't keep PO boxes in separate fields
90 $extend = $location['supplemental_address_1'] ??
NULL;
91 if (!empty($location['supplemental_address_2'])) {
92 $extend .= ', ' . $location['supplemental_address_2'];
94 if (!empty($location['supplemental_address_3'])) {
95 $extend .= ', ' . $location['supplemental_address_3'];
97 $street = $location['street_address'] ??
NULL;
98 $locality = $location['city'] ??
NULL;
100 if (!empty($location['state_province_id'])) {
101 $region = $stateProvices[$location['state_province_id']];
104 if (!empty($location['country_id'])) {
105 $country = $countries[$location['country_id']];
108 $postcode = $location['postal_code'] ??
NULL;
109 if (!empty($location['postal_code_suffix'])) {
110 $postcode .= '-' . $location['postal_code_suffix'];
113 $vcard->addAddress($pob, $extend, $street, $locality, $region, $postcode, $country);
114 $vcardName = $vcardNames[$location['location_type_id']];
116 $vcard->addParam('TYPE', $vcardName);
118 if (!empty($location['is_primary'])) {
119 $vcard->addParam('TYPE', 'PREF');
123 if (!empty($defaults['phone'])) {
124 foreach ($defaults['phone'] as $phone) {
125 $vcard->addTelephone($phone['phone']);
126 $vcardName = $vcardNames[$phone['location_type_id']];
128 $vcard->addParam('TYPE', $vcardName);
130 if (!empty($phone['is_primary'])) {
131 $vcard->addParam('TYPE', 'PREF');
136 if (!empty($defaults['email'])) {
137 foreach ($defaults['email'] as $email) {
138 $vcard->addEmail($email['email']);
139 $vcardName = $vcardNames[$email['location_type_id']];
141 $vcard->addParam('TYPE', $vcardName);
143 if (!empty($email['is_primary'])) {
144 $vcard->addParam('TYPE', 'PREF');
149 // all that's left is sending the vCard to the browser
150 $filename = CRM_Utils_String
::munge($defaults['display_name']);
151 $vcard->send($filename . '.vcf', 'attachment', 'utf-8');
152 CRM_Utils_System
::civiExit();