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