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