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 | * | |
107 | * @var array | |
108 | */ | |
109 | protected $searchFieldMetadata = []; | |
110 | ||
111 | /** | |
112 | * @return array | |
113 | */ | |
114 | public function getSearchFieldMetadata() { | |
115 | return $this->searchFieldMetadata; | |
116 | } | |
117 | ||
118 | /** | |
119 | * @param array $searchFieldMetadata | |
120 | */ | |
121 | public function addSearchFieldMetadata($searchFieldMetadata) { | |
122 | $this->searchFieldMetadata = array_merge($this->searchFieldMetadata, $searchFieldMetadata); | |
123 | } | |
124 | ||
34197a55 | 125 | /** |
0955d6b9 | 126 | * Common buildForm tasks required by all searches. |
34197a55 | 127 | */ |
61b4d091 | 128 | public function buildQuickForm() { |
13d9bc82 | 129 | CRM_Core_Resources::singleton() |
562fda4b CW |
130 | ->addScriptFile('civicrm', 'js/crm.searchForm.js', 1, 'html-header') |
131 | ->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header'); | |
3efb5b86 | 132 | |
be2fb01f CW |
133 | $this->addButtons([ |
134 | [ | |
3efb5b86 CW |
135 | 'type' => 'refresh', |
136 | 'name' => ts('Search'), | |
137 | 'isDefault' => TRUE, | |
be2fb01f CW |
138 | ], |
139 | ]); | |
8d36b801 | 140 | |
023e90c3 | 141 | $this->addClass('crm-search-form'); |
7a3978aa | 142 | |
7a3978aa FG |
143 | $tasks = $this->buildTaskList(); |
144 | $this->addTaskMenu($tasks); | |
8d36b801 CW |
145 | } |
146 | ||
e6dda67a | 147 | /** |
148 | * Add any fields described in metadata to the form. | |
149 | * | |
150 | * The goal is to describe all fields in metadata and handle from metadata rather | |
151 | * than existing ad hoc handling. | |
152 | */ | |
153 | public function addFormFieldsFromMetadata() { | |
2307be08 | 154 | $this->addFormRule(['CRM_Core_Form_Search', 'formRule'], $this); |
e6dda67a | 155 | $this->_action = CRM_Core_Action::ADVANCED; |
156 | foreach ($this->getSearchFieldMetadata() as $entity => $fields) { | |
157 | foreach ($fields as $fieldName => $fieldSpec) { | |
27cedb98 | 158 | if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || $fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { |
159 | $this->addDatePickerRange($fieldName, $fieldSpec['title'], ($fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME))); | |
0bcac7e7 | 160 | } |
161 | else { | |
162 | $this->addField($fieldName, ['entity' => $entity]); | |
163 | } | |
e6dda67a | 164 | } |
165 | } | |
166 | } | |
167 | ||
2307be08 | 168 | /** |
169 | * Global validation rules for the form. | |
170 | * | |
171 | * @param array $fields | |
172 | * Posted values of the form. | |
518fa0ee SL |
173 | * @param array $files |
174 | * @param object $form | |
2307be08 | 175 | * |
176 | * @return array | |
177 | * list of errors to be posted back to the form | |
178 | */ | |
179 | public static function formRule($fields, $files, $form) { | |
180 | $errors = []; | |
b9c90943 | 181 | if (!is_a($form, 'CRM_Core_Form_Search')) { |
182 | // So this gets hit with a form object when doing an activity date search from | |
183 | // advanced search, but a NULL object when doing a pledge search. | |
184 | return $errors; | |
185 | } | |
2307be08 | 186 | foreach ($form->getSearchFieldMetadata() as $entity => $spec) { |
187 | foreach ($spec as $fieldName => $fieldSpec) { | |
188 | if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || $fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { | |
8a6fde27 | 189 | if (!empty($fields[$fieldName . '_high']) && !empty($fields[$fieldName . '_low']) && empty($fields[$fieldName . '_relative'])) { |
2307be08 | 190 | if (strtotime($fields[$fieldName . '_low']) > strtotime($fields[$fieldName . '_high'])) { |
191 | $errors[$fieldName . '_low'] = ts('%1: Please check that your date range is in correct chronological order.', [1 => $fieldSpec['title']]); | |
192 | } | |
193 | } | |
194 | } | |
195 | } | |
196 | } | |
197 | return $errors; | |
198 | } | |
199 | ||
e6dda67a | 200 | /** |
201 | * Get the validation rule to apply to a function. | |
202 | * | |
203 | * Alphanumeric is designed to always be safe & for now we just return | |
204 | * that but in future we can use tighter rules for types like int, bool etc. | |
205 | * | |
206 | * @param string $entity | |
207 | * @param string $fieldName | |
208 | * | |
209 | * @return string | |
210 | */ | |
211 | protected function getValidationTypeForField($entity, $fieldName) { | |
212 | switch ($this->getSearchFieldMetadata()[$entity][$fieldName]['type']) { | |
213 | case CRM_Utils_Type::T_BOOLEAN: | |
214 | return 'Boolean'; | |
215 | ||
216 | case CRM_Utils_Type::T_INT: | |
217 | return 'CommaSeparatedIntegers'; | |
218 | ||
27cedb98 | 219 | case CRM_Utils_Type::T_DATE: |
220 | case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME: | |
221 | return 'Timestamp'; | |
222 | ||
e6dda67a | 223 | default: |
224 | return 'Alphanumeric'; | |
225 | } | |
226 | } | |
227 | ||
228 | /** | |
229 | * Get the defaults for the entity for any fields described in metadata. | |
230 | * | |
231 | * @param string $entity | |
232 | * | |
233 | * @return array | |
234 | */ | |
235 | protected function getEntityDefaults($entity) { | |
236 | $defaults = []; | |
237 | foreach ($this->getSearchFieldMetadata()[$entity] as $fieldSpec) { | |
238 | if (empty($_POST[$fieldSpec['name']])) { | |
239 | $value = CRM_Utils_Request::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($entity, $fieldSpec['name']), FALSE, NULL, 'GET'); | |
240 | if ($value !== FALSE) { | |
241 | $defaults[$fieldSpec['name']] = $value; | |
242 | } | |
27cedb98 | 243 | if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || ($fieldSpec['type'] === CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { |
244 | $low = CRM_Utils_Request::retrieveValue($fieldSpec['name'] . '_low', 'Timestamp', FALSE, NULL, 'GET'); | |
245 | $high = CRM_Utils_Request::retrieveValue($fieldSpec['name'] . '_high', 'Timestamp', FALSE, NULL, 'GET'); | |
246 | if ($low !== FALSE || $high !== FALSE) { | |
247 | $defaults[$fieldSpec['name'] . '_relative'] = 0; | |
248 | $defaults[$fieldSpec['name'] . '_low'] = $low ? date('Y-m-d H:i:s', strtotime($low)) : NULL; | |
249 | $defaults[$fieldSpec['name'] . '_high'] = $high ? date('Y-m-d H:i:s', strtotime($high)) : NULL; | |
250 | } | |
251 | } | |
e6dda67a | 252 | } |
253 | } | |
254 | return $defaults; | |
255 | } | |
256 | ||
7123f88c | 257 | /** |
258 | * Convert any submitted text fields to use 'like' rather than '=' as the operator. | |
259 | * | |
260 | * This excludes any with options. | |
261 | * | |
262 | * Note this will only pick up fields declared via metadata. | |
263 | */ | |
264 | protected function convertTextStringsToUseLikeOperator() { | |
a4755c1e | 265 | foreach (CRM_Utils_Array::value($this->getDefaultEntity(), $this->getSearchFieldMetadata(), []) as $fieldName => $field) { |
7123f88c | 266 | if (!empty($this->_formValues[$fieldName]) && empty($field['options']) && empty($field['pseudoconstant'])) { |
267 | if (in_array($field['type'], [CRM_Utils_Type::T_STRING, CRM_Utils_Type::T_TEXT])) { | |
268 | $this->_formValues[$fieldName] = ['LIKE' => CRM_Contact_BAO_Query::getWildCardedValue(TRUE, 'LIKE', $this->_formValues[$fieldName])]; | |
269 | } | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
8d36b801 | 274 | /** |
0955d6b9 | 275 | * Add checkboxes for each row plus a master checkbox. |
ad37ac8e | 276 | * |
277 | * @param array $rows | |
8d36b801 | 278 | */ |
00be9182 | 279 | public function addRowSelectors($rows) { |
be2fb01f | 280 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, ['class' => 'select-rows']); |
4126499f | 281 | if (!empty($rows)) { |
282 | foreach ($rows as $row) { | |
6eb91e49 | 283 | if (CRM_Utils_Array::value('checkbox', $row)) { |
be2fb01f | 284 | $this->addElement('checkbox', $row['checkbox'], NULL, NULL, ['class' => 'select-row']); |
6eb91e49 | 285 | } |
4126499f | 286 | } |
8d36b801 | 287 | } |
3efb5b86 | 288 | } |
34197a55 | 289 | |
44543184 | 290 | /** |
291 | * Add actions menu to search results form. | |
292 | * | |
293 | * @param array $tasks | |
294 | */ | |
295 | public function addTaskMenu($tasks) { | |
be2fb01f | 296 | $taskMetaData = []; |
44543184 | 297 | foreach ($tasks as $key => $task) { |
be2fb01f | 298 | $taskMetaData[$key] = ['title' => $task]; |
44543184 | 299 | } |
300 | parent::addTaskMenu($taskMetaData); | |
301 | } | |
302 | ||
e597fc33 DG |
303 | /** |
304 | * Add the sort-name field to the form. | |
305 | * | |
306 | * There is a setting to determine whether email is included in the search & we look this up to determine | |
307 | * which text to choose. | |
308 | * | |
309 | * Note that for translation purposes the full string works better than using 'prefix' hence we use override-able functions | |
310 | * to define the string. | |
311 | */ | |
312 | protected function addSortNameField() { | |
313 | $this->addElement( | |
314 | 'text', | |
315 | 'sort_name', | |
be2fb01f | 316 | civicrm_api3('setting', 'getvalue', ['name' => 'includeEmailInName', 'group' => 'Search Preferences']) ? $this->getSortNameLabelWithEmail() : $this->getSortNameLabelWithOutEmail(), |
e597fc33 DG |
317 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name') |
318 | ); | |
319 | } | |
320 | ||
321 | /** | |
322 | * Get the label for the sortName field if email searching is on. | |
323 | * | |
324 | * (email searching is a setting under search preferences). | |
325 | * | |
326 | * @return string | |
327 | */ | |
328 | protected function getSortNameLabelWithEmail() { | |
329 | return ts('Name or Email'); | |
330 | } | |
331 | ||
332 | /** | |
333 | * Get the label for the sortName field if email searching is off. | |
334 | * | |
335 | * (email searching is a setting under search preferences). | |
336 | * | |
337 | * @return string | |
338 | */ | |
339 | protected function getSortNameLabelWithOutEmail() { | |
340 | return ts('Name'); | |
341 | } | |
342 | ||
2ee21eaa CW |
343 | /** |
344 | * Explicitly declare the form context for addField(). | |
345 | */ | |
346 | public function getDefaultContext() { | |
347 | return 'search'; | |
348 | } | |
349 | ||
0573fd28 | 350 | /** |
351 | * Add generic fields that specify the contact. | |
352 | */ | |
353 | protected function addContactSearchFields() { | |
240b0e65 | 354 | if (!$this->isFormInViewOrEditMode()) { |
355 | return; | |
356 | } | |
0573fd28 | 357 | $this->addSortNameField(); |
358 | ||
359 | $this->_group = CRM_Core_PseudoConstant::nestedGroup(); | |
360 | if ($this->_group) { | |
361 | $this->add('select', 'group', $this->getGroupLabel(), $this->_group, FALSE, | |
be2fb01f | 362 | [ |
0573fd28 | 363 | 'id' => 'group', |
364 | 'multiple' => 'multiple', | |
365 | 'class' => 'crm-select2', | |
be2fb01f | 366 | ] |
0573fd28 | 367 | ); |
368 | } | |
369 | ||
370 | $contactTags = CRM_Core_BAO_Tag::getTags(); | |
371 | if ($contactTags) { | |
372 | $this->add('select', 'contact_tags', $this->getTagLabel(), $contactTags, FALSE, | |
be2fb01f | 373 | [ |
0573fd28 | 374 | 'id' => 'contact_tags', |
375 | 'multiple' => 'multiple', | |
376 | 'class' => 'crm-select2', | |
be2fb01f | 377 | ] |
0573fd28 | 378 | ); |
379 | } | |
be2fb01f | 380 | $this->addField('contact_type', ['entity' => 'Contact']); |
0573fd28 | 381 | |
382 | if (CRM_Core_Permission::check('access deleted contacts') && Civi::settings()->get('contact_undelete')) { | |
383 | $this->addElement('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)')); | |
384 | } | |
385 | ||
386 | } | |
387 | ||
09e47399 | 388 | /** |
389 | * we allow the controller to set force/reset externally, useful when we are being | |
390 | * driven by the wizard framework | |
391 | */ | |
64ffcefd | 392 | protected function loadStandardSearchOptionsFromUrl() { |
09e47399 | 393 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean'); |
394 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); | |
395 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
396 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'search'); | |
397 | $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this); | |
398 | $this->assign("context", $this->_context); | |
399 | } | |
400 | ||
401 | /** | |
402 | * Get user submitted values. | |
403 | * | |
404 | * Get it from controller only if form has been submitted, else preProcess has set this | |
405 | */ | |
64ffcefd | 406 | protected function loadFormValues() { |
09e47399 | 407 | if (!empty($_POST) && !$this->controller->isModal()) { |
408 | $this->_formValues = $this->controller->exportValues($this->_name); | |
409 | } | |
410 | else { | |
411 | $this->_formValues = $this->get('formValues'); | |
412 | } | |
413 | ||
414 | if (empty($this->_formValues)) { | |
415 | if (isset($this->_ssID)) { | |
416 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
417 | } | |
418 | } | |
419 | } | |
420 | ||
3efb5b86 | 421 | } |