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