Skip to content
Snippets Groups Projects
Commit d2a29eba authored by colemanw's avatar colemanw
Browse files

SearchKit - Add proximity search to UI

Fixes #3163
parent cb695d3e
Branches
Tags
No related merge requests found
......@@ -598,4 +598,13 @@ class CRM_Utils_Address {
return CRM_Utils_Address::format($addressFields);
}
/**
* @return string
*/
public static function getDefaultDistanceUnit() {
$countryDefault = Civi::settings()->get('defaultContactCountry');
// US, UK use miles. Everything else is Km
return ($countryDefault == '1228' || $countryDefault == '1226') ? 'miles' : 'km';
}
}
......@@ -340,6 +340,7 @@ class BasicGetFieldsAction extends BasicGetAction {
'Radio' => ts('Radio Buttons'),
'Select' => ts('Select'),
'Text' => ts('Text'),
'Location' => ts('Address Location'),
],
],
[
......
......@@ -27,6 +27,7 @@ class AddressGetSpecProvider implements Generic\SpecProviderInterface {
$field = new FieldSpec('proximity', 'Address', 'Boolean');
$field->setLabel(ts('Address Proximity'))
->setTitle(ts('Address Proximity'))
->setInputType('Location')
->setColumnName('geo_code_1')
->setDescription(ts('Address is within a given distance to a location'))
->setType('Filter')
......
......@@ -237,7 +237,7 @@ class CRM_Contact_Form_Search_Custom_Proximity extends CRM_Contact_Form_Search_C
}
/**
* @return array|null
* @return array
*/
public function setDefaultValues() {
if (!empty($this->_formValues)) {
......@@ -246,22 +246,17 @@ class CRM_Contact_Form_Search_Custom_Proximity extends CRM_Contact_Form_Search_C
$config = CRM_Core_Config::singleton();
$countryDefault = $config->defaultContactCountry;
$stateprovinceDefault = $config->defaultContactStateProvince;
$defaults = [];
$defaults = [
'prox_distance_unit' => CRM_Utils_Address::getDefaultDistanceUnit(),
];
if ($countryDefault) {
if ($countryDefault == '1228' || $countryDefault == '1226') {
$defaults['prox_distance_unit'] = 'miles';
}
else {
$defaults['prox_distance_unit'] = 'km';
}
$defaults['country_id'] = $countryDefault;
if ($stateprovinceDefault) {
$defaults['state_province_id'] = $stateprovinceDefault;
}
return $defaults;
}
return NULL;
return $defaults;
}
/**
......
......@@ -47,6 +47,7 @@ class Admin {
'defaultDisplay' => SearchDisplay::getDefault(FALSE)->setSavedSearch(['id' => NULL])->execute()->first(),
'modules' => $extensions,
'defaultContactType' => \CRM_Contact_BAO_ContactType::basicTypeInfo()['Individual']['name'] ?? NULL,
'defaultDistanceUnit' => \CRM_Utils_Address::getDefaultDistanceUnit(),
'tags' => Tag::get()
->addSelect('id', 'name', 'color', 'is_selectable', 'description')
->addWhere('used_for', 'CONTAINS', 'civicrm_saved_search')
......
......@@ -61,6 +61,10 @@
return expr.indexOf('(') > -1;
};
this.areFunctionsAllowed = function(expr) {
return this.allowFunctions && ctrl.getField(expr).type !== 'Filter';
};
this.addGroup = function(op) {
ctrl.clauses.push([op, []]);
};
......
......@@ -15,7 +15,7 @@
</span>
</div>
<div ng-if="!$ctrl.conjunctions[clause[0]]" class="api4-input-group">
<crm-search-function ng-if="$ctrl.allowFunctions" class="form-group" expr="clause[0]" mode="clause"></crm-search-function>
<crm-search-function ng-if="$ctrl.areFunctionsAllowed(clause[0])" class="form-group" expr="clause[0]" mode="clause"></crm-search-function>
<span ng-if="!$ctrl.hasFunction(clause[0])">
<input class="form-control collapsible-optgroups" ng-model="clause[0]" crm-ui-select="{data: $ctrl.fields, allowClear: true, placeholder: 'Field'}" ng-change="$ctrl.changeClauseField(clause, index)" />
</span>
......
<select class="form-control api4-operator" ng-model="$ctrl.getSetOperator" ng-model-options="{getterSetter: true}" ng-options="o.key as o.value for o in $ctrl.getOperators()" ng-change="$ctrl.changeClauseOperator()" ></select>
<select class="form-control api4-operator" ng-model="$ctrl.getSetOperator" ng-if="$ctrl.getOperators().length > 1" ng-model-options="{getterSetter: true}" ng-options="o.key as o.value for o in $ctrl.getOperators()" ng-change="$ctrl.changeClauseOperator()" ></select>
<crm-search-input ng-if="$ctrl.operatorTakesInput()" ng-model="$ctrl.getSetValue" ng-model-options="{getterSetter: true}" field="$ctrl.field" option-key="$ctrl.optionKey" op="$ctrl.getSetOperator()" format="$ctrl.format" class="form-group"></crm-search-input>
......@@ -9,7 +9,7 @@
},
require: {ngModel: 'ngModel'},
template: '<div class="form-group" ng-include="$ctrl.getTemplate()"></div>',
controller: function($scope, formatForSelect2) {
controller: function($scope, formatForSelect2, crmApi4) {
var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'),
ctrl = this;
......@@ -95,6 +95,21 @@
}
};
this.lookupAddress = function() {
ctrl.value.geo_code_1 = null;
ctrl.value.geo_code_2 = null;
if (ctrl.value.address) {
crmApi4('Address', 'getCoordinates', {
address: ctrl.value.address
}).then(function(coordinates) {
if (coordinates[0]) {
ctrl.value.geo_code_1 = coordinates[0].geo_code_1;
ctrl.value.geo_code_2 = coordinates[0].geo_code_2;
}
});
}
};
this.getTemplate = function() {
var field = ctrl.field || {};
......@@ -102,6 +117,11 @@
return '~/crmSearchTasks/crmSearchInput/text.html';
}
if (field.input_type === 'Location') {
ctrl.value = ctrl.value || {distance_unit: CRM.crmSearchAdmin.defaultDistanceUnit};
return '~/crmSearchTasks/crmSearchInput/location.html';
}
if (isDateField(field)) {
return '~/crmSearchTasks/crmSearchInput/date.html';
}
......
<div class="form-group">
<input class="form-control" type="number" ng-model="$ctrl.value.distance" placeholder="{{:: ts('Distance') }}" >
<select class="form-control" ng-model="$ctrl.value.distance_unit">
<option value="km">{{:: ts('Km') }}</option>
<option value="miles">{{:: ts('Miles') }}</option>
</select>
<input class="form-control" ng-model="$ctrl.value.address" placeholder="{{:: ts('Street, City, State, Country') }}" ng-change="$ctrl.lookupAddress()" ng-model-options="{updateOn: 'blur'}" >
</div>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment