Merge pull request #11251 from totten/master-mailtest
[civicrm-core.git] / CRM / Contact / BAO / Contact / Location.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Contact_BAO_Contact_Location {
34
35 /**
67d19299 36 * Get the display name, primary email, location type and location id of a contact.
6a488035 37 *
77c5b619
TO
38 * @param int $id
39 * Id of the contact.
77b97be7
EM
40 *
41 * @param bool $isPrimary
100fef9d 42 * @param int $locationTypeID
6a488035 43 *
a6c01b45 44 * @return array
16b10e64 45 * Array of display_name, email, location type and location id if found, or (null,null,null, null)
6a488035 46 */
00be9182 47 public static function getEmailDetails($id, $isPrimary = TRUE, $locationTypeID = NULL) {
6a488035
TO
48 $primaryClause = NULL;
49 if ($isPrimary) {
50 $primaryClause = " AND civicrm_email.is_primary = 1";
51 }
52
6a488035
TO
53 $locationClause = NULL;
54 if ($locationTypeID) {
55 $locationClause = " AND civicrm_email.location_type_id = $locationTypeID";
56 }
57
58 $sql = "
59SELECT civicrm_contact.display_name,
60 civicrm_email.email,
61 civicrm_email.location_type_id,
62 civicrm_email.id
63FROM civicrm_contact
64LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id {$primaryClause} {$locationClause} )
65WHERE civicrm_contact.id = %1";
66
67 $params = array(1 => array($id, 'Integer'));
68 $dao = CRM_Core_DAO::executeQuery($sql, $params);
69 if ($dao->fetch()) {
70 return array($dao->display_name, $dao->email, $dao->location_type_id, $dao->id);
71 }
72 return array(NULL, NULL, NULL, NULL);
73 }
74
75 /**
fe482240 76 * Get the sms number and display name of a contact.
6a488035 77 *
77c5b619
TO
78 * @param int $id
79 * Id of the contact.
6c8f6e67
EM
80 *
81 * @param null $type
6a488035 82 *
a6c01b45
CW
83 * @return array
84 * tuple of display_name and sms if found, or (null,null)
6a488035 85 */
00be9182 86 public static function getPhoneDetails($id, $type = NULL) {
6a488035
TO
87 if (!$id) {
88 return array(NULL, NULL);
89 }
90
91 $cond = NULL;
92 if ($type) {
93 $cond = " AND civicrm_phone.phone_type_id = '$type'";
94 }
95
6a488035
TO
96 $sql = "
97 SELECT civicrm_contact.display_name, civicrm_phone.phone, civicrm_contact.do_not_sms
98 FROM civicrm_contact
99LEFT JOIN civicrm_phone ON ( civicrm_phone.contact_id = civicrm_contact.id )
100 WHERE civicrm_phone.is_primary = 1
101 $cond
102 AND civicrm_contact.id = %1";
103
104 $params = array(1 => array($id, 'Integer'));
105 $dao = CRM_Core_DAO::executeQuery($sql, $params);
106 if ($dao->fetch()) {
107 return array($dao->display_name, $dao->phone, $dao->do_not_sms);
108 }
109 return array(NULL, NULL, NULL);
110 }
111
112 /**
fe482240 113 * Get the information to map a contact.
6a488035 114 *
77c5b619
TO
115 * @param array $ids
116 * The list of ids for which we want map info.
6a488035
TO
117 * $param int $locationTypeID
118 *
100fef9d 119 * @param int $locationTypeID
da6b46f4
EM
120 * @param bool $imageUrlOnly
121 *
72b3a70c
CW
122 * @return null|string
123 * display name of the contact if found
6a488035 124 */
00be9182 125 public static function &getMapInfo($ids, $locationTypeID = NULL, $imageUrlOnly = FALSE) {
6a488035
TO
126 $idString = ' ( ' . implode(',', $ids) . ' ) ';
127 $sql = "
128 SELECT civicrm_contact.id as contact_id,
129 civicrm_contact.contact_type as contact_type,
130 civicrm_contact.contact_sub_type as contact_sub_type,
131 civicrm_contact.display_name as display_name,
132 civicrm_address.street_address as street_address,
133 civicrm_address.supplemental_address_1 as supplemental_address_1,
134 civicrm_address.supplemental_address_2 as supplemental_address_2,
207f62c6 135 civicrm_address.supplemental_address_3 as supplemental_address_3,
6a488035
TO
136 civicrm_address.city as city,
137 civicrm_address.postal_code as postal_code,
138 civicrm_address.postal_code_suffix as postal_code_suffix,
139 civicrm_address.geo_code_1 as latitude,
140 civicrm_address.geo_code_2 as longitude,
141 civicrm_state_province.abbreviation as state,
142 civicrm_country.name as country,
143 civicrm_location_type.name as location_type
144 FROM civicrm_contact
145LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id
146LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id
147LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
148LEFT JOIN civicrm_location_type ON civicrm_location_type.id = civicrm_address.location_type_id
8ef12e64 149WHERE civicrm_address.geo_code_1 IS NOT NULL
150AND civicrm_address.geo_code_2 IS NOT NULL
6a488035
TO
151AND civicrm_contact.id IN $idString ";
152
153 $params = array();
154 if (!$locationTypeID) {
155 $sql .= " AND civicrm_address.is_primary = 1";
156 }
157 else {
158 $sql .= " AND civicrm_address.location_type_id = %1";
159 $params[1] = array($locationTypeID, 'Integer');
160 }
161
162 $dao = CRM_Core_DAO::executeQuery($sql, $params);
163
164 $locations = array();
165 $config = CRM_Core_Config::singleton();
166
167 while ($dao->fetch()) {
168 $location = array();
169 $location['contactID'] = $dao->contact_id;
170 $location['displayName'] = addslashes($dao->display_name);
171 $location['city'] = $dao->city;
172 $location['state'] = $dao->state;
173 $location['postal_code'] = $dao->postal_code;
174 $location['lat'] = $dao->latitude;
175 $location['lng'] = $dao->longitude;
176 $location['marker_class'] = $dao->contact_type;
177 $address = '';
178
179 CRM_Utils_String::append($address, '<br />',
180 array(
181 $dao->street_address,
182 $dao->supplemental_address_1,
183 $dao->supplemental_address_2,
207f62c6 184 $dao->supplemental_address_3,
6a488035
TO
185 $dao->city,
186 )
187 );
188 CRM_Utils_String::append($address, ', ',
189 array($dao->state, $dao->postal_code)
190 );
191 CRM_Utils_String::append($address, '<br /> ',
192 array($dao->country)
193 );
194 $location['address'] = addslashes($address);
d40be285 195 $location['displayAddress'] = str_replace('<br />', ', ', addslashes($address));
6a488035
TO
196 $location['url'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $dao->contact_id);
197 $location['location_type'] = $dao->location_type;
28a04ea9 198 $location['image'] = CRM_Contact_BAO_Contact_Utils::getImage(isset($dao->contact_sub_type) ? $dao->contact_sub_type : $dao->contact_type, $imageUrlOnly, $dao->contact_id
6a488035
TO
199 );
200 $locations[] = $location;
201 }
202 return $locations;
203 }
96025800 204
6a488035 205}