Skip to content

Commit

Permalink
SearchKit - Add proximity search to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed May 26, 2022
1 parent aaa2718 commit bc06bf2
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CRM/Utils/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,13 @@ public static function getFormattedBillingAddressFieldsFromParameters($params, $
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';
}

}
1 change: 1 addition & 0 deletions Civi/Api4/Generic/BasicGetFieldsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public function fields() {
'Radio' => ts('Radio Buttons'),
'Select' => ts('Select'),
'Text' => ts('Text'),
'Location' => ts('Address Location'),
],
],
[
Expand Down
3 changes: 2 additions & 1 deletion Civi/Api4/Service/Spec/Provider/AddressGetSpecProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public function modifySpec(RequestSpec $spec) {
$field = new FieldSpec('proximity', 'Address', 'Boolean');
$field->setLabel(ts('Proximity'))
->setTitle(ts('Search Proximity'))
->setInputType('Location')
->setColumnName('geo_code_1')
->setDescription(ts('Address is within a given distance'))
->setType('Filter')
->setOperators(['IN'])
->setOperators(['<='])
->addSqlFilter([__CLASS__, 'getProximitySql']);
$spec->addFieldSpec($field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function templateFile() {
}

/**
* @return array|null
* @return array
*/
public function setDefaultValues() {
if (!empty($this->_formValues)) {
Expand All @@ -246,22 +246,17 @@ public function setDefaultValues() {
$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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions ext/search_kit/Civi/Search/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function getAdminSettings():array {
'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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, []]);
};
Expand Down
2 changes: 1 addition & 1 deletion ext/search_kit/ang/crmSearchAdmin/crmSearchClause.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion ext/search_kit/ang/crmSearchAdmin/crmSearchCondition.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<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>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -95,13 +95,33 @@
}
};

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 || {};

if (_.includes(['LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP'], ctrl.op)) {
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';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<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 comments on commit bc06bf2

Please sign in to comment.