3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Contact_Form_Search_Custom_Proximity
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
37 protected $_latitude = NULL;
38 protected $_longitude = NULL;
39 protected $_distance = NULL;
46 public function __construct(&$formValues) {
47 parent
::__construct($formValues);
49 // unset search profile and other search params if set
50 unset($this->_formValues
['uf_group_id']);
51 unset($this->_formValues
['component_mode']);
52 unset($this->_formValues
['operator']);
54 if (!empty($this->_formValues
)) {
55 // add the country and state
56 if (!empty($this->_formValues
['country_id'])) {
57 $this->_formValues
['country'] = CRM_Core_PseudoConstant
::country($this->_formValues
['country_id']);
60 if (!empty($this->_formValues
['state_province_id'])) {
61 $this->_formValues
['state_province'] = CRM_Core_PseudoConstant
::stateProvince($this->_formValues
['state_province_id']);
64 // use the address to get the latitude and longitude
65 CRM_Utils_Geocode_Google
::format($this->_formValues
);
67 if (!is_numeric(CRM_Utils_Array
::value('geo_code_1', $this->_formValues
)) ||
68 !is_numeric(CRM_Utils_Array
::value('geo_code_2', $this->_formValues
)) ||
69 !isset($this->_formValues
['distance'])
71 CRM_Core_Error
::fatal(ts('Could not geocode input'));
74 $this->_latitude
= $this->_formValues
['geo_code_1'];
75 $this->_longitude
= $this->_formValues
['geo_code_2'];
77 if ($this->_formValues
['prox_distance_unit'] == "miles") {
78 $conversionFactor = 1609.344;
81 $conversionFactor = 1000;
83 $this->_distance
= $this->_formValues
['distance'] * $conversionFactor;
85 $this->_group
= CRM_Utils_Array
::value('group', $this->_formValues
);
87 $this->_tag
= CRM_Utils_Array
::value('tag', $this->_formValues
);
89 $this->_columns
= array(
90 ts('Name') => 'sort_name',
91 ts('Street Address') => 'street_address',
93 ts('Postal Code') => 'postal_code',
94 ts('State') => 'state_province',
95 ts('Country') => 'country',
100 * @param CRM_Core_Form $form
102 public function buildForm(&$form) {
104 $config = CRM_Core_Config
::singleton();
105 $countryDefault = $config->defaultContactCountry
;
107 $form->add('text', 'distance', ts('Distance'), NULL, TRUE);
109 $proxUnits = array('km' => ts('km'), 'miles' => ts('miles'));
110 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, TRUE);
128 if ($countryDefault) {
129 $defaults['country_id'] = $countryDefault;
131 $form->addChainSelect('state_province_id');
133 $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant
::country();
134 $form->add('select', 'country_id', ts('Country'), $country, TRUE, array('class' => 'crm-select2'));
136 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant
::nestedGroup();
137 $form->addElement('select', 'group', ts('Group'), $group, array('class' => 'crm-select2 huge'));
139 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
140 $form->addElement('select', 'tag', ts('Tag'), $tag, array('class' => 'crm-select2 huge'));
143 * You can define a custom title for the search form
145 $this->setTitle('Proximity Search');
148 * if you are using the standard template, this array tells the template what elements
149 * are part of the search criteria
151 $form->assign('elements', array(
153 'prox_distance_unit',
166 * @param int $rowcount
168 * @param bool $includeContactIDs
169 * @param bool $justIDs
174 $offset = 0, $rowcount = 0, $sort = NULL,
175 $includeContactIDs = FALSE, $justIDs = FALSE
178 $selectClause = "contact_a.id as contact_id";
182 contact_a.id as contact_id ,
183 contact_a.sort_name as sort_name ,
184 address.street_address as street_address,
185 address.city as city ,
186 address.postal_code as postal_code ,
187 state_province.name as state_province,
188 country.name as country
192 return $this->sql($selectClause,
193 $offset, $rowcount, $sort,
194 $includeContactIDs, NULL
201 public function from() {
203 FROM civicrm_contact contact_a
204 LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
205 address.is_primary = 1 )
206 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
207 LEFT JOIN civicrm_country country ON country.id = address.country_id
210 // This prevents duplicate rows when contacts have more than one tag any you select "any tag"
213 LEFT JOIN civicrm_entity_tag t ON (t.entity_table='civicrm_contact' AND contact_a.id = t.entity_id)
218 LEFT JOIN civicrm_group_contact cgc ON ( cgc.contact_id = contact_a.id AND cgc.status = 'Added')
226 * @param bool $includeContactIDs
230 public function where($includeContactIDs = FALSE) {
234 $where = CRM_Contact_BAO_ProximityQuery
::where($this->_latitude
,
242 AND t.tag_id = {$this->_tag}
247 AND cgc.group_id = {$this->_group}
251 $where .= " AND contact_a.is_deleted != 1 ";
253 return $this->whereClause($where, $params);
259 public function templateFile() {
260 return 'CRM/Contact/Form/Search/Custom/Proximity.tpl';
266 public function setDefaultValues() {
267 $config = CRM_Core_Config
::singleton();
268 $countryDefault = $config->defaultContactCountry
;
269 $stateprovinceDefault = $config->defaultContactStateProvince
;
272 if ($countryDefault) {
273 if ($countryDefault == '1228' ||
$countryDefault == '1226') {
274 $defaults['prox_distance_unit'] = 'miles';
277 $defaults['prox_distance_unit'] = 'km';
279 $defaults['country_id'] = $countryDefault;
280 if ($stateprovinceDefault) {
281 $defaults['state_province_id'] = $stateprovinceDefault;
291 public function alterRow(&$row) {
297 public function setTitle($title) {
299 CRM_Utils_System
::setTitle($title);
302 CRM_Utils_System
::setTitle(ts('Search'));