Merge pull request #18631 from eileenmcnaughton/ppp
[civicrm-core.git] / CRM / Contact / Form / Search / Builder.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 * This class is for search builder processing.
6a488035
TO
20 */
21class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search {
22
23 /**
fe482240 24 * Number of columns in where.
6a488035
TO
25 *
26 * @var int
6a488035
TO
27 */
28 public $_columnCount;
29
30 /**
fe482240 31 * Number of blocks to be shown.
6a488035
TO
32 *
33 * @var int
6a488035
TO
34 */
35 public $_blockCount;
36
37 /**
fe482240 38 * Build the form object.
6a488035
TO
39 */
40 public function preProcess() {
2d09a0c3 41 // SearchFormName is deprecated & to be removed - the replacement is for the task to
42 // call $this->form->getSearchFormValues()
43 // A couple of extensions use it.
6a488035
TO
44 $this->set('searchFormName', 'Builder');
45
46 $this->set('context', 'builder');
47 parent::preProcess();
48
49 // Get the block count
50 $this->_blockCount = $this->get('blockCount');
51 // Initialize new form
52 if (!$this->_blockCount) {
53 $this->_blockCount = 4;
54 $this->set('newBlock', 1);
55 }
56
57 //get the column count
58 $this->_columnCount = $this->get('columnCount');
59
60 for ($i = 1; $i < $this->_blockCount; $i++) {
61 if (empty($this->_columnCount[$i])) {
62 $this->_columnCount[$i] = 5;
63 }
64 }
65
66 $this->_loadedMappingId = $this->get('savedMapping');
67
68 if ($this->get('showSearchForm')) {
69 $this->assign('showSearchForm', TRUE);
70 }
71 else {
72 $this->assign('showSearchForm', FALSE);
73 }
74 }
75
e8e8f3ad 76 /**
77 * Build quick form.
78 */
6a488035
TO
79 public function buildQuickForm() {
80 $fields = self::fields();
be2fb01f 81 $searchByLabelFields = [];
80beace7 82 // This array contain list of available fields and their corresponding data type,
83 // later assigned as json string, to be used to filter list of mysql operators
84 $fieldNameTypes = [];
6a488035 85 foreach ($fields as $name => $field) {
80beace7 86 // Assign date type to respective field name, which will be later used to modify operator list
291db41c 87 $fieldNameTypes[$name] = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $field));
15c80835
UP
88 // it's necessary to know which of the fields are searchable by label
89 if (isset($field['searchByLabel']) && $field['searchByLabel']) {
ed56d481 90 $searchByLabelFields[] = $name;
15c80835 91 }
6a488035
TO
92 }
93 // Add javascript
94 CRM_Core_Resources::singleton()
96ed17aa 95 ->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Search/Builder.js', 1, 'html-header')
be2fb01f
CW
96 ->addSetting([
97 'searchBuilder' => [
6a488035
TO
98 // Index of newly added/expanded block (1-based index)
99 'newBlock' => $this->get('newBlock'),
6a488035 100 'fieldOptions' => self::fieldOptions(),
15c80835 101 'searchByLabelFields' => $searchByLabelFields,
80beace7 102 'fieldTypes' => $fieldNameTypes,
be2fb01f
CW
103 'generalOperators' => ['' => ts('-operator-')] + CRM_Core_SelectValues::getSearchBuilderOperators(),
104 ],
105 ]);
6a488035
TO
106 //get the saved search mapping id
107 $mappingId = NULL;
108 if ($this->_ssID) {
109 $mappingId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id');
110 }
111
46770058 112 CRM_Core_BAO_Mapping::buildMappingForm($this, $mappingId, $this->_columnCount, $this->_blockCount);
6a488035
TO
113
114 parent::buildQuickForm();
115 }
116
117 /**
fe482240 118 * Add local and global form rules.
6a488035 119 */
00be9182 120 public function addRules() {
be2fb01f 121 $this->addFormRule(['CRM_Contact_Form_Search_Builder', 'formRule'], $this);
6a488035
TO
122 }
123
124 /**
fe482240 125 * Global validation rules for the form.
6a488035 126 *
5a409b50 127 * @param array $values
128 * @param array $files
129 * @param CRM_Core_Form $self
fd31fa4c 130 *
a6c01b45
CW
131 * @return array
132 * list of errors to be posted back to the form
6a488035 133 */
00be9182 134 public static function formRule($values, $files, $self) {
8cc574cf 135 if (!empty($values['addMore']) || !empty($values['addBlock'])) {
6a488035
TO
136 return TRUE;
137 }
138 $fields = self::fields();
139 $fld = CRM_Core_BAO_Mapping::formattedFields($values, TRUE);
140
be2fb01f 141 $errorMsg = [];
6a488035
TO
142 foreach ($fld as $k => $v) {
143 if (!$v[1]) {
144 $errorMsg["operator[$v[3]][$v[4]]"] = ts("Please enter the operator.");
145 }
146 else {
147 // CRM-10338
148 $v[2] = self::checkArrayKeyEmpty($v[2]);
149
be2fb01f 150 if (in_array($v[1], [
69078420
SL
151 'IS NULL',
152 'IS NOT NULL',
153 'IS EMPTY',
154 'IS NOT EMPTY',
155 ]) && !empty($v[2])) {
be2fb01f 156 $errorMsg["value[$v[3]][$v[4]]"] = ts('Please clear your value if you want to use %1 operator.', [1 => $v[1]]);
6a488035 157 }
6a488035
TO
158 elseif (substr($v[0], 0, 7) === 'do_not_' or substr($v[0], 0, 3) === 'is_') {
159 if (isset($v[2])) {
be2fb01f 160 $v2 = [$v[2]];
6a488035
TO
161 if (!isset($v[2])) {
162 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value.");
163 }
164
165 $error = CRM_Utils_Type::validate($v2[0], 'Integer', FALSE);
166 if ($error != $v2[0]) {
167 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value.");
168 }
169 }
170 else {
171 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value.");
172 }
173 }
174 else {
175 if (substr($v[0], 0, 7) == 'custom_') {
442df34b
CW
176 // Get rid of appended location type id
177 list($fieldKey) = explode('-', $v[0]);
178 $type = $fields[$fieldKey]['data_type'];
6a488035
TO
179
180 // hack to handle custom data of type state and country
be2fb01f 181 if (in_array($type, [
353ffa53 182 'Country',
408b79bf 183 'StateProvince',
be2fb01f 184 ])) {
6a488035
TO
185 $type = "Integer";
186 }
187 }
188 else {
189 $fldName = $v[0];
190 // FIXME: no idea at this point what to do with this,
191 // FIXME: but definitely needs fixing.
192 if (substr($v[0], 0, 13) == 'contribution_') {
193 $fldName = substr($v[0], 13);
194 }
195
9c1bc317
CW
196 $fldValue = $fields[$fldName] ?? NULL;
197 $fldType = $fldValue['type'] ?? NULL;
6a488035 198 $type = CRM_Utils_Type::typeToString($fldType);
fbc6a4d4 199
200 if (strstr($v[1], 'IN')) {
201 if (empty($v[2])) {
202 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value.");
203 }
204 }
6a488035 205 // Check Empty values for Integer Or Boolean Or Date type For operators other than IS NULL and IS NOT NULL.
fbc6a4d4 206 elseif (!in_array($v[1],
be2fb01f 207 ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])
353ffa53 208 ) {
dbc6f6d6 209 if ((($type == 'Int' || $type == 'Boolean') && !is_array($v[2]) && !trim($v[2])) && $v[2] != '0') {
6a488035
TO
210 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value.");
211 }
212 elseif ($type == 'Date' && !trim($v[2])) {
213 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value.");
214 }
215 }
216 }
217
218 if ($type && empty($errorMsg)) {
219 // check for valid format while using IN Operator
fbc6a4d4 220 if (strstr($v[1], 'IN')) {
dbc6f6d6 221 if (!is_array($v[2])) {
222 $inVal = trim($v[2]);
223 //checking for format to avoid db errors
224 if ($type == 'Int') {
fccb6a0f 225 if (!preg_match('/^[A-Za-z0-9\,]+$/', $inVal)) {
dbc6f6d6 226 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format).");
227 }
6a488035 228 }
dbc6f6d6 229 else {
fccb6a0f 230 if (!preg_match('/^[A-Za-z0-9åäöÅÄÖüÜœŒæÆøØ()\,\s]+$/', $inVal)) {
dbc6f6d6 231 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format).");
232 }
6a488035
TO
233 }
234 }
235
236 // Validate each value in parenthesis to avoid db errors
237 if (empty($errorMsg)) {
be2fb01f 238 $parenValues = [];
efb88612 239 $parenValues = is_array($v[2]) ? (array_key_exists($v[1], $v[2])) ? $v[2][$v[1]] : $v[2] : explode(',', trim($inVal, "(..)"));
6a488035 240 foreach ($parenValues as $val) {
fbc6a4d4 241 if ($type == 'Date' || $type == 'Timestamp') {
242 $val = CRM_Utils_Date::processDate($val);
243 if ($type == 'Date') {
244 $val = substr($val, 0, 8);
245 }
246 }
247 else {
248 $val = trim($val);
249 }
6a488035
TO
250 if (!$val && $val != '0') {
251 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter the values correctly.");
252 }
253 if (empty($errorMsg)) {
254 $error = CRM_Utils_Type::validate($val, $type, FALSE);
255 if ($error != $val) {
256 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value.");
257 }
258 }
259 }
260 }
261 }
262 elseif (trim($v[2])) {
263 //else check value for rest of the Operators
291db41c
CW
264 if ($type == 'Date' || $type == 'Timestamp') {
265 $v[2] = CRM_Utils_Date::processDate($v[2]);
266 if ($type == 'Date') {
267 $v[2] = substr($v[2], 0, 8);
268 }
269 }
6a488035
TO
270 $error = CRM_Utils_Type::validate($v[2], $type, FALSE);
271 if ($error != $v[2]) {
272 $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value.");
273 }
274 }
275 }
276 }
277 }
278 }
279
280 if (!empty($errorMsg)) {
281 $self->set('showSearchForm', TRUE);
282 $self->assign('rows', NULL);
283 return $errorMsg;
284 }
285
286 return TRUE;
287 }
288
e8e8f3ad 289 /**
290 * Normalise form values.
291 */
6ea503d4
TO
292 public function normalizeFormValues() {
293 }
6a488035 294
86538308 295 /**
e8e8f3ad 296 * Convert form values.
297 *
298 * @param array $formValues
86538308
EM
299 *
300 * @return array
301 */
b9e573d4 302 public function convertFormValues(&$formValues) {
6a488035
TO
303 return CRM_Core_BAO_Mapping::formattedFields($formValues);
304 }
305
86538308 306 /**
e8e8f3ad 307 * Get return properties.
308 *
86538308
EM
309 * @return array
310 */
6a488035
TO
311 public function &returnProperties() {
312 return CRM_Core_BAO_Mapping::returnProperties($this->_formValues);
313 }
314
315 /**
fe482240 316 * Process the uploaded file.
6a488035
TO
317 */
318 public function postProcess() {
319 $this->set('isAdvanced', '2');
320 $this->set('isSearchBuilder', '1');
321 $this->set('showSearchForm', FALSE);
322
323 $params = $this->controller->exportValues($this->_name);
324 if (!empty($params)) {
325 // Add another block
326 if (!empty($params['addBlock'])) {
327 $this->set('newBlock', $this->_blockCount);
328 $this->_blockCount += 3;
329 $this->set('blockCount', $this->_blockCount);
330 $this->set('showSearchForm', TRUE);
331 return;
332 }
333 // Add another field
9c1bc317 334 $addMore = $params['addMore'] ?? NULL;
6a488035
TO
335 for ($x = 1; $x <= $this->_blockCount; $x++) {
336 if (!empty($addMore[$x])) {
353ffa53 337 $this->set('newBlock', $x);
6a488035
TO
338 $this->_columnCount[$x] = $this->_columnCount[$x] + 5;
339 $this->set('columnCount', $this->_columnCount);
340 $this->set('showSearchForm', TRUE);
341 return;
342 }
343 }
344 $this->set('newBlock', NULL);
345 $checkEmpty = NULL;
346 foreach ($params['mapper'] as $key => $value) {
347 foreach ($value as $k => $v) {
348 if ($v[0]) {
349 $checkEmpty++;
350 }
351 }
352 }
353
354 if (!$checkEmpty) {
355 $this->set('newBlock', 1);
356 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/search/builder', '_qf_Builder_display=true'));
357 }
358 }
359
360 // get user submitted values
361 // get it from controller only if form has been submitted, else preProcess has set this
362 if (!empty($_POST)) {
363 $this->_formValues = $this->controller->exportValues($this->_name);
364
365 // set the group if group is submitted
a7488080 366 if (!empty($this->_formValues['uf_group_id'])) {
6a488035
TO
367 $this->set('id', $this->_formValues['uf_group_id']);
368 }
369 else {
370 $this->set('id', '');
371 }
372 }
373
374 // we dont want to store the sortByCharacter in the formValue, it is more like
375 // a filter on the result set
376 // this filter is reset if we click on the search button
377 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
378 if (strtolower($this->_sortByCharacter) == 'all') {
379 $this->_formValues['sortByCharacter'] = NULL;
380 }
381 else {
382 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
383 }
384 }
e166ff79
CW
385 else {
386 $this->_sortByCharacter = NULL;
387 }
6a488035 388
b9e573d4 389 $this->_params = $this->convertFormValues($this->_formValues);
6a488035
TO
390 $this->_returnProperties = &$this->returnProperties();
391
392 // CRM-10338 check if value is empty array
393 foreach ($this->_params as $k => $v) {
394 $this->_params[$k][2] = self::checkArrayKeyEmpty($v[2]);
395 }
396
397 parent::postProcess();
398 }
399
86538308 400 /**
e8e8f3ad 401 * Get fields.
402 *
86538308
EM
403 * @return array
404 */
00be9182 405 public static function fields() {
e1ab2e91 406 $fields = array_merge(
6a488035
TO
407 CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE),
408 CRM_Core_Component::getQueryFields(),
eb1e3589 409 CRM_Contact_BAO_Query_Hook::singleton()->getFields(),
6a488035
TO
410 CRM_Activity_BAO_Activity::exportableFields()
411 );
e1ab2e91 412 return $fields;
6a488035
TO
413 }
414
415 /**
416 * CRM-9434 Hackish function to fetch fields with options.
e8e8f3ad 417 *
6a488035 418 * FIXME: When our core fields contain reliable metadata this will be much simpler.
a6c01b45
CW
419 * @return array
420 * (string => string) key: field_name value: api entity name
408b79bf 421 * Note: options are fetched via ajax using the api "getoptions" method
6a488035 422 */
00be9182 423 public static function fieldOptions() {
6a488035
TO
424 // Hack to add options not retrieved by getfields
425 // This list could go on and on, but it would be better to fix getfields
be2fb01f 426 $options = [
e354351f 427 'group' => 'group_contact',
428 'tag' => 'entity_tag',
6a488035
TO
429 'on_hold' => 'yesno',
430 'is_bulkmail' => 'yesno',
6a488035
TO
431 'payment_instrument' => 'contribution',
432 'membership_status' => 'membership',
433 'membership_type' => 'membership',
e5d696ef 434 'member_campaign_id' => 'membership',
67744c4e
CW
435 'member_is_test' => 'yesno',
436 'member_is_pay_later' => 'yesno',
437 'is_override' => 'yesno',
be2fb01f
CW
438 ];
439 $entities = [
353ffa53
TO
440 'contact',
441 'address',
442 'activity',
443 'participant',
444 'pledge',
445 'member',
446 'contribution',
447 'case',
408b79bf 448 'grant',
be2fb01f 449 ];
6a5f199e 450 CRM_Contact_BAO_Query_Hook::singleton()->alterSearchBuilderOptions($entities, $options);
6a488035 451 foreach ($entities as $entity) {
67744c4e 452 $fields = civicrm_api3($entity, 'getfields');
6a488035 453 foreach ($fields['values'] as $field => $info) {
4b5ff63c 454 if (!empty($info['options']) || !empty($info['pseudoconstant']) || !empty($info['option_group_id'])) {
6a488035 455 $options[$field] = $entity;
e61022d7 456 // Hack for when search field doesn't match db field - e.g. "country" instead of "country_id"
6a488035
TO
457 if (substr($field, -3) == '_id') {
458 $options[substr($field, 0, -3)] = $entity;
459 }
460 }
89d4a22f 461 elseif (!empty($info['data_type'])) {
be2fb01f 462 if (in_array($info['data_type'], ['StateProvince', 'Country'])) {
89d4a22f 463 $options[$field] = $entity;
464 }
e61022d7 465 }
be2fb01f 466 elseif (in_array(substr($field, 0, 3), [
69078420
SL
467 'is_',
468 'do_',
469 ]) || CRM_Utils_Array::value('data_type', $info) == 'Boolean'
353ffa53 470 ) {
6a488035
TO
471 $options[$field] = 'yesno';
472 if ($entity != 'contact') {
473 $options[$entity . '_' . $field] = 'yesno';
474 }
475 }
476 elseif (strpos($field, '_is_')) {
477 $options[$field] = 'yesno';
478 }
479 }
480 }
481 return $options;
482 }
483
484 /**
ea3ddccf 485 * CRM-10338 tags and groups use array keys for selection list.
486 *
6a488035 487 * if using IS NULL/NOT NULL, an array with no array key is created
e60f24eb 488 * convert that to simple NULL so processing can proceed
ea3ddccf 489 *
490 * @param string $val
491 *
492 * @return null
6a488035 493 */
00be9182 494 public static function checkArrayKeyEmpty($val) {
6a488035 495 if (is_array($val)) {
4eeb9a5b 496 $v2empty = TRUE;
6a488035
TO
497 foreach ($val as $vk => $vv) {
498 if (!empty($vk)) {
ab8a593e 499 $v2empty = FALSE;
6a488035
TO
500 }
501 }
502 if ($v2empty) {
e60f24eb 503 $val = NULL;
6a488035
TO
504 }
505 }
506 return $val;
507 }
96025800 508
6a488035 509}