PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/CRM/Contact/BAO/Contact/Location.php

https://github.com/ksecor/civicrm
PHP | 198 lines | 119 code | 20 blank | 59 comment | 9 complexity | 177f98df03eff29e9b211f47ed86a8ac MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. class CRM_Contact_BAO_Contact_Location
  34. {
  35. /**
  36. * function to get the display name, primary email, location type and location id of a contact
  37. *
  38. * @param int $id id of the contact
  39. *
  40. * @return array of display_name, email, location type and location id if found, or (null,null,null, null)
  41. * @static
  42. * @access public
  43. */
  44. static function getEmailDetails( $id, $isPrimary = true, $locationTypeID = null )
  45. {
  46. $primaryClause = null;
  47. if ( $isPrimary ) {
  48. $primaryClause = " AND civicrm_email.is_primary = 1";
  49. }
  50. $locationClause = null;
  51. if ( $locationTypeID ) {
  52. $locationClause = " AND civicrm_email.location_type_id = $locationTypeID";
  53. }
  54. $sql = "
  55. SELECT civicrm_contact.display_name,
  56. civicrm_email.email,
  57. civicrm_email.location_type_id,
  58. civicrm_email.id
  59. FROM civicrm_contact
  60. LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id {$primaryClause} {$locationClause} )
  61. WHERE civicrm_contact.id = %1";
  62. $params = array( 1 => array( $id, 'Integer' ) );
  63. $dao =& CRM_Core_DAO::executeQuery( $sql, $params );
  64. if ( $dao->fetch( ) ) {
  65. return array( $dao->display_name, $dao->email, $dao->location_type_id, $dao->id );
  66. }
  67. return array( null, null, null, null );
  68. }
  69. /**
  70. * function to get the sms number and display name of a contact
  71. *
  72. * @param int $id id of the contact
  73. *
  74. * @return array tuple of display_name and sms if found, or (null,null)
  75. * @static
  76. * @access public
  77. */
  78. static function getPhoneDetails( $id, $type = null )
  79. {
  80. if ( ! $id ) {
  81. return array( null, null );
  82. }
  83. $cond = null;
  84. if ( $type ) {
  85. $cond = " AND civicrm_phone.phone_type = '$type'";
  86. }
  87. $sql = "
  88. SELECT civicrm_contact.display_name, civicrm_phone.phone
  89. FROM civicrm_contact
  90. LEFT JOIN civicrm_phone ON ( civicrm_phone.contact_id = civicrm_contact.id )
  91. WHERE civicrm_phone.is_primary = 1
  92. $cond
  93. AND civicrm_contact.id = %1";
  94. $params = array( 1 => array( $id, 'Integer' ) );
  95. $dao =& CRM_Core_DAO::executeQuery( $sql, $params );
  96. if ( $dao->fetch( ) ) {
  97. return array( $dao->display_name, $dao->phone );
  98. }
  99. return array( null, null );
  100. }
  101. /**
  102. * function to get the information to map a contact
  103. *
  104. * @param array $ids the list of ids for which we want map info
  105. * $param int $locationTypeID
  106. *
  107. * @return null|string display name of the contact if found
  108. * @static
  109. * @access public
  110. */
  111. static function &getMapInfo( $ids, $locationTypeID = null )
  112. {
  113. $idString = ' ( ' . implode( ',', $ids ) . ' ) ';
  114. $sql = "
  115. SELECT civicrm_contact.id as contact_id,
  116. civicrm_contact.contact_type as contact_type,
  117. civicrm_contact.display_name as display_name,
  118. civicrm_address.street_address as street_address,
  119. civicrm_address.supplemental_address_1 as supplemental_address_1,
  120. civicrm_address.supplemental_address_2 as supplemental_address_2,
  121. civicrm_address.city as city,
  122. civicrm_address.postal_code as postal_code,
  123. civicrm_address.postal_code_suffix as postal_code_suffix,
  124. civicrm_address.geo_code_1 as latitude,
  125. civicrm_address.geo_code_2 as longitude,
  126. civicrm_state_province.abbreviation as state,
  127. civicrm_country.name as country,
  128. civicrm_location_type.name as location_type
  129. FROM civicrm_contact
  130. LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id
  131. LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id
  132. LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
  133. LEFT JOIN civicrm_location_type ON civicrm_location_type.id = civicrm_address.location_type_id
  134. WHERE civicrm_address.geo_code_1 IS NOT NULL
  135. AND civicrm_address.geo_code_2 IS NOT NULL
  136. AND civicrm_contact.id IN $idString ";
  137. $params = array( );
  138. if (!$locationTypeID) {
  139. $sql .= " AND civicrm_address.is_primary = 1";
  140. } else {
  141. $sql .= " AND civicrm_address.location_type_id = %1";
  142. $params[1] = array( $locationTypeID, 'Integer' );
  143. }
  144. $dao =& CRM_Core_DAO::executeQuery( $sql, $params );
  145. $locations = array( );
  146. $config =& CRM_Core_Config::singleton( );
  147. while ( $dao->fetch( ) ) {
  148. $location = array( );
  149. $location['contactID' ] = $dao->contact_id;
  150. $location['displayName'] = addslashes( $dao->display_name );
  151. $location['city' ] = $dao->city;
  152. $location['state' ] = $dao->state;
  153. $location['postal_code'] = $dao->postal_code;
  154. $location['lat' ] = $dao->latitude;
  155. $location['lng' ] = $dao->longitude;
  156. $address = '';
  157. CRM_Utils_String::append( $address, '<br />',
  158. array( $dao->street_address,
  159. $dao->supplemental_address_1,
  160. $dao->supplemental_address_2,
  161. $dao->city ) );
  162. CRM_Utils_String::append( $address, ', ',
  163. array( $dao->state, $dao->postal_code ) );
  164. CRM_Utils_String::append( $address, '<br /> ',
  165. array( $dao->country ) );
  166. $location['address' ] = addslashes( $address );
  167. $location['displayAddress'] = str_replace( '<br />', ', ', $address );
  168. $location['url' ] = CRM_Utils_System::url( 'civicrm/contact/view', 'reset=1&cid=' . $dao->contact_id );
  169. $location['location_type' ] = $dao->location_type;
  170. require_once 'CRM/Contact/BAO/Contact/Utils.php';
  171. $location['image'] =
  172. CRM_Contact_BAO_Contact_Utils::getImage( $dao->contact_sub_type ?
  173. $dao->contact_sub_type : $dao->contact_type );
  174. $locations[] = $location;
  175. }
  176. return $locations;
  177. }
  178. }