Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / CRM / Contact / Form / Task / ProximityCommon.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
424029e3 19 * This class provides the functionality to support Proximity Searches.
6a488035 20 */
d5f547f8 21class CRM_Contact_Form_Task_ProximityCommon {
6a488035
TO
22
23 /**
fe482240 24 * The context that we are working on.
6a488035
TO
25 *
26 * @var string
27 */
28 protected $_context;
29
30 /**
fe482240 31 * The groupId retrieved from the GET vars.
6a488035
TO
32 *
33 * @var int
34 */
35 protected $_id;
36
37 /**
fe482240 38 * The title of the group.
6a488035
TO
39 *
40 * @var string
41 */
42 protected $_title;
43
6a488035 44 /**
fe482240 45 * Build the form object.
6a488035 46 *
c927c151 47 * @param CRM_Core_Form $form
424029e3 48 * @param int $proxSearch
6a488035 49 */
69078420 50 public static function buildQuickForm($form, $proxSearch) {
6a488035 51 // is proximity search required (2) or optional (1)?
dc0aa487 52 $proxRequired = ($proxSearch == 2);
6a488035
TO
53 $form->assign('proximity_search', TRUE);
54
55 $form->add('text', 'prox_street_address', ts('Street Address'), NULL, FALSE);
56
57 $form->add('text', 'prox_city', ts('City'), NULL, FALSE);
58
59 $form->add('text', 'prox_postal_code', ts('Postal Code'), NULL, FALSE);
60
be2fb01f 61 $form->addChainSelect('prox_state_province_id', ['required' => $proxRequired]);
6a488035 62
be2fb01f 63 $country = ['' => ts('- select -')] + CRM_Core_PseudoConstant::country();
6a488035
TO
64 $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired);
65
66 $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired);
67
be2fb01f 68 $proxUnits = ['km' => ts('km'), 'miles' => ts('miles')];
6a488035
TO
69 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired);
70 // prox_distance_unit
71
be2fb01f 72 $form->addFormRule(['CRM_Contact_Form_Task_ProximityCommon', 'formRule'], $form);
6a488035
TO
73 }
74
75 /**
fe482240 76 * Global form rule.
6a488035 77 *
77c5b619
TO
78 * @param array $fields
79 * The input form values.
80 * @param array $files
81 * The uploaded files if any.
c490a46a 82 * @param CRM_Core_Form $form
6a488035 83 *
72b3a70c
CW
84 * @return bool|array
85 * true if no errors, else array of errors
6a488035 86 */
00be9182 87 public static function formRule($fields, $files, $form) {
be2fb01f 88 $errors = [];
6a488035 89 // If Distance is present, make sure state, country and city or postal code are populated.
a7488080 90 if (!empty($fields['prox_distance'])) {
8cc574cf 91 if (empty($fields['prox_state_province_id']) || empty($fields['prox_country_id'])) {
6a488035
TO
92 $errors["prox_state_province_id"] = ts("Country AND State/Province are required to search by distance.");
93 }
de6c59ca 94 if (empty($fields['prox_postal_code']) && empty($fields['prox_city'])) {
6a488035
TO
95 $errors["prox_distance"] = ts("City OR Postal Code are required to search by distance.");
96 }
97 }
98
99 return empty($errors) ? TRUE : $errors;
100 }
101
102 /**
fe482240 103 * Set the default form values.
6a488035 104 *
c490a46a 105 * @param CRM_Core_Form $form
da6b46f4 106 *
a6c01b45
CW
107 * @return array
108 * the default array reference
6a488035 109 */
69078420 110 public static function setDefaultValues($form) {
be2fb01f 111 $defaults = [];
353ffa53 112 $config = CRM_Core_Config::singleton();
6a488035
TO
113 $countryDefault = $config->defaultContactCountry;
114
115 if ($countryDefault) {
116 $defaults['prox_country_id'] = $countryDefault;
117 if ($countryDefault == '1228') {
118 $defaults['prox_distance_unit'] = 'miles';
119 }
120 else {
121 $defaults['prox_distance_unit'] = 'km';
122 }
123 }
124 $form->setDefaults($defaults);
125 return $defaults;
126 }
96025800 127
6a488035 128}