Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
424029e3 | 35 | * This class provides the functionality to support Proximity Searches. |
6a488035 | 36 | */ |
d5f547f8 | 37 | class CRM_Contact_Form_Task_ProximityCommon { |
6a488035 TO |
38 | |
39 | /** | |
fe482240 | 40 | * The context that we are working on. |
6a488035 TO |
41 | * |
42 | * @var string | |
43 | */ | |
44 | protected $_context; | |
45 | ||
46 | /** | |
fe482240 | 47 | * The groupId retrieved from the GET vars. |
6a488035 TO |
48 | * |
49 | * @var int | |
50 | */ | |
51 | protected $_id; | |
52 | ||
53 | /** | |
fe482240 | 54 | * The title of the group. |
6a488035 TO |
55 | * |
56 | * @var string | |
57 | */ | |
58 | protected $_title; | |
59 | ||
6a488035 | 60 | /** |
fe482240 | 61 | * Build the form object. |
6a488035 | 62 | * |
c927c151 | 63 | * @param CRM_Core_Form $form |
424029e3 | 64 | * @param int $proxSearch |
6a488035 | 65 | */ |
69078420 | 66 | public static function buildQuickForm($form, $proxSearch) { |
6a488035 TO |
67 | // is proximity search required (2) or optional (1)? |
68 | $proxRequired = ($proxSearch == 2 ? TRUE : FALSE); | |
69 | $form->assign('proximity_search', TRUE); | |
70 | ||
71 | $form->add('text', 'prox_street_address', ts('Street Address'), NULL, FALSE); | |
72 | ||
73 | $form->add('text', 'prox_city', ts('City'), NULL, FALSE); | |
74 | ||
75 | $form->add('text', 'prox_postal_code', ts('Postal Code'), NULL, FALSE); | |
76 | ||
be2fb01f | 77 | $form->addChainSelect('prox_state_province_id', ['required' => $proxRequired]); |
6a488035 | 78 | |
be2fb01f | 79 | $country = ['' => ts('- select -')] + CRM_Core_PseudoConstant::country(); |
6a488035 TO |
80 | $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired); |
81 | ||
82 | $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired); | |
83 | ||
be2fb01f | 84 | $proxUnits = ['km' => ts('km'), 'miles' => ts('miles')]; |
6a488035 TO |
85 | $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired); |
86 | // prox_distance_unit | |
87 | ||
be2fb01f | 88 | $form->addFormRule(['CRM_Contact_Form_Task_ProximityCommon', 'formRule'], $form); |
6a488035 TO |
89 | } |
90 | ||
91 | /** | |
fe482240 | 92 | * Global form rule. |
6a488035 | 93 | * |
77c5b619 TO |
94 | * @param array $fields |
95 | * The input form values. | |
96 | * @param array $files | |
97 | * The uploaded files if any. | |
c490a46a | 98 | * @param CRM_Core_Form $form |
6a488035 | 99 | * |
72b3a70c CW |
100 | * @return bool|array |
101 | * true if no errors, else array of errors | |
6a488035 | 102 | */ |
00be9182 | 103 | public static function formRule($fields, $files, $form) { |
be2fb01f | 104 | $errors = []; |
6a488035 | 105 | // If Distance is present, make sure state, country and city or postal code are populated. |
a7488080 | 106 | if (!empty($fields['prox_distance'])) { |
8cc574cf | 107 | if (empty($fields['prox_state_province_id']) || empty($fields['prox_country_id'])) { |
6a488035 TO |
108 | $errors["prox_state_province_id"] = ts("Country AND State/Province are required to search by distance."); |
109 | } | |
de6c59ca | 110 | if (empty($fields['prox_postal_code']) && empty($fields['prox_city'])) { |
6a488035 TO |
111 | $errors["prox_distance"] = ts("City OR Postal Code are required to search by distance."); |
112 | } | |
113 | } | |
114 | ||
115 | return empty($errors) ? TRUE : $errors; | |
116 | } | |
117 | ||
118 | /** | |
fe482240 | 119 | * Set the default form values. |
6a488035 | 120 | * |
c490a46a | 121 | * @param CRM_Core_Form $form |
da6b46f4 | 122 | * |
a6c01b45 CW |
123 | * @return array |
124 | * the default array reference | |
6a488035 | 125 | */ |
69078420 | 126 | public static function setDefaultValues($form) { |
be2fb01f | 127 | $defaults = []; |
353ffa53 | 128 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
129 | $countryDefault = $config->defaultContactCountry; |
130 | ||
131 | if ($countryDefault) { | |
132 | $defaults['prox_country_id'] = $countryDefault; | |
133 | if ($countryDefault == '1228') { | |
134 | $defaults['prox_distance_unit'] = 'miles'; | |
135 | } | |
136 | else { | |
137 | $defaults['prox_distance_unit'] = 'km'; | |
138 | } | |
139 | } | |
140 | $form->setDefaults($defaults); | |
141 | return $defaults; | |
142 | } | |
96025800 | 143 | |
6a488035 | 144 | } |