3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to support Proximity Searches.
21 class CRM_Contact_Form_Task_ProximityCommon
{
24 * The context that we are working on.
31 * The groupId retrieved from the GET vars.
38 * The title of the group.
45 * Build the form object.
47 * @param CRM_Core_Form $form
48 * @param int $proxSearch
50 public static function buildQuickForm($form, $proxSearch) {
51 // is proximity search required (2) or optional (1)?
52 $proxRequired = ($proxSearch == 2);
53 $form->assign('proximity_search', TRUE);
55 $form->add('text', 'prox_street_address', ts('Street Address'), NULL, FALSE);
57 $form->add('text', 'prox_city', ts('City'), NULL, FALSE);
59 $form->add('text', 'prox_postal_code', ts('Postal Code'), NULL, FALSE);
61 $form->addChainSelect('prox_state_province_id', ['required' => $proxRequired]);
63 $country = ['' => ts('- select -')] + CRM_Core_PseudoConstant
::country();
64 $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired);
66 $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired);
68 $proxUnits = ['km' => ts('km'), 'miles' => ts('miles')];
69 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired);
72 $form->addFormRule(['CRM_Contact_Form_Task_ProximityCommon', 'formRule'], $form);
78 * @param array $fields
79 * The input form values.
81 * The uploaded files if any.
82 * @param CRM_Core_Form $form
85 * true if no errors, else array of errors
87 public static function formRule($fields, $files, $form) {
89 // If Distance is present, make sure state, country and city or postal code are populated.
90 if (!empty($fields['prox_distance'])) {
91 if (empty($fields['prox_state_province_id']) ||
empty($fields['prox_country_id'])) {
92 $errors["prox_state_province_id"] = ts("Country AND State/Province are required to search by distance.");
94 if (empty($fields['prox_postal_code']) && empty($fields['prox_city'])) {
95 $errors["prox_distance"] = ts("City OR Postal Code are required to search by distance.");
99 return empty($errors) ?
TRUE : $errors;
103 * Set the default form values.
105 * @param CRM_Core_Form $form
108 * the default array reference
110 public static function setDefaultValues($form) {
112 $config = CRM_Core_Config
::singleton();
113 $countryDefault = $config->defaultContactCountry
;
115 if ($countryDefault) {
116 $defaults['prox_country_id'] = $countryDefault;
117 if ($countryDefault == '1228') {
118 $defaults['prox_distance_unit'] = 'miles';
121 $defaults['prox_distance_unit'] = 'km';
124 $form->setDefaults($defaults);