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