Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-21-20-13-45
[civicrm-core.git] / CRM / Contact / Page / View / Vcard.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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 );
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
83 if (CRM_Utils_Array::value('nick_name', $defaults)) {
84 $vcard->addNickname($defaults['nick_name']);
85 }
86
87 if (CRM_Utils_Array::value('job_title', $defaults)) {
88 $vcard->setTitle($defaults['job_title']);
89 }
90
91 if (CRM_Utils_Array::value('birth_date_display', $defaults)) {
92 $vcard->setBirthday(CRM_Utils_Array::value('birth_date_display', $defaults));
93 }
94
95 if (CRM_Utils_Array::value('home_URL', $defaults)) {
96 $vcard->setURL($defaults['home_URL']);
97 }
98
99 // TODO: $vcard->setGeo($lat, $lon);
100 if (CRM_Utils_Array::value('address', $defaults)) {
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);
107 if (CRM_Utils_Array::value('supplemental_address_2', $location)) {
108 $extend .= ', ' . $location['supplemental_address_2'];
109 }
110 $street = CRM_Utils_Array::value('street_address', $location);
111 $locality = CRM_Utils_Array::value('city', $location);
112 $region = NULL;
113 if (CRM_Utils_Array::value('state_province_id', $location)) {
114 $region = $stateProvices[CRM_Utils_Array::value('state_province_id', $location)];
115 }
116 $country = NULL;
117 if (CRM_Utils_Array::value('country_id', $location)) {
118 $country = $countries[CRM_Utils_Array::value('country_id', $location)];
119 }
120
121 $postcode = CRM_Utils_Array::value('postal_code', $location);
122 if (CRM_Utils_Array::value('postal_code_suffix', $location)) {
123 $postcode .= '-' . $location['postal_code_suffix'];
124 }
125
126 $vcard->addAddress($pob, $extend, $street, $locality, $region, $postcode, $country);
127 $vcardName = $vcardNames[$location['location_type_id']];
128 if ($vcardName) {
129 $vcard->addParam('TYPE', $vcardName);
130 }
131 if (CRM_Utils_Array::value('is_primary', $location)) {
132 $vcard->addParam('TYPE', 'PREF');
133 }
134 }
135 }
136 if (CRM_Utils_Array::value('phone', $defaults)) {
137 foreach ($defaults['phone'] as $phone) {
138 $vcard->addTelephone($phone['phone']);
139 $vcardName = $vcardNames[$phone['location_type_id']];
140 if ($vcardName) {
141 $vcard->addParam('TYPE', $vcardName);
142 }
143 if ($phone['is_primary']) {
144 $vcard->addParam('TYPE', 'PREF');
145 }
146 }
147 }
148
149 if (CRM_Utils_Array::value('email', $defaults)) {
150 foreach ($defaults['email'] as $email) {
151 $vcard->addEmail($email['email']);
152 $vcardName = $vcardNames[$email['location_type_id']];
153 if ($vcardName) {
154 $vcard->addParam('TYPE', $vcardName);
155 }
156 if ($email['is_primary']) {
157 $vcard->addParam('TYPE', 'PREF');
158 }
159 }
160 }
161
162 // all that's left is sending the vCard to the browser
163 $filename = CRM_Utils_String::munge($defaults['display_name']);
164 $vcard->send($filename . '.vcf', 'attachment', 'utf-8');
165 CRM_Utils_System::civiExit();
166 }
167}
168