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