Merge pull request #18799 from deb1990/CPS-332-support-new-button-markup
[civicrm-core.git] / CRM / Contact / BAO / Contact / Location.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_BAO_Contact_Location {
18
19 /**
20 * Get the display name, primary email, location type and location id of a contact.
21 *
22 * @param int $id
23 * Id of the contact.
24 *
25 * @param bool $isPrimary
26 * @param int $locationTypeID
27 *
28 * @return array
29 * Array of display_name, email, location type and location id if found, or (null,null,null, null)
30 */
31 public static function getEmailDetails($id, $isPrimary = TRUE, $locationTypeID = NULL) {
32 $params = array(
33 'contact_id' => $id,
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 );
41 if ($isPrimary) {
42 $params['api.Email.get']['is_primary'] = 1;
43 }
44
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 }
51 }
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;
60 }
61
62 /**
63 * @deprecated Not used anywhere, use the Phone API instead
64 * Get the sms number and display name of a contact.
65 *
66 * @param int $id
67 * Id of the contact.
68 *
69 * @param null $type
70 *
71 * @return array
72 * tuple of display_name and sms if found, or (null,null)
73 */
74 public static function getPhoneDetails($id, $type = NULL) {
75 CRM_Core_Error::deprecatedFunctionWarning('Phone.get API instead');
76 if (!$id) {
77 return [NULL, NULL];
78 }
79
80 $cond = NULL;
81 if ($type) {
82 $cond = " AND civicrm_phone.phone_type_id = '$type'";
83 }
84
85 $sql = "
86 SELECT civicrm_contact.display_name, civicrm_phone.phone, civicrm_contact.do_not_sms
87 FROM civicrm_contact
88 LEFT 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
93 $params = [1 => [$id, 'Integer']];
94 $dao = CRM_Core_DAO::executeQuery($sql, $params);
95 if ($dao->fetch()) {
96 return [$dao->display_name, $dao->phone, $dao->do_not_sms];
97 }
98 return [NULL, NULL, NULL];
99 }
100
101 /**
102 * Get the information to map a contact.
103 *
104 * @param array $ids
105 * The list of ids for which we want map info.
106 * $param int $locationTypeID
107 *
108 * @param int $locationTypeID
109 * @param bool $imageUrlOnly
110 *
111 * @return null|string
112 * display name of the contact if found
113 */
114 public static function &getMapInfo($ids, $locationTypeID = NULL, $imageUrlOnly = FALSE) {
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,
124 civicrm_address.supplemental_address_3 as supplemental_address_3,
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
134 LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id
135 LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id
136 LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
137 LEFT JOIN civicrm_location_type ON civicrm_location_type.id = civicrm_address.location_type_id
138 WHERE civicrm_address.geo_code_1 IS NOT NULL
139 AND civicrm_address.geo_code_2 IS NOT NULL
140 AND civicrm_contact.id IN $idString ";
141
142 $params = [];
143 if (!$locationTypeID) {
144 $sql .= " AND civicrm_address.is_primary = 1";
145 }
146 else {
147 $sql .= " AND civicrm_address.location_type_id = %1";
148 $params[1] = [$locationTypeID, 'Integer'];
149 }
150
151 $dao = CRM_Core_DAO::executeQuery($sql, $params);
152
153 $locations = [];
154 $config = CRM_Core_Config::singleton();
155
156 while ($dao->fetch()) {
157 $location = [];
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 />',
169 [
170 $dao->street_address,
171 $dao->supplemental_address_1,
172 $dao->supplemental_address_2,
173 $dao->supplemental_address_3,
174 $dao->city,
175 ]
176 );
177 CRM_Utils_String::append($address, ', ',
178 [$dao->state, $dao->postal_code]
179 );
180 CRM_Utils_String::append($address, '<br /> ',
181 [$dao->country]
182 );
183 $location['address'] = addslashes($address);
184 $location['displayAddress'] = str_replace('<br />', ', ', addslashes($address));
185 $location['url'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $dao->contact_id);
186 $location['location_type'] = $dao->location_type;
187 $location['image'] = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ?? $dao->contact_type, $imageUrlOnly, $dao->contact_id
188 );
189 $locations[] = $location;
190 }
191 return $locations;
192 }
193
194 }