Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-11-24-15-59-25
[civicrm-core.git] / CRM / Contact / Page / View / Vcard.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36require_once 'Contact/Vcard/Build.php';
37
38/**
39 * vCard export class
40 *
41 */
42class CRM_Contact_Page_View_Vcard extends CRM_Contact_Page_View {
43
44 /**
45 * Heart of the vCard data assignment process. The runner gets all the meta
46 * data for the contact and calls the writeVcard method to output the vCard
47 * to the user.
48 *
49 * @return void
50 */
51 function run() {
52 $this->preProcess();
53
54 $params = array();
55 $defaults = array();
56 $ids = array();
57
58 $params['id'] = $params['contact_id'] = $this->_contactId;
59 $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, $ids);
60
61 // now that we have the contact's data - let's build the vCard
62 // TODO: non-US-ASCII support (requires changes to the Contact_Vcard_Build class)
8f785c9e 63 $vcardNames = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array('labelColumn' => 'vcard_name'));
6a488035
TO
64 $vcard = new Contact_Vcard_Build('2.1');
65
66 if ($defaults['contact_type'] == 'Individual') {
67 $vcard->setName(CRM_Utils_Array::value('last_name', $defaults),
68 CRM_Utils_Array::value('first_name', $defaults),
69 CRM_Utils_Array::value('middle_name', $defaults),
70 CRM_Utils_Array::value('prefix', $defaults),
71 CRM_Utils_Array::value('suffix', $defaults)
72 );
ffc72e17
PJ
73 $organizationName = CRM_Utils_Array::value('organization_name', $defaults);
74 if ($organizationName !== NULL) {
75 $vcard->addOrganization($organizationName);
76 }
6a488035
TO
77 }
78 elseif ($defaults['contact_type'] == 'Organization') {
79 $vcard->setName($defaults['organization_name'], '', '', '', '');
80 }
81 elseif ($defaults['contact_type'] == 'Household') {
82 $vcard->setName($defaults['household_name'], '', '', '', '');
83 }
84 $vcard->setFormattedName($defaults['display_name']);
85 $vcard->setSortString($defaults['sort_name']);
86
a7488080 87 if (!empty($defaults['nick_name'])) {
6a488035
TO
88 $vcard->addNickname($defaults['nick_name']);
89 }
90
a7488080 91 if (!empty($defaults['job_title'])) {
6a488035
TO
92 $vcard->setTitle($defaults['job_title']);
93 }
94
a7488080 95 if (!empty($defaults['birth_date_display'])) {
6a488035
TO
96 $vcard->setBirthday(CRM_Utils_Array::value('birth_date_display', $defaults));
97 }
98
a7488080 99 if (!empty($defaults['home_URL'])) {
6a488035
TO
100 $vcard->setURL($defaults['home_URL']);
101 }
102
103 // TODO: $vcard->setGeo($lat, $lon);
a7488080 104 if (!empty($defaults['address'])) {
6a488035
TO
105 $stateProvices = CRM_Core_PseudoConstant::stateProvince();
106 $countries = CRM_Core_PseudoConstant::country();
107 foreach ($defaults['address'] as $location) {
108 // we don't keep PO boxes in separate fields
109 $pob = '';
110 $extend = CRM_Utils_Array::value('supplemental_address_1', $location);
a7488080 111 if (!empty($location['supplemental_address_2'])) {
6a488035
TO
112 $extend .= ', ' . $location['supplemental_address_2'];
113 }
114 $street = CRM_Utils_Array::value('street_address', $location);
115 $locality = CRM_Utils_Array::value('city', $location);
116 $region = NULL;
a7488080 117 if (!empty($location['state_province_id'])) {
6a488035
TO
118 $region = $stateProvices[CRM_Utils_Array::value('state_province_id', $location)];
119 }
120 $country = NULL;
a7488080 121 if (!empty($location['country_id'])) {
6a488035
TO
122 $country = $countries[CRM_Utils_Array::value('country_id', $location)];
123 }
124
125 $postcode = CRM_Utils_Array::value('postal_code', $location);
a7488080 126 if (!empty($location['postal_code_suffix'])) {
6a488035
TO
127 $postcode .= '-' . $location['postal_code_suffix'];
128 }
129
130 $vcard->addAddress($pob, $extend, $street, $locality, $region, $postcode, $country);
131 $vcardName = $vcardNames[$location['location_type_id']];
132 if ($vcardName) {
133 $vcard->addParam('TYPE', $vcardName);
134 }
a7488080 135 if (!empty($location['is_primary'])) {
6a488035
TO
136 $vcard->addParam('TYPE', 'PREF');
137 }
138 }
139 }
a7488080 140 if (!empty($defaults['phone'])) {
6a488035
TO
141 foreach ($defaults['phone'] as $phone) {
142 $vcard->addTelephone($phone['phone']);
143 $vcardName = $vcardNames[$phone['location_type_id']];
144 if ($vcardName) {
145 $vcard->addParam('TYPE', $vcardName);
146 }
147 if ($phone['is_primary']) {
148 $vcard->addParam('TYPE', 'PREF');
149 }
150 }
151 }
152
a7488080 153 if (!empty($defaults['email'])) {
6a488035
TO
154 foreach ($defaults['email'] as $email) {
155 $vcard->addEmail($email['email']);
156 $vcardName = $vcardNames[$email['location_type_id']];
157 if ($vcardName) {
158 $vcard->addParam('TYPE', $vcardName);
159 }
160 if ($email['is_primary']) {
161 $vcard->addParam('TYPE', 'PREF');
162 }
163 }
164 }
165
166 // all that's left is sending the vCard to the browser
167 $filename = CRM_Utils_String::munge($defaults['display_name']);
168 $vcard->send($filename . '.vcf', 'attachment', 'utf-8');
169 CRM_Utils_System::civiExit();
170 }
171}
172