Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | require_once 'Contact/Vcard/Build.php'; | |
19 | ||
20 | /** | |
95cdcc0f | 21 | * vCard export class. |
6a488035 TO |
22 | */ |
23 | class CRM_Contact_Page_View_Vcard extends CRM_Contact_Page_View { | |
24 | ||
25 | /** | |
95cdcc0f | 26 | * Heart of the vCard data assignment process. |
6a488035 | 27 | * |
95cdcc0f | 28 | * The runner gets all the metadata for the contact and calls the writeVcard method to output the vCard |
29 | * to the user. | |
6a488035 | 30 | */ |
00be9182 | 31 | public function run() { |
6a488035 TO |
32 | $this->preProcess(); |
33 | ||
be2fb01f CW |
34 | $params = []; |
35 | $defaults = []; | |
36 | $ids = []; | |
6a488035 TO |
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) | |
be2fb01f | 43 | $vcardNames = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', ['labelColumn' => 'vcard_name']); |
6a488035 TO |
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 | ); | |
9c1bc317 | 53 | $organizationName = $defaults['organization_name'] ?? NULL; |
ffc72e17 PJ |
54 | if ($organizationName !== NULL) { |
55 | $vcard->addOrganization($organizationName); | |
56 | } | |
6a488035 TO |
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 | ||
a7488080 | 67 | if (!empty($defaults['nick_name'])) { |
6a488035 TO |
68 | $vcard->addNickname($defaults['nick_name']); |
69 | } | |
70 | ||
a7488080 | 71 | if (!empty($defaults['job_title'])) { |
6a488035 TO |
72 | $vcard->setTitle($defaults['job_title']); |
73 | } | |
74 | ||
ed0ca248 | 75 | if (!empty($defaults['birth_date'])) { |
2cfe2cf4 | 76 | $vcard->setBirthday($defaults['birth_date']); |
6a488035 TO |
77 | } |
78 | ||
a7488080 | 79 | if (!empty($defaults['home_URL'])) { |
6a488035 TO |
80 | $vcard->setURL($defaults['home_URL']); |
81 | } | |
82 | ||
83 | // TODO: $vcard->setGeo($lat, $lon); | |
a7488080 | 84 | if (!empty($defaults['address'])) { |
6a488035 TO |
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 = ''; | |
9c1bc317 | 90 | $extend = $location['supplemental_address_1'] ?? NULL; |
a7488080 | 91 | if (!empty($location['supplemental_address_2'])) { |
6a488035 TO |
92 | $extend .= ', ' . $location['supplemental_address_2']; |
93 | } | |
207f62c6 AS |
94 | if (!empty($location['supplemental_address_3'])) { |
95 | $extend .= ', ' . $location['supplemental_address_3']; | |
96 | } | |
9c1bc317 CW |
97 | $street = $location['street_address'] ?? NULL; |
98 | $locality = $location['city'] ?? NULL; | |
353ffa53 | 99 | $region = NULL; |
a7488080 | 100 | if (!empty($location['state_province_id'])) { |
2cfe2cf4 | 101 | $region = $stateProvices[$location['state_province_id']]; |
6a488035 TO |
102 | } |
103 | $country = NULL; | |
a7488080 | 104 | if (!empty($location['country_id'])) { |
2cfe2cf4 | 105 | $country = $countries[$location['country_id']]; |
6a488035 TO |
106 | } |
107 | ||
9c1bc317 | 108 | $postcode = $location['postal_code'] ?? NULL; |
a7488080 | 109 | if (!empty($location['postal_code_suffix'])) { |
6a488035 TO |
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 | } | |
a7488080 | 118 | if (!empty($location['is_primary'])) { |
6a488035 TO |
119 | $vcard->addParam('TYPE', 'PREF'); |
120 | } | |
121 | } | |
122 | } | |
a7488080 | 123 | if (!empty($defaults['phone'])) { |
6a488035 TO |
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 | } | |
d94c6483 | 130 | if (!empty($phone['is_primary'])) { |
6a488035 TO |
131 | $vcard->addParam('TYPE', 'PREF'); |
132 | } | |
133 | } | |
134 | } | |
135 | ||
a7488080 | 136 | if (!empty($defaults['email'])) { |
6a488035 TO |
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 | } | |
d94c6483 | 143 | if (!empty($email['is_primary'])) { |
6a488035 TO |
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 | } | |
96025800 | 154 | |
6a488035 | 155 | } |