dev/core#538 fix advanced search on activity subject, detail to use wildcards like...
[civicrm-core.git] / CRM / Contact / Form / Search / Advanced.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Advanced search, extends basic search.
36 */
37 class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search {
38
39 /**
40 * Processing needed for buildForm and later.
41 */
42 public function preProcess() {
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 /**
52 * Build the form object.
53 */
54 public function buildQuickForm() {
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
67 $allPanes = array();
68 $paneNames = array(
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',
76 );
77
78 //check if there are any custom data searchable fields
79 $extends = array_merge(array('Contact', 'Individual', 'Household', 'Organization'),
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
98 $componentPanes = array();
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
109 usort($componentPanes, array('CRM_Utils_Sort', 'cmpFunc'));
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
115 $hookPanes = array();
116 CRM_Contact_BAO_Query_Hook::singleton()->registerAdvancedSearchPane($hookPanes);
117 $paneNames = array_merge($paneNames, $hookPanes);
118
119 $this->_paneTemplatePath = array();
120 foreach ($paneNames as $name => $type) {
121 if (!array_key_exists($type, $this->_searchOptions) && !in_array($type, $hookPanes)) {
122 continue;
123 }
124
125 $allPanes[$name] = array(
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,
131 );
132
133 // see if we need to include this paneName in the current form
134 if ($this->_searchPane == $type || !empty($_POST["hidden_{$type}"]) ||
135 CRM_Utils_Array::value("hidden_{$type}", $this->_formValues)
136 ) {
137 $allPanes[$name]['open'] = 'true';
138
139 if (!empty($components[$type])) {
140 $c = $components[$type];
141 $this->add('hidden', "hidden_$type", 1);
142 $c->buildAdvancedSearchPaneForm($this);
143 $this->_paneTemplatePath[$type] = $c->getAdvancedSearchPaneTemplatePath();
144 }
145 elseif (in_array($type, $hookPanes)) {
146 CRM_Contact_BAO_Query_Hook::singleton()->buildAdvancedSearchPaneForm($this, $type);
147 CRM_Contact_BAO_Query_Hook::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath, $type);
148 }
149 else {
150 CRM_Contact_Form_Search_Criteria::$type($this);
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
166 /**
167 * Use the form name to create the tpl file name.
168 *
169 * @return string
170 */
171
172 /**
173 * @return string
174 */
175 public function getTemplateFileName() {
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 /**
191 * Set the default form values.
192 *
193 * @return array
194 * the default array reference
195 * @throws \Exception
196 */
197 public function setDefaultValues() {
198 $defaults = parent::setDefaultValues();
199 // Set ssID for unit tests.
200 if (empty($this->_ssID)) {
201 $this->_ssID = $this->get('ssID');
202 }
203
204 $defaults = array_merge($this->_formValues, [
205 'privacy_toggle' => 1,
206 'operator' => 'AND',
207 ], $defaults);
208 $this->normalizeDefaultValues($defaults);
209
210 if ($this->_context === 'amtg') {
211 $defaults['task'] = CRM_Contact_Task::GROUP_ADD;
212 }
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
220 * - check for reset or next request. if present, skip post processing.
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.
223 * - if user has done a submit with new values the regular post submitting is
224 * done.
225 * The processing consists of using a Selector / Controller framework for getting the
226 * search results.
227 */
228 public function postProcess() {
229 $this->set('isAdvanced', '1');
230
231 $this->setFormValues();
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)) {
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)) {
239 foreach (array(
240 'contribution_amount_low',
241 'contribution_amount_high',
242 ) as $f) {
243 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
244 }
245 }
246
247 // set the group if group is submitted
248 if (!empty($this->_formValues['uf_group_id'])) {
249 $this->set('id', $this->_formValues['uf_group_id']);
250 }
251 else {
252 $this->set('id', '');
253 }
254 }
255
256 // retrieve ssID values only if formValues is null, i.e. form has never been posted
257 if (empty($this->_formValues) && isset($this->_ssID)) {
258 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
259 }
260
261 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
262 $this->_formValues['group'] = array($this->_groupID => 1);
263 }
264
265 //search for civicase
266 if (is_array($this->_formValues)) {
267 $allCases = FALSE;
268 if (array_key_exists('case_owner', $this->_formValues) &&
269 !$this->_formValues['case_owner'] &&
270 !$this->_force
271 ) {
272 foreach (array(
273 'case_type_id',
274 'case_status_id',
275 'case_deleted',
276 'case_tags',
277 ) as $caseCriteria) {
278 if (!empty($this->_formValues[$caseCriteria])) {
279 $allCases = TRUE;
280 $this->_formValues['case_owner'] = 1;
281 continue;
282 }
283 }
284 if ($allCases) {
285 if (CRM_Core_Permission::check('access all cases and activities')) {
286 $this->_formValues['case_owner'] = 1;
287 }
288 else {
289 $this->_formValues['case_owner'] = 2;
290 }
291 }
292 else {
293 $this->_formValues['case_owner'] = 0;
294 }
295 }
296 if (array_key_exists('case_owner', $this->_formValues) && empty($this->_formValues['case_deleted'])) {
297 $this->_formValues['case_deleted'] = 0;
298 }
299 }
300
301 // we dont want to store the sortByCharacter in the formValue, it is more like
302 // a filter on the result set
303 // this filter is reset if we click on the search button
304 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
305 if (strtolower($this->_sortByCharacter) == 'all') {
306 $this->_formValues['sortByCharacter'] = NULL;
307 }
308 else {
309 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
310 }
311 }
312 else {
313 $this->_sortByCharacter = NULL;
314 }
315
316 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
317
318 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
319 $this->_returnProperties = &$this->returnProperties();
320 parent::postProcess();
321 }
322
323 /**
324 * Normalize the form values to make it look similar to the advanced form values.
325 *
326 * This prevents a ton of work downstream and allows us to use the same code for
327 * multiple purposes (queries, save/edit etc)
328 */
329 public function normalizeFormValues() {
330 $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues);
331
332 if ($contactType && is_array($contactType)) {
333 unset($this->_formValues['contact_type']);
334 foreach ($contactType as $key => $value) {
335 $this->_formValues['contact_type'][$value] = 1;
336 }
337 }
338
339 $config = CRM_Core_Config::singleton();
340 $specialParams = array(
341 'financial_type_id',
342 'contribution_soft_credit_type_id',
343 'contribution_status',
344 'contribution_status_id',
345 'contribution_source',
346 'membership_status_id',
347 'participant_status_id',
348 'contribution_trxn_id',
349 'activity_type_id',
350 'status_id',
351 'priority_id',
352 'contribution_page_id',
353 'contribution_product_id',
354 'payment_instrument_id',
355 'group',
356 'contact_tags',
357 'preferred_communication_method',
358 );
359 $changeNames = array(
360 'status_id' => 'activity_status_id',
361 'priority_id' => 'activity_priority_id',
362 );
363 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames);
364
365 $taglist = CRM_Utils_Array::value('contact_taglist', $this->_formValues);
366
367 if ($taglist && is_array($taglist)) {
368 unset($this->_formValues['contact_taglist']);
369 foreach ($taglist as $value) {
370 if ($value) {
371 $value = explode(',', $value);
372 foreach ($value as $tId) {
373 if (is_numeric($tId)) {
374 $this->_formValues['contact_tags'][] = $tId;
375 }
376 }
377 }
378 }
379 }
380 }
381
382 /**
383 * Normalize default values for multiselect plugins.
384 *
385 * @param array $defaults
386 *
387 * @return array
388 */
389 public function normalizeDefaultValues(&$defaults) {
390 if (!is_array($defaults)) {
391 $defaults = array();
392 }
393 $this->loadDefaultCountryBasedOnState($defaults);
394 if ($this->_ssID && empty($_POST)) {
395 $defaults = array_merge($defaults, CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID));
396 }
397
398 /*
399 * CRM-18656 - reverse the normalisation of 'contact_taglist' done in
400 * self::normalizeFormValues(). Remove tagset tags from the default
401 * 'contact_tags' and put them in 'contact_taglist[N]' where N is the
402 * id of the tagset.
403 */
404 if (isset($defaults['contact_tags'])) {
405 foreach ($defaults['contact_tags'] as $key => $tagId) {
406 if (!is_array($tagId)) {
407 $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $tagId, 'parent_id');
408 $element = "contact_taglist[$parentId]";
409 if ($this->elementExists($element)) {
410 // This tag is a tagset
411 unset($defaults['contact_tags'][$key]);
412 if (!isset($defaults[$element])) {
413 $defaults[$element] = array();
414 }
415 $defaults[$element][] = $tagId;
416 }
417 }
418 }
419 if (empty($defaults['contact_tags'])) {
420 unset($defaults['contact_tags']);
421 }
422 }
423
424 return $defaults;
425 }
426
427 /**
428 * Set the default country for the form.
429 *
430 * For performance reasons country might be removed from the form CRM-18125
431 * but we need to include it in our defaults or the state will not be visible.
432 *
433 * @param array $defaults
434 */
435 public function loadDefaultCountryBasedOnState(&$defaults) {
436 if (!empty($defaults['state_province'])) {
437 $defaults['country'] = CRM_Core_DAO::singleValueQuery(
438 "SELECT country_id FROM civicrm_state_province
439 WHERE id = %1",
440 array(1 => array($defaults['state_province'][0], 'Integer'))
441 );
442 }
443 }
444
445 }