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