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