Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / CRM / Contact / Form / Task / ProximityCommon.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to support Proximity Searches.
20 */
21 class CRM_Contact_Form_Task_ProximityCommon {
22
23 /**
24 * The context that we are working on.
25 *
26 * @var string
27 */
28 protected $_context;
29
30 /**
31 * The groupId retrieved from the GET vars.
32 *
33 * @var int
34 */
35 protected $_id;
36
37 /**
38 * The title of the group.
39 *
40 * @var string
41 */
42 protected $_title;
43
44 /**
45 * Build the form object.
46 *
47 * @param CRM_Core_Form $form
48 * @param int $proxSearch
49 */
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);
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
61 $form->addChainSelect('prox_state_province_id', ['required' => $proxRequired]);
62
63 $country = ['' => ts('- select -')] + CRM_Core_PseudoConstant::country();
64 $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired);
65
66 $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired);
67
68 $proxUnits = ['km' => ts('km'), 'miles' => ts('miles')];
69 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired);
70 // prox_distance_unit
71
72 $form->addFormRule(['CRM_Contact_Form_Task_ProximityCommon', 'formRule'], $form);
73 }
74
75 /**
76 * Global form rule.
77 *
78 * @param array $fields
79 * The input form values.
80 * @param array $files
81 * The uploaded files if any.
82 * @param CRM_Core_Form $form
83 *
84 * @return bool|array
85 * true if no errors, else array of errors
86 */
87 public static function formRule($fields, $files, $form) {
88 $errors = [];
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.");
93 }
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.");
96 }
97 }
98
99 return empty($errors) ? TRUE : $errors;
100 }
101
102 /**
103 * Set the default form values.
104 *
105 * @param CRM_Core_Form $form
106 *
107 * @return array
108 * the default array reference
109 */
110 public static function setDefaultValues($form) {
111 $defaults = [];
112 $config = CRM_Core_Config::singleton();
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 }
127
128 }