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