Merge pull request #20196 from eileenmcnaughton/import
[civicrm-core.git] / CRM / Contact / Page / View / Vcard.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 require_once 'Contact/Vcard/Build.php';
19
20 /**
21 * vCard export class.
22 */
23 class CRM_Contact_Page_View_Vcard extends CRM_Contact_Page_View {
24
25 /**
26 * Heart of the vCard data assignment process.
27 *
28 * The runner gets all the metadata for the contact and calls the writeVcard method to output the vCard
29 * to the user.
30 */
31 public function run() {
32 $this->preProcess();
33
34 $params = [];
35 $defaults = [];
36 $ids = [];
37
38 $params['id'] = $params['contact_id'] = $this->_contactId;
39 $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, $ids);
40
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');
45
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)
52 );
53 $organizationName = $defaults['organization_name'] ?? NULL;
54 if ($organizationName !== NULL) {
55 $vcard->addOrganization($organizationName);
56 }
57 }
58 elseif ($defaults['contact_type'] == 'Organization') {
59 $vcard->setName($defaults['organization_name'], '', '', '', '');
60 }
61 elseif ($defaults['contact_type'] == 'Household') {
62 $vcard->setName($defaults['household_name'], '', '', '', '');
63 }
64 $vcard->setFormattedName($defaults['display_name']);
65 $vcard->setSortString($defaults['sort_name']);
66
67 if (!empty($defaults['nick_name'])) {
68 $vcard->addNickname($defaults['nick_name']);
69 }
70
71 if (!empty($defaults['job_title'])) {
72 $vcard->setTitle($defaults['job_title']);
73 }
74
75 if (!empty($defaults['birth_date'])) {
76 $vcard->setBirthday($defaults['birth_date']);
77 }
78
79 if (!empty($defaults['home_URL'])) {
80 $vcard->setURL($defaults['home_URL']);
81 }
82
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
89 $pob = '';
90 $extend = $location['supplemental_address_1'] ?? NULL;
91 if (!empty($location['supplemental_address_2'])) {
92 $extend .= ', ' . $location['supplemental_address_2'];
93 }
94 if (!empty($location['supplemental_address_3'])) {
95 $extend .= ', ' . $location['supplemental_address_3'];
96 }
97 $street = $location['street_address'] ?? NULL;
98 $locality = $location['city'] ?? NULL;
99 $region = NULL;
100 if (!empty($location['state_province_id'])) {
101 $region = $stateProvices[$location['state_province_id']];
102 }
103 $country = NULL;
104 if (!empty($location['country_id'])) {
105 $country = $countries[$location['country_id']];
106 }
107
108 $postcode = $location['postal_code'] ?? NULL;
109 if (!empty($location['postal_code_suffix'])) {
110 $postcode .= '-' . $location['postal_code_suffix'];
111 }
112
113 $vcard->addAddress($pob, $extend, $street, $locality, $region, $postcode, $country);
114 $vcardName = $vcardNames[$location['location_type_id']];
115 if ($vcardName) {
116 $vcard->addParam('TYPE', $vcardName);
117 }
118 if (!empty($location['is_primary'])) {
119 $vcard->addParam('TYPE', 'PREF');
120 }
121 }
122 }
123 if (!empty($defaults['phone'])) {
124 foreach ($defaults['phone'] as $phone) {
125 $vcard->addTelephone($phone['phone']);
126 $vcardName = $vcardNames[$phone['location_type_id']];
127 if ($vcardName) {
128 $vcard->addParam('TYPE', $vcardName);
129 }
130 if ($phone['is_primary']) {
131 $vcard->addParam('TYPE', 'PREF');
132 }
133 }
134 }
135
136 if (!empty($defaults['email'])) {
137 foreach ($defaults['email'] as $email) {
138 $vcard->addEmail($email['email']);
139 $vcardName = $vcardNames[$email['location_type_id']];
140 if ($vcardName) {
141 $vcard->addParam('TYPE', $vcardName);
142 }
143 if ($email['is_primary']) {
144 $vcard->addParam('TYPE', 'PREF');
145 }
146 }
147 }
148
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();
153 }
154
155 }