REF - Switch CRM_Utils_Array::value to empty in conditionals
[civicrm-core.git] / CRM / Contact / Form / Search / Advanced.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/**
5a409b50 19 * Advanced search, extends basic search.
6a488035
TO
20 */
21class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
22
23 /**
fe482240 24 * Processing needed for buildForm and later.
6a488035 25 */
00be9182 26 public function preProcess() {
6a488035
TO
27 $this->set('searchFormName', 'Advanced');
28
29 parent::preProcess();
30 $openedPanes = CRM_Contact_BAO_Query::$_openedPanes;
31 $openedPanes = array_merge($openedPanes, $this->_openedPanes);
32 $this->assign('openedPanes', $openedPanes);
33 }
34
35 /**
fe482240 36 * Build the form object.
6a488035 37 */
00be9182 38 public function buildQuickForm() {
6a488035
TO
39 $this->set('context', 'advanced');
40
9c1bc317 41 $this->_searchPane = $_GET['searchPane'] ?? NULL;
6a488035
TO
42
43 $this->_searchOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
44 'advanced_search_options'
45 );
46
47 if (!$this->_searchPane || $this->_searchPane == 'basic') {
48 CRM_Contact_Form_Search_Criteria::basic($this);
49 }
50
2c4998cc 51 $allPanes = [];
52 $paneNames = [
6a488035
TO
53 ts('Address Fields') => 'location',
54 ts('Custom Fields') => 'custom',
55 ts('Activities') => 'activity',
56 ts('Relationships') => 'relationship',
57 ts('Demographics') => 'demographics',
58 ts('Notes') => 'notes',
59 ts('Change Log') => 'changeLog',
2c4998cc 60 ];
6a488035
TO
61
62 //check if there are any custom data searchable fields
2c4998cc 63 $extends = array_merge(['Contact', 'Individual', 'Household', 'Organization'],
6a488035
TO
64 CRM_Contact_BAO_ContactType::subTypes()
65 );
66 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE,
67 $extends
68 );
69 // if no searchable fields unset panel
70 if (empty($groupDetails)) {
71 unset($paneNames[ts('Custom Fields')]);
72 }
73
74 foreach ($paneNames as $name => $type) {
75 if (!$this->_searchOptions[$type]) {
76 unset($paneNames[$name]);
77 }
78 }
79
80 $components = CRM_Core_Component::getEnabledComponents();
81
2c4998cc 82 $componentPanes = [];
6a488035
TO
83 foreach ($components as $name => $component) {
84 if (in_array($name, array_keys($this->_searchOptions)) &&
85 $this->_searchOptions[$name] &&
86 CRM_Core_Permission::access($component->name)
87 ) {
88 $componentPanes[$name] = $component->registerAdvancedSearchPane();
89 $componentPanes[$name]['name'] = $name;
90 }
91 }
92
2c4998cc 93 usort($componentPanes, ['CRM_Utils_Sort', 'cmpFunc']);
6a488035
TO
94 foreach ($componentPanes as $name => $pane) {
95 // FIXME: we should change the use of $name here to keyword
96 $paneNames[$pane['title']] = $pane['name'];
97 }
98
2c4998cc 99 $hookPanes = [];
efa3a566
DS
100 CRM_Contact_BAO_Query_Hook::singleton()->registerAdvancedSearchPane($hookPanes);
101 $paneNames = array_merge($paneNames, $hookPanes);
6a488035 102
2c4998cc 103 $this->_paneTemplatePath = [];
6a488035 104 foreach ($paneNames as $name => $type) {
efa3a566 105 if (!array_key_exists($type, $this->_searchOptions) && !in_array($type, $hookPanes)) {
6a488035
TO
106 continue;
107 }
108
2c4998cc 109 $allPanes[$name] = [
6a488035
TO
110 'url' => CRM_Utils_System::url('civicrm/contact/search/advanced',
111 "snippet=1&searchPane=$type&qfKey={$this->controller->_key}"
112 ),
113 'open' => 'false',
114 'id' => $type,
2c4998cc 115 ];
6a488035
TO
116
117 // see if we need to include this paneName in the current form
8cc574cf 118 if ($this->_searchPane == $type || !empty($_POST["hidden_{$type}"]) ||
b99f3e96 119 !empty($this->_formValues["hidden_{$type}"])
6a488035
TO
120 ) {
121 $allPanes[$name]['open'] = 'true';
122
a7488080 123 if (!empty($components[$type])) {
6a488035
TO
124 $c = $components[$type];
125 $this->add('hidden', "hidden_$type", 1);
126 $c->buildAdvancedSearchPaneForm($this);
127 $this->_paneTemplatePath[$type] = $c->getAdvancedSearchPaneTemplatePath();
128 }
4c9b6178 129 elseif (in_array($type, $hookPanes)) {
efa3a566
DS
130 CRM_Contact_BAO_Query_Hook::singleton()->buildAdvancedSearchPaneForm($this, $type);
131 CRM_Contact_BAO_Query_Hook::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath, $type);
132 }
6a488035 133 else {
0e6e8724 134 CRM_Contact_Form_Search_Criteria::$type($this);
6a488035
TO
135 $template = ucfirst($type);
136 $this->_paneTemplatePath[$type] = "CRM/Contact/Form/Search/Criteria/{$template}.tpl";
137 }
138 }
139 }
140
141 $this->assign('allPanes', $allPanes);
142 if (!$this->_searchPane) {
143 parent::buildQuickForm();
144 }
145 else {
146 $this->assign('suppressForm', TRUE);
147 }
148 }
149
86538308 150 /**
fe482240 151 * Use the form name to create the tpl file name.
86538308
EM
152 *
153 * @return string
86538308 154 */
69078420 155
86538308
EM
156 /**
157 * @return string
158 */
00be9182 159 public function getTemplateFileName() {
6a488035
TO
160 if (!$this->_searchPane) {
161 return parent::getTemplateFileName();
162 }
163 else {
164 if (isset($this->_paneTemplatePath[$this->_searchPane])) {
165 return $this->_paneTemplatePath[$this->_searchPane];
166 }
167 else {
168 $name = ucfirst($this->_searchPane);
169 return "CRM/Contact/Form/Search/Criteria/{$name}.tpl";
170 }
171 }
172 }
173
174 /**
fe482240 175 * Set the default form values.
6a488035 176 *
a6c01b45
CW
177 * @return array
178 * the default array reference
8c9caddc 179 * @throws \Exception
6a488035 180 */
00be9182 181 public function setDefaultValues() {
8c9caddc 182 $defaults = parent::setDefaultValues();
41e6c841 183 // Set ssID for unit tests.
184 if (empty($this->_ssID)) {
185 $this->_ssID = $this->get('ssID');
186 }
187
e9f51713 188 $defaults = array_merge((array) $this->_formValues, [
852cda66 189 'privacy_toggle' => 1,
4abd8340 190 'operator' => 'AND',
8c9caddc 191 ], $defaults);
6a488035
TO
192 $this->normalizeDefaultValues($defaults);
193
194 if ($this->_context === 'amtg') {
eda34f9b 195 $defaults['task'] = CRM_Contact_Task::GROUP_ADD;
6a488035 196 }
6a488035
TO
197 return $defaults;
198 }
199
200 /**
201 * The post processing of the form gets done here.
202 *
203 * Key things done during post processing are
5a409b50 204 * - check for reset or next request. if present, skip post processing.
6a488035
TO
205 * - now check if user requested running a saved search, if so, then
206 * the form values associated with the saved search are used for searching.
5a409b50 207 * - if user has done a submit with new values the regular post submitting is
6a488035
TO
208 * done.
209 * The processing consists of using a Selector / Controller framework for getting the
210 * search results.
6a488035 211 */
00be9182 212 public function postProcess() {
6a488035
TO
213 $this->set('isAdvanced', '1');
214
359fdb6f 215 $this->setFormValues();
6a488035
TO
216 // get user submitted values
217 // get it from controller only if form has been submitted, else preProcess has set this
218 if (!empty($_POST)) {
6a488035
TO
219 $this->normalizeFormValues();
220 // FIXME: couldn't figure out a good place to do this,
221 // FIXME: so leaving this as a dependency for now
222 if (array_key_exists('contribution_amount_low', $this->_formValues)) {
2c4998cc 223 foreach (['contribution_amount_low', 'contribution_amount_high'] as $f) {
6a488035
TO
224 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
225 }
226 }
227
228 // set the group if group is submitted
81aa6678 229 if (!empty($this->_formValues['uf_group_id'])) {
6a488035
TO
230 $this->set('id', $this->_formValues['uf_group_id']);
231 }
232 else {
233 $this->set('id', '');
234 }
235 }
236
237 // retrieve ssID values only if formValues is null, i.e. form has never been posted
238 if (empty($this->_formValues) && isset($this->_ssID)) {
239 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
240 }
241
8cc574cf 242 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
2c4998cc 243 $this->_formValues['group'] = [$this->_groupID => 1];
6a488035
TO
244 }
245
6a488035
TO
246 //search for civicase
247 if (is_array($this->_formValues)) {
248 $allCases = FALSE;
249 if (array_key_exists('case_owner', $this->_formValues) &&
250 !$this->_formValues['case_owner'] &&
251 !$this->_force
252 ) {
2c4998cc 253 foreach (['case_type_id', 'case_status_id', 'case_deleted', 'case_tags'] as $caseCriteria) {
a7488080 254 if (!empty($this->_formValues[$caseCriteria])) {
6a488035
TO
255 $allCases = TRUE;
256 $this->_formValues['case_owner'] = 1;
257 continue;
258 }
259 }
260 if ($allCases) {
261 if (CRM_Core_Permission::check('access all cases and activities')) {
262 $this->_formValues['case_owner'] = 1;
263 }
264 else {
265 $this->_formValues['case_owner'] = 2;
266 }
267 }
268 else {
269 $this->_formValues['case_owner'] = 0;
270 }
271 }
d8f7f92c
RK
272 if (array_key_exists('case_owner', $this->_formValues) && empty($this->_formValues['case_deleted'])) {
273 $this->_formValues['case_deleted'] = 0;
274 }
6a488035
TO
275 }
276
277 // we dont want to store the sortByCharacter in the formValue, it is more like
278 // a filter on the result set
279 // this filter is reset if we click on the search button
e166ff79 280 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
6a488035
TO
281 if (strtolower($this->_sortByCharacter) == 'all') {
282 $this->_formValues['sortByCharacter'] = NULL;
283 }
284 else {
285 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
286 }
287 }
e166ff79
CW
288 else {
289 $this->_sortByCharacter = NULL;
290 }
6a488035 291
c94d39fd 292 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035 293
87061940 294 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
6a488035
TO
295 $this->_returnProperties = &$this->returnProperties();
296 parent::postProcess();
297 }
298
299 /**
5a409b50 300 * Normalize the form values to make it look similar to the advanced form values.
6a488035 301 *
5a409b50 302 * This prevents a ton of work downstream and allows us to use the same code for
303 * multiple purposes (queries, save/edit etc)
6a488035 304 */
00be9182 305 public function normalizeFormValues() {
9c1bc317 306 $contactType = $this->_formValues['contact_type'] ?? NULL;
6a488035
TO
307
308 if ($contactType && is_array($contactType)) {
309 unset($this->_formValues['contact_type']);
310 foreach ($contactType as $key => $value) {
311 $this->_formValues['contact_type'][$value] = 1;
312 }
313 }
314
315 $config = CRM_Core_Config::singleton();
2c4998cc 316 $specialParams = [
afa0b07c 317 'financial_type_id',
318 'contribution_soft_credit_type_id',
319 'contribution_status',
fb11d707 320 'contribution_status_id',
994bde4e
SB
321 'membership_status_id',
322 'participant_status_id',
afa0b07c 323 'contribution_trxn_id',
324 'activity_type_id',
da236f9a 325 'priority_id',
6ffab5b7 326 'contribution_product_id',
8f3dc989 327 'payment_instrument_id',
7cc09daf 328 'group',
329 'contact_tags',
330 'preferred_communication_method',
2c4998cc 331 ];
332 $changeNames = [
da236f9a 333 'priority_id' => 'activity_priority_id',
2c4998cc 334 ];
0b38e8f1 335 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames);
86538308 336
9c1bc317 337 $taglist = $this->_formValues['contact_taglist'] ?? NULL;
6a488035
TO
338
339 if ($taglist && is_array($taglist)) {
340 unset($this->_formValues['contact_taglist']);
341 foreach ($taglist as $value) {
342 if ($value) {
343 $value = explode(',', $value);
fe9517de 344 foreach ($value as $tId) {
345 if (is_numeric($tId)) {
346 $this->_formValues['contact_tags'][] = $tId;
347 }
348 }
6a488035
TO
349 }
350 }
351 }
6a488035
TO
352 }
353
354 /**
fe482240 355 * Normalize default values for multiselect plugins.
6a488035 356 *
5a4f6742 357 * @param array $defaults
bb05da0c 358 *
f4bff68a 359 * @return array
6a488035 360 */
d81916a2 361 public function normalizeDefaultValues($defaults) {
20306bb8 362 $this->loadDefaultCountryBasedOnState($defaults);
6a488035 363 if ($this->_ssID && empty($_POST)) {
06d67d53 364 $defaults = array_merge($defaults, CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID));
6a488035 365 }
1a4127a0 366
367 /*
368 * CRM-18656 - reverse the normalisation of 'contact_taglist' done in
369 * self::normalizeFormValues(). Remove tagset tags from the default
370 * 'contact_tags' and put them in 'contact_taglist[N]' where N is the
371 * id of the tagset.
372 */
373 if (isset($defaults['contact_tags'])) {
d7cc9ca9 374 foreach ((array) $defaults['contact_tags'] as $key => $tagId) {
08de6823 375 if (!is_array($tagId)) {
376 $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $tagId, 'parent_id');
377 $element = "contact_taglist[$parentId]";
378 if ($this->elementExists($element)) {
379 // This tag is a tagset
380 unset($defaults['contact_tags'][$key]);
381 if (!isset($defaults[$element])) {
2c4998cc 382 $defaults[$element] = [];
08de6823 383 }
384 $defaults[$element][] = $tagId;
1a4127a0 385 }
1a4127a0 386 }
387 }
388 if (empty($defaults['contact_tags'])) {
389 unset($defaults['contact_tags']);
390 }
391 }
392
6a488035
TO
393 return $defaults;
394 }
96025800 395
20306bb8 396 /**
397 * Set the default country for the form.
398 *
399 * For performance reasons country might be removed from the form CRM-18125
400 * but we need to include it in our defaults or the state will not be visible.
401 *
402 * @param array $defaults
403 */
404 public function loadDefaultCountryBasedOnState(&$defaults) {
405 if (!empty($defaults['state_province'])) {
406 $defaults['country'] = CRM_Core_DAO::singleValueQuery(
407 "SELECT country_id FROM civicrm_state_province
408 WHERE id = %1",
2c4998cc 409 [1 => [$defaults['state_province'][0], 'Integer']]
20306bb8 410 );
411 }
412 }
413
6a488035 414}