Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | require_once 'Contact/Vcard/Build.php'; | |
35 | ||
36 | /** | |
95cdcc0f | 37 | * vCard export class. |
6a488035 TO |
38 | */ |
39 | class CRM_Contact_Page_View_Vcard extends CRM_Contact_Page_View { | |
40 | ||
41 | /** | |
95cdcc0f | 42 | * Heart of the vCard data assignment process. |
6a488035 | 43 | * |
95cdcc0f | 44 | * The runner gets all the metadata for the contact and calls the writeVcard method to output the vCard |
45 | * to the user. | |
6a488035 | 46 | */ |
00be9182 | 47 | public function run() { |
6a488035 TO |
48 | $this->preProcess(); |
49 | ||
353ffa53 | 50 | $params = array(); |
6a488035 | 51 | $defaults = array(); |
353ffa53 | 52 | $ids = array(); |
6a488035 TO |
53 | |
54 | $params['id'] = $params['contact_id'] = $this->_contactId; | |
55 | $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, $ids); | |
56 | ||
57 | // now that we have the contact's data - let's build the vCard | |
58 | // TODO: non-US-ASCII support (requires changes to the Contact_Vcard_Build class) | |
8f785c9e | 59 | $vcardNames = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array('labelColumn' => 'vcard_name')); |
6a488035 TO |
60 | $vcard = new Contact_Vcard_Build('2.1'); |
61 | ||
62 | if ($defaults['contact_type'] == 'Individual') { | |
63 | $vcard->setName(CRM_Utils_Array::value('last_name', $defaults), | |
64 | CRM_Utils_Array::value('first_name', $defaults), | |
65 | CRM_Utils_Array::value('middle_name', $defaults), | |
66 | CRM_Utils_Array::value('prefix', $defaults), | |
67 | CRM_Utils_Array::value('suffix', $defaults) | |
68 | ); | |
ffc72e17 PJ |
69 | $organizationName = CRM_Utils_Array::value('organization_name', $defaults); |
70 | if ($organizationName !== NULL) { | |
71 | $vcard->addOrganization($organizationName); | |
72 | } | |
6a488035 TO |
73 | } |
74 | elseif ($defaults['contact_type'] == 'Organization') { | |
75 | $vcard->setName($defaults['organization_name'], '', '', '', ''); | |
76 | } | |
77 | elseif ($defaults['contact_type'] == 'Household') { | |
78 | $vcard->setName($defaults['household_name'], '', '', '', ''); | |
79 | } | |
80 | $vcard->setFormattedName($defaults['display_name']); | |
81 | $vcard->setSortString($defaults['sort_name']); | |
82 | ||
a7488080 | 83 | if (!empty($defaults['nick_name'])) { |
6a488035 TO |
84 | $vcard->addNickname($defaults['nick_name']); |
85 | } | |
86 | ||
a7488080 | 87 | if (!empty($defaults['job_title'])) { |
6a488035 TO |
88 | $vcard->setTitle($defaults['job_title']); |
89 | } | |
90 | ||
ed0ca248 | 91 | if (!empty($defaults['birth_date'])) { |
92 | $vcard->setBirthday(CRM_Utils_Array::value('birth_date', $defaults)); | |
6a488035 TO |
93 | } |
94 | ||
a7488080 | 95 | if (!empty($defaults['home_URL'])) { |
6a488035 TO |
96 | $vcard->setURL($defaults['home_URL']); |
97 | } | |
98 | ||
99 | // TODO: $vcard->setGeo($lat, $lon); | |
a7488080 | 100 | if (!empty($defaults['address'])) { |
6a488035 TO |
101 | $stateProvices = CRM_Core_PseudoConstant::stateProvince(); |
102 | $countries = CRM_Core_PseudoConstant::country(); | |
103 | foreach ($defaults['address'] as $location) { | |
104 | // we don't keep PO boxes in separate fields | |
105 | $pob = ''; | |
106 | $extend = CRM_Utils_Array::value('supplemental_address_1', $location); | |
a7488080 | 107 | if (!empty($location['supplemental_address_2'])) { |
6a488035 TO |
108 | $extend .= ', ' . $location['supplemental_address_2']; |
109 | } | |
207f62c6 AS |
110 | if (!empty($location['supplemental_address_3'])) { |
111 | $extend .= ', ' . $location['supplemental_address_3']; | |
112 | } | |
353ffa53 | 113 | $street = CRM_Utils_Array::value('street_address', $location); |
6a488035 | 114 | $locality = CRM_Utils_Array::value('city', $location); |
353ffa53 | 115 | $region = NULL; |
a7488080 | 116 | if (!empty($location['state_province_id'])) { |
6a488035 TO |
117 | $region = $stateProvices[CRM_Utils_Array::value('state_province_id', $location)]; |
118 | } | |
119 | $country = NULL; | |
a7488080 | 120 | if (!empty($location['country_id'])) { |
6a488035 TO |
121 | $country = $countries[CRM_Utils_Array::value('country_id', $location)]; |
122 | } | |
123 | ||
124 | $postcode = CRM_Utils_Array::value('postal_code', $location); | |
a7488080 | 125 | if (!empty($location['postal_code_suffix'])) { |
6a488035 TO |
126 | $postcode .= '-' . $location['postal_code_suffix']; |
127 | } | |
128 | ||
129 | $vcard->addAddress($pob, $extend, $street, $locality, $region, $postcode, $country); | |
130 | $vcardName = $vcardNames[$location['location_type_id']]; | |
131 | if ($vcardName) { | |
132 | $vcard->addParam('TYPE', $vcardName); | |
133 | } | |
a7488080 | 134 | if (!empty($location['is_primary'])) { |
6a488035 TO |
135 | $vcard->addParam('TYPE', 'PREF'); |
136 | } | |
137 | } | |
138 | } | |
a7488080 | 139 | if (!empty($defaults['phone'])) { |
6a488035 TO |
140 | foreach ($defaults['phone'] as $phone) { |
141 | $vcard->addTelephone($phone['phone']); | |
142 | $vcardName = $vcardNames[$phone['location_type_id']]; | |
143 | if ($vcardName) { | |
144 | $vcard->addParam('TYPE', $vcardName); | |
145 | } | |
146 | if ($phone['is_primary']) { | |
147 | $vcard->addParam('TYPE', 'PREF'); | |
148 | } | |
149 | } | |
150 | } | |
151 | ||
a7488080 | 152 | if (!empty($defaults['email'])) { |
6a488035 TO |
153 | foreach ($defaults['email'] as $email) { |
154 | $vcard->addEmail($email['email']); | |
155 | $vcardName = $vcardNames[$email['location_type_id']]; | |
156 | if ($vcardName) { | |
157 | $vcard->addParam('TYPE', $vcardName); | |
158 | } | |
159 | if ($email['is_primary']) { | |
160 | $vcard->addParam('TYPE', 'PREF'); | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
165 | // all that's left is sending the vCard to the browser | |
166 | $filename = CRM_Utils_String::munge($defaults['display_name']); | |
167 | $vcard->send($filename . '.vcf', 'attachment', 'utf-8'); | |
168 | CRM_Utils_System::civiExit(); | |
169 | } | |
96025800 | 170 | |
6a488035 | 171 | } |