3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contact_BAO_Contact_Location
{
20 * Get the display name, primary email, location type and location id of a contact.
25 * @param bool $isPrimary
26 * @param int $locationTypeID
29 * Array of display_name, email, location type and location id if found, or (null,null,null, null)
31 public static function getEmailDetails($id, $isPrimary = TRUE, $locationTypeID = NULL) {
34 'return' => array('display_name', 'email.email'),
35 'api.Email.get' => array(
36 'location_type_id' => $locationTypeID,
38 'return' => array('email', 'location_type_id', 'id'),
42 $params['api.Email.get']['is_primary'] = 1;
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']);
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,
63 * @deprecated Not used anywhere, use the Phone API instead
64 * Get the sms number and display name of a contact.
72 * tuple of display_name and sms if found, or (null,null)
74 public static function getPhoneDetails($id, $type = NULL) {
75 CRM_Core_Error
::deprecatedFunctionWarning('Phone.get API instead');
82 $cond = " AND civicrm_phone.phone_type_id = '$type'";
86 SELECT civicrm_contact.display_name, civicrm_phone.phone, civicrm_contact.do_not_sms
88 LEFT JOIN civicrm_phone ON ( civicrm_phone.contact_id = civicrm_contact.id )
89 WHERE civicrm_phone.is_primary = 1
91 AND civicrm_contact.id = %1";
93 $params = [1 => [$id, 'Integer']];
94 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
96 return [$dao->display_name
, $dao->phone
, $dao->do_not_sms
];
98 return [NULL, NULL, NULL];
102 * Get the information to map a contact.
105 * The list of ids for which we want map info.
106 * $param int $locationTypeID
108 * @param int $locationTypeID
109 * @param bool $imageUrlOnly
111 * @return null|string
112 * display name of the contact if found
114 public static function &getMapInfo($ids, $locationTypeID = NULL, $imageUrlOnly = FALSE) {
115 $idString = ' ( ' . implode(',', $ids) . ' ) ';
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
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 ";
143 if (!$locationTypeID) {
144 $sql .= " AND civicrm_address.is_primary = 1";
147 $sql .= " AND civicrm_address.location_type_id = %1";
148 $params[1] = [$locationTypeID, 'Integer'];
151 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
154 $config = CRM_Core_Config
::singleton();
156 while ($dao->fetch()) {
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
;
168 CRM_Utils_String
::append($address, '<br />',
170 $dao->street_address
,
171 $dao->supplemental_address_1
,
172 $dao->supplemental_address_2
,
173 $dao->supplemental_address_3
,
177 CRM_Utils_String
::append($address, ', ',
178 [$dao->state
, $dao->postal_code
]
180 CRM_Utils_String
::append($address, '<br /> ',
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(isset($dao->contact_sub_type
) ?
$dao->contact_sub_type
: $dao->contact_type
, $imageUrlOnly, $dao->contact_id
189 $locations[] = $location;