Commit | Line | Data |
---|---|---|
3efb5b86 CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
3efb5b86 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
3efb5b86 CW |
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 | | |
c73475ea | 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
3efb5b86 CW |
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 | | |
c73475ea WA |
20 | | License and the CiviCRM Licensing Exception along | |
21 | | with this program; if not, contact CiviCRM LLC | | |
3efb5b86 CW |
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 | */ |
3efb5b86 CW |
27 | |
28 | /** | |
29 | * Base class for most search forms | |
30 | */ | |
31 | class CRM_Core_Form_Search extends CRM_Core_Form { | |
32 | ||
48473171 CW |
33 | /** |
34 | * Are we forced to run a search | |
35 | * | |
36 | * @var int | |
48473171 CW |
37 | */ |
38 | protected $_force; | |
39 | ||
40 | /** | |
100fef9d | 41 | * Name of search button |
48473171 CW |
42 | * |
43 | * @var string | |
48473171 CW |
44 | */ |
45 | protected $_searchButtonName; | |
46 | ||
47 | /** | |
100fef9d | 48 | * Name of action button |
48473171 CW |
49 | * |
50 | * @var string | |
48473171 CW |
51 | */ |
52 | protected $_actionButtonName; | |
53 | ||
54 | /** | |
100fef9d | 55 | * Form values that we will be using |
48473171 CW |
56 | * |
57 | * @var array | |
48473171 CW |
58 | */ |
59 | public $_formValues; | |
60 | ||
61 | /** | |
100fef9d | 62 | * Have we already done this search |
48473171 | 63 | * |
b67daa72 | 64 | * @var bool |
48473171 CW |
65 | */ |
66 | protected $_done; | |
67 | ||
68 | /** | |
100fef9d | 69 | * What context are we being invoked from |
48473171 | 70 | * |
48473171 CW |
71 | * @var string |
72 | */ | |
73 | protected $_context = NULL; | |
74 | ||
7a3978aa FG |
75 | /** |
76 | * The list of tasks or actions that a searcher can perform on a result set. | |
77 | * | |
78 | * @var array | |
79 | */ | |
be2fb01f | 80 | protected $_taskList = []; |
7a3978aa | 81 | |
df60621b | 82 | /** |
83 | * Declare entity reference fields as they will need to be converted. | |
84 | * | |
85 | * The entity reference format looks like '2,3' whereas the Query object expects array(2, 3) | |
86 | * or array('IN' => array(2, 3). The latter is the one we are moving towards standardising on. | |
87 | * | |
88 | * @var array | |
89 | */ | |
be2fb01f | 90 | protected $entityReferenceFields = []; |
df60621b | 91 | |
7a3978aa FG |
92 | /** |
93 | * Builds the list of tasks or actions that a searcher can perform on a result set. | |
94 | * | |
95 | * To modify the task list, child classes should alter $this->_taskList, | |
96 | * preferably by extending this method. | |
97 | * | |
98 | * @return array | |
99 | */ | |
100 | protected function buildTaskList() { | |
101 | return $this->_taskList; | |
102 | } | |
103 | ||
e6dda67a | 104 | /** |
105 | * Metadata for fields on the search form. | |
106 | * | |
8c9caddc | 107 | * Instantiate with empty array for contact to prevent e-notices. |
108 | * | |
e6dda67a | 109 | * @var array |
110 | */ | |
8c9caddc | 111 | protected $searchFieldMetadata = ['Contact' => []]; |
e6dda67a | 112 | |
113 | /** | |
114 | * @return array | |
115 | */ | |
116 | public function getSearchFieldMetadata() { | |
117 | return $this->searchFieldMetadata; | |
118 | } | |
119 | ||
120 | /** | |
121 | * @param array $searchFieldMetadata | |
122 | */ | |
123 | public function addSearchFieldMetadata($searchFieldMetadata) { | |
124 | $this->searchFieldMetadata = array_merge($this->searchFieldMetadata, $searchFieldMetadata); | |
125 | } | |
126 | ||
8c9caddc | 127 | /** |
128 | * This virtual function is used to set the default values of various form elements. | |
129 | * | |
130 | * @return array|NULL | |
131 | * reference to the array of default values | |
5f1bf12f | 132 | * @throws \CRM_Core_Exception |
8c9caddc | 133 | */ |
134 | public function setDefaultValues() { | |
e8082ca5 | 135 | $defaults = (array) $this->_formValues; |
f90f8d32 | 136 | foreach (array_keys($this->getSearchFieldMetadata()) as $entity) { |
e8082ca5 | 137 | $defaults = array_merge($this->getEntityDefaults($entity), $defaults); |
138 | } | |
139 | return $defaults; | |
8c9caddc | 140 | } |
141 | ||
359fdb6f | 142 | /** |
143 | * Set the form values based on input and preliminary processing. | |
f90f8d32 | 144 | * |
145 | * @throws \Exception | |
359fdb6f | 146 | */ |
147 | protected function setFormValues() { | |
5f1bf12f | 148 | $this->_formValues = $this->getFormValues(); |
359fdb6f | 149 | $this->convertTextStringsToUseLikeOperator(); |
150 | } | |
151 | ||
34197a55 | 152 | /** |
0955d6b9 | 153 | * Common buildForm tasks required by all searches. |
34197a55 | 154 | */ |
61b4d091 | 155 | public function buildQuickForm() { |
13d9bc82 | 156 | CRM_Core_Resources::singleton() |
562fda4b CW |
157 | ->addScriptFile('civicrm', 'js/crm.searchForm.js', 1, 'html-header') |
158 | ->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header'); | |
3efb5b86 | 159 | |
be2fb01f CW |
160 | $this->addButtons([ |
161 | [ | |
3efb5b86 CW |
162 | 'type' => 'refresh', |
163 | 'name' => ts('Search'), | |
164 | 'isDefault' => TRUE, | |
be2fb01f CW |
165 | ], |
166 | ]); | |
8d36b801 | 167 | |
023e90c3 | 168 | $this->addClass('crm-search-form'); |
7a3978aa | 169 | |
7a3978aa FG |
170 | $tasks = $this->buildTaskList(); |
171 | $this->addTaskMenu($tasks); | |
8d36b801 CW |
172 | } |
173 | ||
e6dda67a | 174 | /** |
175 | * Add any fields described in metadata to the form. | |
176 | * | |
177 | * The goal is to describe all fields in metadata and handle from metadata rather | |
178 | * than existing ad hoc handling. | |
179 | */ | |
180 | public function addFormFieldsFromMetadata() { | |
2307be08 | 181 | $this->addFormRule(['CRM_Core_Form_Search', 'formRule'], $this); |
e6dda67a | 182 | $this->_action = CRM_Core_Action::ADVANCED; |
183 | foreach ($this->getSearchFieldMetadata() as $entity => $fields) { | |
184 | foreach ($fields as $fieldName => $fieldSpec) { | |
d7cc9ca9 | 185 | $fieldType = $fieldSpec['type'] ?? ''; |
186 | if ($fieldType === CRM_Utils_Type::T_DATE || $fieldType === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { | |
1157f03f | 187 | $title = empty($fieldSpec['unique_title']) ? $fieldSpec['title'] : $fieldSpec['unique_title']; |
d7cc9ca9 | 188 | $this->addDatePickerRange($fieldName, $title, ($fieldType === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME))); |
0bcac7e7 | 189 | } |
190 | else { | |
d7cc9ca9 | 191 | // Not quite sure about moving to a mix of keying by entity vs permitting entity to |
192 | // be passed in. The challenge of the former is that it doesn't permit ordering. | |
193 | // Perhaps keying was the wrong starting point & we should do a flat array as all | |
194 | // fields eventually need to be unique. | |
195 | $props = ['entity' => $fieldSpec['entity'] ?? $entity]; | |
1157f03f SL |
196 | if (isset($fields[$fieldName]['unique_title'])) { |
197 | $props['label'] = $fields[$fieldName]['unique_title']; | |
198 | } | |
199 | elseif (isset($fields[$fieldName]['title'])) { | |
8c9caddc | 200 | $props['label'] = $fields[$fieldName]['title']; |
201 | } | |
359fdb6f | 202 | if (empty($fieldSpec['is_pseudofield'])) { |
203 | $this->addField($fieldName, $props); | |
204 | } | |
0bcac7e7 | 205 | } |
e6dda67a | 206 | } |
207 | } | |
208 | } | |
209 | ||
2307be08 | 210 | /** |
211 | * Global validation rules for the form. | |
212 | * | |
213 | * @param array $fields | |
214 | * Posted values of the form. | |
518fa0ee SL |
215 | * @param array $files |
216 | * @param object $form | |
2307be08 | 217 | * |
218 | * @return array | |
219 | * list of errors to be posted back to the form | |
220 | */ | |
221 | public static function formRule($fields, $files, $form) { | |
222 | $errors = []; | |
b9c90943 | 223 | if (!is_a($form, 'CRM_Core_Form_Search')) { |
224 | // So this gets hit with a form object when doing an activity date search from | |
225 | // advanced search, but a NULL object when doing a pledge search. | |
226 | return $errors; | |
227 | } | |
2307be08 | 228 | foreach ($form->getSearchFieldMetadata() as $entity => $spec) { |
229 | foreach ($spec as $fieldName => $fieldSpec) { | |
230 | if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || $fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { | |
8a6fde27 | 231 | if (!empty($fields[$fieldName . '_high']) && !empty($fields[$fieldName . '_low']) && empty($fields[$fieldName . '_relative'])) { |
2307be08 | 232 | if (strtotime($fields[$fieldName . '_low']) > strtotime($fields[$fieldName . '_high'])) { |
233 | $errors[$fieldName . '_low'] = ts('%1: Please check that your date range is in correct chronological order.', [1 => $fieldSpec['title']]); | |
234 | } | |
235 | } | |
236 | } | |
237 | } | |
238 | } | |
239 | return $errors; | |
240 | } | |
241 | ||
e6dda67a | 242 | /** |
243 | * Get the validation rule to apply to a function. | |
244 | * | |
245 | * Alphanumeric is designed to always be safe & for now we just return | |
246 | * that but in future we can use tighter rules for types like int, bool etc. | |
247 | * | |
248 | * @param string $entity | |
249 | * @param string $fieldName | |
250 | * | |
251 | * @return string | |
252 | */ | |
253 | protected function getValidationTypeForField($entity, $fieldName) { | |
254 | switch ($this->getSearchFieldMetadata()[$entity][$fieldName]['type']) { | |
255 | case CRM_Utils_Type::T_BOOLEAN: | |
256 | return 'Boolean'; | |
257 | ||
258 | case CRM_Utils_Type::T_INT: | |
259 | return 'CommaSeparatedIntegers'; | |
260 | ||
27cedb98 | 261 | case CRM_Utils_Type::T_DATE: |
262 | case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME: | |
263 | return 'Timestamp'; | |
264 | ||
e6dda67a | 265 | default: |
266 | return 'Alphanumeric'; | |
267 | } | |
268 | } | |
269 | ||
270 | /** | |
271 | * Get the defaults for the entity for any fields described in metadata. | |
272 | * | |
273 | * @param string $entity | |
274 | * | |
275 | * @return array | |
81959827 | 276 | * |
277 | * @throws \CRM_Core_Exception | |
e6dda67a | 278 | */ |
279 | protected function getEntityDefaults($entity) { | |
280 | $defaults = []; | |
e8082ca5 | 281 | foreach (CRM_Utils_Array::value($entity, $this->getSearchFieldMetadata(), []) as $fieldName => $fieldSpec) { |
282 | if (empty($_POST[$fieldName])) { | |
283 | $value = CRM_Utils_Request::retrieveValue($fieldName, $this->getValidationTypeForField($entity, $fieldName), NULL, NULL, 'GET'); | |
c5c17034 | 284 | if ($value !== NULL) { |
285 | $defaults[$fieldName] = $value; | |
e6dda67a | 286 | } |
27cedb98 | 287 | if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || ($fieldSpec['type'] === CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { |
e8082ca5 | 288 | $low = CRM_Utils_Request::retrieveValue($fieldName . '_low', 'Timestamp', NULL, NULL, 'GET'); |
289 | $high = CRM_Utils_Request::retrieveValue($fieldName . '_high', 'Timestamp', NULL, NULL, 'GET'); | |
290 | if ($low !== NULL || $high !== NULL) { | |
c5c17034 | 291 | $defaults[$fieldName . '_relative'] = 0; |
292 | $defaults[$fieldName . '_low'] = $low ? date('Y-m-d H:i:s', strtotime($low)) : NULL; | |
293 | $defaults[$fieldName . '_high'] = $high ? date('Y-m-d H:i:s', strtotime($high)) : NULL; | |
27cedb98 | 294 | } |
81959827 | 295 | else { |
296 | $relative = CRM_Utils_Request::retrieveValue($fieldName . '_relative', 'String', NULL, NULL, 'GET'); | |
297 | if (!empty($relative) && isset(CRM_Core_OptionGroup::values('relative_date_filters')[$relative])) { | |
298 | $defaults[$fieldName . '_relative'] = $relative; | |
299 | } | |
300 | } | |
27cedb98 | 301 | } |
e6dda67a | 302 | } |
303 | } | |
304 | return $defaults; | |
305 | } | |
306 | ||
7123f88c | 307 | /** |
308 | * Convert any submitted text fields to use 'like' rather than '=' as the operator. | |
309 | * | |
310 | * This excludes any with options. | |
311 | * | |
312 | * Note this will only pick up fields declared via metadata. | |
313 | */ | |
314 | protected function convertTextStringsToUseLikeOperator() { | |
359fdb6f | 315 | foreach ($this->getSearchFieldMetadata() as $entity => $fields) { |
316 | foreach ($fields as $fieldName => $field) { | |
317 | if (!empty($this->_formValues[$fieldName]) && empty($field['options']) && empty($field['pseudoconstant'])) { | |
318 | if (in_array($field['type'], [CRM_Utils_Type::T_STRING, CRM_Utils_Type::T_TEXT])) { | |
00ed17db | 319 | $this->_formValues[$fieldName] = ['LIKE' => CRM_Contact_BAO_Query::getWildCardedValue(TRUE, 'LIKE', trim($this->_formValues[$fieldName]))]; |
359fdb6f | 320 | } |
7123f88c | 321 | } |
322 | } | |
323 | } | |
324 | } | |
325 | ||
8d36b801 | 326 | /** |
0955d6b9 | 327 | * Add checkboxes for each row plus a master checkbox. |
ad37ac8e | 328 | * |
329 | * @param array $rows | |
8d36b801 | 330 | */ |
00be9182 | 331 | public function addRowSelectors($rows) { |
be2fb01f | 332 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, ['class' => 'select-rows']); |
4126499f | 333 | if (!empty($rows)) { |
334 | foreach ($rows as $row) { | |
de6c59ca | 335 | if (!empty($row['checkbox'])) { |
be2fb01f | 336 | $this->addElement('checkbox', $row['checkbox'], NULL, NULL, ['class' => 'select-row']); |
6eb91e49 | 337 | } |
4126499f | 338 | } |
8d36b801 | 339 | } |
3efb5b86 | 340 | } |
34197a55 | 341 | |
44543184 | 342 | /** |
343 | * Add actions menu to search results form. | |
344 | * | |
345 | * @param array $tasks | |
346 | */ | |
347 | public function addTaskMenu($tasks) { | |
be2fb01f | 348 | $taskMetaData = []; |
44543184 | 349 | foreach ($tasks as $key => $task) { |
be2fb01f | 350 | $taskMetaData[$key] = ['title' => $task]; |
44543184 | 351 | } |
352 | parent::addTaskMenu($taskMetaData); | |
353 | } | |
354 | ||
e597fc33 DG |
355 | /** |
356 | * Add the sort-name field to the form. | |
357 | * | |
358 | * There is a setting to determine whether email is included in the search & we look this up to determine | |
359 | * which text to choose. | |
360 | * | |
361 | * Note that for translation purposes the full string works better than using 'prefix' hence we use override-able functions | |
362 | * to define the string. | |
363 | */ | |
364 | protected function addSortNameField() { | |
e8082ca5 | 365 | $title = civicrm_api3('setting', 'getvalue', ['name' => 'includeEmailInName', 'group' => 'Search Preferences']) ? $this->getSortNameLabelWithEmail() : $this->getSortNameLabelWithOutEmail(); |
e597fc33 DG |
366 | $this->addElement( |
367 | 'text', | |
368 | 'sort_name', | |
e8082ca5 | 369 | $title, |
e597fc33 DG |
370 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name') |
371 | ); | |
e8082ca5 | 372 | $this->searchFieldMetadata['Contact']['sort_name'] = ['name' => 'sort_name', 'title' => $title, 'type' => CRM_Utils_Type::T_STRING]; |
e597fc33 DG |
373 | } |
374 | ||
375 | /** | |
376 | * Get the label for the sortName field if email searching is on. | |
377 | * | |
378 | * (email searching is a setting under search preferences). | |
379 | * | |
380 | * @return string | |
381 | */ | |
382 | protected function getSortNameLabelWithEmail() { | |
383 | return ts('Name or Email'); | |
384 | } | |
385 | ||
386 | /** | |
387 | * Get the label for the sortName field if email searching is off. | |
388 | * | |
389 | * (email searching is a setting under search preferences). | |
390 | * | |
391 | * @return string | |
392 | */ | |
393 | protected function getSortNameLabelWithOutEmail() { | |
394 | return ts('Name'); | |
395 | } | |
396 | ||
2ee21eaa CW |
397 | /** |
398 | * Explicitly declare the form context for addField(). | |
399 | */ | |
400 | public function getDefaultContext() { | |
401 | return 'search'; | |
402 | } | |
403 | ||
0573fd28 | 404 | /** |
405 | * Add generic fields that specify the contact. | |
406 | */ | |
407 | protected function addContactSearchFields() { | |
240b0e65 | 408 | if (!$this->isFormInViewOrEditMode()) { |
409 | return; | |
410 | } | |
0573fd28 | 411 | $this->addSortNameField(); |
412 | ||
413 | $this->_group = CRM_Core_PseudoConstant::nestedGroup(); | |
414 | if ($this->_group) { | |
415 | $this->add('select', 'group', $this->getGroupLabel(), $this->_group, FALSE, | |
be2fb01f | 416 | [ |
0573fd28 | 417 | 'id' => 'group', |
418 | 'multiple' => 'multiple', | |
419 | 'class' => 'crm-select2', | |
be2fb01f | 420 | ] |
0573fd28 | 421 | ); |
422 | } | |
423 | ||
424 | $contactTags = CRM_Core_BAO_Tag::getTags(); | |
425 | if ($contactTags) { | |
426 | $this->add('select', 'contact_tags', $this->getTagLabel(), $contactTags, FALSE, | |
be2fb01f | 427 | [ |
0573fd28 | 428 | 'id' => 'contact_tags', |
429 | 'multiple' => 'multiple', | |
430 | 'class' => 'crm-select2', | |
be2fb01f | 431 | ] |
0573fd28 | 432 | ); |
433 | } | |
be2fb01f | 434 | $this->addField('contact_type', ['entity' => 'Contact']); |
0573fd28 | 435 | |
436 | if (CRM_Core_Permission::check('access deleted contacts') && Civi::settings()->get('contact_undelete')) { | |
437 | $this->addElement('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)')); | |
438 | } | |
439 | ||
440 | } | |
441 | ||
09e47399 | 442 | /** |
443 | * we allow the controller to set force/reset externally, useful when we are being | |
444 | * driven by the wizard framework | |
445 | */ | |
64ffcefd | 446 | protected function loadStandardSearchOptionsFromUrl() { |
09e47399 | 447 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean'); |
448 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); | |
449 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
450 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'search'); | |
451 | $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this); | |
452 | $this->assign("context", $this->_context); | |
453 | } | |
454 | ||
455 | /** | |
456 | * Get user submitted values. | |
457 | * | |
458 | * Get it from controller only if form has been submitted, else preProcess has set this | |
459 | */ | |
64ffcefd | 460 | protected function loadFormValues() { |
09e47399 | 461 | if (!empty($_POST) && !$this->controller->isModal()) { |
462 | $this->_formValues = $this->controller->exportValues($this->_name); | |
463 | } | |
464 | else { | |
465 | $this->_formValues = $this->get('formValues'); | |
466 | } | |
467 | ||
468 | if (empty($this->_formValues)) { | |
469 | if (isset($this->_ssID)) { | |
470 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
471 | } | |
472 | } | |
473 | } | |
474 | ||
5f1bf12f | 475 | /** |
476 | * Get the form values. | |
477 | * | |
478 | * @todo consolidate with loadFormValues() | |
479 | * | |
480 | * @return array | |
481 | * | |
482 | * @throws \CRM_Core_Exception | |
483 | */ | |
484 | protected function getFormValues() { | |
485 | if (!empty($_POST) && !$this->_force) { | |
486 | return $this->controller->exportValues($this->_name); | |
487 | } | |
488 | if ($this->_force) { | |
489 | return $this->setDefaultValues(); | |
490 | } | |
491 | return (array) $this->get('formValues'); | |
492 | } | |
493 | ||
3a27e13e | 494 | /** |
495 | * Set the metadata for the form. | |
496 | * | |
497 | * @throws \CiviCRM_API3_Exception | |
498 | */ | |
499 | protected function setSearchMetadata() {} | |
500 | ||
501 | /** | |
502 | * Handle force=1 in the url. | |
503 | * | |
504 | * Search field metadata is normally added in buildForm but we are bypassing that in this flow | |
505 | * (I've always found the flow kinda confusing & perhaps that is the problem but this mitigates) | |
506 | * | |
507 | * @throws \CiviCRM_API3_Exception | |
508 | */ | |
509 | protected function handleForcedSearch() { | |
510 | $this->setSearchMetadata(); | |
511 | $this->postProcess(); | |
512 | $this->set('force', 0); | |
513 | } | |
514 | ||
3efb5b86 | 515 | } |