Commit | Line | Data |
---|---|---|
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 | /** | |
dc195289 | 19 | * This is the heart of the search query building mechanism. |
6a488035 TO |
20 | */ |
21 | class CRM_Contact_BAO_Query { | |
22 | ||
23 | /** | |
fe482240 | 24 | * The various search modes. |
6a488035 | 25 | * |
746aa919 CB |
26 | * As of February 2017, entries not present for 4, 32, 64, 1024. |
27 | * | |
28 | * MODE_ALL seems to be out of sync with the available constants; | |
29 | * if this is intentionally excluding MODE_MAILING then that may | |
30 | * bear documenting? | |
31 | * | |
32 | * Likewise if there's reason for the missing modes (4, 32, 64 etc). | |
33 | * | |
6a488035 TO |
34 | * @var int |
35 | */ | |
7da04cde | 36 | const |
33092c89 | 37 | NO_RETURN_PROPERTIES = 'CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES', |
6a488035 TO |
38 | MODE_CONTACTS = 1, |
39 | MODE_CONTRIBUTE = 2, | |
746aa919 | 40 | // There is no 4, |
6a488035 TO |
41 | MODE_MEMBER = 8, |
42 | MODE_EVENT = 16, | |
eda34f9b MW |
43 | MODE_CONTACTSRELATED = 32, |
44 | // no 64. | |
6a488035 TO |
45 | MODE_GRANT = 128, |
46 | MODE_PLEDGEBANK = 256, | |
47 | MODE_PLEDGE = 512, | |
746aa919 | 48 | // There is no 1024, |
6a488035 | 49 | MODE_CASE = 2048, |
6a488035 TO |
50 | MODE_ACTIVITY = 4096, |
51 | MODE_CAMPAIGN = 8192, | |
746aa919 CB |
52 | MODE_MAILING = 16384, |
53 | MODE_ALL = 17407; | |
6a488035 | 54 | |
eda34f9b MW |
55 | /** |
56 | * Constants for search operators | |
57 | */ | |
58 | const | |
59 | SEARCH_OPERATOR_AND = 'AND', | |
60 | SEARCH_OPERATOR_OR = 'OR'; | |
61 | ||
6a488035 | 62 | /** |
fe482240 | 63 | * The default set of return properties. |
6a488035 TO |
64 | * |
65 | * @var array | |
6a488035 | 66 | */ |
b832662c | 67 | public static $_defaultReturnProperties; |
6a488035 TO |
68 | |
69 | /** | |
fe482240 | 70 | * The default set of hier return properties. |
6a488035 TO |
71 | * |
72 | * @var array | |
6a488035 | 73 | */ |
69078420 | 74 | public static $_defaultHierReturnProperties; |
6a488035 TO |
75 | |
76 | /** | |
fe482240 | 77 | * The set of input params. |
6a488035 TO |
78 | * |
79 | * @var array | |
80 | */ | |
81 | public $_params; | |
82 | ||
83 | public $_cfIDs; | |
84 | ||
85 | public $_paramLookup; | |
86 | ||
ac4985d5 | 87 | public $_sort; |
746aa919 | 88 | |
6a488035 | 89 | /** |
100fef9d | 90 | * The set of output params |
6a488035 TO |
91 | * |
92 | * @var array | |
93 | */ | |
94 | public $_returnProperties; | |
95 | ||
96 | /** | |
100fef9d | 97 | * The select clause |
6a488035 TO |
98 | * |
99 | * @var array | |
100 | */ | |
101 | public $_select; | |
102 | ||
103 | /** | |
100fef9d | 104 | * The name of the elements that are in the select clause |
746aa919 | 105 | * used to extract the values. |
6a488035 TO |
106 | * |
107 | * @var array | |
108 | */ | |
109 | public $_element; | |
110 | ||
111 | /** | |
746aa919 | 112 | * The tables involved in the query. |
6a488035 TO |
113 | * |
114 | * @var array | |
115 | */ | |
116 | public $_tables; | |
117 | ||
118 | /** | |
746aa919 | 119 | * The table involved in the where clause. |
6a488035 TO |
120 | * |
121 | * @var array | |
122 | */ | |
123 | public $_whereTables; | |
124 | ||
125 | /** | |
746aa919 | 126 | * Array of WHERE clause components. |
6a488035 TO |
127 | * |
128 | * @var array | |
129 | */ | |
130 | public $_where; | |
131 | ||
132 | /** | |
746aa919 | 133 | * The WHERE clause as a string. |
6a488035 TO |
134 | * |
135 | * @var string | |
6a488035 TO |
136 | */ |
137 | public $_whereClause; | |
138 | ||
139 | /** | |
746aa919 | 140 | * Additional WHERE clause for permissions. |
6a488035 TO |
141 | * |
142 | * @var string | |
6a488035 TO |
143 | */ |
144 | public $_permissionWhereClause; | |
a30e57e0 | 145 | |
6a488035 | 146 | /** |
100fef9d | 147 | * The from string |
6a488035 TO |
148 | * |
149 | * @var string | |
6a488035 TO |
150 | */ |
151 | public $_fromClause; | |
152 | ||
153 | /** | |
100fef9d | 154 | * Additional permission from clause |
6a488035 TO |
155 | * |
156 | * @var string | |
6a488035 TO |
157 | */ |
158 | public $_permissionFromClause; | |
159 | ||
160 | /** | |
100fef9d | 161 | * The from clause for the simple select and alphabetical |
6a488035 TO |
162 | * select |
163 | * | |
164 | * @var string | |
165 | */ | |
166 | public $_simpleFromClause; | |
167 | ||
168 | /** | |
100fef9d | 169 | * The having values |
6a488035 | 170 | * |
b832662c | 171 | * @var array |
6a488035 TO |
172 | */ |
173 | public $_having; | |
174 | ||
175 | /** | |
176 | * The english language version of the query | |
177 | * | |
178 | * @var array | |
179 | */ | |
180 | public $_qill; | |
181 | ||
182 | /** | |
183 | * All the fields that could potentially be involved in | |
184 | * this query | |
185 | * | |
186 | * @var array | |
187 | */ | |
188 | public $_fields; | |
189 | ||
714ab070 | 190 | /** |
191 | * Fields hacked for legacy reasons. | |
192 | * | |
193 | * Generally where a field has a option group defining it's options we add them to | |
194 | * the fields array as pseudofields - eg for gender we would add the key 'gender' to fields | |
195 | * using CRM_Core_DAO::appendPseudoConstantsToFields($fields); | |
196 | * | |
197 | * The rendered results would hold an id in the gender_id field and the label in the pseudo 'Gender' | |
198 | * field. The heading for the pseudofield would come form the the option group name & for the id field | |
199 | * from the xml. | |
200 | * | |
201 | * These fields are handled in a more legacy way - ie overwriting 'gender_id' with the label on output | |
202 | * via the convertToPseudoNames function. Ideally we would convert them but they would then need to be fixed | |
203 | * in some other places & there are also some issues around the name (ie. Gender currently has the label in the | |
204 | * schema 'Gender' so adding a second 'Gender' field to search builder & export would be confusing and the standard is | |
205 | * not fully agreed here. | |
206 | * | |
207 | * @var array | |
208 | */ | |
209 | protected $legacyHackedFields = [ | |
210 | 'gender_id' => 'gender', | |
211 | 'prefix_id' => 'individual_prefix', | |
212 | 'suffix_id' => 'individual_suffix', | |
213 | 'communication_style_id' => 'communication_style', | |
214 | ]; | |
215 | ||
6a488035 | 216 | /** |
fe482240 | 217 | * Are we in search mode. |
6a488035 | 218 | * |
b67daa72 | 219 | * @var bool |
6a488035 TO |
220 | */ |
221 | public $_search = TRUE; | |
222 | ||
223 | /** | |
fe482240 | 224 | * Should we skip permission checking. |
6a488035 | 225 | * |
b67daa72 | 226 | * @var bool |
6a488035 TO |
227 | */ |
228 | public $_skipPermission = FALSE; | |
229 | ||
230 | /** | |
fe482240 | 231 | * Should we skip adding of delete clause. |
6a488035 | 232 | * |
b67daa72 | 233 | * @var bool |
6a488035 TO |
234 | */ |
235 | public $_skipDeleteClause = FALSE; | |
236 | ||
237 | /** | |
100fef9d | 238 | * Are we in strict mode (use equality over LIKE) |
6a488035 | 239 | * |
b67daa72 | 240 | * @var bool |
6a488035 TO |
241 | */ |
242 | public $_strict = FALSE; | |
243 | ||
244 | /** | |
fe482240 | 245 | * What operator to use to group the clauses. |
6a488035 TO |
246 | * |
247 | * @var string | |
248 | */ | |
249 | public $_operator = 'AND'; | |
250 | ||
251 | public $_mode = 1; | |
252 | ||
253 | /** | |
fe482240 | 254 | * Should we only search on primary location. |
6a488035 | 255 | * |
b67daa72 | 256 | * @var bool |
6a488035 TO |
257 | */ |
258 | public $_primaryLocation = TRUE; | |
259 | ||
260 | /** | |
fe482240 | 261 | * Are contact ids part of the query. |
6a488035 | 262 | * |
b67daa72 | 263 | * @var bool |
6a488035 TO |
264 | */ |
265 | public $_includeContactIds = FALSE; | |
266 | ||
267 | /** | |
fe482240 | 268 | * Should we use the smart group cache. |
6a488035 | 269 | * |
b67daa72 | 270 | * @var bool |
6a488035 TO |
271 | */ |
272 | public $_smartGroupCache = TRUE; | |
273 | ||
274 | /** | |
fe482240 | 275 | * Should we display contacts with a specific relationship type. |
6a488035 TO |
276 | * |
277 | * @var string | |
278 | */ | |
b832662c | 279 | public $_displayRelationshipType; |
6a488035 TO |
280 | |
281 | /** | |
fe482240 | 282 | * Reference to the query object for custom values. |
6a488035 TO |
283 | * |
284 | * @var Object | |
285 | */ | |
286 | public $_customQuery; | |
287 | ||
288 | /** | |
100fef9d | 289 | * Should we enable the distinct clause, used if we are including |
6a488035 TO |
290 | * more than one group |
291 | * | |
b67daa72 | 292 | * @var bool |
6a488035 TO |
293 | */ |
294 | public $_useDistinct = FALSE; | |
295 | ||
296 | /** | |
297 | * Should we just display one contact record | |
69078420 | 298 | * @var bool |
6a488035 TO |
299 | */ |
300 | public $_useGroupBy = FALSE; | |
301 | ||
302 | /** | |
100fef9d | 303 | * The relationship type direction |
6a488035 TO |
304 | * |
305 | * @var array | |
6a488035 | 306 | */ |
69078420 | 307 | public static $_relType; |
6a488035 TO |
308 | |
309 | /** | |
100fef9d | 310 | * The activity role |
6a488035 TO |
311 | * |
312 | * @var array | |
6a488035 | 313 | */ |
69078420 | 314 | public static $_activityRole; |
6a488035 TO |
315 | |
316 | /** | |
317 | * Consider the component activity type | |
318 | * during activity search. | |
319 | * | |
320 | * @var array | |
6a488035 | 321 | */ |
69078420 | 322 | public static $_considerCompActivities; |
6a488035 TO |
323 | |
324 | /** | |
325 | * Consider with contact activities only, | |
326 | * during activity search. | |
327 | * | |
328 | * @var array | |
6a488035 | 329 | */ |
69078420 | 330 | public static $_withContactActivitiesOnly; |
6a488035 TO |
331 | |
332 | /** | |
100fef9d | 333 | * Use distinct component clause for component searches |
6a488035 TO |
334 | * |
335 | * @var string | |
336 | */ | |
337 | public $_distinctComponentClause; | |
338 | ||
3dbf477c DS |
339 | public $_rowCountClause; |
340 | ||
6a488035 | 341 | /** |
100fef9d | 342 | * Use groupBy component clause for component searches |
6a488035 TO |
343 | * |
344 | * @var string | |
345 | */ | |
346 | public $_groupByComponentClause; | |
347 | ||
348 | /** | |
349 | * Track open panes, useful in advance search | |
350 | * | |
351 | * @var array | |
6a488035 | 352 | */ |
be2fb01f | 353 | public static $_openedPanes = []; |
6a488035 | 354 | |
442df34b CW |
355 | /** |
356 | * For search builder - which custom fields are location-dependent | |
357 | * @var array | |
358 | */ | |
be2fb01f | 359 | public $_locationSpecificCustomFields = []; |
442df34b | 360 | |
6a488035 TO |
361 | /** |
362 | * The tables which have a dependency on location and/or address | |
363 | * | |
364 | * @var array | |
6a488035 | 365 | */ |
69078420 | 366 | public static $_dependencies = [ |
6a488035 TO |
367 | 'civicrm_state_province' => 1, |
368 | 'civicrm_country' => 1, | |
369 | 'civicrm_county' => 1, | |
370 | 'civicrm_address' => 1, | |
371 | 'civicrm_location_type' => 1, | |
be2fb01f | 372 | ]; |
6a488035 TO |
373 | |
374 | /** | |
fe482240 | 375 | * List of location specific fields. |
69078420 | 376 | * @var array |
6a488035 | 377 | */ |
69078420 | 378 | public static $_locationSpecificFields = [ |
6a488035 TO |
379 | 'street_address', |
380 | 'street_number', | |
381 | 'street_name', | |
382 | 'street_unit', | |
383 | 'supplemental_address_1', | |
384 | 'supplemental_address_2', | |
207f62c6 | 385 | 'supplemental_address_3', |
6a488035 TO |
386 | 'city', |
387 | 'postal_code', | |
388 | 'postal_code_suffix', | |
389 | 'geo_code_1', | |
390 | 'geo_code_2', | |
391 | 'state_province', | |
392 | 'country', | |
393 | 'county', | |
394 | 'phone', | |
395 | 'email', | |
396 | 'im', | |
397 | 'address_name', | |
994a070c | 398 | 'master_id', |
8a60e058 | 399 | 'location_type', |
be2fb01f | 400 | ]; |
6a488035 TO |
401 | |
402 | /** | |
4e6072d5 | 403 | * Remember if we handle either end of a number or date range |
6a488035 | 404 | * so we can skip the other |
69078420 | 405 | * @var array |
6a488035 | 406 | */ |
be2fb01f | 407 | protected $_rangeCache = []; |
d9ab802d | 408 | /** |
b67daa72 | 409 | * Set to true when $this->relationship is run to avoid adding twice. |
410 | * | |
411 | * @var bool | |
d9ab802d | 412 | */ |
ad682134 | 413 | protected $_relationshipValuesAdded = FALSE; |
d9ab802d | 414 | |
7c96365b | 415 | /** |
9f266042 | 416 | * Set to the name of the temp table if one has been created. |
417 | * | |
418 | * @var string | |
7c96365b | 419 | */ |
b832662c | 420 | public static $_relationshipTempTable; |
d9ab802d | 421 | |
be2fb01f | 422 | public $_pseudoConstantsSelect = []; |
9131c564 | 423 | |
b832662c | 424 | public $_groupUniqueKey; |
e24f9388 | 425 | public $_groupKeys = []; |
ed17b5e2 | 426 | |
6a488035 | 427 | /** |
fe482240 | 428 | * Class constructor which also does all the work. |
6a488035 | 429 | * |
2a6da8d7 EM |
430 | * @param array $params |
431 | * @param array $returnProperties | |
432 | * @param array $fields | |
77c5b619 TO |
433 | * @param bool $includeContactIds |
434 | * @param bool $strict | |
2a6da8d7 | 435 | * @param bool|int $mode - mode the search is operating on |
6a488035 | 436 | * |
2a6da8d7 EM |
437 | * @param bool $skipPermission |
438 | * @param bool $searchDescendentGroups | |
439 | * @param bool $smartGroupCache | |
440 | * @param null $displayRelationshipType | |
441 | * @param string $operator | |
66670e4d | 442 | * @param string $apiEntity |
041ecc95 | 443 | * @param bool|null $primaryLocationOnly |
e14c912f | 444 | * |
445 | * @throws \CRM_Core_Exception | |
6a488035 | 446 | */ |
59f4c9ee | 447 | public function __construct( |
6a488035 TO |
448 | $params = NULL, $returnProperties = NULL, $fields = NULL, |
449 | $includeContactIds = FALSE, $strict = FALSE, $mode = 1, | |
450 | $skipPermission = FALSE, $searchDescendentGroups = TRUE, | |
451 | $smartGroupCache = TRUE, $displayRelationshipType = NULL, | |
66670e4d | 452 | $operator = 'AND', |
0606198b | 453 | $apiEntity = NULL, |
454 | $primaryLocationOnly = NULL | |
6a488035 | 455 | ) { |
0606198b | 456 | if ($primaryLocationOnly === NULL) { |
457 | $primaryLocationOnly = Civi::settings()->get('searchPrimaryDetailsOnly'); | |
458 | } | |
459 | $this->_primaryLocation = $primaryLocationOnly; | |
6a488035 TO |
460 | $this->_params = &$params; |
461 | if ($this->_params == NULL) { | |
be2fb01f | 462 | $this->_params = []; |
6a488035 TO |
463 | } |
464 | ||
33092c89 | 465 | if ($returnProperties === self::NO_RETURN_PROPERTIES) { |
be2fb01f | 466 | $this->_returnProperties = []; |
33092c89 SB |
467 | } |
468 | elseif (empty($returnProperties)) { | |
6a488035 TO |
469 | $this->_returnProperties = self::defaultReturnProperties($mode); |
470 | } | |
471 | else { | |
472 | $this->_returnProperties = &$returnProperties; | |
473 | } | |
474 | ||
475 | $this->_includeContactIds = $includeContactIds; | |
476 | $this->_strict = $strict; | |
477 | $this->_mode = $mode; | |
478 | $this->_skipPermission = $skipPermission; | |
479 | $this->_smartGroupCache = $smartGroupCache; | |
480 | $this->_displayRelationshipType = $displayRelationshipType; | |
481 | $this->setOperator($operator); | |
482 | ||
483 | if ($fields) { | |
484 | $this->_fields = &$fields; | |
485 | $this->_search = FALSE; | |
486 | $this->_skipPermission = TRUE; | |
487 | } | |
488 | else { | |
5837835b | 489 | $this->_fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE, TRUE, FALSE, !$skipPermission); |
714ab070 | 490 | // The legacy hacked fields will output as a string rather than their underlying type. |
491 | foreach (array_keys($this->legacyHackedFields) as $fieldName) { | |
492 | $this->_fields[$fieldName]['type'] = CRM_Utils_Type::T_STRING; | |
493 | } | |
41b8dd1d | 494 | $relationMetadata = CRM_Contact_BAO_Relationship::fields(); |
495 | $relationFields = array_intersect_key($relationMetadata, array_fill_keys(['relationship_start_date', 'relationship_end_date'], 1)); | |
7d8f464a | 496 | // No good option other than hard-coding metadata for this 'special' field in. |
497 | $relationFields['relation_active_period_date'] = [ | |
498 | 'name' => 'relation_active_period_date', | |
499 | 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, | |
500 | 'title' => ts('Active Period'), | |
501 | 'table_name' => 'civicrm_relationship', | |
502 | 'where' => 'civicrm_relationship.start_date', | |
503 | 'where_end' => 'civicrm_relationship.end_date', | |
504 | 'html' => ['type' => 'SelectDate', 'formatType' => 'activityDateTime'], | |
505 | ]; | |
41b8dd1d | 506 | $this->_fields = array_merge($relationFields, $this->_fields); |
6a488035 | 507 | |
5837835b | 508 | $fields = CRM_Core_Component::getQueryFields(!$this->_skipPermission); |
6a488035 TO |
509 | unset($fields['note']); |
510 | $this->_fields = array_merge($this->_fields, $fields); | |
511 | ||
512 | // add activity fields | |
fd11fc34 | 513 | $this->_fields = array_merge($this->_fields, CRM_Activity_BAO_Activity::exportableFields()); |
cc81d063 | 514 | $this->_fields = array_merge($this->_fields, CRM_Activity_BAO_Activity::exportableFields('Case')); |
fd11fc34 | 515 | // Add hack as no unique name is defined for the field but the search form is in denial. |
516 | $this->_fields['activity_priority_id'] = $this->_fields['priority_id']; | |
f2a9b25c | 517 | |
99e9587a DS |
518 | // add any fields provided by hook implementers |
519 | $extFields = CRM_Contact_BAO_Query_Hook::singleton()->getFields(); | |
f2a9b25c | 520 | $this->_fields = array_merge($this->_fields, $extFields); |
6a488035 TO |
521 | } |
522 | ||
523 | // basically do all the work once, and then reuse it | |
66670e4d | 524 | $this->initialize($apiEntity); |
6a488035 TO |
525 | } |
526 | ||
527 | /** | |
fe482240 | 528 | * Function which actually does all the work for the constructor. |
8f165fa5 | 529 | * |
530 | * @param string $apiEntity | |
531 | * The api entity being called. | |
532 | * This sort-of duplicates $mode in a confusing way. Probably not by design. | |
e14c912f | 533 | * |
534 | * @throws \CRM_Core_Exception | |
6a488035 | 535 | */ |
66670e4d | 536 | public function initialize($apiEntity = NULL) { |
be2fb01f CW |
537 | $this->_select = []; |
538 | $this->_element = []; | |
539 | $this->_tables = []; | |
540 | $this->_whereTables = []; | |
541 | $this->_where = []; | |
542 | $this->_qill = []; | |
be2fb01f CW |
543 | $this->_cfIDs = []; |
544 | $this->_paramLookup = []; | |
545 | $this->_having = []; | |
6a488035 TO |
546 | |
547 | $this->_customQuery = NULL; | |
548 | ||
549 | // reset cached static variables - CRM-5803 | |
550 | self::$_activityRole = NULL; | |
551 | self::$_considerCompActivities = NULL; | |
552 | self::$_withContactActivitiesOnly = NULL; | |
553 | ||
554 | $this->_select['contact_id'] = 'contact_a.id as contact_id'; | |
555 | $this->_element['contact_id'] = 1; | |
556 | $this->_tables['civicrm_contact'] = 1; | |
557 | ||
558 | if (!empty($this->_params)) { | |
559 | $this->buildParamsLookup(); | |
560 | } | |
561 | ||
562 | $this->_whereTables = $this->_tables; | |
563 | ||
66670e4d | 564 | $this->selectClause($apiEntity); |
a7d6a3b6 | 565 | if (!empty($this->_cfIDs)) { |
566 | // @todo This function is the select function but instead of running 'select' it | |
567 | // is running the whole query. | |
568 | $this->_customQuery = new CRM_Core_BAO_CustomQuery($this->_cfIDs, TRUE, $this->_locationSpecificCustomFields); | |
569 | $this->_customQuery->query(); | |
570 | $this->_select = array_merge($this->_select, $this->_customQuery->_select); | |
571 | $this->_element = array_merge($this->_element, $this->_customQuery->_element); | |
572 | $this->_tables = array_merge($this->_tables, $this->_customQuery->_tables); | |
a7d6a3b6 | 573 | } |
9178793e | 574 | $isForcePrimaryOnly = !empty($apiEntity); |
575 | $this->_whereClause = $this->whereClause($isForcePrimaryOnly); | |
be274d8a E |
576 | if (array_key_exists('civicrm_contribution', $this->_whereTables)) { |
577 | $component = 'contribution'; | |
578 | } | |
579 | if (array_key_exists('civicrm_membership', $this->_whereTables)) { | |
580 | $component = 'membership'; | |
581 | } | |
40ae908c | 582 | if (isset($component) && !$this->_skipPermission) { |
583 | // Unit test coverage in api_v3_FinancialTypeACLTest::testGetACLContribution. | |
56f5e9db EM |
584 | $clauses = []; |
585 | if ($component === 'contribution') { | |
586 | $clauses = CRM_Contribute_BAO_Contribution::getSelectWhereClause(); | |
587 | } | |
588 | if ($component === 'membership') { | |
589 | $clauses = CRM_Member_BAO_Membership::getSelectWhereClause(); | |
590 | } | |
591 | if ($clauses) { | |
592 | $this->_whereClause .= ' AND ' . implode(' AND ', $clauses); | |
1c5787ad | 593 | } |
ecb39dfd | 594 | } |
6a488035 | 595 | |
b3e1c09d | 596 | $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode, $apiEntity); |
6a488035 TO |
597 | $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, $this->_primaryLocation, $this->_mode); |
598 | ||
599 | $this->openedSearchPanes(TRUE); | |
600 | } | |
601 | ||
67d19299 | 602 | /** |
bb05da0c | 603 | * Function for same purpose as convertFormValues. |
604 | * | |
605 | * Like convert form values this function exists to pre-Process parameters from the form. | |
606 | * | |
607 | * It is unclear why they are different functions & likely relates to advances search | |
608 | * versus search builder. | |
609 | * | |
610 | * The direction we are going is having the form convert values to a standardised format & | |
8f165fa5 | 611 | * moving away from weird & wonderful where clause switches. |
bb05da0c | 612 | * |
67d19299 | 613 | * Fix and handle contact deletion nicely. |
614 | * | |
615 | * this code is primarily for search builder use case where different clauses can specify if they want deleted. | |
616 | * | |
0e480632 | 617 | * @see https://issues.civicrm.org/jira/browse/CRM-11971 |
67d19299 | 618 | */ |
00be9182 | 619 | public function buildParamsLookup() { |
6a488035 | 620 | $trashParamExists = FALSE; |
be2fb01f | 621 | $paramByGroup = []; |
5c7dad85 TO |
622 | foreach ($this->_params as $k => $param) { |
623 | if (!empty($param[0]) && $param[0] == 'contact_is_deleted') { | |
6a488035 TO |
624 | $trashParamExists = TRUE; |
625 | } | |
e6f3f602 | 626 | if (!empty($param[3])) { |
627 | $paramByGroup[$param[3]][$k] = $param; | |
628 | } | |
6a488035 TO |
629 | } |
630 | ||
5c7dad85 | 631 | if ($trashParamExists) { |
6a488035 TO |
632 | $this->_skipDeleteClause = TRUE; |
633 | ||
634 | //cycle through group sets and explicitly add trash param if not set | |
5c7dad85 | 635 | foreach ($paramByGroup as $setID => $set) { |
6a488035 | 636 | if ( |
be2fb01f CW |
637 | !in_array(['contact_is_deleted', '=', '1', $setID, '0'], $this->_params) && |
638 | !in_array(['contact_is_deleted', '=', '0', $setID, '0'], $this->_params) | |
5c7dad85 | 639 | ) { |
be2fb01f | 640 | $this->_params[] = [ |
6a488035 TO |
641 | 'contact_is_deleted', |
642 | '=', | |
643 | '0', | |
644 | $setID, | |
645 | '0', | |
be2fb01f | 646 | ]; |
6a488035 TO |
647 | } |
648 | } | |
649 | } | |
650 | ||
651 | foreach ($this->_params as $value) { | |
a7488080 | 652 | if (empty($value[0])) { |
6a488035 TO |
653 | continue; |
654 | } | |
d1eb5519 | 655 | $cfID = CRM_Core_BAO_CustomField::getKeyID(str_replace(['_relative', '_low', '_high', '_to', '_high'], '', $value[0])); |
6a488035 TO |
656 | if ($cfID) { |
657 | if (!array_key_exists($cfID, $this->_cfIDs)) { | |
be2fb01f | 658 | $this->_cfIDs[$cfID] = []; |
6a488035 | 659 | } |
3130209f CW |
660 | // Set wildcard value based on "and/or" selection |
661 | foreach ($this->_params as $key => $param) { | |
662 | if ($param[0] == $value[0] . '_operator') { | |
663 | $value[4] = $param[2] == 'or'; | |
664 | break; | |
665 | } | |
666 | } | |
6a488035 TO |
667 | $this->_cfIDs[$cfID][] = $value; |
668 | } | |
669 | ||
670 | if (!array_key_exists($value[0], $this->_paramLookup)) { | |
be2fb01f | 671 | $this->_paramLookup[$value[0]] = []; |
6a488035 | 672 | } |
485a3a1f | 673 | if ($value[0] !== 'group') { |
8f165fa5 | 674 | // Just trying to unravel how group interacts here! This whole function is weird. |
485a3a1f | 675 | $this->_paramLookup[$value[0]][] = $value; |
676 | } | |
6a488035 TO |
677 | } |
678 | } | |
679 | ||
680 | /** | |
67d19299 | 681 | * Some composite fields do not appear in the fields array hack to make them part of the query. |
8f165fa5 | 682 | * |
683 | * @param $apiEntity | |
684 | * The api entity being called. | |
685 | * This sort-of duplicates $mode in a confusing way. Probably not by design. | |
6a488035 | 686 | */ |
66670e4d | 687 | public function addSpecialFields($apiEntity) { |
be2fb01f | 688 | static $special = ['contact_type', 'contact_sub_type', 'sort_name', 'display_name']; |
66670e4d | 689 | // if get called via Contact.get API having address_id as return parameter |
9de9a433 | 690 | if ($apiEntity === 'Contact') { |
66670e4d | 691 | $special[] = 'address_id'; |
692 | } | |
6a488035 | 693 | foreach ($special as $name) { |
a7488080 | 694 | if (!empty($this->_returnProperties[$name])) { |
9de9a433 | 695 | if ($name === 'address_id') { |
66670e4d | 696 | $this->_tables['civicrm_address'] = 1; |
697 | $this->_select['address_id'] = 'civicrm_address.id as address_id'; | |
698 | $this->_element['address_id'] = 1; | |
699 | } | |
700 | else { | |
701 | $this->_select[$name] = "contact_a.{$name} as $name"; | |
702 | $this->_element[$name] = 1; | |
703 | } | |
6a488035 TO |
704 | } |
705 | } | |
706 | } | |
707 | ||
708 | /** | |
709 | * Given a list of conditions in params and a list of desired | |
710 | * return Properties generate the required select and from | |
711 | * clauses. Note that since the where clause introduces new | |
712 | * tables, the initial attempt also retrieves all variables used | |
713 | * in the params list | |
8f165fa5 | 714 | * |
715 | * @param string $apiEntity | |
716 | * The api entity being called. | |
717 | * This sort-of duplicates $mode in a confusing way. Probably not by design. | |
6a488035 | 718 | */ |
66670e4d | 719 | public function selectClause($apiEntity = NULL) { |
6a488035 | 720 | |
3af96592 | 721 | // @todo Tidy up this. This arises because 1) we are ignoring the $mode & adding a new |
722 | // param ($apiEntity) instead - presumably an oversight & 2 because | |
723 | // contact is not implemented as a component. | |
66670e4d | 724 | $this->addSpecialFields($apiEntity); |
6a488035 TO |
725 | |
726 | foreach ($this->_fields as $name => $field) { | |
6a488035 TO |
727 | // skip component fields |
728 | // there are done by the alter query below | |
729 | // and need not be done on every field | |
3af96592 | 730 | // @todo remove these & handle using metadata - only obscure fields |
731 | // that are hack-added should need to be excluded from the main loop. | |
1071730c | 732 | if ( |
b832662c | 733 | (substr($name, 0, 12) === 'participant_') || |
734 | (substr($name, 0, 7) === 'pledge_') || | |
735 | (substr($name, 0, 5) === 'case_') | |
6a488035 TO |
736 | ) { |
737 | continue; | |
738 | } | |
739 | ||
740 | // redirect to activity select clause | |
4c24c842 | 741 | if ( |
b832662c | 742 | (substr($name, 0, 9) === 'activity_') || |
743 | ($name === 'parent_id') | |
4c24c842 | 744 | ) { |
6a488035 | 745 | CRM_Activity_BAO_Query::select($this); |
6a488035 TO |
746 | } |
747 | ||
748 | // if this is a hierarchical name, we ignore it | |
749 | $names = explode('-', $name); | |
0742f843 | 750 | if (count($names) > 1 && isset($names[1]) && is_numeric($names[1])) { |
6a488035 TO |
751 | continue; |
752 | } | |
753 | ||
c6ff5b0d | 754 | // make an exception for special cases, to add the field in select clause |
fd18baa6 | 755 | $makeException = FALSE; |
c6ff5b0d KJ |
756 | |
757 | //special handling for groups/tags | |
be2fb01f | 758 | if (in_array($name, ['groups', 'tags', 'notes']) |
fd18baa6 KJ |
759 | && isset($this->_returnProperties[substr($name, 0, -1)]) |
760 | ) { | |
8f165fa5 | 761 | // @todo instead of setting make exception to get us into |
762 | // an if clause that has handling for these fields buried with in it | |
763 | // move the handling to here. | |
fd18baa6 KJ |
764 | $makeException = TRUE; |
765 | } | |
766 | ||
c6ff5b0d KJ |
767 | // since note has 3 different options we need special handling |
768 | // note / note_subject / note_body | |
b832662c | 769 | if ($name === 'notes') { |
be2fb01f | 770 | foreach (['note', 'note_subject', 'note_body'] as $noteField) { |
c6ff5b0d KJ |
771 | if (isset($this->_returnProperties[$noteField])) { |
772 | $makeException = TRUE; | |
773 | break; | |
774 | } | |
775 | } | |
776 | } | |
a87ba7c0 | 777 | |
6a488035 | 778 | $cfID = CRM_Core_BAO_CustomField::getKeyID($name); |
86ab13b7 | 779 | if ( |
780 | !empty($this->_paramLookup[$name]) | |
781 | || !empty($this->_returnProperties[$name]) | |
124f3c1b | 782 | || $this->pseudoConstantNameIsInReturnProperties($field, $name) |
86ab13b7 | 783 | || $makeException |
6a488035 | 784 | ) { |
6a488035 TO |
785 | if ($cfID) { |
786 | // add to cfIDs array if not present | |
787 | if (!array_key_exists($cfID, $this->_cfIDs)) { | |
be2fb01f | 788 | $this->_cfIDs[$cfID] = []; |
6a488035 TO |
789 | } |
790 | } | |
791 | elseif (isset($field['where'])) { | |
792 | list($tableName, $fieldName) = explode('.', $field['where'], 2); | |
793 | if (isset($tableName)) { | |
f3acfdd9 | 794 | if (!empty(self::$_dependencies[$tableName])) { |
6a488035 TO |
795 | $this->_tables['civicrm_address'] = 1; |
796 | $this->_select['address_id'] = 'civicrm_address.id as address_id'; | |
797 | $this->_element['address_id'] = 1; | |
798 | } | |
799 | ||
b832662c | 800 | if ($tableName === 'im_provider' || $tableName === 'email_greeting' || |
801 | $tableName === 'postal_greeting' || $tableName === 'addressee' | |
6a488035 | 802 | ) { |
b832662c | 803 | if ($tableName === 'im_provider') { |
d9ab802d PJ |
804 | CRM_Core_OptionValue::select($this); |
805 | } | |
806 | ||
807 | if (in_array($tableName, | |
be2fb01f | 808 | ['email_greeting', 'postal_greeting', 'addressee'])) { |
d9ab802d PJ |
809 | $this->_element["{$name}_id"] = 1; |
810 | $this->_select["{$name}_id"] = "contact_a.{$name}_id as {$name}_id"; | |
be2fb01f | 811 | $this->_pseudoConstantsSelect[$name] = ['pseudoField' => $tableName, 'idCol' => "{$name}_id"]; |
d9ab802d PJ |
812 | $this->_pseudoConstantsSelect[$name]['select'] = "{$name}.{$fieldName} as $name"; |
813 | $this->_pseudoConstantsSelect[$name]['element'] = $name; | |
814 | ||
b832662c | 815 | if ($tableName === 'email_greeting') { |
3af96592 | 816 | // @todo bad join. |
59f4c9ee TO |
817 | $this->_pseudoConstantsSelect[$name]['join'] |
818 | = " LEFT JOIN civicrm_option_group option_group_email_greeting ON (option_group_email_greeting.name = 'email_greeting')"; | |
d9ab802d PJ |
819 | $this->_pseudoConstantsSelect[$name]['join'] .= |
820 | " LEFT JOIN civicrm_option_value email_greeting ON (contact_a.email_greeting_id = email_greeting.value AND option_group_email_greeting.id = email_greeting.option_group_id ) "; | |
821 | } | |
b832662c | 822 | elseif ($tableName === 'postal_greeting') { |
3af96592 | 823 | // @todo bad join. |
59f4c9ee TO |
824 | $this->_pseudoConstantsSelect[$name]['join'] |
825 | = " LEFT JOIN civicrm_option_group option_group_postal_greeting ON (option_group_postal_greeting.name = 'postal_greeting')"; | |
d9ab802d PJ |
826 | $this->_pseudoConstantsSelect[$name]['join'] .= |
827 | " LEFT JOIN civicrm_option_value postal_greeting ON (contact_a.postal_greeting_id = postal_greeting.value AND option_group_postal_greeting.id = postal_greeting.option_group_id ) "; | |
828 | } | |
829 | elseif ($tableName == 'addressee') { | |
3af96592 | 830 | // @todo bad join. |
59f4c9ee TO |
831 | $this->_pseudoConstantsSelect[$name]['join'] |
832 | = " LEFT JOIN civicrm_option_group option_group_addressee ON (option_group_addressee.name = 'addressee')"; | |
d9ab802d PJ |
833 | $this->_pseudoConstantsSelect[$name]['join'] .= |
834 | " LEFT JOIN civicrm_option_value addressee ON (contact_a.addressee_id = addressee.value AND option_group_addressee.id = addressee.option_group_id ) "; | |
835 | } | |
836 | $this->_pseudoConstantsSelect[$name]['table'] = $tableName; | |
837 | ||
6a488035 TO |
838 | //get display |
839 | $greetField = "{$name}_display"; | |
840 | $this->_select[$greetField] = "contact_a.{$greetField} as {$greetField}"; | |
841 | $this->_element[$greetField] = 1; | |
842 | //get custom | |
843 | $greetField = "{$name}_custom"; | |
844 | $this->_select[$greetField] = "contact_a.{$greetField} as {$greetField}"; | |
845 | $this->_element[$greetField] = 1; | |
846 | } | |
847 | } | |
848 | else { | |
be2fb01f | 849 | if (!in_array($tableName, ['civicrm_state_province', 'civicrm_country', 'civicrm_county'])) { |
d9ab802d PJ |
850 | $this->_tables[$tableName] = 1; |
851 | } | |
6a488035 TO |
852 | |
853 | // also get the id of the tableName | |
854 | $tName = substr($tableName, 8); | |
be2fb01f | 855 | if (in_array($tName, ['country', 'state_province', 'county'])) { |
d9ab802d | 856 | if ($tName == 'state_province') { |
be2fb01f | 857 | $this->_pseudoConstantsSelect['state_province_name'] = [ |
59f4c9ee TO |
858 | 'pseudoField' => "{$tName}", |
859 | 'idCol' => "{$tName}_id", | |
860 | 'bao' => 'CRM_Core_BAO_Address', | |
861 | 'table' => "civicrm_{$tName}", | |
862 | 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ", | |
be2fb01f | 863 | ]; |
59f4c9ee | 864 | |
be2fb01f | 865 | $this->_pseudoConstantsSelect[$tName] = [ |
59f4c9ee TO |
866 | 'pseudoField' => 'state_province_abbreviation', |
867 | 'idCol' => "{$tName}_id", | |
868 | 'table' => "civicrm_{$tName}", | |
869 | 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ", | |
be2fb01f | 870 | ]; |
d9ab802d | 871 | } |
7f84f734 | 872 | else { |
be2fb01f | 873 | $this->_pseudoConstantsSelect[$name] = [ |
59f4c9ee TO |
874 | 'pseudoField' => "{$tName}_id", |
875 | 'idCol' => "{$tName}_id", | |
876 | 'bao' => 'CRM_Core_BAO_Address', | |
877 | 'table' => "civicrm_{$tName}", | |
878 | 'join' => " LEFT JOIN civicrm_{$tName} ON civicrm_address.{$tName}_id = civicrm_{$tName}.id ", | |
be2fb01f | 879 | ]; |
7f84f734 | 880 | } |
6a488035 | 881 | |
d9ab802d PJ |
882 | $this->_select["{$tName}_id"] = "civicrm_address.{$tName}_id as {$tName}_id"; |
883 | $this->_element["{$tName}_id"] = 1; | |
884 | } | |
885 | elseif ($tName != 'contact') { | |
6a488035 TO |
886 | $this->_select["{$tName}_id"] = "{$tableName}.id as {$tName}_id"; |
887 | $this->_element["{$tName}_id"] = 1; | |
888 | } | |
889 | ||
890 | //special case for phone | |
891 | if ($name == 'phone') { | |
892 | $this->_select['phone_type_id'] = "civicrm_phone.phone_type_id as phone_type_id"; | |
893 | $this->_element['phone_type_id'] = 1; | |
894 | } | |
895 | ||
896 | // if IM then select provider_id also | |
897 | // to get "IM Service Provider" in a file to be exported, CRM-3140 | |
898 | if ($name == 'im') { | |
899 | $this->_select['provider_id'] = "civicrm_im.provider_id as provider_id"; | |
900 | $this->_element['provider_id'] = 1; | |
901 | } | |
902 | ||
86ab13b7 | 903 | if ($tName == 'contact' && $fieldName == 'organization_name') { |
6a488035 | 904 | // special case, when current employer is set for Individual contact |
86ab13b7 | 905 | $this->_select[$name] = "IF ( contact_a.contact_type = 'Individual', NULL, contact_a.organization_name ) as organization_name"; |
906 | } | |
907 | elseif ($tName == 'contact' && $fieldName === 'id') { | |
908 | // Handled elsewhere, explicitly ignore. Possibly for all tables... | |
6a488035 | 909 | } |
be2fb01f | 910 | elseif (in_array($tName, ['country', 'county'])) { |
4e6490d4 E |
911 | $this->_pseudoConstantsSelect[$name]['select'] = "{$field['where']} as `$name`"; |
912 | $this->_pseudoConstantsSelect[$name]['element'] = $name; | |
913 | } | |
914 | elseif ($tName == 'state_province') { | |
915 | $this->_pseudoConstantsSelect[$tName]['select'] = "{$field['where']} as `$name`"; | |
916 | $this->_pseudoConstantsSelect[$tName]['element'] = $name; | |
d9ab802d | 917 | } |
e4ce3252 | 918 | elseif (strpos($name, 'contribution_soft_credit') !== FALSE) { |
2d94ea54 | 919 | if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_params)) { |
920 | $this->_select[$name] = "{$field['where']} as `$name`"; | |
921 | } | |
922 | } | |
124f3c1b | 923 | elseif ($this->pseudoConstantNameIsInReturnProperties($field, $name)) { |
924 | $this->addPseudoconstantFieldToSelect($name); | |
925 | } | |
6a488035 | 926 | else { |
86ab13b7 | 927 | $this->_select[$name] = str_replace('civicrm_contact.', 'contact_a.', "{$field['where']} as `$name`"); |
6a488035 | 928 | } |
be2fb01f | 929 | if (!in_array($tName, ['state_province', 'country', 'county'])) { |
d9ab802d PJ |
930 | $this->_element[$name] = 1; |
931 | } | |
6a488035 TO |
932 | } |
933 | } | |
934 | } | |
935 | elseif ($name === 'tags') { | |
8f165fa5 | 936 | //@todo move this handling outside the big IF & ditch $makeException |
6a488035 TO |
937 | $this->_useGroupBy = TRUE; |
938 | $this->_select[$name] = "GROUP_CONCAT(DISTINCT(civicrm_tag.name)) as tags"; | |
939 | $this->_element[$name] = 1; | |
940 | $this->_tables['civicrm_tag'] = 1; | |
941 | $this->_tables['civicrm_entity_tag'] = 1; | |
942 | } | |
943 | elseif ($name === 'groups') { | |
8f165fa5 | 944 | //@todo move this handling outside the big IF & ditch $makeException |
6a488035 | 945 | $this->_useGroupBy = TRUE; |
3875e6b6 | 946 | // Duplicates will be created here but better to sort them out in php land. |
947 | $this->_select[$name] = " | |
948 | CONCAT_WS(',', | |
949 | GROUP_CONCAT(DISTINCT IF(civicrm_group_contact.status = 'Added', civicrm_group_contact.group_id, '')), | |
950 | GROUP_CONCAT(DISTINCT civicrm_group_contact_cache.group_id) | |
951 | ) | |
d9d23a91 | 952 | as `groups`"; |
6a488035 | 953 | $this->_element[$name] = 1; |
3875e6b6 | 954 | $this->_tables['civicrm_group_contact'] = 1; |
955 | $this->_tables['civicrm_group_contact_cache'] = 1; | |
be2fb01f | 956 | $this->_pseudoConstantsSelect["{$name}"] = [ |
3875e6b6 | 957 | 'pseudoField' => "groups", |
9de9a433 | 958 | 'idCol' => 'groups', |
be2fb01f | 959 | ]; |
6a488035 TO |
960 | } |
961 | elseif ($name === 'notes') { | |
8f165fa5 | 962 | //@todo move this handling outside the big IF & ditch $makeException |
e3199114 | 963 | // if note field is subject then return subject else body of the note |
964 | $noteColumn = 'note'; | |
9de9a433 | 965 | if (isset($noteField) && $noteField === 'note_subject') { |
e3199114 | 966 | $noteColumn = 'subject'; |
967 | } | |
968 | ||
6a488035 | 969 | $this->_useGroupBy = TRUE; |
e3199114 | 970 | $this->_select[$name] = "GROUP_CONCAT(DISTINCT(civicrm_note.$noteColumn)) as notes"; |
6a488035 TO |
971 | $this->_element[$name] = 1; |
972 | $this->_tables['civicrm_note'] = 1; | |
973 | } | |
974 | elseif ($name === 'current_employer') { | |
975 | $this->_select[$name] = "IF ( contact_a.contact_type = 'Individual', contact_a.organization_name, NULL ) as current_employer"; | |
976 | $this->_element[$name] = 1; | |
977 | } | |
978 | } | |
979 | ||
8cc574cf | 980 | if ($cfID && !empty($field['is_search_range'])) { |
6a488035 | 981 | // this is a custom field with range search enabled, so we better check for two/from values |
a7488080 | 982 | if (!empty($this->_paramLookup[$name . '_from'])) { |
6a488035 | 983 | if (!array_key_exists($cfID, $this->_cfIDs)) { |
be2fb01f | 984 | $this->_cfIDs[$cfID] = []; |
6a488035 TO |
985 | } |
986 | foreach ($this->_paramLookup[$name . '_from'] as $pID => $p) { | |
987 | // search in the cdID array for the same grouping | |
988 | $fnd = FALSE; | |
989 | foreach ($this->_cfIDs[$cfID] as $cID => $c) { | |
990 | if ($c[3] == $p[3]) { | |
991 | $this->_cfIDs[$cfID][$cID][2]['from'] = $p[2]; | |
992 | $fnd = TRUE; | |
993 | } | |
994 | } | |
995 | if (!$fnd) { | |
be2fb01f | 996 | $p[2] = ['from' => $p[2]]; |
6a488035 TO |
997 | $this->_cfIDs[$cfID][] = $p; |
998 | } | |
999 | } | |
1000 | } | |
a7488080 | 1001 | if (!empty($this->_paramLookup[$name . '_to'])) { |
6a488035 | 1002 | if (!array_key_exists($cfID, $this->_cfIDs)) { |
be2fb01f | 1003 | $this->_cfIDs[$cfID] = []; |
6a488035 TO |
1004 | } |
1005 | foreach ($this->_paramLookup[$name . '_to'] as $pID => $p) { | |
1006 | // search in the cdID array for the same grouping | |
1007 | $fnd = FALSE; | |
1008 | foreach ($this->_cfIDs[$cfID] as $cID => $c) { | |
1009 | if ($c[4] == $p[4]) { | |
1010 | $this->_cfIDs[$cfID][$cID][2]['to'] = $p[2]; | |
1011 | $fnd = TRUE; | |
1012 | } | |
1013 | } | |
1014 | if (!$fnd) { | |
be2fb01f | 1015 | $p[2] = ['to' => $p[2]]; |
6a488035 TO |
1016 | $this->_cfIDs[$cfID][] = $p; |
1017 | } | |
1018 | } | |
1019 | } | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | // add location as hierarchical elements | |
1024 | $this->addHierarchicalElements(); | |
1025 | ||
1026 | // add multiple field like website | |
1027 | $this->addMultipleElements(); | |
1028 | ||
1029 | //fix for CRM-951 | |
1030 | CRM_Core_Component::alterQuery($this, 'select'); | |
1031 | ||
99e9587a | 1032 | CRM_Contact_BAO_Query_Hook::singleton()->alterSearchQuery($this, 'select'); |
6a488035 TO |
1033 | } |
1034 | ||
1035 | /** | |
c037736a | 1036 | * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values. |
6a488035 | 1037 | */ |
00be9182 | 1038 | public function addHierarchicalElements() { |
a7488080 | 1039 | if (empty($this->_returnProperties['location'])) { |
6a488035 TO |
1040 | return; |
1041 | } | |
1042 | if (!is_array($this->_returnProperties['location'])) { | |
1043 | return; | |
1044 | } | |
1045 | ||
f9ba4d01 | 1046 | $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate'); |
be2fb01f | 1047 | $processed = []; |
6a488035 TO |
1048 | $index = 0; |
1049 | ||
1050 | $addressCustomFields = CRM_Core_BAO_CustomField::getFieldsForImport('Address'); | |
be2fb01f | 1051 | $addressCustomFieldIds = []; |
6a488035 TO |
1052 | |
1053 | foreach ($this->_returnProperties['location'] as $name => $elements) { | |
1054 | $lCond = self::getPrimaryCondition($name); | |
708ce91f | 1055 | $locationTypeId = is_numeric($name) ? NULL : array_search($name, $locationTypes); |
6a488035 TO |
1056 | |
1057 | if (!$lCond) { | |
6a488035 TO |
1058 | if ($locationTypeId === FALSE) { |
1059 | continue; | |
1060 | } | |
1061 | $lCond = "location_type_id = $locationTypeId"; | |
1062 | $this->_useDistinct = TRUE; | |
1063 | ||
1064 | //commented for CRM-3256 | |
1065 | $this->_useGroupBy = TRUE; | |
1066 | } | |
1067 | ||
1068 | $name = str_replace(' ', '_', $name); | |
6a488035 TO |
1069 | $tName = "$name-location_type"; |
1070 | $ltName = "`$name-location_type`"; | |
1071 | $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`"; | |
1072 | $this->_select["{$tName}"] = "`$tName`.name as `{$tName}`"; | |
1073 | $this->_element["{$tName}_id"] = 1; | |
1074 | $this->_element["{$tName}"] = 1; | |
1075 | ||
1076 | $locationTypeName = $tName; | |
be2fb01f | 1077 | $locationTypeJoin = []; |
6a488035 | 1078 | |
6a488035 TO |
1079 | $addWhereCount = 0; |
1080 | foreach ($elements as $elementFullName => $dontCare) { | |
1081 | $index++; | |
1082 | $elementName = $elementCmpName = $elementFullName; | |
1083 | ||
1084 | if (substr($elementCmpName, 0, 5) == 'phone') { | |
1085 | $elementCmpName = 'phone'; | |
1086 | } | |
1087 | ||
c3f7ab62 | 1088 | if (array_key_exists($elementCmpName, $addressCustomFields)) { |
6a488035 TO |
1089 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($elementCmpName)) { |
1090 | $addressCustomFieldIds[$cfID][$name] = 1; | |
1091 | } | |
1092 | } | |
708ce91f | 1093 | // add address table - doesn't matter if we do it mutliple times - it's the same data |
1094 | // @todo ditch the double processing of addressJoin | |
6a488035 | 1095 | if ((in_array($elementCmpName, self::$_locationSpecificFields) || !empty($addressCustomFieldIds)) |
be2fb01f | 1096 | && !in_array($elementCmpName, ['email', 'phone', 'im', 'openid']) |
6a488035 | 1097 | ) { |
708ce91f | 1098 | list($aName, $addressJoin) = $this->addAddressTable($name, $lCond); |
6a488035 TO |
1099 | $locationTypeJoin[$tName] = " ( $aName.location_type_id = $ltName.id ) "; |
1100 | $processed[$aName] = 1; | |
6a488035 TO |
1101 | } |
1102 | ||
1103 | $cond = $elementType = ''; | |
1104 | if (strpos($elementName, '-') !== FALSE) { | |
1105 | // this is either phone, email or IM | |
1106 | list($elementName, $elementType) = explode('-', $elementName); | |
1107 | ||
6a488035 TO |
1108 | if (($elementName != 'phone') && ($elementName != 'im')) { |
1109 | $cond = self::getPrimaryCondition($elementType); | |
1110 | } | |
6e7e6dc0 PJ |
1111 | // CRM-13011 : If location type is primary, do not restrict search to the phone |
1112 | // type id - we want the primary phone, regardless of what type it is. | |
1113 | // Otherwise, restrict to the specified phone type for the given field. | |
9457ac35 | 1114 | if ((!$cond) && ($elementName == 'phone')) { |
6a488035 TO |
1115 | $cond = "phone_type_id = '$elementType'"; |
1116 | } | |
1117 | elseif ((!$cond) && ($elementName == 'im')) { | |
1118 | // IM service provider id, CRM-3140 | |
1119 | $cond = "provider_id = '$elementType'"; | |
1120 | } | |
1121 | $elementType = '-' . $elementType; | |
1122 | } | |
1123 | ||
9c1bc317 | 1124 | $field = $this->_fields[$elementName] ?? NULL; |
5db2212e | 1125 | if (!empty($field)) { |
1126 | if (isset($this->_pseudoConstantsSelect[$field['name']])) { | |
1127 | $this->_pseudoConstantsSelect[$name . '-' . $field['name']] = $this->_pseudoConstantsSelect[$field['name']]; | |
1128 | } | |
0b23ab2b | 1129 | } |
6a488035 TO |
1130 | |
1131 | // hack for profile, add location id | |
1132 | if (!$field) { | |
1133 | if ($elementType && | |
1134 | // fix for CRM-882( to handle phone types ) | |
1135 | !is_numeric($elementType) | |
1136 | ) { | |
1137 | if (is_numeric($name)) { | |
9c1bc317 | 1138 | $field = $this->_fields[$elementName . "-Primary$elementType"] ?? NULL; |
6a488035 TO |
1139 | } |
1140 | else { | |
9c1bc317 | 1141 | $field = $this->_fields[$elementName . "-$locationTypeId$elementType"] ?? NULL; |
6a488035 TO |
1142 | } |
1143 | } | |
1144 | elseif (is_numeric($name)) { | |
1145 | //this for phone type to work | |
be2fb01f | 1146 | if (in_array($elementName, ['phone', 'phone_ext'])) { |
9c1bc317 | 1147 | $field = $this->_fields[$elementName . "-Primary" . $elementType] ?? NULL; |
6a488035 TO |
1148 | } |
1149 | else { | |
9c1bc317 | 1150 | $field = $this->_fields[$elementName . "-Primary"] ?? NULL; |
6a488035 TO |
1151 | } |
1152 | } | |
1153 | else { | |
1154 | //this is for phone type to work for profile edit | |
be2fb01f | 1155 | if (in_array($elementName, ['phone', 'phone_ext'])) { |
9c1bc317 | 1156 | $field = $this->_fields[$elementName . "-$locationTypeId$elementType"] ?? NULL; |
6a488035 TO |
1157 | } |
1158 | else { | |
9c1bc317 | 1159 | $field = $this->_fields[$elementName . "-$locationTypeId"] ?? NULL; |
6a488035 TO |
1160 | } |
1161 | } | |
1162 | } | |
1163 | ||
314dbef8 | 1164 | // Check if there is a value, if so also add to where Clause |
6a488035 TO |
1165 | $addWhere = FALSE; |
1166 | if ($this->_params) { | |
1167 | $nm = $elementName; | |
1168 | if (isset($locationTypeId)) { | |
1169 | $nm .= "-$locationTypeId"; | |
1170 | } | |
1171 | if (!is_numeric($elementType)) { | |
1172 | $nm .= "$elementType"; | |
1173 | } | |
1174 | ||
1175 | foreach ($this->_params as $id => $values) { | |
8af73472 | 1176 | if ((is_array($values) && $values[0] == $nm) || |
be2fb01f | 1177 | (in_array($elementName, ['phone', 'im']) |
6a488035 TO |
1178 | && (strpos($values[0], $nm) !== FALSE) |
1179 | ) | |
1180 | ) { | |
1181 | $addWhere = TRUE; | |
1182 | $addWhereCount++; | |
1183 | break; | |
1184 | } | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | if ($field && isset($field['where'])) { | |
1189 | list($tableName, $fieldName) = explode('.', $field['where'], 2); | |
d9ab802d PJ |
1190 | $pf = substr($tableName, 8); |
1191 | $tName = $name . '-' . $pf . $elementType; | |
6a488035 | 1192 | if (isset($tableName)) { |
d9ab802d PJ |
1193 | if ($tableName == 'civicrm_state_province' || $tableName == 'civicrm_country' || $tableName == 'civicrm_county') { |
1194 | $this->_select["{$tName}_id"] = "{$aName}.{$pf}_id as `{$tName}_id`"; | |
1195 | } | |
1196 | else { | |
1197 | $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`"; | |
1198 | } | |
1199 | ||
6a488035 TO |
1200 | $this->_element["{$tName}_id"] = 1; |
1201 | if (substr($tName, -15) == '-state_province') { | |
1202 | // FIXME: hack to fix CRM-1900 | |
aaffa79f | 1203 | $a = Civi::settings()->get('address_format'); |
6a488035 TO |
1204 | |
1205 | if (substr_count($a, 'state_province_name') > 0) { | |
be2fb01f | 1206 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"] = [ |
59f4c9ee TO |
1207 | 'pseudoField' => "{$pf}_id", |
1208 | 'idCol' => "{$tName}_id", | |
1209 | 'bao' => 'CRM_Core_BAO_Address', | |
be2fb01f | 1210 | ]; |
d9ab802d | 1211 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['select'] = "`$tName`.name as `{$name}-{$elementFullName}`"; |
6a488035 TO |
1212 | } |
1213 | else { | |
be2fb01f | 1214 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"] = [ |
59f4c9ee TO |
1215 | 'pseudoField' => 'state_province_abbreviation', |
1216 | 'idCol' => "{$tName}_id", | |
be2fb01f | 1217 | ]; |
d9ab802d | 1218 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['select'] = "`$tName`.abbreviation as `{$name}-{$elementFullName}`"; |
6a488035 TO |
1219 | } |
1220 | } | |
1221 | else { | |
1222 | if (substr($elementFullName, 0, 2) == 'im') { | |
1223 | $provider = "{$name}-{$elementFullName}-provider_id"; | |
1224 | $this->_select[$provider] = "`$tName`.provider_id as `{$name}-{$elementFullName}-provider_id`"; | |
1225 | $this->_element[$provider] = 1; | |
1226 | } | |
d9ab802d | 1227 | if ($pf == 'country' || $pf == 'county') { |
be2fb01f | 1228 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"] = [ |
5c7dad85 TO |
1229 | 'pseudoField' => "{$pf}_id", |
1230 | 'idCol' => "{$tName}_id", | |
21dfd5f5 | 1231 | 'bao' => 'CRM_Core_BAO_Address', |
be2fb01f | 1232 | ]; |
d9ab802d PJ |
1233 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['select'] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`"; |
1234 | } | |
1235 | else { | |
1236 | $this->_select["{$name}-{$elementFullName}"] = "`$tName`.$fieldName as `{$name}-{$elementFullName}`"; | |
1237 | } | |
1238 | } | |
6a488035 | 1239 | |
be2fb01f | 1240 | if (in_array($pf, ['state_province', 'country', 'county'])) { |
d9ab802d PJ |
1241 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['element'] = "{$name}-{$elementFullName}"; |
1242 | } | |
1243 | else { | |
1244 | $this->_element["{$name}-{$elementFullName}"] = 1; | |
6a488035 TO |
1245 | } |
1246 | ||
a7488080 | 1247 | if (empty($processed["`$tName`"])) { |
6a488035 TO |
1248 | $processed["`$tName`"] = 1; |
1249 | $newName = $tableName . '_' . $index; | |
1250 | switch ($tableName) { | |
1251 | case 'civicrm_phone': | |
1252 | case 'civicrm_email': | |
1253 | case 'civicrm_im': | |
1254 | case 'civicrm_openid': | |
1255 | ||
9457ac35 | 1256 | $this->_tables[$tName] = "\nLEFT JOIN $tableName `$tName` ON contact_a.id = `$tName`.contact_id"; |
1257 | if ($tableName != 'civicrm_phone') { | |
1258 | $this->_tables[$tName] .= " AND `$tName`.$lCond"; | |
1259 | } | |
1260 | elseif (is_numeric($name)) { | |
1261 | $this->_select[$tName] = "IF (`$tName`.is_primary = $name, `$tName`.phone, NULL) as `$tName`"; | |
1262 | } | |
1263 | ||
6a488035 TO |
1264 | // this special case to add phone type |
1265 | if ($cond) { | |
1266 | $phoneTypeCondition = " AND `$tName`.$cond "; | |
1267 | //gross hack to pickup corrupted data also, CRM-7603 | |
1268 | if (strpos($cond, 'phone_type_id') !== FALSE) { | |
1269 | $phoneTypeCondition = " AND ( `$tName`.$cond OR `$tName`.phone_type_id IS NULL ) "; | |
8b3d5966 J |
1270 | if (!empty($lCond)) { |
1271 | $phoneTypeCondition .= " AND ( `$tName`.$lCond ) "; | |
1272 | } | |
6a488035 TO |
1273 | } |
1274 | $this->_tables[$tName] .= $phoneTypeCondition; | |
1275 | } | |
1276 | ||
1277 | //build locationType join | |
1278 | $locationTypeJoin[$tName] = " ( `$tName`.location_type_id = $ltName.id )"; | |
1279 | ||
1280 | if ($addWhere) { | |
1281 | $this->_whereTables[$tName] = $this->_tables[$tName]; | |
1282 | } | |
1283 | break; | |
1284 | ||
1285 | case 'civicrm_state_province': | |
d9ab802d | 1286 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['table'] = $tName; |
59f4c9ee TO |
1287 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['join'] |
1288 | = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.state_province_id"; | |
6a488035 TO |
1289 | if ($addWhere) { |
1290 | $this->_whereTables["{$name}-address"] = $addressJoin; | |
6a488035 TO |
1291 | } |
1292 | break; | |
1293 | ||
8a60e058 MD |
1294 | case 'civicrm_location_type': |
1295 | $this->_tables[$tName] = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.location_type_id"; | |
1296 | ||
1297 | if ($addWhere) { | |
1298 | $this->_whereTables["{$name}-address"] = $addressJoin; | |
1299 | } | |
1300 | break; | |
1301 | ||
6a488035 | 1302 | case 'civicrm_country': |
d9ab802d | 1303 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['table'] = $newName; |
59f4c9ee TO |
1304 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['join'] |
1305 | = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.country_id"; | |
6a488035 TO |
1306 | if ($addWhere) { |
1307 | $this->_whereTables["{$name}-address"] = $addressJoin; | |
6a488035 TO |
1308 | } |
1309 | break; | |
1310 | ||
1311 | case 'civicrm_county': | |
d9ab802d | 1312 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['table'] = $newName; |
59f4c9ee TO |
1313 | $this->_pseudoConstantsSelect["{$name}-{$elementFullName}"]['join'] |
1314 | = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.county_id"; | |
6a488035 TO |
1315 | if ($addWhere) { |
1316 | $this->_whereTables["{$name}-address"] = $addressJoin; | |
6a488035 TO |
1317 | } |
1318 | break; | |
1319 | ||
1320 | default: | |
4ad408ae VP |
1321 | if (isset($addressCustomFields[$elementName]['custom_field_id']) && !empty($addressCustomFields[$elementName]['custom_field_id'])) { |
1322 | $this->_tables[$tName] = "\nLEFT JOIN $tableName `$tName` ON `$tName`.id = $aName.id"; | |
1323 | } | |
6a488035 TO |
1324 | if ($addWhere) { |
1325 | $this->_whereTables["{$name}-address"] = $addressJoin; | |
1326 | } | |
1327 | break; | |
1328 | } | |
1329 | } | |
1330 | } | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | // add location type join | |
1335 | $ltypeJoin = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locationTypeJoin) . " )"; | |
1336 | $this->_tables[$locationTypeName] = $ltypeJoin; | |
1337 | ||
1338 | // table should be present in $this->_whereTables, | |
1339 | // to add its condition in location type join, CRM-3939. | |
1340 | if ($addWhereCount) { | |
be2fb01f | 1341 | $locClause = []; |
6a488035 | 1342 | foreach ($this->_whereTables as $tableName => $clause) { |
a7488080 | 1343 | if (!empty($locationTypeJoin[$tableName])) { |
6a488035 TO |
1344 | $locClause[] = $locationTypeJoin[$tableName]; |
1345 | } | |
1346 | } | |
1347 | ||
1348 | if (!empty($locClause)) { | |
1349 | $this->_whereTables[$locationTypeName] = "\nLEFT JOIN civicrm_location_type $ltName ON ( " . implode('OR', $locClause) . " )"; | |
1350 | } | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | if (!empty($addressCustomFieldIds)) { | |
442df34b | 1355 | $customQuery = new CRM_Core_BAO_CustomQuery($addressCustomFieldIds); |
6a488035 TO |
1356 | foreach ($addressCustomFieldIds as $cfID => $locTypeName) { |
1357 | foreach ($locTypeName as $name => $dnc) { | |
be2fb01f | 1358 | $this->_locationSpecificCustomFields[$cfID] = [$name, array_search($name, $locationTypes)]; |
6a488035 TO |
1359 | $fieldName = "$name-custom_{$cfID}"; |
1360 | $tName = "$name-address-custom-{$cfID}"; | |
1361 | $aName = "`$name-address-custom-{$cfID}`"; | |
1362 | $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`"; | |
1363 | $this->_element["{$tName}_id"] = 1; | |
1364 | $this->_select[$fieldName] = "`$tName`.{$customQuery->_fields[$cfID]['column_name']} as `{$fieldName}`"; | |
1365 | $this->_element[$fieldName] = 1; | |
1366 | $this->_tables[$tName] = "\nLEFT JOIN {$customQuery->_fields[$cfID]['table_name']} $aName ON ($aName.entity_id = `$name-address`.id)"; | |
1367 | } | |
1368 | } | |
1369 | } | |
1370 | } | |
1371 | ||
1372 | /** | |
c037736a | 1373 | * If the return Properties are set in a hierarchy, traverse the hierarchy to get the return values. |
6a488035 | 1374 | */ |
00be9182 | 1375 | public function addMultipleElements() { |
a7488080 | 1376 | if (empty($this->_returnProperties['website'])) { |
6a488035 TO |
1377 | return; |
1378 | } | |
1379 | if (!is_array($this->_returnProperties['website'])) { | |
1380 | return; | |
1381 | } | |
1382 | ||
1383 | foreach ($this->_returnProperties['website'] as $key => $elements) { | |
1384 | foreach ($elements as $elementFullName => $dontCare) { | |
1385 | $tName = "website-{$key}-{$elementFullName}"; | |
1386 | $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`"; | |
1387 | $this->_select["{$tName}"] = "`$tName`.url as `{$tName}`"; | |
1388 | $this->_element["{$tName}_id"] = 1; | |
1389 | $this->_element["{$tName}"] = 1; | |
1390 | ||
1391 | $type = "website-{$key}-website_type_id"; | |
1392 | $this->_select[$type] = "`$tName`.website_type_id as `{$type}`"; | |
1393 | $this->_element[$type] = 1; | |
887e764d | 1394 | $this->_tables[$tName] = "\nLEFT JOIN civicrm_website `$tName` ON (`$tName`.contact_id = contact_a.id AND `$tName`.website_type_id = $key )"; |
6a488035 TO |
1395 | } |
1396 | } | |
1397 | } | |
1398 | ||
1399 | /** | |
fe482240 | 1400 | * Generate the query based on what type of query we need. |
6a488035 | 1401 | * |
77c5b619 TO |
1402 | * @param bool $count |
1403 | * @param bool $sortByChar | |
1404 | * @param bool $groupContacts | |
1405 | * @param bool $onlyDeleted | |
6a488035 | 1406 | * |
a6c01b45 CW |
1407 | * @return array |
1408 | * sql query parts as an array | |
6a488035 | 1409 | */ |
00be9182 | 1410 | public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALSE, $onlyDeleted = FALSE) { |
2c3ad4b7 | 1411 | // build permission clause |
1412 | $this->generatePermissionClause($onlyDeleted, $count); | |
1413 | ||
6a488035 | 1414 | if ($count) { |
3dbf477c DS |
1415 | if (isset($this->_rowCountClause)) { |
1416 | $select = "SELECT {$this->_rowCountClause}"; | |
5c7dad85 | 1417 | } |
4c9b6178 | 1418 | elseif (isset($this->_distinctComponentClause)) { |
6a488035 TO |
1419 | // we add distinct to get the right count for components |
1420 | // for the more complex result set, we use GROUP BY the same id | |
1421 | // CRM-9630 | |
9ae25b56 | 1422 | $select = "SELECT count( DISTINCT {$this->_distinctComponentClause} ) as rowCount"; |
6a488035 TO |
1423 | } |
1424 | else { | |
1425 | $select = 'SELECT count(DISTINCT contact_a.id) as rowCount'; | |
1426 | } | |
1427 | $from = $this->_simpleFromClause; | |
1428 | if ($this->_useDistinct) { | |
1429 | $this->_useGroupBy = TRUE; | |
1430 | } | |
1431 | } | |
1432 | elseif ($sortByChar) { | |
caefed7d MWMC |
1433 | // @fixme add the deprecated warning back in (it breaks CRM_Contact_SelectorTest::testSelectorQuery) |
1434 | // CRM_Core_Error::deprecatedFunctionWarning('sort by char is deprecated - use alphabetQuery method'); | |
52cda5dc | 1435 | $select = 'SELECT DISTINCT LEFT(contact_a.sort_name, 1) as sort_name'; |
6a488035 TO |
1436 | $from = $this->_simpleFromClause; |
1437 | } | |
1438 | elseif ($groupContacts) { | |
1439 | $select = 'SELECT contact_a.id as id'; | |
1440 | if ($this->_useDistinct) { | |
1441 | $this->_useGroupBy = TRUE; | |
1442 | } | |
1443 | $from = $this->_simpleFromClause; | |
1444 | } | |
1445 | else { | |
a7488080 | 1446 | if (!empty($this->_paramLookup['group'])) { |
ca91bd7f | 1447 | |
1448 | list($name, $op, $value, $grouping, $wildcard) = $this->_paramLookup['group'][0]; | |
1449 | ||
1450 | if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { | |
1451 | $this->_paramLookup['group'][0][1] = key($value); | |
1452 | } | |
1453 | ||
3875e6b6 | 1454 | // Presumably the lines below come into manage groups screen. |
6a488035 TO |
1455 | // make sure there is only one element |
1456 | // this is used when we are running under smog and need to know | |
1457 | // how the contact was added (CRM-1203) | |
db135a44 | 1458 | $groups = (array) CRM_Utils_Array::value($this->_paramLookup['group'][0][1], $this->_paramLookup['group'][0][2], $this->_paramLookup['group'][0][2]); |
6a488035 | 1459 | if ((count($this->_paramLookup['group']) == 1) && |
7cc09daf | 1460 | (count($groups) == 1) |
6a488035 | 1461 | ) { |
6a488035 TO |
1462 | $groupId = $groups[0]; |
1463 | ||
1464 | //check if group is saved search | |
1465 | $group = new CRM_Contact_BAO_Group(); | |
1466 | $group->id = $groupId; | |
1467 | $group->find(TRUE); | |
1468 | ||
1469 | if (!isset($group->saved_search_id)) { | |
f30aa754 | 1470 | $tbName = "civicrm_group_contact"; |
82ae55f4 | 1471 | // CRM-17254 don't retrieve extra fields if contact_id is specifically requested |
1472 | // as this will add load to an intentionally light query. | |
1473 | // ideally this code would be removed as it appears to be to support CRM-1203 | |
1474 | // and passing in the required returnProperties from the url would | |
1475 | // make more sense that globally applying the requirements of one form. | |
be2fb01f | 1476 | if (($this->_returnProperties != ['contact_id'])) { |
82ae55f4 | 1477 | $this->_select['group_contact_id'] = "$tbName.id as group_contact_id"; |
1478 | $this->_element['group_contact_id'] = 1; | |
1479 | $this->_select['status'] = "$tbName.status as status"; | |
1480 | $this->_element['status'] = 1; | |
1481 | } | |
6a488035 TO |
1482 | } |
1483 | } | |
1484 | $this->_useGroupBy = TRUE; | |
1485 | } | |
1486 | if ($this->_useDistinct && !isset($this->_distinctComponentClause)) { | |
1487 | if (!($this->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY)) { | |
1488 | // CRM-5954 | |
1489 | $this->_select['contact_id'] = 'contact_a.id as contact_id'; | |
1490 | $this->_useDistinct = FALSE; | |
1491 | $this->_useGroupBy = TRUE; | |
1492 | } | |
1493 | } | |
1494 | ||
f946d152 | 1495 | $select = $this->getSelect(); |
6a488035 TO |
1496 | $from = $this->_fromClause; |
1497 | } | |
1498 | ||
1499 | $where = ''; | |
1500 | if (!empty($this->_whereClause)) { | |
1501 | $where = "WHERE {$this->_whereClause}"; | |
1502 | } | |
1503 | ||
0619ff2f | 1504 | if (!empty($this->_permissionWhereClause) && empty($this->_displayRelationshipType)) { |
21c6be28 SL |
1505 | if (!empty($this->_permissionFromClause)) { |
1506 | $from .= " $this->_permissionFromClause"; | |
1507 | } | |
2c3ad4b7 | 1508 | if (empty($where)) { |
1509 | $where = "WHERE $this->_permissionWhereClause"; | |
1510 | } | |
1511 | else { | |
1512 | $where = "$where AND $this->_permissionWhereClause"; | |
1513 | } | |
1514 | } | |
1515 | ||
6a488035 TO |
1516 | $having = ''; |
1517 | if (!empty($this->_having)) { | |
2ef90ca7 EM |
1518 | foreach ($this->_having as $havingSets) { |
1519 | foreach ($havingSets as $havingSet) { | |
1520 | $havingValue[] = $havingSet; | |
6a488035 TO |
1521 | } |
1522 | } | |
2ef90ca7 | 1523 | $having = ' HAVING ' . implode(' AND ', $havingValue); |
6a488035 TO |
1524 | } |
1525 | ||
1526 | // if we are doing a transform, do it here | |
1527 | // use the $from, $where and $having to get the contact ID | |
1528 | if ($this->_displayRelationshipType) { | |
1529 | $this->filterRelatedContacts($from, $where, $having); | |
1530 | } | |
1531 | ||
be2fb01f | 1532 | return [$select, $from, $where, $having]; |
6a488035 TO |
1533 | } |
1534 | ||
86538308 | 1535 | /** |
c037736a | 1536 | * Get where values from the parameters. |
1537 | * | |
100fef9d | 1538 | * @param string $name |
c037736a | 1539 | * @param mixed $grouping |
86538308 | 1540 | * |
c037736a | 1541 | * @return mixed |
86538308 | 1542 | */ |
c037736a | 1543 | public function getWhereValues($name, $grouping) { |
6a488035 | 1544 | $result = NULL; |
f4bff68a | 1545 | foreach ($this->_params as $values) { |
6a488035 TO |
1546 | if ($values[0] == $name && $values[3] == $grouping) { |
1547 | return $values; | |
1548 | } | |
1549 | } | |
1550 | ||
1551 | return $result; | |
1552 | } | |
1553 | ||
86538308 | 1554 | /** |
c037736a | 1555 | * Fix date values. |
1556 | * | |
1557 | * @param bool $relative | |
1558 | * @param string $from | |
1559 | * @param string $to | |
86538308 | 1560 | */ |
00be9182 | 1561 | public static function fixDateValues($relative, &$from, &$to) { |
6a488035 TO |
1562 | if ($relative) { |
1563 | list($from, $to) = CRM_Utils_Date::getFromTo($relative, $from, $to); | |
1564 | } | |
1565 | } | |
1566 | ||
86538308 | 1567 | /** |
bb05da0c | 1568 | * Convert values from form-appropriate to query-object appropriate. |
1569 | * | |
1570 | * The query object is increasingly supporting the sql-filter syntax which is the most flexible syntax. | |
1571 | * So, ideally we would convert all fields to look like | |
1572 | * array( | |
1573 | * 0 => $fieldName | |
1574 | * // Set the operator for legacy reasons, but it is ignored | |
1575 | * 1 => '=' | |
1576 | * // array in sql filter syntax | |
1577 | * 2 => array('BETWEEN' => array(1,60), | |
1578 | * 3 => null | |
1579 | * 4 => null | |
1580 | * ); | |
1581 | * | |
1582 | * There are some examples of the syntax in | |
1583 | * https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples/Relationship | |
1584 | * | |
1585 | * More notes at CRM_Core_DAO::createSQLFilter | |
1586 | * | |
1587 | * and a list of supported operators in CRM_Core_DAO | |
c037736a | 1588 | * |
1589 | * @param array $formValues | |
86538308 EM |
1590 | * @param int $wildcard |
1591 | * @param bool $useEquals | |
1592 | * | |
0e2e76cf EM |
1593 | * @param string $apiEntity |
1594 | * | |
df60621b | 1595 | * @param array $entityReferenceFields |
1596 | * Field names of any entity reference fields (which will need reformatting to IN syntax). | |
1597 | * | |
86538308 EM |
1598 | * @return array |
1599 | */ | |
df60621b | 1600 | public static function convertFormValues(&$formValues, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL, |
be2fb01f CW |
1601 | $entityReferenceFields = []) { |
1602 | $params = []; | |
6a488035 TO |
1603 | if (empty($formValues)) { |
1604 | return $params; | |
1605 | } | |
1606 | ||
20306bb8 | 1607 | self::filterCountryFromValuesIfStateExists($formValues); |
71dfa06c | 1608 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($formValues); |
e34642c9 | 1609 | |
5840d253 | 1610 | foreach ($formValues as $id => $values) { |
df60621b | 1611 | if (self::isAlreadyProcessedForQueryFormat($values)) { |
1612 | $params[] = $values; | |
1613 | continue; | |
1614 | } | |
06d67d53 | 1615 | |
1616 | self::legacyConvertFormValues($id, $values); | |
1617 | ||
46b3417a | 1618 | // The form uses 1 field to represent two db fields |
b832662c | 1619 | if ($id === 'contact_type' && $values && (!is_array($values) || !array_intersect(array_keys($values), CRM_Core_DAO::acceptedSQLOperators()))) { |
be2fb01f CW |
1620 | $contactType = []; |
1621 | $subType = []; | |
46b3417a | 1622 | foreach ((array) $values as $key => $type) { |
f12a86ad | 1623 | $types = explode('__', is_numeric($type) ? $key : $type, 2); |
46b3417a CW |
1624 | $contactType[$types[0]] = $types[0]; |
1625 | // Add sub-type if specified | |
1626 | if (!empty($types[1])) { | |
1627 | $subType[$types[1]] = $types[1]; | |
1628 | } | |
1629 | } | |
be2fb01f | 1630 | $params[] = ['contact_type', 'IN', $contactType, 0, 0]; |
46b3417a | 1631 | if ($subType) { |
be2fb01f | 1632 | $params[] = ['contact_sub_type', 'IN', $subType, 0, 0]; |
46b3417a CW |
1633 | } |
1634 | } | |
b832662c | 1635 | elseif ($id === 'privacy') { |
6a488035 | 1636 | if (is_array($formValues['privacy'])) { |
0d8afee2 | 1637 | $op = !empty($formValues['privacy']['do_not_toggle']) ? '=' : '!='; |
6a488035 TO |
1638 | foreach ($formValues['privacy'] as $key => $value) { |
1639 | if ($value) { | |
be2fb01f | 1640 | $params[] = [$key, $op, $value, 0, 0]; |
6a488035 TO |
1641 | } |
1642 | } | |
1643 | } | |
1644 | } | |
9de9a433 | 1645 | elseif ($id === 'email_on_hold') { |
38056b36 | 1646 | if ($onHoldValue = CRM_Utils_Array::value('email_on_hold', $formValues)) { |
1d94ee08 | 1647 | // onHoldValue should be 0 or 1 or an array. Some legacy groups may hold '' |
1648 | // so in 5.11 we have an extra if that should become redundant over time. | |
1649 | // https://lab.civicrm.org/dev/core/issues/745 | |
1650 | // @todo this renaming of email_on_hold to on_hold needs revisiting | |
b832662c | 1651 | // it precedes recent changes but causes the default not to reload. |
828c2392 | 1652 | $onHoldValue = array_filter((array) $onHoldValue, 'is_numeric'); |
1653 | if (!empty($onHoldValue)) { | |
1d94ee08 | 1654 | $params[] = ['on_hold', 'IN', $onHoldValue, 0, 0]; |
1655 | } | |
6a488035 TO |
1656 | } |
1657 | } | |
9de9a433 | 1658 | elseif (substr($id, 0, 7) === 'custom_' |
4c2fe77b | 1659 | && ( |
9de9a433 | 1660 | substr($id, -5, 5) === '_from' |
1661 | || substr($id, -3, 3) === '_to' | |
4c2fe77b | 1662 | ) |
bb05da0c | 1663 | ) { |
4c2fe77b | 1664 | self::convertCustomRelativeFields($formValues, $params, $values, $id); |
bb05da0c | 1665 | } |
df60621b | 1666 | elseif (in_array($id, $entityReferenceFields) && !empty($values) && is_string($values) && (strpos($values, ',') != |
1667 | FALSE)) { | |
be2fb01f | 1668 | $params[] = [$id, 'IN', explode(',', $values), 0, 0]; |
df60621b | 1669 | } |
6a488035 | 1670 | else { |
3c151c70 | 1671 | $values = CRM_Contact_BAO_Query::fixWhereValues($id, $values, $wildcard, $useEquals, $apiEntity); |
6a488035 TO |
1672 | |
1673 | if (!$values) { | |
1674 | continue; | |
1675 | } | |
1676 | $params[] = $values; | |
1677 | } | |
1678 | } | |
1679 | return $params; | |
1680 | } | |
1681 | ||
e34642c9 | 1682 | /** |
1683 | * Function to support legacy format for groups and tags. | |
1684 | * | |
1685 | * @param string $id | |
1686 | * @param array|int $values | |
1687 | * | |
1688 | */ | |
5450bf88 | 1689 | public static function legacyConvertFormValues($id, &$values) { |
be2fb01f | 1690 | $legacyElements = [ |
06d67d53 | 1691 | 'group', |
1692 | 'tag', | |
1693 | 'contact_tags', | |
1694 | 'contact_type', | |
1695 | 'membership_type_id', | |
1696 | 'membership_status_id', | |
be2fb01f | 1697 | ]; |
06d67d53 | 1698 | if (in_array($id, $legacyElements) && is_array($values)) { |
5450bf88 | 1699 | // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1), |
1700 | // as per the recent Search fixes $values need to be in standard array(id1, id2) format | |
e5ad0335 | 1701 | $values = CRM_Utils_Array::convertCheckboxFormatToArray($values); |
5450bf88 | 1702 | } |
e34642c9 | 1703 | } |
1704 | ||
86538308 | 1705 | /** |
c037736a | 1706 | * Fix values from query from/to something no-one cared enough to document. |
1707 | * | |
100fef9d | 1708 | * @param int $id |
c037736a | 1709 | * @param array $values |
86538308 EM |
1710 | * @param int $wildcard |
1711 | * @param bool $useEquals | |
1712 | * | |
0e2e76cf EM |
1713 | * @param string $apiEntity |
1714 | * | |
86538308 EM |
1715 | * @return array|null |
1716 | */ | |
c037736a | 1717 | public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals = FALSE, $apiEntity = NULL) { |
6a488035 TO |
1718 | // skip a few search variables |
1719 | static $skipWhere = NULL; | |
6a488035 TO |
1720 | static $likeNames = NULL; |
1721 | $result = NULL; | |
57f8e7f0 | 1722 | |
244bbdd8 CW |
1723 | // Change camelCase EntityName to lowercase with underscores |
1724 | $apiEntity = _civicrm_api_get_entity_name_from_camel($apiEntity); | |
6a488035 | 1725 | |
57f8e7f0 | 1726 | // check if $value is in OK (Operator as Key) format as used by Get API |
6a488035 TO |
1727 | if (CRM_Utils_System::isNull($values)) { |
1728 | return $result; | |
1729 | } | |
1730 | ||
1731 | if (!$skipWhere) { | |
be2fb01f | 1732 | $skipWhere = [ |
5c7dad85 TO |
1733 | 'task', |
1734 | 'radio_ts', | |
1735 | 'uf_group_id', | |
1736 | 'component_mode', | |
1737 | 'qfKey', | |
1738 | 'operator', | |
6a488035 | 1739 | 'display_relationship_type', |
be2fb01f | 1740 | ]; |
6a488035 TO |
1741 | } |
1742 | ||
1743 | if (in_array($id, $skipWhere) || | |
1744 | substr($id, 0, 4) == '_qf_' || | |
1745 | substr($id, 0, 7) == 'hidden_' | |
1746 | ) { | |
1747 | return $result; | |
1748 | } | |
1749 | ||
afa0b07c | 1750 | if ($apiEntity && |
1751 | (substr($id, 0, strlen($apiEntity)) != $apiEntity) && | |
1a3e22cc MM |
1752 | (substr($id, 0, 10) != 'financial_' && substr($id, 0, 8) != 'payment_') && |
1753 | (substr($id, 0, 7) != 'custom_') | |
afa0b07c | 1754 | ) { |
3c151c70 | 1755 | $id = $apiEntity . '_' . $id; |
1756 | } | |
1757 | ||
6a488035 | 1758 | if (!$likeNames) { |
be2fb01f | 1759 | $likeNames = ['sort_name', 'email', 'note', 'display_name']; |
6a488035 TO |
1760 | } |
1761 | ||
1762 | // email comes in via advanced search | |
1763 | // so use wildcard always | |
1764 | if ($id == 'email') { | |
1765 | $wildcard = 1; | |
1766 | } | |
1767 | ||
5c7dad85 | 1768 | if (!$useEquals && in_array($id, $likeNames)) { |
be2fb01f | 1769 | $result = [$id, 'LIKE', $values, 0, 1]; |
6a488035 TO |
1770 | } |
1771 | elseif (is_string($values) && strpos($values, '%') !== FALSE) { | |
be2fb01f | 1772 | $result = [$id, 'LIKE', $values, 0, 0]; |
6a488035 | 1773 | } |
a0cbe4fa | 1774 | elseif ($id == 'contact_type' || |
1775 | (!empty($values) && is_array($values) && !in_array(key($values), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) | |
1776 | ) { | |
be2fb01f | 1777 | $result = [$id, 'IN', $values, 0, $wildcard]; |
c33e01a5 | 1778 | } |
6a488035 | 1779 | else { |
be2fb01f | 1780 | $result = [$id, '=', $values, 0, $wildcard]; |
6a488035 TO |
1781 | } |
1782 | ||
1783 | return $result; | |
1784 | } | |
1785 | ||
86538308 | 1786 | /** |
c037736a | 1787 | * Get the where clause for a single field. |
1788 | * | |
1789 | * @param array $values | |
9178793e | 1790 | * @param bool $isForcePrimaryOnly |
1791 | * | |
1792 | * @throws \CRM_Core_Exception | |
86538308 | 1793 | */ |
9178793e | 1794 | public function whereClauseSingle(&$values, $isForcePrimaryOnly = FALSE) { |
8fc9f99a | 1795 | if ($this->isARelativeDateField($values[0])) { |
1796 | $this->buildRelativeDateQuery($values); | |
1797 | return; | |
1798 | } | |
33a17d7e | 1799 | // @todo also handle _low, _high generically here with if ($query->buildDateRangeQuery($values)) {return} |
8fc9f99a | 1800 | |
6a488035 TO |
1801 | // do not process custom fields or prefixed contact ids or component params |
1802 | if (CRM_Core_BAO_CustomField::getKeyID($values[0]) || | |
1803 | (substr($values[0], 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) || | |
b832662c | 1804 | (substr($values[0], 0, 13) === 'contribution_') || |
1805 | (substr($values[0], 0, 6) === 'event_') || | |
1806 | (substr($values[0], 0, 12) === 'participant_') || | |
1807 | (substr($values[0], 0, 7) === 'member_') || | |
1808 | (substr($values[0], 0, 6) === 'grant_') || | |
1809 | (substr($values[0], 0, 7) === 'pledge_') || | |
1810 | (substr($values[0], 0, 5) === 'case_') || | |
1811 | (substr($values[0], 0, 10) === 'financial_') || | |
1812 | (substr($values[0], 0, 8) === 'payment_') || | |
1813 | (substr($values[0], 0, 11) === 'membership_') | |
58bcc9ae | 1814 | // temporary fix for regression https://lab.civicrm.org/dev/core/issues/1551 |
1815 | // ideally the metadata would allow this field to be parsed below & the special handling would not | |
1816 | // be needed. | |
1817 | || $values[0] === 'mailing_id' | |
6a488035 TO |
1818 | ) { |
1819 | return; | |
1820 | } | |
1821 | ||
efa3a566 DS |
1822 | // skip for hook injected fields / params |
1823 | $extFields = CRM_Contact_BAO_Query_Hook::singleton()->getFields(); | |
1824 | if (array_key_exists($values[0], $extFields)) { | |
1825 | return; | |
1826 | } | |
1827 | ||
6a488035 TO |
1828 | switch ($values[0]) { |
1829 | case 'deleted_contacts': | |
1830 | $this->deletedContacts($values); | |
1831 | return; | |
1832 | ||
6a488035 TO |
1833 | case 'contact_sub_type': |
1834 | $this->contactSubType($values); | |
1835 | return; | |
1836 | ||
1837 | case 'group': | |
6a488035 | 1838 | case 'group_type': |
6a488035 TO |
1839 | $this->group($values); |
1840 | return; | |
6a488035 | 1841 | |
c905b59f | 1842 | // case tag comes from find contacts |
6a488035 TO |
1843 | case 'tag_search': |
1844 | $this->tagSearch($values); | |
1845 | return; | |
1846 | ||
1847 | case 'tag': | |
1848 | case 'contact_tags': | |
1849 | $this->tag($values); | |
1850 | return; | |
1851 | ||
1852 | case 'note': | |
1853 | case 'note_body': | |
1854 | case 'note_subject': | |
1855 | $this->notes($values); | |
1856 | return; | |
1857 | ||
1858 | case 'uf_user': | |
1859 | $this->ufUser($values); | |
1860 | return; | |
1861 | ||
1862 | case 'sort_name': | |
1863 | case 'display_name': | |
1864 | $this->sortName($values); | |
6a488035 TO |
1865 | return; |
1866 | ||
02adf2f9 BS |
1867 | case 'addressee': |
1868 | case 'postal_greeting': | |
1869 | case 'email_greeting': | |
1870 | $this->greetings($values); | |
1871 | return; | |
1872 | ||
6a488035 | 1873 | case 'email': |
57f8e7f0 | 1874 | case 'email_id': |
9178793e | 1875 | $this->email($values, $isForcePrimaryOnly); |
6a488035 TO |
1876 | return; |
1877 | ||
1878 | case 'phone_numeric': | |
1879 | $this->phone_numeric($values); | |
1880 | return; | |
1881 | ||
1882 | case 'phone_phone_type_id': | |
1883 | case 'phone_location_type_id': | |
1884 | $this->phone_option_group($values); | |
1885 | return; | |
1886 | ||
1887 | case 'street_address': | |
1888 | $this->street_address($values); | |
1889 | return; | |
1890 | ||
1891 | case 'street_number': | |
1892 | $this->street_number($values); | |
1893 | return; | |
1894 | ||
1895 | case 'sortByCharacter': | |
1896 | $this->sortByCharacter($values); | |
1897 | return; | |
1898 | ||
1899 | case 'location_type': | |
1900 | $this->locationType($values); | |
1901 | return; | |
1902 | ||
1903 | case 'county': | |
1904 | $this->county($values); | |
1905 | return; | |
1906 | ||
1907 | case 'state_province': | |
3493947a | 1908 | case 'state_province_id': |
1909 | case 'state_province_name': | |
6a488035 TO |
1910 | $this->stateProvince($values); |
1911 | return; | |
1912 | ||
1913 | case 'country': | |
3493947a | 1914 | case 'country_id': |
6a488035 TO |
1915 | $this->country($values, FALSE); |
1916 | return; | |
1917 | ||
1918 | case 'postal_code': | |
1919 | case 'postal_code_low': | |
1920 | case 'postal_code_high': | |
1921 | $this->postalCode($values); | |
1922 | return; | |
1923 | ||
1924 | case 'activity_date': | |
1925 | case 'activity_date_low': | |
1926 | case 'activity_date_high': | |
27cedb98 | 1927 | case 'activity_date_time_low': |
1928 | case 'activity_date_time_high': | |
6a488035 | 1929 | case 'activity_role': |
9ab34172 | 1930 | case 'activity_status_id': |
6a488035 | 1931 | case 'activity_status': |
da236f9a | 1932 | case 'activity_priority': |
1933 | case 'activity_priority_id': | |
4c24c842 PN |
1934 | case 'followup_parent_id': |
1935 | case 'parent_id': | |
9ab34172 | 1936 | case 'source_contact_id': |
92e9c781 | 1937 | case 'activity_text': |
c9c5c58d | 1938 | case 'activity_option': |
6a488035 TO |
1939 | case 'test_activities': |
1940 | case 'activity_type_id': | |
3e4a4597 | 1941 | case 'activity_type': |
6a488035 TO |
1942 | case 'activity_survey_id': |
1943 | case 'activity_tags': | |
1944 | case 'activity_taglist': | |
1945 | case 'activity_test': | |
6a488035 TO |
1946 | case 'activity_campaign_id': |
1947 | case 'activity_engagement_level': | |
1948 | case 'activity_id': | |
4041c9b6 | 1949 | case 'activity_result': |
6a488035 TO |
1950 | case 'source_contact': |
1951 | CRM_Activity_BAO_Query::whereClauseSingle($values, $this); | |
1952 | return; | |
1953 | ||
c4a7c967 | 1954 | case 'age_low': |
1955 | case 'age_high': | |
6a488035 TO |
1956 | case 'birth_date_low': |
1957 | case 'birth_date_high': | |
1958 | case 'deceased_date_low': | |
1959 | case 'deceased_date_high': | |
1960 | $this->demographics($values); | |
1961 | return; | |
1962 | ||
c4a7c967 | 1963 | case 'age_asof_date': |
1964 | // handled by demographics | |
1965 | return; | |
1966 | ||
6a488035 TO |
1967 | case 'log_date_low': |
1968 | case 'log_date_high': | |
1969 | $this->modifiedDates($values); | |
1970 | return; | |
1971 | ||
1972 | case 'changed_by': | |
1973 | $this->changeLog($values); | |
1974 | return; | |
1975 | ||
1976 | case 'do_not_phone': | |
1977 | case 'do_not_email': | |
1978 | case 'do_not_mail': | |
1979 | case 'do_not_sms': | |
1980 | case 'do_not_trade': | |
1981 | case 'is_opt_out': | |
1982 | $this->privacy($values); | |
1983 | return; | |
1984 | ||
1985 | case 'privacy_options': | |
1986 | $this->privacyOptions($values); | |
1987 | return; | |
1988 | ||
1989 | case 'privacy_operator': | |
1990 | case 'privacy_toggle': | |
1991 | // these are handled by privacy options | |
1992 | return; | |
1993 | ||
1994 | case 'preferred_communication_method': | |
1995 | $this->preferredCommunication($values); | |
1996 | return; | |
1997 | ||
1998 | case 'relation_type_id': | |
41b8dd1d | 1999 | case 'relationship_start_date_high': |
2000 | case 'relationship_start_date_low': | |
2001 | case 'relationship_end_date_high': | |
2002 | case 'relationship_end_date_low': | |
eea5db81 | 2003 | case 'relation_active_period_date_high': |
2004 | case 'relation_active_period_date_low': | |
6a488035 TO |
2005 | case 'relation_target_name': |
2006 | case 'relation_status': | |
6e83b317 | 2007 | case 'relation_description': |
6a488035 TO |
2008 | case 'relation_date_low': |
2009 | case 'relation_date_high': | |
7c96365b | 2010 | $this->relationship($values); |
2011 | $this->_relationshipValuesAdded = TRUE; | |
6a488035 TO |
2012 | return; |
2013 | ||
2014 | case 'task_status_id': | |
2015 | $this->task($values); | |
2016 | return; | |
2017 | ||
2018 | case 'task_id': | |
2019 | // since this case is handled with the above | |
2020 | return; | |
2021 | ||
2022 | case 'prox_distance': | |
2023 | CRM_Contact_BAO_ProximityQuery::process($this, $values); | |
2024 | return; | |
2025 | ||
2026 | case 'prox_street_address': | |
2027 | case 'prox_city': | |
2028 | case 'prox_postal_code': | |
2029 | case 'prox_state_province_id': | |
2030 | case 'prox_country_id': | |
4b2d36d7 | 2031 | case 'prox_geo_code_1': |
2032 | case 'prox_geo_code_2': | |
6a488035 TO |
2033 | // handled by the proximity_distance clause |
2034 | return; | |
2035 | ||
2036 | default: | |
2037 | $this->restWhere($values); | |
2038 | return; | |
2039 | } | |
2040 | } | |
2041 | ||
2042 | /** | |
c037736a | 2043 | * Given a list of conditions in params generate the required where clause. |
6a488035 | 2044 | * |
9178793e | 2045 | * @param bool $isForcePrimaryEmailOnly |
57f8e7f0 | 2046 | * |
fffe9ee1 | 2047 | * @return string |
9178793e | 2048 | * @throws \CRM_Core_Exception |
6a488035 | 2049 | */ |
8489e9e0 | 2050 | public function whereClause($isForcePrimaryEmailOnly = NULL): string { |
be2fb01f CW |
2051 | $this->_where[0] = []; |
2052 | $this->_qill[0] = []; | |
6a488035 | 2053 | |
b832662c | 2054 | $this->includeContactIDs(); |
6a488035 | 2055 | if (!empty($this->_params)) { |
6a488035 | 2056 | foreach (array_keys($this->_params) as $id) { |
a7488080 | 2057 | if (empty($this->_params[$id][0])) { |
6a488035 TO |
2058 | continue; |
2059 | } | |
2060 | // check for both id and contact_id | |
2061 | if ($this->_params[$id][0] == 'id' || $this->_params[$id][0] == 'contact_id') { | |
f819588f | 2062 | $this->_where[0][] = self::buildClause("contact_a.id", $this->_params[$id][1], $this->_params[$id][2]); |
9c1bc317 | 2063 | $field = $this->_fields['id'] ?? NULL; |
9ea799a3 PN |
2064 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue( |
2065 | 'CRM_Contact_BAO_Contact', | |
2066 | "contact_a.id", | |
2067 | $this->_params[$id][2], | |
2068 | $this->_params[$id][1] | |
2069 | ); | |
2070 | $this->_qill[0][] = ts("%1 %2 %3", [ | |
33974098 | 2071 | 1 => $field['title'] ?? '', |
9ea799a3 | 2072 | 2 => $qillop, |
69078420 | 2073 | 3 => $qillVal, |
9ea799a3 | 2074 | ]); |
6a488035 TO |
2075 | } |
2076 | else { | |
9178793e | 2077 | $this->whereClauseSingle($this->_params[$id], $isForcePrimaryEmailOnly); |
6a488035 TO |
2078 | } |
2079 | } | |
2080 | ||
2081 | CRM_Core_Component::alterQuery($this, 'where'); | |
f2a9b25c | 2082 | |
99e9587a | 2083 | CRM_Contact_BAO_Query_Hook::singleton()->alterSearchQuery($this, 'where'); |
6a488035 TO |
2084 | } |
2085 | ||
2086 | if ($this->_customQuery) { | |
2206409b | 2087 | $this->_whereTables = array_merge($this->_whereTables, $this->_customQuery->_whereTables); |
c037736a | 2088 | // Added following if condition to avoid the wrong value display for 'my account' / any UF info. |
6a488035 TO |
2089 | // Hope it wont affect the other part of civicrm.. if it does please remove it. |
2090 | if (!empty($this->_customQuery->_where)) { | |
2091 | $this->_where = CRM_Utils_Array::crmArrayMerge($this->_where, $this->_customQuery->_where); | |
2092 | } | |
6a488035 TO |
2093 | $this->_qill = CRM_Utils_Array::crmArrayMerge($this->_qill, $this->_customQuery->_qill); |
2094 | } | |
2095 | ||
be2fb01f CW |
2096 | $clauses = []; |
2097 | $andClauses = []; | |
6a488035 TO |
2098 | |
2099 | $validClauses = 0; | |
2100 | if (!empty($this->_where)) { | |
2101 | foreach ($this->_where as $grouping => $values) { | |
2102 | if ($grouping > 0 && !empty($values)) { | |
2103 | $clauses[$grouping] = ' ( ' . implode(" {$this->_operator} ", $values) . ' ) '; | |
2104 | $validClauses++; | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | if (!empty($this->_where[0])) { | |
2109 | $andClauses[] = ' ( ' . implode(" {$this->_operator} ", $this->_where[0]) . ' ) '; | |
2110 | } | |
2111 | if (!empty($clauses)) { | |
2112 | $andClauses[] = ' ( ' . implode(' OR ', $clauses) . ' ) '; | |
2113 | } | |
2114 | ||
2115 | if ($validClauses > 1) { | |
2116 | $this->_useDistinct = TRUE; | |
2117 | } | |
2118 | } | |
2119 | ||
56f5e9db | 2120 | return $andClauses ? implode(' AND ', $andClauses) : ' 1 '; |
6a488035 TO |
2121 | } |
2122 | ||
86538308 | 2123 | /** |
c037736a | 2124 | * Generate where clause for any parameters not already handled. |
2125 | * | |
2126 | * @param array $values | |
86538308 EM |
2127 | * |
2128 | * @throws Exception | |
2129 | */ | |
00be9182 | 2130 | public function restWhere(&$values) { |
9c1bc317 CW |
2131 | $name = $values[0] ?? NULL; |
2132 | $op = $values[1] ?? NULL; | |
2133 | $value = $values[2] ?? NULL; | |
2134 | $grouping = $values[3] ?? NULL; | |
2135 | $wildcard = $values[4] ?? NULL; | |
6a488035 | 2136 | |
8cc574cf | 2137 | if (isset($grouping) && empty($this->_where[$grouping])) { |
be2fb01f | 2138 | $this->_where[$grouping] = []; |
6a488035 TO |
2139 | } |
2140 | ||
be2fb01f | 2141 | $multipleFields = ['url']; |
6a488035 | 2142 | |
3493947a | 2143 | //check if the location type exists for fields |
6a488035 TO |
2144 | $lType = ''; |
2145 | $locType = explode('-', $name); | |
2146 | ||
2147 | if (!in_array($locType[0], $multipleFields)) { | |
2148 | //add phone type if exists | |
2149 | if (isset($locType[2]) && $locType[2]) { | |
2150 | $locType[2] = CRM_Core_DAO::escapeString($locType[2]); | |
2151 | } | |
2152 | } | |
2153 | ||
9c1bc317 | 2154 | $field = $this->_fields[$name] ?? NULL; |
6a488035 TO |
2155 | |
2156 | if (!$field) { | |
9c1bc317 | 2157 | $field = $this->_fields[$locType[0]] ?? NULL; |
6a488035 TO |
2158 | |
2159 | if (!$field) { | |
5b5ea9b6 | 2160 | // Strip any trailing _high & _low that might be appended. |
2161 | $realFieldName = str_replace(['_high', '_low'], '', $name); | |
2162 | if (isset($this->_fields[$realFieldName])) { | |
2163 | $field = $this->_fields[str_replace(['_high', '_low'], '', $realFieldName)]; | |
2206409b | 2164 | $columnName = $field['column_name'] ?? $field['name']; |
2165 | $this->dateQueryBuilder($values, $field['table_name'], $realFieldName, $columnName, $field['title']); | |
5b5ea9b6 | 2166 | } |
6a488035 TO |
2167 | return; |
2168 | } | |
2169 | } | |
2170 | ||
2171 | $setTables = TRUE; | |
2172 | ||
db1a73f5 | 2173 | $locationType = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate'); |
fdfca3e9 VG |
2174 | if (isset($locType[1]) && is_numeric($locType[1])) { |
2175 | $lType = $locationType[$locType[1]]; | |
2176 | } | |
2177 | if ($lType) { | |
2178 | $field['title'] .= " ($lType)"; | |
2179 | } | |
6a488035 TO |
2180 | |
2181 | if (substr($name, 0, 14) === 'state_province') { | |
2182 | if (isset($locType[1]) && is_numeric($locType[1])) { | |
2e155dbd | 2183 | $setTables = FALSE; |
fdfca3e9 | 2184 | $aName = "{$lType}-address"; |
d9ab802d | 2185 | $where = "`$aName`.state_province_id"; |
6a488035 TO |
2186 | } |
2187 | else { | |
d9ab802d | 2188 | $where = "civicrm_address.state_province_id"; |
6a488035 TO |
2189 | } |
2190 | ||
8ab09481 | 2191 | $this->_where[$grouping][] = self::buildClause($where, $op, $value); |
8f0fd14c | 2192 | $this->_tables[$aName] = $this->_whereTables[$aName] = 1; |
7cc09daf | 2193 | list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op); |
be2fb01f | 2194 | $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['title'], 2 => $qillop, 3 => $qillVal]); |
6a488035 | 2195 | } |
1c1a84a2 | 2196 | elseif (!empty($field['pseudoconstant'])) { |
9724097e | 2197 | // For the hacked fields we want to undo the hack to type to avoid missing the index by adding quotes. |
2198 | $dataType = !empty($this->legacyHackedFields[$name]) ? CRM_Utils_Type::T_INT : $field['type']; | |
1071730c DL |
2199 | $this->optionValueQuery( |
2200 | $name, $op, $value, $grouping, | |
9b183452 | 2201 | 'CRM_Contact_DAO_Contact', |
1071730c | 2202 | $field, |
9ac33d0f | 2203 | $field['html']['label'] ?? $field['title'], |
bf0f0d19 | 2204 | CRM_Utils_Type::typeToString($dataType) |
1071730c | 2205 | ); |
9724097e | 2206 | if ($name === 'gender_id') { |
1071730c DL |
2207 | self::$_openedPanes[ts('Demographics')] = TRUE; |
2208 | } | |
2209 | } | |
9b183452 | 2210 | elseif (substr($name, 0, 7) === 'country' || substr($name, 0, 6) === 'county') { |
2211 | $name = (substr($name, 0, 7) === 'country') ? "country_id" : "county_id"; | |
6a488035 | 2212 | if (isset($locType[1]) && is_numeric($locType[1])) { |
2e155dbd | 2213 | $setTables = FALSE; |
fdfca3e9 | 2214 | $aName = "{$lType}-address"; |
9b183452 | 2215 | $where = "`$aName`.$name"; |
6a488035 TO |
2216 | } |
2217 | else { | |
9b183452 | 2218 | $where = "civicrm_address.$name"; |
6a488035 | 2219 | } |
d9ab802d | 2220 | |
9b183452 | 2221 | $this->_where[$grouping][] = self::buildClause($where, $op, $value, 'Positive'); |
8f0fd14c | 2222 | $this->_tables[$aName] = $this->_whereTables[$aName] = 1; |
6a488035 | 2223 | |
9d9cfb9f | 2224 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op); |
be2fb01f | 2225 | $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['title'], 2 => $qillop, 3 => $qillVal]); |
6a488035 TO |
2226 | } |
2227 | elseif ($name === 'world_region') { | |
2228 | $this->optionValueQuery( | |
2229 | $name, $op, $value, $grouping, | |
728eefa0 | 2230 | NULL, |
6a488035 | 2231 | $field, |
8b695e91 | 2232 | ts('World Region'), |
bf0f0d19 | 2233 | 'Positive' |
6a488035 TO |
2234 | ); |
2235 | } | |
6a488035 | 2236 | elseif ($name === 'is_deceased') { |
962f4484 | 2237 | $this->setQillAndWhere($name, $op, $value, $grouping, $field); |
6a488035 TO |
2238 | self::$_openedPanes[ts('Demographics')] = TRUE; |
2239 | } | |
d820f4e8 | 2240 | elseif ($name === 'created_date' || $name === 'modified_date' || $name === 'deceased_date' || $name === 'birth_date') { |
ee657a1c | 2241 | $appendDateTime = TRUE; |
d820f4e8 | 2242 | if ($name === 'deceased_date' || $name === 'birth_date') { |
ee657a1c | 2243 | $appendDateTime = FALSE; |
d820f4e8 | 2244 | self::$_openedPanes[ts('Demographics')] = TRUE; |
2245 | } | |
0b83b6c2 | 2246 | $this->dateQueryBuilder($values, 'contact_a', $name, $name, $field['title'], $appendDateTime); |
d820f4e8 | 2247 | } |
6a488035 TO |
2248 | elseif ($name === 'contact_id') { |
2249 | if (is_int($value)) { | |
2250 | $this->_where[$grouping][] = self::buildClause($field['where'], $op, $value); | |
2251 | $this->_qill[$grouping][] = "$field[title] $op $value"; | |
2252 | } | |
2253 | } | |
2254 | elseif ($name === 'name') { | |
2bde57c6 | 2255 | $value = CRM_Core_DAO::escapeString($value); |
6a488035 | 2256 | if ($wildcard) { |
6a488035 | 2257 | $op = 'LIKE'; |
0b38e8f1 | 2258 | $value = self::getWildCardedValue($wildcard, $op, $value); |
6a488035 | 2259 | } |
2bde57c6 | 2260 | CRM_Core_Error::deprecatedFunctionWarning('Untested code path'); |
2261 | // @todo it's likely this code path is obsolete / never called. It is definitely not | |
2262 | // passed through in our test suite. | |
2263 | $this->_where[$grouping][] = self::buildClause($field['where'], $op, "'$value'"); | |
6a488035 TO |
2264 | $this->_qill[$grouping][] = "$field[title] $op \"$value\""; |
2265 | } | |
2266 | elseif ($name === 'current_employer') { | |
6a488035 | 2267 | if ($wildcard) { |
6a488035 | 2268 | $op = 'LIKE'; |
0b38e8f1 | 2269 | $value = self::getWildCardedValue($wildcard, $op, $value); |
6a488035 | 2270 | } |
30415e03 | 2271 | $ceWhereClause = self::buildClause("contact_a.organization_name", $op, |
0d77b56a | 2272 | $value |
6a488035 | 2273 | ); |
0d77b56a PJ |
2274 | $ceWhereClause .= " AND contact_a.contact_type = 'Individual'"; |
2275 | $this->_where[$grouping][] = $ceWhereClause; | |
6a488035 TO |
2276 | $this->_qill[$grouping][] = "$field[title] $op \"$value\""; |
2277 | } | |
6a488035 TO |
2278 | elseif (substr($name, 0, 4) === 'url-') { |
2279 | $tName = 'civicrm_website'; | |
2280 | $this->_whereTables[$tName] = $this->_tables[$tName] = "\nLEFT JOIN civicrm_website ON ( civicrm_website.contact_id = contact_a.id )"; | |
2fc64082 | 2281 | $value = CRM_Core_DAO::escapeString($value); |
6a488035 | 2282 | if ($wildcard) { |
6a488035 | 2283 | $op = 'LIKE'; |
0b38e8f1 | 2284 | $value = self::getWildCardedValue($wildcard, $op, $value); |
6a488035 TO |
2285 | } |
2286 | ||
962f4484 | 2287 | $this->_where[$grouping][] = $d = self::buildClause('civicrm_website.url', $op, $value); |
6a488035 TO |
2288 | $this->_qill[$grouping][] = "$field[title] $op \"$value\""; |
2289 | } | |
2290 | elseif ($name === 'contact_is_deleted') { | |
962f4484 | 2291 | $this->setQillAndWhere('is_deleted', $op, $value, $grouping, $field); |
6a488035 | 2292 | } |
efb88612 | 2293 | elseif (!empty($field['where'])) { |
2294 | $type = NULL; | |
2295 | if (!empty($field['type'])) { | |
2296 | $type = CRM_Utils_Type::typeToString($field['type']); | |
2297 | } | |
2298 | ||
2299 | list($tableName, $fieldName) = explode('.', $field['where'], 2); | |
2300 | ||
2301 | if (isset($locType[1]) && | |
2302 | is_numeric($locType[1]) | |
2303 | ) { | |
2304 | $setTables = FALSE; | |
2305 | ||
2306 | //get the location name | |
2307 | list($tName, $fldName) = self::getLocationTableName($field['where'], $locType); | |
54e02ce8 | 2308 | $fieldName = "`$tName`.$fldName"; |
efb88612 | 2309 | |
2310 | // we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should | |
2311 | $this->_tables[$tName] = $this->_whereTables[$tName] = 1; | |
2312 | ||
2313 | } | |
2314 | else { | |
2315 | if ($tableName == 'civicrm_contact') { | |
1809f3cf | 2316 | $fieldName = "contact_a.{$fieldName}"; |
efb88612 | 2317 | } |
2318 | else { | |
7fde0061 | 2319 | $fieldName = $field['where']; |
efb88612 | 2320 | } |
2321 | } | |
2322 | ||
2323 | list($qillop, $qillVal) = self::buildQillForFieldValue(NULL, $field['title'], $value, $op); | |
be2fb01f | 2324 | $this->_qill[$grouping][] = ts("%1 %2 %3", [ |
9b183452 | 2325 | 1 => $field['title'], |
2326 | 2 => $qillop, | |
69078420 SL |
2327 | 3 => (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) ? $qillVal : "'$qillVal'", |
2328 | ]); | |
efb88612 | 2329 | |
6a488035 | 2330 | if (is_array($value)) { |
a75c13cc EM |
2331 | // traditionally an array being passed has been a fatal error. We can take advantage of this to add support |
2332 | // for api style operators for functions that hit this point without worrying about regression | |
2333 | // (the previous comments indicated the condition for hitting this point were unknown | |
2334 | // per CRM-14743 we are adding modified_date & created_date operator support | |
2335 | $operations = array_keys($value); | |
2336 | foreach ($operations as $operator) { | |
11a5aa5d | 2337 | if (!in_array($operator, CRM_Core_DAO::acceptedSQLOperators())) { |
efb88612 | 2338 | //Via Contact get api value is not in array(operator => array(values)) format ONLY for IN/NOT IN operators |
2339 | //so this condition will satisfy the search for now | |
0a74debb | 2340 | if (strpos($op, 'IN') !== FALSE) { |
be2fb01f | 2341 | $value = [$op => $value]; |
efb88612 | 2342 | } |
a75c13cc | 2343 | // we don't know when this might happen |
efb88612 | 2344 | else { |
79e11805 | 2345 | throw new CRM_Core_Exception(ts("%1 is not a valid operator", [1 => $operator])); |
efb88612 | 2346 | } |
a75c13cc EM |
2347 | } |
2348 | } | |
efb88612 | 2349 | $this->_where[$grouping][] = CRM_Core_DAO::createSQLFilter($fieldName, $value, $type); |
6a488035 | 2350 | } |
efb88612 | 2351 | else { |
6a488035 | 2352 | if ($wildcard) { |
6a488035 | 2353 | $op = 'LIKE'; |
0b38e8f1 | 2354 | $value = self::getWildCardedValue($wildcard, $op, $value); |
6a488035 TO |
2355 | } |
2356 | ||
efb88612 | 2357 | $this->_where[$grouping][] = self::buildClause($fieldName, $op, $value, $type); |
6a488035 TO |
2358 | } |
2359 | } | |
2360 | ||
2361 | if ($setTables && isset($field['where'])) { | |
2362 | list($tableName, $fieldName) = explode('.', $field['where'], 2); | |
2363 | if (isset($tableName)) { | |
2364 | $this->_tables[$tableName] = 1; | |
2365 | $this->_whereTables[$tableName] = 1; | |
2366 | } | |
2367 | } | |
2368 | } | |
2369 | ||
86538308 EM |
2370 | /** |
2371 | * @param $where | |
2372 | * @param $locType | |
2373 | * | |
2374 | * @return array | |
2375 | * @throws Exception | |
2376 | */ | |
00be9182 | 2377 | public static function getLocationTableName(&$where, &$locType) { |
6a488035 TO |
2378 | if (isset($locType[1]) && is_numeric($locType[1])) { |
2379 | list($tbName, $fldName) = explode(".", $where); | |
2380 | ||
2381 | //get the location name | |
f9ba4d01 | 2382 | $locationType = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate'); |
be2fb01f | 2383 | $specialFields = ['email', 'im', 'phone', 'openid', 'phone_ext']; |
6a488035 TO |
2384 | if (in_array($locType[0], $specialFields)) { |
2385 | //hack to fix / special handing for phone_ext | |
2386 | if ($locType[0] == 'phone_ext') { | |
2387 | $locType[0] = 'phone'; | |
2388 | } | |
2389 | if (isset($locType[2]) && $locType[2]) { | |
2390 | $tName = "{$locationType[$locType[1]]}-{$locType[0]}-{$locType[2]}"; | |
2391 | } | |
2392 | else { | |
2393 | $tName = "{$locationType[$locType[1]]}-{$locType[0]}"; | |
2394 | } | |
2395 | } | |
2396 | elseif (in_array($locType[0], | |
be2fb01f | 2397 | [ |
5c7dad85 TO |
2398 | 'address_name', |
2399 | 'street_address', | |
7cc09daf | 2400 | 'street_name', |
2401 | 'street_number_suffix', | |
2402 | 'street_unit', | |
5c7dad85 TO |
2403 | 'supplemental_address_1', |
2404 | 'supplemental_address_2', | |
207f62c6 | 2405 | 'supplemental_address_3', |
5c7dad85 TO |
2406 | 'city', |
2407 | 'postal_code', | |
2408 | 'postal_code_suffix', | |
2409 | 'geo_code_1', | |
2410 | 'geo_code_2', | |
7cc09daf | 2411 | 'master_id', |
be2fb01f | 2412 | ] |
5c7dad85 | 2413 | )) { |
6a488035 | 2414 | //fix for search by profile with address fields. |
a822d3d8 | 2415 | $tName = "{$locationType[$locType[1]]}-address"; |
6a488035 | 2416 | } |
7cc09daf | 2417 | elseif (in_array($locType[0], |
be2fb01f | 2418 | [ |
7cc09daf | 2419 | 'on_hold', |
2420 | 'signature_html', | |
2421 | 'signature_text', | |
2422 | 'is_bulkmail', | |
be2fb01f | 2423 | ] |
7cc09daf | 2424 | )) { |
6a488035 TO |
2425 | $tName = "{$locationType[$locType[1]]}-email"; |
2426 | } | |
7cc09daf | 2427 | elseif ($locType[0] == 'provider_id') { |
2428 | $tName = "{$locationType[$locType[1]]}-im"; | |
2429 | } | |
2430 | elseif ($locType[0] == 'openid') { | |
2431 | $tName = "{$locationType[$locType[1]]}-openid"; | |
2432 | } | |
6a488035 TO |
2433 | else { |
2434 | $tName = "{$locationType[$locType[1]]}-{$locType[0]}"; | |
2435 | } | |
2436 | $tName = str_replace(' ', '_', $tName); | |
be2fb01f | 2437 | return [$tName, $fldName]; |
6a488035 | 2438 | } |
79e11805 | 2439 | throw new CRM_Core_Exception('Cannot determine location table information'); |
6a488035 TO |
2440 | } |
2441 | ||
2442 | /** | |
2443 | * Given a result dao, extract the values and return that array | |
2444 | * | |
c490a46a | 2445 | * @param CRM_Core_DAO $dao |
6a488035 | 2446 | * |
a6c01b45 CW |
2447 | * @return array |
2448 | * values for this query | |
6a488035 | 2449 | */ |
00be9182 | 2450 | public function store($dao) { |
be2fb01f | 2451 | $value = []; |
6a488035 TO |
2452 | |
2453 | foreach ($this->_element as $key => $dontCare) { | |
2454 | if (property_exists($dao, $key)) { | |
2455 | if (strpos($key, '-') !== FALSE) { | |
2456 | $values = explode('-', $key); | |
2457 | $lastElement = array_pop($values); | |
2458 | $current = &$value; | |
2459 | $cnt = count($values); | |
2460 | $count = 1; | |
2461 | foreach ($values as $v) { | |
2462 | if (!array_key_exists($v, $current)) { | |
be2fb01f | 2463 | $current[$v] = []; |
6a488035 TO |
2464 | } |
2465 | //bad hack for im_provider | |
2466 | if ($lastElement == 'provider_id') { | |
2467 | if ($count < $cnt) { | |
2468 | $current = &$current[$v]; | |
2469 | } | |
2470 | else { | |
2471 | $lastElement = "{$v}_{$lastElement}"; | |
2472 | } | |
2473 | } | |
2474 | else { | |
2475 | $current = &$current[$v]; | |
2476 | } | |
2477 | $count++; | |
2478 | } | |
2479 | ||
2480 | $current[$lastElement] = $dao->$key; | |
2481 | } | |
2482 | else { | |
2483 | $value[$key] = $dao->$key; | |
2484 | } | |
2485 | } | |
2486 | } | |
2487 | return $value; | |
2488 | } | |
2489 | ||
2490 | /** | |
fe482240 | 2491 | * Getter for tables array. |
6a488035 TO |
2492 | * |
2493 | * @return array | |
6a488035 | 2494 | */ |
00be9182 | 2495 | public function tables() { |
6a488035 TO |
2496 | return $this->_tables; |
2497 | } | |
2498 | ||
86538308 | 2499 | /** |
746aa919 CB |
2500 | * Sometimes used to create the from clause, but, not reliably, set |
2501 | * this AND set tables. | |
2502 | * | |
2503 | * It's unclear the intent - there is a 'simpleFrom' clause which | |
2504 | * takes whereTables into account & a fromClause which doesn't. | |
2505 | * | |
2506 | * logic may have eroded? | |
2507 | * | |
86538308 EM |
2508 | * @return array |
2509 | */ | |
00be9182 | 2510 | public function whereTables() { |
6a488035 TO |
2511 | return $this->_whereTables; |
2512 | } | |
2513 | ||
2514 | /** | |
100fef9d | 2515 | * Generate the where clause (used in match contacts and permissions) |
6a488035 TO |
2516 | * |
2517 | * @param array $params | |
2518 | * @param array $fields | |
2519 | * @param array $tables | |
77b97be7 | 2520 | * @param $whereTables |
77c5b619 | 2521 | * @param bool $strict |
6a488035 TO |
2522 | * |
2523 | * @return string | |
b832662c | 2524 | * @throws \CRM_Core_Exception |
6a488035 | 2525 | */ |
00be9182 | 2526 | public static function getWhereClause($params, $fields, &$tables, &$whereTables, $strict = FALSE) { |
6a488035 TO |
2527 | $query = new CRM_Contact_BAO_Query($params, NULL, $fields, |
2528 | FALSE, $strict | |
2529 | ); | |
2530 | ||
2531 | $tables = array_merge($query->tables(), $tables); | |
2532 | $whereTables = array_merge($query->whereTables(), $whereTables); | |
2533 | ||
2534 | return $query->_whereClause; | |
2535 | } | |
2536 | ||
2537 | /** | |
fe482240 | 2538 | * Create the from clause. |
6a488035 | 2539 | * |
77c5b619 | 2540 | * @param array $tables |
84193f76 CB |
2541 | * Tables that need to be included in this from clause. If null, |
2542 | * return mimimal from clause (i.e. civicrm_contact). | |
77c5b619 TO |
2543 | * @param array $inner |
2544 | * Tables that should be inner-joined. | |
2545 | * @param array $right | |
2546 | * Tables that should be right-joined. | |
77b97be7 | 2547 | * @param bool $primaryLocation |
84193f76 | 2548 | * Search on primary location. See note below. |
77b97be7 | 2549 | * @param int $mode |
84193f76 | 2550 | * Determines search mode based on bitwise MODE_* constants. |
aec7c57d | 2551 | * @param string|null $apiEntity |
84193f76 CB |
2552 | * Determines search mode based on entity by string. |
2553 | * | |
2554 | * The $primaryLocation flag only seems to be used when | |
2555 | * locationType() has been called. This may be a search option | |
2556 | * exposed, or perhaps it's a "search all details" approach which | |
2557 | * predates decoupling of location types and primary fields? | |
6a488035 | 2558 | * |
6c58f897 CB |
2559 | * @see https://issues.civicrm.org/jira/browse/CRM-19967 |
2560 | * | |
a6c01b45 CW |
2561 | * @return string |
2562 | * the from clause | |
6a488035 | 2563 | */ |
b3e1c09d | 2564 | public static function fromClause(&$tables, $inner = NULL, $right = NULL, $primaryLocation = TRUE, $mode = 1, $apiEntity = NULL) { |
6a488035 TO |
2565 | |
2566 | $from = ' FROM civicrm_contact contact_a'; | |
2567 | if (empty($tables)) { | |
2568 | return $from; | |
2569 | } | |
2570 | ||
a7488080 | 2571 | if (!empty($tables['civicrm_worldregion'])) { |
be2fb01f | 2572 | $tables = array_merge(['civicrm_country' => 1], $tables); |
6a488035 TO |
2573 | } |
2574 | ||
8cc574cf | 2575 | if ((!empty($tables['civicrm_state_province']) || !empty($tables['civicrm_country']) || |
b99f3e96 | 2576 | !empty($tables['civicrm_county'])) && empty($tables['civicrm_address'])) { |
be2fb01f | 2577 | $tables = array_merge(['civicrm_address' => 1], |
6a488035 TO |
2578 | $tables |
2579 | ); | |
2580 | } | |
2581 | ||
6a488035 | 2582 | // add group_contact and group table is subscription history is present |
8cc574cf | 2583 | if (!empty($tables['civicrm_subscription_history']) && empty($tables['civicrm_group'])) { |
be2fb01f | 2584 | $tables = array_merge([ |
69078420 SL |
2585 | 'civicrm_group' => 1, |
2586 | 'civicrm_group_contact' => 1, | |
2587 | ], | |
6a488035 TO |
2588 | $tables |
2589 | ); | |
2590 | } | |
2591 | ||
2592 | // to handle table dependencies of components | |
2593 | CRM_Core_Component::tableNames($tables); | |
efa3a566 DS |
2594 | // to handle table dependencies of hook injected tables |
2595 | CRM_Contact_BAO_Query_Hook::singleton()->setTableDependency($tables); | |
6a488035 TO |
2596 | |
2597 | //format the table list according to the weight | |
2598 | $info = CRM_Core_TableHierarchy::info(); | |
2599 | ||
2600 | foreach ($tables as $key => $value) { | |
2601 | $k = 99; | |
2602 | if (strpos($key, '-') !== FALSE) { | |
2603 | $keyArray = explode('-', $key); | |
2604 | $k = CRM_Utils_Array::value('civicrm_' . $keyArray[1], $info, 99); | |
2605 | } | |
2606 | elseif (strpos($key, '_') !== FALSE) { | |
2607 | $keyArray = explode('_', $key); | |
2608 | if (is_numeric(array_pop($keyArray))) { | |
2609 | $k = CRM_Utils_Array::value(implode('_', $keyArray), $info, 99); | |
2610 | } | |
2611 | else { | |
2612 | $k = CRM_Utils_Array::value($key, $info, 99); | |
2613 | } | |
2614 | } | |
2615 | else { | |
2616 | $k = CRM_Utils_Array::value($key, $info, 99); | |
2617 | } | |
2618 | $tempTable[$k . ".$key"] = $key; | |
2619 | } | |
2620 | ksort($tempTable); | |
be2fb01f | 2621 | $newTables = []; |
6a488035 TO |
2622 | foreach ($tempTable as $key) { |
2623 | $newTables[$key] = $tables[$key]; | |
2624 | } | |
2625 | ||
2626 | $tables = $newTables; | |
2627 | ||
2628 | foreach ($tables as $name => $value) { | |
2629 | if (!$value) { | |
2630 | continue; | |
2631 | } | |
2632 | ||
a7488080 | 2633 | if (!empty($inner[$name])) { |
6a488035 TO |
2634 | $side = 'INNER'; |
2635 | } | |
a7488080 | 2636 | elseif (!empty($right[$name])) { |
6a488035 TO |
2637 | $side = 'RIGHT'; |
2638 | } | |
2639 | else { | |
2640 | $side = 'LEFT'; | |
2641 | } | |
2642 | ||
2643 | if ($value != 1) { | |
2644 | // if there is already a join statement in value, use value itself | |
2645 | if (strpos($value, 'JOIN')) { | |
2646 | $from .= " $value "; | |
2647 | } | |
2648 | else { | |
2649 | $from .= " $side JOIN $name ON ( $value ) "; | |
2650 | } | |
2651 | continue; | |
2652 | } | |
0606198b | 2653 | |
ccd8e56f | 2654 | $from .= ' ' . trim(self::getEntitySpecificJoins($name, $mode, $side, $primaryLocation)) . ' '; |
af023bf8 EE |
2655 | } |
2656 | return $from; | |
2657 | } | |
6a488035 | 2658 | |
af023bf8 EE |
2659 | /** |
2660 | * Get join statements for the from clause depending on entity type | |
2661 | * | |
2662 | * @param string $name | |
2663 | * @param int $mode | |
2664 | * @param string $side | |
2665 | * @param string $primaryLocation | |
2666 | * @return string | |
2667 | */ | |
2668 | protected static function getEntitySpecificJoins($name, $mode, $side, $primaryLocation) { | |
2669 | $limitToPrimaryClause = $primaryLocation ? "AND {$name}.is_primary = 1" : ''; | |
2670 | switch ($name) { | |
2671 | case 'civicrm_address': | |
2672 | //CRM-14263 further handling of address joins further down... | |
2673 | return " $side JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id {$limitToPrimaryClause} )"; | |
6a488035 | 2674 | |
7f594311 | 2675 | case 'civicrm_state_province': |
2676 | // This is encountered when doing an export after having applied a 'sort' - it pretty much implies primary | |
2677 | // but that will have been implied-in by the calling function. | |
2678 | // test cover in testContactIDQuery | |
2679 | return " $side JOIN civicrm_state_province ON ( civicrm_address.state_province_id = civicrm_state_province.id )"; | |
2680 | ||
2681 | case 'civicrm_country': | |
2682 | // This is encountered when doing an export after having applied a 'sort' - it pretty much implies primary | |
2683 | // but that will have been implied-in by the calling function. | |
2684 | // test cover in testContactIDQuery | |
2685 | return " $side JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id )"; | |
2686 | ||
af023bf8 EE |
2687 | case 'civicrm_phone': |
2688 | return " $side JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id {$limitToPrimaryClause}) "; | |
6a488035 | 2689 | |
af023bf8 EE |
2690 | case 'civicrm_email': |
2691 | return " $side JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id {$limitToPrimaryClause})"; | |
6a488035 | 2692 | |
af023bf8 EE |
2693 | case 'civicrm_im': |
2694 | return " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id {$limitToPrimaryClause}) "; | |
6a488035 | 2695 | |
af023bf8 EE |
2696 | case 'im_provider': |
2697 | $from = " $side JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id) "; | |
2698 | $from .= " $side JOIN civicrm_option_group option_group_imProvider ON option_group_imProvider.name = 'instant_messenger_service'"; | |
2699 | $from .= " $side JOIN civicrm_option_value im_provider ON (civicrm_im.provider_id = im_provider.value AND option_group_imProvider.id = im_provider.option_group_id)"; | |
2700 | return $from; | |
6a488035 | 2701 | |
af023bf8 EE |
2702 | case 'civicrm_openid': |
2703 | return " $side JOIN civicrm_openid ON ( civicrm_openid.contact_id = contact_a.id {$limitToPrimaryClause} )"; | |
6a488035 | 2704 | |
af023bf8 | 2705 | case 'civicrm_worldregion': |
7f594311 | 2706 | // We can be sure from the calling function that country will already be joined in. |
2707 | // we really don't need world_region - we could use a pseudoconstant for it. | |
c156762a | 2708 | return " $side JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id "; |
0cd50623 | 2709 | |
af023bf8 EE |
2710 | case 'civicrm_location_type': |
2711 | return " $side JOIN civicrm_location_type ON civicrm_address.location_type_id = civicrm_location_type.id "; | |
6a488035 | 2712 | |
af023bf8 EE |
2713 | case 'civicrm_group': |
2714 | return " $side JOIN civicrm_group ON civicrm_group.id = civicrm_group_contact.group_id "; | |
6a488035 | 2715 | |
af023bf8 EE |
2716 | case 'civicrm_group_contact': |
2717 | return " $side JOIN civicrm_group_contact ON contact_a.id = civicrm_group_contact.contact_id "; | |
6a488035 | 2718 | |
af023bf8 EE |
2719 | case 'civicrm_group_contact_cache': |
2720 | return " $side JOIN civicrm_group_contact_cache ON contact_a.id = civicrm_group_contact_cache.contact_id "; | |
6a488035 | 2721 | |
af023bf8 EE |
2722 | case 'civicrm_activity': |
2723 | case 'civicrm_activity_tag': | |
2724 | case 'activity_type': | |
2725 | case 'activity_status': | |
2726 | case 'parent_id': | |
2727 | case 'civicrm_activity_contact': | |
2728 | case 'source_contact': | |
2729 | case 'activity_priority': | |
2730 | return CRM_Activity_BAO_Query::from($name, $mode, $side); | |
2731 | ||
2732 | case 'civicrm_entity_tag': | |
2733 | $from = " $side JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact'"; | |
2734 | return "$from AND civicrm_entity_tag.entity_id = contact_a.id ) "; | |
2735 | ||
2736 | case 'civicrm_note': | |
2737 | $from = " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact'"; | |
2738 | return "$from AND contact_a.id = civicrm_note.entity_id ) "; | |
2739 | ||
2740 | case 'civicrm_subscription_history': | |
2741 | $from = " $side JOIN civicrm_subscription_history"; | |
2742 | $from .= " ON civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id"; | |
2743 | return "$from AND civicrm_group_contact.group_id = civicrm_subscription_history.group_id"; | |
2744 | ||
2745 | case 'civicrm_relationship': | |
2746 | if (self::$_relType == 'reciprocal') { | |
2747 | if (self::$_relationshipTempTable) { | |
2748 | // we have a temptable to join on | |
2749 | $tbl = self::$_relationshipTempTable; | |
2750 | return " INNER JOIN {$tbl} civicrm_relationship ON civicrm_relationship.contact_id = contact_a.id"; | |
6a488035 TO |
2751 | } |
2752 | else { | |
af023bf8 EE |
2753 | $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id OR civicrm_relationship.contact_id_a = contact_a.id)"; |
2754 | $from .= " $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id OR civicrm_relationship.contact_id_b = contact_b.id)"; | |
2755 | return $from; | |
6a488035 | 2756 | } |
af023bf8 EE |
2757 | } |
2758 | elseif (self::$_relType == 'b') { | |
2759 | $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id )"; | |
2760 | return "$from $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )"; | |
2761 | } | |
2762 | else { | |
2763 | $from = " $side JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id )"; | |
2764 | return "$from $side JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )"; | |
2765 | } | |
6a488035 | 2766 | |
af023bf8 EE |
2767 | case 'civicrm_log': |
2768 | $from = " INNER JOIN civicrm_log ON (civicrm_log.entity_id = contact_a.id AND civicrm_log.entity_table = 'civicrm_contact')"; | |
2769 | return "$from INNER JOIN civicrm_contact contact_b_log ON (civicrm_log.modified_id = contact_b_log.id)"; | |
6a488035 | 2770 | |
af023bf8 EE |
2771 | case 'civicrm_tag': |
2772 | return " $side JOIN civicrm_tag ON civicrm_entity_tag.tag_id = civicrm_tag.id "; | |
6a488035 | 2773 | |
af023bf8 EE |
2774 | case 'civicrm_grant': |
2775 | return CRM_Grant_BAO_Query::from($name, $mode, $side); | |
6a488035 | 2776 | |
af023bf8 EE |
2777 | case 'civicrm_website': |
2778 | return " $side JOIN civicrm_website ON contact_a.id = civicrm_website.contact_id "; | |
7851dc81 | 2779 | |
af023bf8 EE |
2780 | case 'civicrm_campaign': |
2781 | //Move to default case if not in either mode. | |
2782 | if ($mode & CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { | |
2783 | return CRM_Contribute_BAO_Query::from($name, $mode, $side); | |
2784 | } | |
2785 | elseif ($mode & CRM_Contact_BAO_Query::MODE_MAILING) { | |
2786 | return CRM_Mailing_BAO_Query::from($name, $mode, $side); | |
2787 | } | |
2788 | elseif ($mode & CRM_Contact_BAO_Query::MODE_CAMPAIGN) { | |
2789 | return CRM_Campaign_BAO_Query::from($name, $mode, $side); | |
2790 | } | |
6a488035 | 2791 | |
af023bf8 EE |
2792 | default: |
2793 | $locationTypeName = ''; | |
2794 | if (strpos($name, '-address') != 0) { | |
2795 | $locationTypeName = 'address'; | |
2796 | } | |
2797 | elseif (strpos($name, '-phone') != 0) { | |
2798 | $locationTypeName = 'phone'; | |
2799 | } | |
2800 | elseif (strpos($name, '-email') != 0) { | |
2801 | $locationTypeName = 'email'; | |
2802 | } | |
2803 | elseif (strpos($name, '-im') != 0) { | |
2804 | $locationTypeName = 'im'; | |
2805 | } | |
2806 | elseif (strpos($name, '-openid') != 0) { | |
2807 | $locationTypeName = 'openid'; | |
2808 | } | |
7cc09daf | 2809 | |
af023bf8 EE |
2810 | if ($locationTypeName) { |
2811 | //we have a join on an location table - possibly in conjunction with search builder - CRM-14263 | |
2812 | $parts = explode('-', $name); | |
2813 | $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate'); | |
2814 | foreach ($locationTypes as $locationTypeID => $locationType) { | |
2815 | if ($parts[0] == str_replace(' ', '_', $locationType)) { | |
2816 | $locationID = $locationTypeID; | |
140e25ee | 2817 | } |
e5fccefb | 2818 | } |
af023bf8 EE |
2819 | $from = " $side JOIN civicrm_{$locationTypeName} `{$name}` ON ( contact_a.id = `{$name}`.contact_id ) and `{$name}`.location_type_id = $locationID "; |
2820 | } | |
2821 | else { | |
2822 | $from = CRM_Core_Component::from($name, $mode, $side); | |
2823 | } | |
2824 | $from .= CRM_Contact_BAO_Query_Hook::singleton()->buildSearchfrom($name, $mode, $side); | |
99e9587a | 2825 | |
af023bf8 | 2826 | return $from; |
6a488035 | 2827 | } |
6a488035 TO |
2828 | } |
2829 | ||
2830 | /** | |
2831 | * WHERE / QILL clause for deleted_contacts | |
2832 | * | |
c037736a | 2833 | * @param array $values |
6a488035 | 2834 | */ |
00be9182 | 2835 | public function deletedContacts($values) { |
6a488035 TO |
2836 | list($_, $_, $value, $grouping, $_) = $values; |
2837 | if ($value) { | |
2838 | // *prepend* to the relevant grouping as this is quite an important factor | |
2839 | array_unshift($this->_qill[$grouping], ts('Search in Trash')); | |
2840 | } | |
2841 | } | |
2842 | ||
2843 | /** | |
100fef9d | 2844 | * Where / qill clause for contact_type |
6a488035 | 2845 | * |
77b97be7 | 2846 | * @param $values |
b832662c | 2847 | * |
2848 | * @throws \CRM_Core_Exception | |
6a488035 | 2849 | */ |
00be9182 | 2850 | public function contactType(&$values) { |
6a488035 TO |
2851 | list($name, $op, $value, $grouping, $wildcard) = $values; |
2852 | ||
be2fb01f CW |
2853 | $subTypes = []; |
2854 | $clause = []; | |
6a488035 TO |
2855 | |
2856 | // account for search builder mapping multiple values | |
2857 | if (!is_array($value)) { | |
2858 | $values = self::parseSearchBuilderString($value, 'String'); | |
2859 | if (is_array($values)) { | |
2860 | $value = array_flip($values); | |
2861 | } | |
2862 | } | |
2863 | ||
2864 | if (is_array($value)) { | |
2865 | foreach ($value as $k => $v) { | |
2866 | // fix for CRM-771 | |
2867 | if ($k) { | |
2868 | $subType = NULL; | |
2869 | $contactType = $k; | |
2870 | if (strpos($k, CRM_Core_DAO::VALUE_SEPARATOR)) { | |
2871 | list($contactType, $subType) = explode(CRM_Core_DAO::VALUE_SEPARATOR, $k, 2); | |
2872 | } | |
2873 | ||
2874 | if (!empty($subType)) { | |
2875 | $subTypes[$subType] = 1; | |
2876 | } | |
2877 | $clause[$contactType] = "'" . CRM_Utils_Type::escape($contactType, 'String') . "'"; | |
2878 | } | |
2879 | } | |
2880 | } | |
2881 | else { | |
2882 | $contactTypeANDSubType = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value, 2); | |
2883 | $contactType = $contactTypeANDSubType[0]; | |
9c1bc317 | 2884 | $subType = $contactTypeANDSubType[1] ?? NULL; |
6a488035 TO |
2885 | if (!empty($subType)) { |
2886 | $subTypes[$subType] = 1; | |
2887 | } | |
2888 | $clause[$contactType] = "'" . CRM_Utils_Type::escape($contactType, 'String') . "'"; | |
2889 | } | |
2890 | ||
2891 | // fix for CRM-771 | |
2892 | if (!empty($clause)) { | |
c33e01a5 | 2893 | $quill = $clause; |
3b3bc827 CW |
2894 | if ($op == 'IN' || $op == 'NOT IN') { |
2895 | $this->_where[$grouping][] = "contact_a.contact_type $op (" . implode(',', $clause) . ')'; | |
2896 | } | |
2897 | else { | |
2898 | $type = array_pop($clause); | |
dbc6f6d6 | 2899 | $this->_where[$grouping][] = self::buildClause("contact_a.contact_type", $op, $contactType); |
3b3bc827 | 2900 | } |
d4b81b9a | 2901 | |
aa3c3360 | 2902 | $this->_qill[$grouping][] = ts('Contact Type') . " $op " . implode(' ' . ts('or') . ' ', $quill); |
6a488035 TO |
2903 | |
2904 | if (!empty($subTypes)) { | |
2905 | $this->includeContactSubTypes($subTypes, $grouping); | |
2906 | } | |
2907 | } | |
2908 | } | |
2909 | ||
2910 | /** | |
8f165fa5 | 2911 | * Where / qill clause for contact_sub_type. |
6a488035 | 2912 | * |
8f165fa5 | 2913 | * @param array $values |
6a488035 | 2914 | */ |
00be9182 | 2915 | public function contactSubType(&$values) { |
6a488035 | 2916 | list($name, $op, $value, $grouping, $wildcard) = $values; |
1d2bcc81 | 2917 | $this->includeContactSubTypes($value, $grouping, $op); |
6a488035 TO |
2918 | } |
2919 | ||
86538308 EM |
2920 | /** |
2921 | * @param $value | |
2922 | * @param $grouping | |
0e2e76cf | 2923 | * @param string $op |
b832662c | 2924 | * |
2925 | * @throws \CRM_Core_Exception | |
86538308 | 2926 | */ |
00be9182 | 2927 | public function includeContactSubTypes($value, $grouping, $op = 'LIKE') { |
6a488035 | 2928 | |
e9fd1f48 | 2929 | if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { |
2930 | $op = key($value); | |
2931 | $value = $value[$op]; | |
2932 | } | |
2933 | ||
be2fb01f | 2934 | $clause = []; |
6a488035 | 2935 | $alias = "contact_a.contact_sub_type"; |
46b3417a | 2936 | $qillOperators = CRM_Core_SelectValues::getSearchBuilderOperators(); |
6a488035 | 2937 | |
1d2bcc81 | 2938 | $op = str_replace('IN', 'LIKE', $op); |
ccc79dfc | 2939 | $op = str_replace('=', 'LIKE', $op); |
2940 | $op = str_replace('!', 'NOT ', $op); | |
1d2bcc81 | 2941 | |
2942 | if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) { | |
2943 | $this->_where[$grouping][] = self::buildClause($alias, $op, $value, 'String'); | |
2944 | } | |
4c9b6178 | 2945 | elseif (is_array($value)) { |
6a488035 | 2946 | foreach ($value as $k => $v) { |
35fbf8a2 | 2947 | $clause[$k] = "($alias $op '%" . CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($v, 'String') . CRM_Core_DAO::VALUE_SEPARATOR . "%')"; |
6a488035 TO |
2948 | } |
2949 | } | |
2950 | else { | |
1d2bcc81 | 2951 | $clause[$value] = "($alias $op '%" . CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($value, 'String') . CRM_Core_DAO::VALUE_SEPARATOR . "%')"; |
6a488035 TO |
2952 | } |
2953 | ||
2954 | if (!empty($clause)) { | |
2955 | $this->_where[$grouping][] = "( " . implode(' OR ', $clause) . " )"; | |
6a488035 | 2956 | } |
be2fb01f | 2957 | $this->_qill[$grouping][] = ts('Contact Subtype %1 ', [1 => $qillOperators[$op]]) . implode(' ' . ts('or') . ' ', array_keys($clause)); |
6a488035 TO |
2958 | } |
2959 | ||
2960 | /** | |
c3137c08 | 2961 | * Where / qill clause for groups. |
6a488035 | 2962 | * |
77b97be7 | 2963 | * @param $values |
0d38cb6d | 2964 | * |
2965 | * @throws \CRM_Core_Exception | |
b832662c | 2966 | * @throws \Exception |
6a488035 | 2967 | */ |
c3137c08 | 2968 | public function group($values) { |
6a488035 TO |
2969 | list($name, $op, $value, $grouping, $wildcard) = $values; |
2970 | ||
ca91bd7f | 2971 | // If the $value is in OK (operator as key) array format we need to extract the key as operator and value first |
2972 | if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { | |
2973 | $op = key($value); | |
2974 | $value = $value[$op]; | |
2975 | } | |
673fae6c | 2976 | // Translate EMPTY to NULL as EMPTY is cannot be used in it's intended meaning here |
2977 | // so has to be 'squashed into' NULL. (ie. group membership cannot be ''). | |
2978 | // even one group might equate to multiple when looking at children so IN is simpler. | |
2979 | // @todo - also look at != casting but there are rows below to review. | |
2980 | $opReplacements = [ | |
dd606225 JP |
2981 | 'IS EMPTY' => 'IS NULL', |
2982 | 'IS NOT EMPTY' => 'IS NOT NULL', | |
673fae6c | 2983 | '=' => 'IN', |
2984 | ]; | |
2985 | if (isset($opReplacements[$op])) { | |
2986 | $op = $opReplacements[$op]; | |
2987 | } | |
e8401ae2 | 2988 | |
ca91bd7f | 2989 | if (strpos($op, 'NULL')) { |
2990 | $value = NULL; | |
2991 | } | |
2992 | ||
0d71d885 | 2993 | if (is_array($value) && count($value) > 1) { |
e8401ae2 | 2994 | if (strpos($op, 'IN') === FALSE && strpos($op, 'NULL') === FALSE) { |
79e11805 | 2995 | throw new CRM_Core_Exception(ts("%1 is not a valid operator", [1 => $op])); |
e8401ae2 | 2996 | } |
6a488035 TO |
2997 | $this->_useDistinct = TRUE; |
2998 | } | |
2999 | ||
7cc09daf | 3000 | if (isset($value)) { |
3001 | $value = CRM_Utils_Array::value($op, $value, $value); | |
3002 | } | |
3003 | ||
9de9a433 | 3004 | if ($name === 'group_type') { |
d412bcca | 3005 | $value = array_keys($this->getGroupsFromTypeCriteria($value)); |
3006 | } | |
3007 | ||
be2fb01f | 3008 | $regularGroupIDs = $smartGroupIDs = []; |
181962cf | 3009 | foreach ((array) $value as $id) { |
3010 | if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) { | |
6c5056ec | 3011 | $smartGroupIDs[] = (int) $id; |
181962cf | 3012 | } |
3013 | else { | |
6c5056ec | 3014 | $regularGroupIDs[] = (int) trim($id); |
181962cf | 3015 | } |
3016 | } | |
d7570a7f | 3017 | $hasNonSmartGroups = count($regularGroupIDs); |
c3137c08 | 3018 | |
9de9a433 | 3019 | $isNotOp = ($op === 'NOT IN' || $op === '!='); |
d79d2a59 | 3020 | |
d7570a7f | 3021 | $statusJoinClause = $this->getGroupStatusClause($grouping); |
0d38cb6d | 3022 | // If we are searching for 'Removed' contacts then despite it being a smart group we only care about the group_contact table. |
3023 | $isGroupStatusSearch = (!empty($this->getSelectedGroupStatuses($grouping)) && $this->getSelectedGroupStatuses($grouping) !== ["'Added'"]); | |
be2fb01f | 3024 | $groupClause = []; |
0d38cb6d | 3025 | if ($hasNonSmartGroups || empty($value) || $isGroupStatusSearch) { |
783144b0 | 3026 | // include child groups IDs if any |
3027 | $childGroupIds = (array) CRM_Contact_BAO_Group::getChildGroupIds($regularGroupIDs); | |
3028 | foreach ($childGroupIds as $key => $id) { | |
3029 | if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) { | |
3030 | $smartGroupIDs[] = $id; | |
3031 | unset($childGroupIds[$key]); | |
3032 | } | |
3033 | } | |
3034 | if (count($childGroupIds)) { | |
3035 | $regularGroupIDs = array_merge($regularGroupIDs, $childGroupIds); | |
3036 | } | |
3037 | ||
9ca472a1 | 3038 | if (empty($regularGroupIDs)) { |
0d38cb6d | 3039 | if ($isGroupStatusSearch) { |
3040 | $regularGroupIDs = $smartGroupIDs; | |
3041 | } | |
3042 | // If it is still empty we want a filter that blocks all results. | |
3043 | if (empty($regularGroupIDs)) { | |
3044 | $regularGroupIDs = [0]; | |
3045 | } | |
9ca472a1 PN |
3046 | } |
3047 | ||
9de9a433 | 3048 | $gcTable = '`civicrm_group_contact-' . uniqid() . "`"; |
be2fb01f | 3049 | $joinClause = ["contact_a.id = {$gcTable}.contact_id"]; |
783144b0 | 3050 | |
6c5056ec | 3051 | // @todo consider just casting != to NOT IN & handling both together. |
9de9a433 | 3052 | if ($op === '!=') { |
6c5056ec PN |
3053 | $groupIds = ''; |
3054 | if (!empty($regularGroupIDs)) { | |
8a6d5abd | 3055 | $groupIds = CRM_Utils_Type::validate(implode(',', (array) $regularGroupIDs), 'CommaSeparatedIntegers'); |
6c5056ec | 3056 | } |
783144b0 | 3057 | $clause = "{$gcTable}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact cgc WHERE cgc.group_id = $groupIds )"; |
c112a6f9 | 3058 | } |
3059 | else { | |
673fae6c | 3060 | $clause = self::buildClause("{$gcTable}.group_id", $op, $regularGroupIDs); |
c112a6f9 | 3061 | } |
783144b0 | 3062 | $groupClause[] = "( {$clause} )"; |
6a488035 | 3063 | |
d7570a7f | 3064 | if ($statusJoinClause) { |
3065 | $joinClause[] = "{$gcTable}.$statusJoinClause"; | |
6a488035 | 3066 | } |
783144b0 | 3067 | $this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")"; |
6a488035 TO |
3068 | } |
3069 | ||
181962cf | 3070 | //CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table |
0d38cb6d | 3071 | // If we are only searching for Removed or Pending contacts we don't need to resolve the smart group |
3072 | // as that info is in the group_contact table. | |
3073 | if ((count($smartGroupIDs) || empty($value)) && !$isGroupStatusSearch) { | |
ed17b5e2 | 3074 | $this->_groupUniqueKey = uniqid(); |
e24f9388 | 3075 | $this->_groupKeys[] = $this->_groupUniqueKey; |
ed17b5e2 | 3076 | $gccTableAlias = "civicrm_group_contact_cache_{$this->_groupUniqueKey}"; |
9f6a1556 | 3077 | $groupContactCacheClause = $this->addGroupContactCache($smartGroupIDs, $gccTableAlias, "contact_a", $op); |
6451118c | 3078 | if (!empty($groupContactCacheClause)) { |
9f6a1556 | 3079 | if ($isNotOp) { |
e15bd6d1 | 3080 | $groupIds = CRM_Utils_Type::validate(implode(',', (array) $smartGroupIDs), 'CommaSeparatedIntegers'); |
ed17b5e2 | 3081 | $gcTable = "civicrm_group_contact_{$this->_groupUniqueKey}"; |
be2fb01f | 3082 | $joinClause = ["contact_a.id = {$gcTable}.contact_id"]; |
9f6a1556 JP |
3083 | $this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")"; |
3084 | if (strpos($op, 'IN') !== FALSE) { | |
3085 | $groupClause[] = "{$gcTable}.group_id $op ( $groupIds ) AND {$gccTableAlias}.group_id IS NULL"; | |
3086 | } | |
3087 | else { | |
3088 | $groupClause[] = "{$gcTable}.group_id $op $groupIds AND {$gccTableAlias}.group_id IS NULL"; | |
3089 | } | |
3090 | } | |
6451118c JP |
3091 | $groupClause[] = " ( {$groupContactCacheClause} ) "; |
3092 | } | |
6a488035 TO |
3093 | } |
3094 | ||
883e1e76 | 3095 | $and = ($op == 'IS NULL') ? ' AND ' : ' OR '; |
6451118c JP |
3096 | if (!empty($groupClause)) { |
3097 | $this->_where[$grouping][] = ' ( ' . implode($and, $groupClause) . ' ) '; | |
3098 | } | |
181962cf | 3099 | |
e8401ae2 | 3100 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Group', 'id', $value, $op); |
be2fb01f | 3101 | $this->_qill[$grouping][] = ts("Group(s) %1 %2", [1 => $qillop, 2 => $qillVal]); |
0cd50623 | 3102 | if (strpos($op, 'NULL') === FALSE) { |
d7570a7f | 3103 | $this->_qill[$grouping][] = ts("Group Status %1", [1 => implode(' ' . ts('or') . ' ', $this->getSelectedGroupStatuses($grouping))]); |
0cd50623 | 3104 | } |
6a488035 | 3105 | } |
d424ffde | 3106 | |
b832662c | 3107 | /** |
3108 | * @return array | |
3109 | */ | |
e24f9388 SL |
3110 | public function getGroupCacheTableKeys() { |
3111 | return $this->_groupKeys; | |
ed17b5e2 SL |
3112 | } |
3113 | ||
86538308 | 3114 | /** |
fe482240 | 3115 | * Function translates selection of group type into a list of groups. |
86538308 EM |
3116 | * @param $value |
3117 | * | |
3118 | * @return array | |
3119 | */ | |
5c7dad85 | 3120 | public function getGroupsFromTypeCriteria($value) { |
be2fb01f | 3121 | $groupIds = []; |
d79d2a59 | 3122 | foreach ((array) $value as $groupTypeValue) { |
6a488035 TO |
3123 | $groupList = CRM_Core_PseudoConstant::group($groupTypeValue); |
3124 | $groupIds = ($groupIds + $groupList); | |
3125 | } | |
3126 | return $groupIds; | |
3127 | } | |
3128 | ||
e6b676a0 | 3129 | /** |
72a2eeab AS |
3130 | * Prime smart group cache for smart groups in the search, and join |
3131 | * civicrm_group_contact_cache table into the query. | |
3132 | * | |
3133 | * @param array $groups IDs of groups specified in search criteria. | |
3134 | * @param string $tableAlias Alias to use for civicrm_group_contact_cache table. | |
3135 | * @param string $joinTable Table on which to join civicrm_group_contact_cache | |
3136 | * @param string $op SQL comparison operator (NULL, IN, !=, IS NULL, etc.) | |
3137 | * @param string $joinColumn Column in $joinTable on which to join civicrm_group_contact_cache.contact_id | |
e6b676a0 | 3138 | * |
72a2eeab | 3139 | * @return string WHERE clause component for smart group criteria. |
b832662c | 3140 | * @throws \CRM_Core_Exception |
e6b676a0 | 3141 | */ |
e9cde327 | 3142 | public function addGroupContactCache($groups, $tableAlias, $joinTable, $op, $joinColumn = 'id') { |
e8401ae2 | 3143 | $isNullOp = (strpos($op, 'NULL') !== FALSE); |
3144 | $groupsIds = $groups; | |
6451118c JP |
3145 | |
3146 | $operator = ['=' => 'IN', '!=' => 'NOT IN']; | |
3147 | if (!empty($operator[$op]) && is_array($groups)) { | |
3148 | $op = $operator[$op]; | |
3149 | } | |
e8401ae2 | 3150 | if (!$isNullOp && !$groups) { |
6a488035 TO |
3151 | return NULL; |
3152 | } | |
e8401ae2 | 3153 | elseif (strpos($op, 'IN') !== FALSE) { |
be2fb01f | 3154 | $groups = [$op => $groups]; |
e8401ae2 | 3155 | } |
d4ed6fbd | 3156 | elseif (is_array($groups) && count($groups)) { |
be2fb01f | 3157 | $groups = ['IN' => $groups]; |
d4ed6fbd | 3158 | } |
6a488035 | 3159 | |
e8401ae2 | 3160 | // Find all the groups that are part of a saved search. |
3161 | $smartGroupClause = self::buildClause("id", $op, $groups, 'Int'); | |
6a488035 TO |
3162 | $sql = " |
3163 | SELECT id, cache_date, saved_search_id, children | |
3164 | FROM civicrm_group | |
e8401ae2 | 3165 | WHERE $smartGroupClause |
6a488035 TO |
3166 | AND ( saved_search_id != 0 |
3167 | OR saved_search_id IS NOT NULL | |
3168 | OR children IS NOT NULL ) | |
3169 | "; | |
e6b676a0 | 3170 | |
5c7dad85 | 3171 | $group = CRM_Core_DAO::executeQuery($sql); |
e6b676a0 | 3172 | |
6a488035 | 3173 | while ($group->fetch()) { |
6a488035 | 3174 | $this->_useDistinct = TRUE; |
6a488035 TO |
3175 | if (!$this->_smartGroupCache || $group->cache_date == NULL) { |
3176 | CRM_Contact_BAO_GroupContactCache::load($group); | |
3177 | } | |
6a488035 | 3178 | } |
9f6a1556 | 3179 | if ($group->N == 0 && $op != 'NOT IN') { |
485a3a1f | 3180 | return NULL; |
3181 | } | |
6a488035 | 3182 | |
72a2eeab | 3183 | $this->_tables[$tableAlias] = $this->_whereTables[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.{$joinColumn} = {$tableAlias}.contact_id "; |
9f6a1556 JP |
3184 | |
3185 | if ($op == 'NOT IN') { | |
3186 | return "{$tableAlias}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( " . implode(',', (array) $groupsIds) . " ) )"; | |
3187 | } | |
e8401ae2 | 3188 | return self::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int'); |
6a488035 TO |
3189 | } |
3190 | ||
3191 | /** | |
100fef9d | 3192 | * Where / qill clause for cms users |
6a488035 | 3193 | * |
77b97be7 | 3194 | * @param $values |
6a488035 | 3195 | */ |
00be9182 | 3196 | public function ufUser(&$values) { |
6a488035 TO |
3197 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3198 | ||
3199 | if ($value == 1) { | |
3200 | $this->_tables['civicrm_uf_match'] = $this->_whereTables['civicrm_uf_match'] = ' INNER JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id '; | |
3201 | ||
3202 | $this->_qill[$grouping][] = ts('CMS User'); | |
3203 | } | |
3204 | elseif ($value == 0) { | |
3205 | $this->_tables['civicrm_uf_match'] = $this->_whereTables['civicrm_uf_match'] = ' LEFT JOIN civicrm_uf_match ON civicrm_uf_match.contact_id = contact_a.id '; | |
3206 | ||
3207 | $this->_where[$grouping][] = " civicrm_uf_match.contact_id IS NULL"; | |
3208 | $this->_qill[$grouping][] = ts('Not a CMS User'); | |
3209 | } | |
3210 | } | |
3211 | ||
3212 | /** | |
fe482240 | 3213 | * All tag search specific. |
6a488035 | 3214 | * |
c037736a | 3215 | * @param array $values |
b832662c | 3216 | * |
3217 | * @throws \CRM_Core_Exception | |
6a488035 | 3218 | */ |
00be9182 | 3219 | public function tagSearch(&$values) { |
6a488035 TO |
3220 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3221 | ||
3222 | $op = "LIKE"; | |
3223 | $value = "%{$value}%"; | |
0cf0a3f3 | 3224 | $escapedValue = CRM_Utils_Type::escape("%{$value}%", 'String'); |
6a488035 | 3225 | |
6a488035 TO |
3226 | $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping); |
3227 | $tagTypesText = $this->getWhereValues('tag_types_text', $grouping); | |
3228 | ||
394643b9 PF |
3229 | $etTable = "`civicrm_entity_tag-" . uniqid() . "`"; |
3230 | $tTable = "`civicrm_tag-" . uniqid() . "`"; | |
6a488035 TO |
3231 | |
3232 | if ($useAllTagTypes[2]) { | |
59f4c9ee TO |
3233 | $this->_tables[$etTable] = $this->_whereTables[$etTable] |
3234 | = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id) | |
3235 | LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id )"; | |
6a488035 TO |
3236 | |
3237 | // search tag in cases | |
394643b9 PF |
3238 | $etCaseTable = "`civicrm_entity_case_tag-" . uniqid() . "`"; |
3239 | $tCaseTable = "`civicrm_case_tag-" . uniqid() . "`"; | |
59f4c9ee TO |
3240 | $this->_tables[$etCaseTable] = $this->_whereTables[$etCaseTable] |
3241 | = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id | |
3242 | LEFT JOIN civicrm_case | |
6a488035 TO |
3243 | ON (civicrm_case_contact.case_id = civicrm_case.id |
3244 | AND civicrm_case.is_deleted = 0 ) | |
59f4c9ee TO |
3245 | LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id ) |
3246 | LEFT JOIN civicrm_tag {$tCaseTable} ON ( {$etCaseTable}.tag_id = {$tCaseTable}.id )"; | |
6a488035 | 3247 | // search tag in activities |
394643b9 PF |
3248 | $etActTable = "`civicrm_entity_act_tag-" . uniqid() . "`"; |
3249 | $tActTable = "`civicrm_act_tag-" . uniqid() . "`"; | |
44f817d4 | 3250 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
9e74e3ce | 3251 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
1071730c | 3252 | |
59f4c9ee TO |
3253 | $this->_tables[$etActTable] = $this->_whereTables[$etActTable] |
3254 | = " LEFT JOIN civicrm_activity_contact | |
9e74e3ce | 3255 | ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} ) |
59f4c9ee | 3256 | LEFT JOIN civicrm_activity |
91da6cd5 | 3257 | ON ( civicrm_activity.id = civicrm_activity_contact.activity_id |
6a488035 | 3258 | AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 ) |
59f4c9ee TO |
3259 | LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id ) |
3260 | LEFT JOIN civicrm_tag {$tActTable} ON ( {$etActTable}.tag_id = {$tActTable}.id )"; | |
6a488035 | 3261 | |
0cf0a3f3 | 3262 | $this->_where[$grouping][] = "({$tTable}.name $op '" . $escapedValue . "' OR {$tCaseTable}.name $op '" . $escapedValue . "' OR {$tActTable}.name $op '" . $escapedValue . "')"; |
be2fb01f | 3263 | $this->_qill[$grouping][] = ts('Tag %1 %2', [1 => $tagTypesText[2], 2 => $op]) . ' ' . $value; |
5c7dad85 TO |
3264 | } |
3265 | else { | |
394643b9 PF |
3266 | $etTable = "`civicrm_entity_tag-" . uniqid() . "`"; |
3267 | $tTable = "`civicrm_tag-" . uniqid() . "`"; | |
6a488035 TO |
3268 | $this->_tables[$etTable] = $this->_whereTables[$etTable] = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND |
3269 | {$etTable}.entity_table = 'civicrm_contact' ) | |
3270 | LEFT JOIN civicrm_tag {$tTable} ON ( {$etTable}.tag_id = {$tTable}.id ) "; | |
3271 | ||
3272 | $this->_where[$grouping][] = self::buildClause("{$tTable}.name", $op, $value, 'String'); | |
be2fb01f | 3273 | $this->_qill[$grouping][] = ts('Tagged %1', [1 => $op]) . ' ' . $value; |
6a488035 TO |
3274 | } |
3275 | } | |
3276 | ||
3277 | /** | |
8f165fa5 | 3278 | * Where / qill clause for tag. |
6a488035 | 3279 | * |
c037736a | 3280 | * @param array $values |
b832662c | 3281 | * |
3282 | * @throws \CRM_Core_Exception | |
6a488035 | 3283 | */ |
00be9182 | 3284 | public function tag(&$values) { |
6a488035 TO |
3285 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3286 | ||
7cc09daf | 3287 | // API/Search Builder format array(operator => array(values)) |
6a488035 | 3288 | if (is_array($value)) { |
7cc09daf | 3289 | if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { |
3290 | $op = key($value); | |
3291 | $value = $value[$op]; | |
3292 | } | |
6a488035 TO |
3293 | if (count($value) > 1) { |
3294 | $this->_useDistinct = TRUE; | |
3295 | } | |
6a488035 TO |
3296 | } |
3297 | ||
fe128a5a MD |
3298 | if (strpos($op, 'NULL') || strpos($op, 'EMPTY')) { |
3299 | $value = NULL; | |
3300 | } | |
3301 | ||
3302 | $tagTree = CRM_Core_BAO_Tag::getChildTags(); | |
3303 | foreach ((array) $value as $tagID) { | |
3304 | if (!empty($tagTree[$tagID])) { | |
7ce1e8f3 E |
3305 | // make sure value is an array here (see CORE-2502) |
3306 | $value = array_unique(array_merge((array) $value, $tagTree[$tagID])); | |
fe128a5a MD |
3307 | } |
3308 | } | |
3309 | ||
3310 | list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, ['onlyActive' => FALSE]); | |
3311 | ||
f2ec9ca7 PF |
3312 | // implode array, then remove all spaces |
3313 | $value = str_replace(' ', '', implode(',', (array) $value)); | |
3314 | if (!empty($value)) { | |
8a6d5abd | 3315 | $value = CRM_Utils_Type::validate($value, 'CommaSeparatedIntegers'); |
f2ec9ca7 | 3316 | } |
0cf0a3f3 | 3317 | |
6a488035 TO |
3318 | $useAllTagTypes = $this->getWhereValues('all_tag_types', $grouping); |
3319 | $tagTypesText = $this->getWhereValues('tag_types_text', $grouping); | |
3320 | ||
394643b9 | 3321 | $etTable = "`civicrm_entity_tag-" . uniqid() . "`"; |
6a488035 | 3322 | |
32c3b33f | 3323 | if (!empty($useAllTagTypes[2])) { |
59f4c9ee TO |
3324 | $this->_tables[$etTable] = $this->_whereTables[$etTable] |
3325 | = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') "; | |
6a488035 TO |
3326 | |
3327 | // search tag in cases | |
394643b9 | 3328 | $etCaseTable = "`civicrm_entity_case_tag-" . uniqid() . "`"; |
44f817d4 | 3329 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
9e74e3ce | 3330 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
3331 | ||
59f4c9ee TO |
3332 | $this->_tables[$etCaseTable] = $this->_whereTables[$etCaseTable] |
3333 | = " LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id | |
3334 | LEFT JOIN civicrm_case | |
6a488035 TO |
3335 | ON (civicrm_case_contact.case_id = civicrm_case.id |
3336 | AND civicrm_case.is_deleted = 0 ) | |
59f4c9ee | 3337 | LEFT JOIN civicrm_entity_tag {$etCaseTable} ON ( {$etCaseTable}.entity_table = 'civicrm_case' AND {$etCaseTable}.entity_id = civicrm_case.id ) "; |
6a488035 | 3338 | // search tag in activities |
394643b9 | 3339 | $etActTable = "`civicrm_entity_act_tag-" . uniqid() . "`"; |
59f4c9ee TO |
3340 | $this->_tables[$etActTable] = $this->_whereTables[$etActTable] |
3341 | = " LEFT JOIN civicrm_activity_contact | |
9e74e3ce | 3342 | ON ( civicrm_activity_contact.contact_id = contact_a.id AND civicrm_activity_contact.record_type_id = {$targetID} ) |
59f4c9ee | 3343 | LEFT JOIN civicrm_activity |
91da6cd5 | 3344 | ON ( civicrm_activity.id = civicrm_activity_contact.activity_id |
6a488035 | 3345 | AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 ) |
59f4c9ee | 3346 | LEFT JOIN civicrm_entity_tag as {$etActTable} ON ( {$etActTable}.entity_table = 'civicrm_activity' AND {$etActTable}.entity_id = civicrm_activity.id ) "; |
6a488035 TO |
3347 | |
3348 | // CRM-10338 | |
be2fb01f | 3349 | if (in_array($op, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) { |
6a488035 TO |
3350 | $this->_where[$grouping][] = "({$etTable}.tag_id $op OR {$etCaseTable}.tag_id $op OR {$etActTable}.tag_id $op)"; |
3351 | } | |
3352 | else { | |
5c7dad85 | 3353 | $this->_where[$grouping][] = "({$etTable}.tag_id $op (" . $value . ") OR {$etCaseTable}.tag_id $op (" . $value . ") OR {$etActTable}.tag_id $op (" . $value . "))"; |
6a488035 | 3354 | } |
5c7dad85 TO |
3355 | } |
3356 | else { | |
59f4c9ee TO |
3357 | $this->_tables[$etTable] = $this->_whereTables[$etTable] |
3358 | = " LEFT JOIN civicrm_entity_tag {$etTable} ON ( {$etTable}.entity_id = contact_a.id AND {$etTable}.entity_table = 'civicrm_contact') "; | |
6a488035 TO |
3359 | |
3360 | // CRM-10338 | |
be2fb01f | 3361 | if (in_array($op, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) { |
6a488035 TO |
3362 | // this converts IS (NOT)? EMPTY to IS (NOT)? NULL |
3363 | $op = str_replace('EMPTY', 'NULL', $op); | |
3364 | $this->_where[$grouping][] = "{$etTable}.tag_id $op"; | |
3365 | } | |
c112a6f9 | 3366 | // CRM-16941: for tag tried with != operator we don't show contact who don't have given $value AND also in other tag |
3367 | elseif ($op == '!=') { | |
3368 | $this->_where[$grouping][] = "{$etTable}.entity_id NOT IN (SELECT entity_id FROM civicrm_entity_tag cet WHERE cet.entity_table = 'civicrm_contact' AND " . self::buildClause("cet.tag_id", '=', $value, 'Int') . ")"; | |
3369 | } | |
3370 | elseif ($op == '=' || strstr($op, 'IN')) { | |
3371 | $op = ($op == '=') ? 'IN' : $op; | |
3372 | $this->_where[$grouping][] = "{$etTable}.tag_id $op ( $value )"; | |
6a488035 | 3373 | } |
6a488035 | 3374 | } |
be2fb01f | 3375 | $this->_qill[$grouping][] = ts('Tagged %1 %2', [1 => $qillop, 2 => $qillVal]); |
6a488035 TO |
3376 | } |
3377 | ||
3378 | /** | |
100fef9d | 3379 | * Where/qill clause for notes |
6a488035 | 3380 | * |
c037736a | 3381 | * @param array $values |
b832662c | 3382 | * |
3383 | * @throws \CRM_Core_Exception | |
6a488035 | 3384 | */ |
00be9182 | 3385 | public function notes(&$values) { |
6a488035 TO |
3386 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3387 | ||
3388 | $noteOptionValues = $this->getWhereValues('note_option', $grouping); | |
3389 | $noteOption = CRM_Utils_Array::value('2', $noteOptionValues, '6'); | |
3390 | $noteOption = ($name == 'note_body') ? 2 : (($name == 'note_subject') ? 3 : $noteOption); | |
3391 | ||
3392 | $this->_useDistinct = TRUE; | |
3393 | ||
59f4c9ee TO |
3394 | $this->_tables['civicrm_note'] = $this->_whereTables['civicrm_note'] |
3395 | = " LEFT JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contact' AND contact_a.id = civicrm_note.entity_id ) "; | |
6a488035 | 3396 | |
6a488035 | 3397 | $n = trim($value); |
2fc64082 | 3398 | $value = CRM_Core_DAO::escapeString($n); |
05d214de | 3399 | if ($wildcard) { |
6a488035 TO |
3400 | if (strpos($value, '%') === FALSE) { |
3401 | $value = "%$value%"; | |
3402 | } | |
3403 | $op = 'LIKE'; | |
3404 | } | |
3405 | elseif ($op == 'IS NULL' || $op == 'IS NOT NULL') { | |
3406 | $value = NULL; | |
3407 | } | |
3408 | ||
3409 | $label = NULL; | |
be2fb01f | 3410 | $clauses = []; |
5c7dad85 | 3411 | if ($noteOption % 2 == 0) { |
6a488035 TO |
3412 | $clauses[] = self::buildClause('civicrm_note.note', $op, $value, 'String'); |
3413 | $label = ts('Note: Body Only'); | |
3414 | } | |
5c7dad85 | 3415 | if ($noteOption % 3 == 0) { |
6a488035 TO |
3416 | $clauses[] = self::buildClause('civicrm_note.subject', $op, $value, 'String'); |
3417 | $label = $label ? ts('Note: Body and Subject') : ts('Note: Subject Only'); | |
3418 | } | |
3419 | $this->_where[$grouping][] = "( " . implode(' OR ', $clauses) . " )"; | |
05d214de | 3420 | list($qillOp, $qillVal) = self::buildQillForFieldValue(NULL, $name, $n, $op); |
be2fb01f | 3421 | $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $label, 2 => $qillOp, 3 => $qillVal]); |
6a488035 TO |
3422 | } |
3423 | ||
86538308 | 3424 | /** |
100fef9d | 3425 | * @param string $name |
86538308 EM |
3426 | * @param $op |
3427 | * @param $grouping | |
3428 | * | |
3429 | * @return bool | |
3430 | */ | |
00be9182 | 3431 | public function nameNullOrEmptyOp($name, $op, $grouping) { |
5c7dad85 | 3432 | switch ($op) { |
6a488035 TO |
3433 | case 'IS NULL': |
3434 | case 'IS NOT NULL': | |
3435 | $this->_where[$grouping][] = "contact_a.$name $op"; | |
3436 | $this->_qill[$grouping][] = ts('Name') . ' ' . $op; | |
5c7dad85 | 3437 | return TRUE; |
6a488035 TO |
3438 | |
3439 | case 'IS EMPTY': | |
3440 | $this->_where[$grouping][] = "(contact_a.$name IS NULL OR contact_a.$name = '')"; | |
3441 | $this->_qill[$grouping][] = ts('Name') . ' ' . $op; | |
5c7dad85 | 3442 | return TRUE; |
6a488035 TO |
3443 | |
3444 | case 'IS NOT EMPTY': | |
3445 | $this->_where[$grouping][] = "(contact_a.$name IS NOT NULL AND contact_a.$name <> '')"; | |
3446 | $this->_qill[$grouping][] = ts('Name') . ' ' . $op; | |
5c7dad85 | 3447 | return TRUE; |
6a488035 TO |
3448 | |
3449 | default: | |
5c7dad85 | 3450 | return FALSE; |
6a488035 TO |
3451 | } |
3452 | } | |
3453 | ||
3454 | /** | |
100fef9d | 3455 | * Where / qill clause for sort_name |
6a488035 | 3456 | * |
c037736a | 3457 | * @param array $values |
6a488035 | 3458 | */ |
00be9182 | 3459 | public function sortName(&$values) { |
fd816db5 | 3460 | list($fieldName, $op, $value, $grouping, $wildcard) = $values; |
6a488035 TO |
3461 | |
3462 | // handle IS NULL / IS NOT NULL / IS EMPTY / IS NOT EMPTY | |
fd816db5 | 3463 | if ($this->nameNullOrEmptyOp($fieldName, $op, $grouping)) { |
6a488035 TO |
3464 | return; |
3465 | } | |
3466 | ||
359fdb6f | 3467 | $input = $value = is_array($value) ? trim($value['LIKE']) : trim($value); |
6a488035 | 3468 | |
fd816db5 | 3469 | if (!strlen($value)) { |
6a488035 TO |
3470 | return; |
3471 | } | |
3472 | ||
3473 | $config = CRM_Core_Config::singleton(); | |
3474 | ||
be2fb01f | 3475 | $sub = []; |
6a488035 TO |
3476 | |
3477 | //By default, $sub elements should be joined together with OR statements (don't change this variable). | |
3478 | $subGlue = ' OR '; | |
3479 | ||
fd816db5 CW |
3480 | $firstChar = substr($value, 0, 1); |
3481 | $lastChar = substr($value, -1, 1); | |
be2fb01f | 3482 | $quotes = ["'", '"']; |
fd816db5 CW |
3483 | // If string is quoted, strip quotes and otherwise don't alter it |
3484 | if ((strlen($value) > 2) && in_array($firstChar, $quotes) && in_array($lastChar, $quotes)) { | |
3485 | $value = trim($value, implode('', $quotes)); | |
3486 | } | |
3487 | // Replace spaces with wildcards for a LIKE operation | |
3488 | // UNLESS string contains a comma (this exception is a tiny bit questionable) | |
bc1d5d42 | 3489 | // Also need to check if there is space in between sort name. |
1962d3d5 | 3490 | elseif ($op == 'LIKE' && strpos($value, ',') === FALSE && strpos($value, ' ') !== FALSE) { |
fd816db5 CW |
3491 | $value = str_replace(' ', '%', $value); |
3492 | } | |
9436d5d5 | 3493 | $value = CRM_Core_DAO::escapeString(trim($value)); |
fd816db5 | 3494 | if (strlen($value)) { |
be2fb01f | 3495 | $fieldsub = []; |
0b38e8f1 | 3496 | $value = "'" . self::getWildCardedValue($wildcard, $op, $value) . "'"; |
fd816db5 | 3497 | if ($fieldName == 'sort_name') { |
9436d5d5 | 3498 | $wc = "contact_a.sort_name"; |
6a488035 TO |
3499 | } |
3500 | else { | |
9436d5d5 | 3501 | $wc = "contact_a.display_name"; |
6a488035 | 3502 | } |
fd816db5 | 3503 | $fieldsub[] = " ( $wc $op $value )"; |
6a488035 | 3504 | if ($config->includeNickNameInName) { |
9436d5d5 | 3505 | $wc = "contact_a.nick_name"; |
fd816db5 | 3506 | $fieldsub[] = " ( $wc $op $value )"; |
6a488035 TO |
3507 | } |
3508 | if ($config->includeEmailInName) { | |
fd816db5 | 3509 | $fieldsub[] = " ( civicrm_email.email $op $value ) "; |
6a488035 | 3510 | } |
fd816db5 | 3511 | $sub[] = ' ( ' . implode(' OR ', $fieldsub) . ' ) '; |
6a488035 TO |
3512 | } |
3513 | ||
3514 | $sub = ' ( ' . implode($subGlue, $sub) . ' ) '; | |
3515 | ||
3516 | $this->_where[$grouping][] = $sub; | |
3517 | if ($config->includeEmailInName) { | |
3518 | $this->_tables['civicrm_email'] = $this->_whereTables['civicrm_email'] = 1; | |
fd816db5 | 3519 | $this->_qill[$grouping][] = ts('Name or Email') . " $op - '$input'"; |
6a488035 TO |
3520 | } |
3521 | else { | |
fd816db5 | 3522 | $this->_qill[$grouping][] = ts('Name') . " $op - '$input'"; |
6a488035 TO |
3523 | } |
3524 | } | |
3525 | ||
0e2e76cf | 3526 | /** |
cde2037d | 3527 | * Where/qill clause for greeting fields. |
02adf2f9 | 3528 | * |
cd2dfbd7 | 3529 | * @param array $values |
b832662c | 3530 | * |
3531 | * @throws \CRM_Core_Exception | |
02adf2f9 | 3532 | */ |
0e2e76cf | 3533 | public function greetings(&$values) { |
02adf2f9 BS |
3534 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3535 | $name .= '_display'; | |
3536 | ||
9b183452 | 3537 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op); |
be2fb01f | 3538 | $this->_qill[$grouping][] = ts('Greeting %1 %2', [1 => $qillop, 2 => $qillVal]); |
9b183452 | 3539 | $this->_where[$grouping][] = self::buildClause("contact_a.{$name}", $op, $value, 'String'); |
02adf2f9 BS |
3540 | } |
3541 | ||
6a488035 | 3542 | /** |
100fef9d | 3543 | * Where / qill clause for email |
6a488035 | 3544 | * |
c037736a | 3545 | * @param array $values |
9178793e | 3546 | * @param string $isForcePrimaryOnly |
3547 | * | |
3548 | * @throws \CRM_Core_Exception | |
6a488035 | 3549 | */ |
9178793e | 3550 | protected function email(&$values, $isForcePrimaryOnly) { |
6a488035 | 3551 | list($name, $op, $value, $grouping, $wildcard) = $values; |
11f8e194 | 3552 | $this->_tables['civicrm_email'] = $this->_whereTables['civicrm_email'] = 1; |
6a488035 | 3553 | |
57f8e7f0 | 3554 | // CRM-18147: for Contact's GET API, email fieldname got appended with its entity as in {$apiEntiy}_{$name} |
3555 | // so following code is use build whereClause for contact's primart email id | |
9178793e | 3556 | if (!empty($isForcePrimaryOnly)) { |
57f8e7f0 | 3557 | $this->_where[$grouping][] = self::buildClause('civicrm_email.is_primary', '=', 1, 'Integer'); |
9178793e | 3558 | } |
3559 | // @todo - this should come from the $this->_fields array | |
3560 | $dbName = $name === 'email_id' ? 'id' : $name; | |
3561 | ||
3562 | if (is_array($value) || $name === 'email_id') { | |
a43deb74 | 3563 | $this->_qill[$grouping][] = $this->getQillForField($name, $value, $op, [], ts('Email')); |
9178793e | 3564 | $this->_where[$grouping][] = self::buildClause('civicrm_email.' . $dbName, $op, $value, 'String'); |
57f8e7f0 | 3565 | return; |
3566 | } | |
3567 | ||
9178793e | 3568 | // Is this ever hit now? Ideally ensure always an array & handle above. |
c245fd99 | 3569 | $n = trim($value); |
6a488035 | 3570 | if ($n) { |
6a488035 TO |
3571 | if (substr($n, 0, 1) == '"' && |
3572 | substr($n, -1, 1) == '"' | |
3573 | ) { | |
3574 | $n = substr($n, 1, -1); | |
836eb043 | 3575 | $value = CRM_Core_DAO::escapeString($n); |
6a488035 TO |
3576 | $op = '='; |
3577 | } | |
3578 | else { | |
0b38e8f1 | 3579 | $value = self::getWildCardedValue($wildcard, $op, $n); |
6a488035 TO |
3580 | } |
3581 | $this->_qill[$grouping][] = ts('Email') . " $op '$n'"; | |
3582 | $this->_where[$grouping][] = self::buildClause('civicrm_email.email', $op, $value, 'String'); | |
3583 | } | |
3584 | else { | |
3585 | $this->_qill[$grouping][] = ts('Email') . " $op "; | |
3586 | $this->_where[$grouping][] = self::buildClause('civicrm_email.email', $op, NULL, 'String'); | |
3587 | } | |
6a488035 TO |
3588 | } |
3589 | ||
3590 | /** | |
100fef9d | 3591 | * Where / qill clause for phone number |
6a488035 | 3592 | * |
c037736a | 3593 | * @param array $values |
b832662c | 3594 | * |
3595 | * @throws \CRM_Core_Exception | |
6a488035 | 3596 | */ |
00be9182 | 3597 | public function phone_numeric(&$values) { |
6a488035 | 3598 | list($name, $op, $value, $grouping, $wildcard) = $values; |
a195fab2 BS |
3599 | // Strip non-numeric characters; allow wildcards |
3600 | $number = preg_replace('/[^\d%]/', '', $value); | |
6a488035 | 3601 | if ($number) { |
5c7dad85 | 3602 | if (strpos($number, '%') === FALSE) { |
a195fab2 BS |
3603 | $number = "%$number%"; |
3604 | } | |
3605 | ||
6a488035 | 3606 | $this->_qill[$grouping][] = ts('Phone number contains') . " $number"; |
a195fab2 | 3607 | $this->_where[$grouping][] = self::buildClause('civicrm_phone.phone_numeric', 'LIKE', "$number", 'String'); |
6a488035 TO |
3608 | $this->_tables['civicrm_phone'] = $this->_whereTables['civicrm_phone'] = 1; |
3609 | } | |
3610 | } | |
3611 | ||
3612 | /** | |
100fef9d | 3613 | * Where / qill clause for phone type/location |
6a488035 | 3614 | * |
c037736a | 3615 | * @param array $values |
e14c912f | 3616 | * |
3617 | * @throws \CRM_Core_Exception | |
6a488035 | 3618 | */ |
00be9182 | 3619 | public function phone_option_group($values) { |
6a488035 | 3620 | list($name, $op, $value, $grouping, $wildcard) = $values; |
cbf48754 AS |
3621 | $option = ($name == 'phone_phone_type_id' ? 'phone_type_id' : 'location_type_id'); |
3622 | $options = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', $option); | |
6a488035 TO |
3623 | $optionName = $options[$value]; |
3624 | $this->_qill[$grouping][] = ts('Phone') . ' ' . ($name == 'phone_phone_type_id' ? ts('type') : ('location')) . " $op $optionName"; | |
3625 | $this->_where[$grouping][] = self::buildClause('civicrm_phone.' . substr($name, 6), $op, $value, 'Integer'); | |
3626 | $this->_tables['civicrm_phone'] = $this->_whereTables['civicrm_phone'] = 1; | |
3627 | } | |
3628 | ||
3629 | /** | |
c037736a | 3630 | * Where / qill clause for street_address. |
6a488035 | 3631 | * |
c037736a | 3632 | * @param array $values |
b832662c | 3633 | * |
3634 | * @throws \CRM_Core_Exception | |
6a488035 | 3635 | */ |
00be9182 | 3636 | public function street_address(&$values) { |
6d52bfe5 | 3637 | list($name, $op, $value, $grouping) = $values; |
6a488035 TO |
3638 | |
3639 | if (!$op) { | |
3640 | $op = 'LIKE'; | |
3641 | } | |
3642 | ||
3643 | $n = trim($value); | |
3644 | ||
3645 | if ($n) { | |
6a488035 TO |
3646 | if (strpos($value, '%') === FALSE) { |
3647 | // only add wild card if not there | |
3648 | $value = "%{$value}%"; | |
3649 | } | |
3650 | $op = 'LIKE'; | |
fea8ae41 | 3651 | $this->_where[$grouping][] = self::buildClause('civicrm_address.street_address', $op, $value, 'String'); |
6a488035 TO |
3652 | $this->_qill[$grouping][] = ts('Street') . " $op '$n'"; |
3653 | } | |
3654 | else { | |
3655 | $this->_where[$grouping][] = self::buildClause('civicrm_address.street_address', $op, NULL, 'String'); | |
3656 | $this->_qill[$grouping][] = ts('Street') . " $op "; | |
3657 | } | |
3658 | ||
3659 | $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1; | |
3660 | } | |
3661 | ||
3662 | /** | |
c037736a | 3663 | * Where / qill clause for street_unit. |
77b97be7 | 3664 | * |
c037736a | 3665 | * @param array $values |
b832662c | 3666 | * |
3667 | * @throws \CRM_Core_Exception | |
6a488035 | 3668 | */ |
00be9182 | 3669 | public function street_number(&$values) { |
6a488035 TO |
3670 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3671 | ||
3672 | if (!$op) { | |
3673 | $op = '='; | |
3674 | } | |
3675 | ||
3676 | $n = trim($value); | |
3677 | ||
3678 | if (strtolower($n) == 'odd') { | |
3679 | $this->_where[$grouping][] = " ( civicrm_address.street_number % 2 = 1 )"; | |
3680 | $this->_qill[$grouping][] = ts('Street Number is odd'); | |
3681 | } | |
3682 | elseif (strtolower($n) == 'even') { | |
3683 | $this->_where[$grouping][] = " ( civicrm_address.street_number % 2 = 0 )"; | |
3684 | $this->_qill[$grouping][] = ts('Street Number is even'); | |
3685 | } | |
3686 | else { | |
2fc64082 | 3687 | $value = $n; |
3688 | $this->_where[$grouping][] = self::buildClause('civicrm_address.street_number', $op, $value, 'String'); | |
6a488035 TO |
3689 | $this->_qill[$grouping][] = ts('Street Number') . " $op '$n'"; |
3690 | } | |
3691 | ||
3692 | $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1; | |
3693 | } | |
3694 | ||
3695 | /** | |
c037736a | 3696 | * Where / qill clause for sorting by character. |
77b97be7 | 3697 | * |
c037736a | 3698 | * @param array $values |
6a488035 | 3699 | */ |
00be9182 | 3700 | public function sortByCharacter(&$values) { |
6a488035 TO |
3701 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3702 | ||
3703 | $name = trim($value); | |
2fc64082 | 3704 | $cond = " contact_a.sort_name LIKE '" . CRM_Core_DAO::escapeWildCardString($name) . "%'"; |
6a488035 | 3705 | $this->_where[$grouping][] = $cond; |
be2fb01f | 3706 | $this->_qill[$grouping][] = ts('Showing only Contacts starting with: \'%1\'', [1 => $name]); |
6a488035 TO |
3707 | } |
3708 | ||
3709 | /** | |
c037736a | 3710 | * Where / qill clause for including contact ids. |
6a488035 | 3711 | */ |
00be9182 | 3712 | public function includeContactIDs() { |
6a488035 TO |
3713 | if (!$this->_includeContactIds || empty($this->_params)) { |
3714 | return; | |
3715 | } | |
3716 | ||
be2fb01f | 3717 | $contactIds = []; |
6a488035 TO |
3718 | foreach ($this->_params as $id => $values) { |
3719 | if (substr($values[0], 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { | |
3720 | $contactIds[] = substr($values[0], CRM_Core_Form::CB_PREFIX_LEN); | |
3721 | } | |
3722 | } | |
b1128d0b | 3723 | CRM_Utils_Type::validateAll($contactIds, 'Positive'); |
6a488035 | 3724 | if (!empty($contactIds)) { |
95325bb8 | 3725 | $this->_where[0][] = ' ( contact_a.id IN (' . implode(',', $contactIds) . " ) ) "; |
6a488035 TO |
3726 | } |
3727 | } | |
3728 | ||
3729 | /** | |
c037736a | 3730 | * Where / qill clause for postal code. |
77b97be7 | 3731 | * |
c037736a | 3732 | * @param array $values |
b832662c | 3733 | * |
3734 | * @throws \CRM_Core_Exception | |
6a488035 | 3735 | */ |
00be9182 | 3736 | public function postalCode(&$values) { |
6a488035 | 3737 | // skip if the fields dont have anything to do with postal_code |
a7488080 | 3738 | if (empty($this->_fields['postal_code'])) { |
6a488035 TO |
3739 | return; |
3740 | } | |
3741 | ||
3742 | list($name, $op, $value, $grouping, $wildcard) = $values; | |
3743 | ||
3744 | // Handle numeric postal code range searches properly by casting the column as numeric | |
3745 | if (is_numeric($value)) { | |
abc78dd4 | 3746 | $field = "IF (civicrm_address.postal_code REGEXP '^[0-9]{1,10}$', CAST(civicrm_address.postal_code AS UNSIGNED), 0)"; |
6a488035 TO |
3747 | $val = CRM_Utils_Type::escape($value, 'Integer'); |
3748 | } | |
3749 | else { | |
3750 | $field = 'civicrm_address.postal_code'; | |
715277e7 | 3751 | // Per CRM-17060 we might be looking at an 'IN' syntax so don't case arrays to string. |
3752 | if (!is_array($value)) { | |
3753 | $val = CRM_Utils_Type::escape($value, 'String'); | |
3754 | } | |
3755 | else { | |
3756 | // Do we need to escape values here? I would expect buildClause does. | |
3757 | $val = $value; | |
3758 | } | |
6a488035 TO |
3759 | } |
3760 | ||
3761 | $this->_tables['civicrm_address'] = $this->_whereTables['civicrm_address'] = 1; | |
3762 | ||
3763 | if ($name == 'postal_code') { | |
3764 | $this->_where[$grouping][] = self::buildClause($field, $op, $val, 'String'); | |
3765 | $this->_qill[$grouping][] = ts('Postal code') . " {$op} {$value}"; | |
3766 | } | |
3767 | elseif ($name == 'postal_code_low') { | |
3768 | $this->_where[$grouping][] = " ( $field >= '$val' ) "; | |
be2fb01f | 3769 | $this->_qill[$grouping][] = ts('Postal code greater than or equal to \'%1\'', [1 => $value]); |
6a488035 TO |
3770 | } |
3771 | elseif ($name == 'postal_code_high') { | |
3772 | $this->_where[$grouping][] = " ( $field <= '$val' ) "; | |
be2fb01f | 3773 | $this->_qill[$grouping][] = ts('Postal code less than or equal to \'%1\'', [1 => $value]); |
6a488035 TO |
3774 | } |
3775 | } | |
3776 | ||
3777 | /** | |
c037736a | 3778 | * Where / qill clause for location type. |
6a488035 | 3779 | * |
c037736a | 3780 | * @param array $values |
77b97be7 EM |
3781 | * @param null $status |
3782 | * | |
c037736a | 3783 | * @return string |
6a488035 | 3784 | */ |
00be9182 | 3785 | public function locationType(&$values, $status = NULL) { |
6a488035 TO |
3786 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3787 | ||
3788 | if (is_array($value)) { | |
d8a8bb0b | 3789 | $this->_where[$grouping][] = 'civicrm_address.location_type_id IN (' . implode(',', $value) . ')'; |
6a488035 TO |
3790 | $this->_tables['civicrm_address'] = 1; |
3791 | $this->_whereTables['civicrm_address'] = 1; | |
3792 | ||
b2b0530a | 3793 | $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
be2fb01f | 3794 | $names = []; |
d8a8bb0b | 3795 | foreach ($value as $id) { |
6a488035 TO |
3796 | $names[] = $locationType[$id]; |
3797 | } | |
3798 | ||
3799 | $this->_primaryLocation = FALSE; | |
3800 | ||
3801 | if (!$status) { | |
3802 | $this->_qill[$grouping][] = ts('Location Type') . ' - ' . implode(' ' . ts('or') . ' ', $names); | |
3803 | } | |
3804 | else { | |
3805 | return implode(' ' . ts('or') . ' ', $names); | |
3806 | } | |
3807 | } | |
3808 | } | |
3809 | ||
86538308 EM |
3810 | /** |
3811 | * @param $values | |
3812 | * @param bool $fromStateProvince | |
3813 | * | |
59f4c9ee | 3814 | * @return array|NULL |
b832662c | 3815 | * @throws \CRM_Core_Exception |
86538308 | 3816 | */ |
00be9182 | 3817 | public function country(&$values, $fromStateProvince = TRUE) { |
6a488035 TO |
3818 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3819 | ||
3820 | if (!$fromStateProvince) { | |
3821 | $stateValues = $this->getWhereValues('state_province', $grouping); | |
3822 | if (!empty($stateValues)) { | |
3823 | // return back to caller if there are state province values | |
3824 | // since that handles this case | |
59f4c9ee | 3825 | return NULL; |
6a488035 TO |
3826 | } |
3827 | } | |
3828 | ||
3829 | $countryClause = $countryQill = NULL; | |
be2fb01f | 3830 | if (in_array($op, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY']) || ($values && !empty($value))) { |
d9ab802d PJ |
3831 | $this->_tables['civicrm_address'] = 1; |
3832 | $this->_whereTables['civicrm_address'] = 1; | |
6a488035 | 3833 | |
836f1f3f | 3834 | $countryClause = self::buildClause('civicrm_address.country_id', $op, $value, 'Positive'); |
3835 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, 'country_id', $value, $op); | |
be2fb01f | 3836 | $countryQill = ts("%1 %2 %3", [1 => 'Country', 2 => $qillop, 3 => $qillVal]); |
6a488035 TO |
3837 | |
3838 | if (!$fromStateProvince) { | |
3839 | $this->_where[$grouping][] = $countryClause; | |
3840 | $this->_qill[$grouping][] = $countryQill; | |
3841 | } | |
3842 | } | |
3843 | ||
3844 | if ($fromStateProvince) { | |
3845 | if (!empty($countryClause)) { | |
be2fb01f | 3846 | return [ |
6a488035 TO |
3847 | $countryClause, |
3848 | " ...AND... " . $countryQill, | |
be2fb01f | 3849 | ]; |
6a488035 TO |
3850 | } |
3851 | else { | |
be2fb01f | 3852 | return [NULL, NULL]; |
6a488035 TO |
3853 | } |
3854 | } | |
3855 | } | |
3856 | ||
3857 | /** | |
c037736a | 3858 | * Where / qill clause for county (if present). |
6a488035 | 3859 | * |
c037736a | 3860 | * @param array $values |
77b97be7 EM |
3861 | * @param null $status |
3862 | * | |
c037736a | 3863 | * @return string |
6a488035 | 3864 | */ |
5c7dad85 | 3865 | public function county(&$values, $status = NULL) { |
6a488035 TO |
3866 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3867 | ||
5c7dad85 | 3868 | if (!is_array($value)) { |
6a488035 | 3869 | // force the county to be an array |
be2fb01f | 3870 | $value = [$value]; |
6a488035 TO |
3871 | } |
3872 | ||
3873 | // check if the values are ids OR names of the counties | |
3874 | $inputFormat = 'id'; | |
3875 | foreach ($value as $v) { | |
3876 | if (!is_numeric($v)) { | |
3877 | $inputFormat = 'name'; | |
3878 | break; | |
3879 | } | |
3880 | } | |
be2fb01f | 3881 | $names = []; |
d9ab802d PJ |
3882 | if ($op == '=') { |
3883 | $op = 'IN'; | |
3884 | } | |
4c9b6178 | 3885 | elseif ($op == '!=') { |
d9ab802d PJ |
3886 | $op = 'NOT IN'; |
3887 | } | |
3888 | else { | |
3889 | // this converts IS (NOT)? EMPTY to IS (NOT)? NULL | |
3890 | $op = str_replace('EMPTY', 'NULL', $op); | |
3891 | } | |
3892 | ||
be2fb01f | 3893 | if (in_array($op, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) { |
d9ab802d PJ |
3894 | $clause = "civicrm_address.county_id $op"; |
3895 | } | |
3896 | elseif ($inputFormat == 'id') { | |
3897 | $clause = 'civicrm_address.county_id IN (' . implode(',', $value) . ')'; | |
6a488035 TO |
3898 | |
3899 | $county = CRM_Core_PseudoConstant::county(); | |
3900 | foreach ($value as $id) { | |
9c1bc317 | 3901 | $names[] = $county[$id] ?? NULL; |
6a488035 TO |
3902 | } |
3903 | } | |
3904 | else { | |
be2fb01f | 3905 | $inputClause = []; |
d9ab802d | 3906 | $county = CRM_Core_PseudoConstant::county(); |
6a488035 TO |
3907 | foreach ($value as $name) { |
3908 | $name = trim($name); | |
d9ab802d | 3909 | $inputClause[] = CRM_Utils_Array::key($name, $county); |
6a488035 | 3910 | } |
d9ab802d | 3911 | $clause = 'civicrm_address.county_id IN (' . implode(',', $inputClause) . ')'; |
6a488035 TO |
3912 | $names = $value; |
3913 | } | |
d9ab802d PJ |
3914 | $this->_tables['civicrm_address'] = 1; |
3915 | $this->_whereTables['civicrm_address'] = 1; | |
6a488035 TO |
3916 | |
3917 | $this->_where[$grouping][] = $clause; | |
d9ab802d | 3918 | if (!$status) { |
6a488035 | 3919 | $this->_qill[$grouping][] = ts('County') . ' - ' . implode(' ' . ts('or') . ' ', $names); |
5c7dad85 TO |
3920 | } |
3921 | else { | |
6a488035 TO |
3922 | return implode(' ' . ts('or') . ' ', $names); |
3923 | } | |
3924 | } | |
3925 | ||
3926 | /** | |
c037736a | 3927 | * Where / qill clause for state/province AND country (if present). |
6a488035 | 3928 | * |
c037736a | 3929 | * @param array $values |
77b97be7 EM |
3930 | * @param null $status |
3931 | * | |
c037736a | 3932 | * @return string |
b832662c | 3933 | * @throws \CRM_Core_Exception |
6a488035 | 3934 | */ |
00be9182 | 3935 | public function stateProvince(&$values, $status = NULL) { |
6a488035 TO |
3936 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3937 | ||
6bd09f41 | 3938 | $stateClause = self::buildClause('civicrm_address.state_province_id', $op, $value, 'Positive'); |
d9ab802d PJ |
3939 | $this->_tables['civicrm_address'] = 1; |
3940 | $this->_whereTables['civicrm_address'] = 1; | |
6a488035 TO |
3941 | |
3942 | $countryValues = $this->getWhereValues('country', $grouping); | |
3943 | list($countryClause, $countryQill) = $this->country($countryValues, TRUE); | |
6a488035 TO |
3944 | if ($countryClause) { |
3945 | $clause = "( $stateClause AND $countryClause )"; | |
3946 | } | |
3947 | else { | |
3948 | $clause = $stateClause; | |
3949 | } | |
3950 | ||
3951 | $this->_where[$grouping][] = $clause; | |
6bd09f41 | 3952 | list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_Address', "state_province_id", $value, $op); |
6a488035 | 3953 | if (!$status) { |
be2fb01f | 3954 | $this->_qill[$grouping][] = ts("State/Province %1 %2 %3", [1 => $qillop, 2 => $qillVal, 3 => $countryQill]); |
6a488035 TO |
3955 | } |
3956 | else { | |
6bd09f41 | 3957 | return implode(' ' . ts('or') . ' ', $qillVal) . $countryQill; |
6a488035 TO |
3958 | } |
3959 | } | |
3960 | ||
3961 | /** | |
c037736a | 3962 | * Where / qill clause for change log. |
77b97be7 | 3963 | * |
c037736a | 3964 | * @param array $values |
6a488035 | 3965 | */ |
00be9182 | 3966 | public function changeLog(&$values) { |
6a488035 TO |
3967 | list($name, $op, $value, $grouping, $wildcard) = $values; |
3968 | ||
3969 | $targetName = $this->getWhereValues('changed_by', $grouping); | |
3970 | if (!$targetName) { | |
3971 | return; | |
3972 | } | |
3973 | ||
3974 | $name = trim($targetName[2]); | |
2fc64082 | 3975 | $name = CRM_Core_DAO::escapeString($name); |
6a488035 TO |
3976 | $name = $targetName[4] ? "%$name%" : $name; |
3977 | $this->_where[$grouping][] = "contact_b_log.sort_name LIKE '%$name%'"; | |
3978 | $this->_tables['civicrm_log'] = $this->_whereTables['civicrm_log'] = 1; | |
80b5c9f1 | 3979 | $fieldTitle = ts('Altered By'); |
3980 | ||
96b84142 | 3981 | list($qillop, $qillVal) = self::buildQillForFieldValue(NULL, 'changed_by', $name, 'LIKE'); |
b478616b | 3982 | $this->_qill[$grouping][] = ts("%1 %2 '%3'", [ |
96b84142 PN |
3983 | 1 => $fieldTitle, |
3984 | 2 => $qillop, | |
3985 | 3 => $qillVal, | |
3986 | ]); | |
6a488035 TO |
3987 | } |
3988 | ||
86538308 EM |
3989 | /** |
3990 | * @param $values | |
b832662c | 3991 | * |
3992 | * @throws \CRM_Core_Exception | |
86538308 | 3993 | */ |
00be9182 | 3994 | public function modifiedDates($values) { |
6a488035 | 3995 | $this->_useDistinct = TRUE; |
5c692063 | 3996 | CRM_Core_Error::deprecatedWarning('function should not be reachable'); |
9e9b8fdb | 3997 | // CRM-11281, default to added date if not set |
228917cd | 3998 | $fieldTitle = ts('Added Date'); |
06103b23 | 3999 | $fieldName = 'created_date'; |
6a488035 TO |
4000 | foreach (array_keys($this->_params) as $id) { |
4001 | if ($this->_params[$id][0] == 'log_date') { | |
228917cd DL |
4002 | if ($this->_params[$id][2] == 2) { |
4003 | $fieldTitle = ts('Modified Date'); | |
06103b23 | 4004 | $fieldName = 'modified_date'; |
6a488035 TO |
4005 | } |
4006 | } | |
4007 | } | |
4008 | ||
06103b23 | 4009 | $this->dateQueryBuilder($values, 'contact_a', 'log_date', $fieldName, $fieldTitle); |
4010 | ||
4011 | self::$_openedPanes[ts('Change Log')] = TRUE; | |
6a488035 TO |
4012 | } |
4013 | ||
86538308 EM |
4014 | /** |
4015 | * @param $values | |
e14c912f | 4016 | * |
4017 | * @throws \CRM_Core_Exception | |
86538308 | 4018 | */ |
00be9182 | 4019 | public function demographics(&$values) { |
6a488035 TO |
4020 | list($name, $op, $value, $grouping, $wildcard) = $values; |
4021 | ||
c4a7c967 | 4022 | if (($name == 'age_low') || ($name == 'age_high')) { |
4023 | $this->ageRangeQueryBuilder($values, | |
4024 | 'contact_a', 'age', 'birth_date', ts('Age') | |
4025 | ); | |
4026 | } | |
4027 | elseif (($name == 'birth_date_low') || ($name == 'birth_date_high')) { | |
6a488035 TO |
4028 | |
4029 | $this->dateQueryBuilder($values, | |
4030 | 'contact_a', 'birth_date', 'birth_date', ts('Birth Date') | |
4031 | ); | |
4032 | } | |
4033 | elseif (($name == 'deceased_date_low') || ($name == 'deceased_date_high')) { | |
4034 | ||
4035 | $this->dateQueryBuilder($values, | |
4036 | 'contact_a', 'deceased_date', 'deceased_date', ts('Deceased Date') | |
4037 | ); | |
4038 | } | |
4039 | ||
4040 | self::$_openedPanes[ts('Demographics')] = TRUE; | |
4041 | } | |
4042 | ||
86538308 EM |
4043 | /** |
4044 | * @param $values | |
4045 | */ | |
00be9182 | 4046 | public function privacy(&$values) { |
6d52bfe5 | 4047 | list($name, $op, $value, $grouping) = $values; |
e15bd6d1 SL |
4048 | if (is_array($value)) { |
4049 | if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { | |
4050 | $op = key($value); | |
4051 | $value = $value[$op]; | |
4052 | } | |
6a488035 | 4053 | } |
9c1bc317 | 4054 | $field = $this->_fields[$name] ?? NULL; |
dca9c5bc SL |
4055 | CRM_Utils_Type::validate($value, 'Integer'); |
4056 | $this->_where[$grouping][] = "contact_a.{$name} $op $value"; | |
9b183452 | 4057 | $op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op); |
6a488035 TO |
4058 | $title = $field ? $field['title'] : $name; |
4059 | $this->_qill[$grouping][] = "$title $op $value"; | |
4060 | } | |
4061 | ||
86538308 EM |
4062 | /** |
4063 | * @param $values | |
4064 | */ | |
00be9182 | 4065 | public function privacyOptions($values) { |
6a488035 TO |
4066 | list($name, $op, $value, $grouping, $wildcard) = $values; |
4067 | ||
9fd71915 CW |
4068 | if (empty($value) || !is_array($value)) { |
4069 | return; | |
6a488035 TO |
4070 | } |
4071 | ||
4072 | // get the operator and toggle values | |
4073 | $opValues = $this->getWhereValues('privacy_operator', $grouping); | |
4074 | $operator = 'OR'; | |
4075 | if ($opValues && | |
4076 | strtolower($opValues[2] == 'AND') | |
4077 | ) { | |
2fc64082 | 4078 | // @todo this line is logially unreachable |
6a488035 TO |
4079 | $operator = 'AND'; |
4080 | } | |
4081 | ||
4082 | $toggleValues = $this->getWhereValues('privacy_toggle', $grouping); | |
e10c7983 | 4083 | $compareOP = '!'; |
6a488035 TO |
4084 | if ($toggleValues && |
4085 | $toggleValues[2] == 2 | |
4086 | ) { | |
e10c7983 | 4087 | $compareOP = ''; |
6a488035 TO |
4088 | } |
4089 | ||
be2fb01f CW |
4090 | $clauses = []; |
4091 | $qill = []; | |
6a488035 | 4092 | foreach ($value as $dontCare => $pOption) { |
e10c7983 | 4093 | $clauses[] = " ( contact_a.{$pOption} = 1 ) "; |
9c1bc317 | 4094 | $field = $this->_fields[$pOption] ?? NULL; |
6a488035 | 4095 | $title = $field ? $field['title'] : $pOption; |
e10c7983 | 4096 | $qill[] = " $title = 1 "; |
6a488035 TO |
4097 | } |
4098 | ||
e10c7983 JL |
4099 | $this->_where[$grouping][] = $compareOP . '( ' . implode($operator, $clauses) . ' )'; |
4100 | $this->_qill[$grouping][] = $compareOP . '( ' . implode($operator, $qill) . ' )'; | |
6a488035 TO |
4101 | } |
4102 | ||
86538308 EM |
4103 | /** |
4104 | * @param $values | |
b832662c | 4105 | * |
4106 | * @throws \CRM_Core_Exception | |
86538308 | 4107 | */ |
00be9182 | 4108 | public function preferredCommunication(&$values) { |
6a488035 TO |
4109 | list($name, $op, $value, $grouping, $wildcard) = $values; |
4110 | ||
c112a6f9 | 4111 | if (!is_array($value)) { |
be2fb01f | 4112 | $value = str_replace(['(', ')'], '', explode(",", $value)); |
6a488035 | 4113 | } |
7cc09daf | 4114 | elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { |
4115 | $op = key($value); | |
4116 | $value = $value[$op]; | |
6a488035 | 4117 | } |
05d214de | 4118 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Contact', $name, $value, $op); |
6a488035 | 4119 | |
05d214de | 4120 | if (self::caseImportant($op)) { |
4121 | $value = implode("[[:cntrl:]]|[[:cntrl:]]", (array) $value); | |
4122 | $op = (strstr($op, '!') || strstr($op, 'NOT')) ? 'NOT RLIKE' : 'RLIKE'; | |
4123 | $value = "[[:cntrl:]]" . $value . "[[:cntrl:]]"; | |
4b20f7df | 4124 | } |
6a488035 | 4125 | |
05d214de | 4126 | $this->_where[$grouping][] = self::buildClause("contact_a.preferred_communication_method", $op, $value); |
be2fb01f | 4127 | $this->_qill[$grouping][] = ts('Preferred Communication Method %1 %2', [1 => $qillop, 2 => $qillVal]); |
6a488035 TO |
4128 | } |
4129 | ||
6a488035 | 4130 | /** |
c037736a | 4131 | * Where / qill clause for relationship. |
77b97be7 | 4132 | * |
c037736a | 4133 | * @param array $values |
6a488035 | 4134 | */ |
00be9182 | 4135 | public function relationship(&$values) { |
6a488035 | 4136 | list($name, $op, $value, $grouping, $wildcard) = $values; |
5c7dad85 TO |
4137 | if ($this->_relationshipValuesAdded) { |
4138 | return; | |
ad682134 | 4139 | } |
6a488035 | 4140 | // also get values array for relation_target_name |
2fef460a | 4141 | // for relationship search we always do wildcard |
b3342109 | 4142 | $relationType = $this->getWhereValues('relation_type_id', $grouping); |
6e83b317 | 4143 | $description = $this->getWhereValues('relation_description', $grouping); |
6a488035 TO |
4144 | $targetName = $this->getWhereValues('relation_target_name', $grouping); |
4145 | $relStatus = $this->getWhereValues('relation_status', $grouping); | |
4146 | $targetGroup = $this->getWhereValues('relation_target_group', $grouping); | |
b9cb427a | 4147 | |
6a488035 TO |
4148 | $nameClause = $name = NULL; |
4149 | if ($targetName) { | |
4150 | $name = trim($targetName[2]); | |
4151 | if (substr($name, 0, 1) == '"' && | |
4152 | substr($name, -1, 1) == '"' | |
4153 | ) { | |
4154 | $name = substr($name, 1, -1); | |
dd2db5e5 | 4155 | $name = CRM_Core_DAO::escapeString($name); |
6a488035 TO |
4156 | $nameClause = "= '$name'"; |
4157 | } | |
4158 | else { | |
dd2db5e5 | 4159 | $name = CRM_Core_DAO::escapeString($name); |
6a488035 TO |
4160 | $nameClause = "LIKE '%{$name}%'"; |
4161 | } | |
4162 | } | |
4163 | ||
be2fb01f | 4164 | $relTypes = $relTypesIds = []; |
5c7dad85 | 4165 | if (!empty($relationType)) { |
026242fc SL |
4166 | $relationType[2] = (array) $relationType[2]; |
4167 | foreach ($relationType[2] as $relType) { | |
4168 | $rel = explode('_', $relType); | |
c0184a48 | 4169 | self::$_relType = $rel[1]; |
be2fb01f CW |
4170 | $params = ['id' => $rel[0]]; |
4171 | $typeValues = []; | |
5ad36be5 SL |
4172 | $rTypeValue = CRM_Contact_BAO_RelationshipType::retrieve($params, $typeValues); |
4173 | if (!empty($rTypeValue)) { | |
dd2db5e5 | 4174 | if ($rTypeValue->name_a_b == $rTypeValue->name_b_a) { |
4175 | // if we don't know which end of the relationship we are dealing with we'll create a temp table | |
4176 | self::$_relType = 'reciprocal'; | |
4177 | } | |
5ad36be5 SL |
4178 | $relTypesIds[] = $rel[0]; |
4179 | $relTypes[] = $relType; | |
4180 | } | |
026242fc SL |
4181 | } |
4182 | } | |
dd2db5e5 | 4183 | |
7c96365b | 4184 | // if we are creating a temp table we build our own where for the relationship table |
085e6226 | 4185 | $relationshipTempTable = NULL; |
72a2eeab | 4186 | if (self::$_relType == 'reciprocal') { |
be2fb01f | 4187 | $where = []; |
5b508244 | 4188 | self::$_relationshipTempTable = $relationshipTempTable = CRM_Utils_SQL_TempTable::build() |
804261ab | 4189 | ->createWithColumns("`contact_id` int(10) unsigned NOT NULL DEFAULT '0', `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0', id int unsigned, KEY `contact_id` (`contact_id`), KEY `contact_id_alt` (`contact_id_alt`)") |
5b508244 | 4190 | ->getName(); |
11a5aa5d | 4191 | if ($nameClause) { |
7c96365b | 4192 | $where[$grouping][] = " sort_name $nameClause "; |
4193 | } | |
72a2eeab AS |
4194 | $groupJoinTable = "civicrm_relationship"; |
4195 | $groupJoinColumn = "contact_id_alt"; | |
6a488035 | 4196 | } |
7c96365b | 4197 | else { |
4198 | $where = &$this->_where; | |
4199 | if ($nameClause) { | |
4200 | $where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )"; | |
4201 | } | |
72a2eeab AS |
4202 | $groupJoinTable = "contact_b"; |
4203 | $groupJoinColumn = "id"; | |
7c96365b | 4204 | } |
804261ab | 4205 | $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE, 'label', FALSE); |
6a488035 | 4206 | if ($nameClause || !$targetGroup) { |
5c7dad85 | 4207 | if (!empty($relationType)) { |
026242fc | 4208 | $relQill = ''; |
5ad36be5 | 4209 | foreach ($relTypes as $rel) { |
026242fc SL |
4210 | if (!empty($relQill)) { |
4211 | $relQill .= ' OR '; | |
4212 | } | |
4213 | $relQill .= $allRelationshipType[$rel]; | |
4214 | } | |
4215 | $this->_qill[$grouping][] = 'Relationship Type(s) ' . $relQill . " $name"; | |
5c7dad85 | 4216 | } |
c47a2365 | 4217 | elseif ($name) { |
2b0c31bc DG |
4218 | $this->_qill[$grouping][] = $name; |
4219 | } | |
6a488035 TO |
4220 | } |
4221 | ||
6a488035 TO |
4222 | //check to see if the target contact is in specified group |
4223 | if ($targetGroup) { | |
4224 | //add contacts from static groups | |
59f4c9ee | 4225 | $this->_tables['civicrm_relationship_group_contact'] = $this->_whereTables['civicrm_relationship_group_contact'] |
72a2eeab | 4226 | = " LEFT JOIN civicrm_group_contact civicrm_relationship_group_contact ON civicrm_relationship_group_contact.contact_id = {$groupJoinTable}.{$groupJoinColumn} AND civicrm_relationship_group_contact.status = 'Added'"; |
59f4c9ee | 4227 | $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN (" . |
5c7dad85 | 4228 | implode(",", $targetGroup[2]) . ") ) "; |
6a488035 TO |
4229 | |
4230 | //add contacts from saved searches | |
72a2eeab | 4231 | $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", $groupJoinTable, $op, $groupJoinColumn); |
6a488035 TO |
4232 | |
4233 | //set the group where clause | |
4234 | if ($ssWhere) { | |
4235 | $groupWhere[] = "( " . $ssWhere . " )"; | |
4236 | } | |
4237 | $this->_where[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )"; | |
4238 | ||
4239 | //Get the names of the target groups for the qill | |
085e6226 | 4240 | $groupNames = CRM_Core_PseudoConstant::group(); |
be2fb01f | 4241 | $qillNames = []; |
6a488035 TO |
4242 | foreach ($targetGroup[2] as $groupId) { |
4243 | if (array_key_exists($groupId, $groupNames)) { | |
4244 | $qillNames[] = $groupNames[$groupId]; | |
4245 | } | |
4246 | } | |
5c7dad85 | 4247 | if (!empty($relationType)) { |
026242fc | 4248 | $relQill = ''; |
5ad36be5 | 4249 | foreach ($relTypes as $rel) { |
026242fc SL |
4250 | if (!empty($relQill)) { |
4251 | $relQill .= ' OR '; | |
4252 | } | |
72a2eeab | 4253 | $relQill .= CRM_Utils_Array::value($rel, $allRelationshipType); |
026242fc SL |
4254 | } |
4255 | $this->_qill[$grouping][] = 'Relationship Type(s) ' . $relQill . " ( " . implode(", ", $qillNames) . " )"; | |
5c7dad85 TO |
4256 | } |
4257 | else { | |
2b0c31bc DG |
4258 | $this->_qill[$grouping][] = implode(", ", $qillNames); |
4259 | } | |
6a488035 TO |
4260 | } |
4261 | ||
6e83b317 CW |
4262 | // Description |
4263 | if (!empty($description[2]) && trim($description[2])) { | |
4264 | $this->_qill[$grouping][] = ts('Relationship description - ' . $description[2]); | |
4265 | $description = CRM_Core_DAO::escapeString(trim($description[2])); | |
4266 | $where[$grouping][] = "civicrm_relationship.description LIKE '%{$description}%'"; | |
4267 | } | |
4268 | ||
12ba37a0 CW |
4269 | // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way |
4270 | $today = date('Ymd'); | |
6a488035 | 4271 | //check for active, inactive and all relation status |
32c3b33f | 4272 | if (empty($relStatus[2])) { |
7c96365b | 4273 | $where[$grouping][] = "( |
6a488035 | 4274 | civicrm_relationship.is_active = 1 AND |
12ba37a0 CW |
4275 | ( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND |
4276 | ( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} ) | |
6a488035 TO |
4277 | )"; |
4278 | $this->_qill[$grouping][] = ts('Relationship - Active and Current'); | |
4279 | } | |
4280 | elseif ($relStatus[2] == 1) { | |
7c96365b | 4281 | $where[$grouping][] = "( |
6a488035 | 4282 | civicrm_relationship.is_active = 0 OR |
12ba37a0 CW |
4283 | civicrm_relationship.end_date < {$today} OR |
4284 | civicrm_relationship.start_date > {$today} | |
6a488035 TO |
4285 | )"; |
4286 | $this->_qill[$grouping][] = ts('Relationship - Inactive or not Current'); | |
4287 | } | |
4288 | ||
d64e31ab | 4289 | $onlyDeleted = 0; |
be2fb01f | 4290 | if (in_array(['deleted_contacts', '=', '1', '0', '0'], $this->_params)) { |
d64e31ab | 4291 | $onlyDeleted = 1; |
4292 | } | |
4293 | $where[$grouping][] = "(contact_b.is_deleted = {$onlyDeleted})"; | |
4294 | ||
f871c3a9 | 4295 | $this->addRelationshipPermissionClauses($grouping, $where); |
7c96365b | 4296 | $this->addRelationshipDateClauses($grouping, $where); |
eea5db81 | 4297 | $this->addRelationshipActivePeriodClauses($grouping, $where); |
026242fc | 4298 | if (!empty($relTypes)) { |
5ad36be5 | 4299 | $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypesIds) . ')'; |
6a488035 | 4300 | } |
6a488035 TO |
4301 | $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1; |
4302 | $this->_useDistinct = TRUE; | |
ad682134 | 4303 | $this->_relationshipValuesAdded = TRUE; |
7c96365b | 4304 | // it could be a or b, using an OR creates an unindexed join - better to create a temp table & |
4305 | // join on that, | |
2b0c31bc DG |
4306 | if ($relationshipTempTable) { |
4307 | $whereClause = ''; | |
5c7dad85 | 4308 | if (!empty($where[$grouping])) { |
2b0c31bc | 4309 | $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]); |
792750a5 | 4310 | $whereClause = str_replace('contact_b', 'c', $whereClause); |
2b0c31bc | 4311 | } |
7c96365b | 4312 | $sql = " |
804261ab | 4313 | INSERT INTO {$relationshipTempTable} (contact_id, contact_id_alt, id) |
72a2eeab | 4314 | (SELECT contact_id_b as contact_id, contact_id_a as contact_id_alt, civicrm_relationship.id |
7c96365b | 4315 | FROM civicrm_relationship |
4316 | INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id | |
4317 | $whereClause ) | |
4318 | UNION | |
72a2eeab | 4319 | (SELECT contact_id_a as contact_id, contact_id_b as contact_id_alt, civicrm_relationship.id |
7c96365b | 4320 | FROM civicrm_relationship |
4321 | INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_b = c.id | |
4322 | $whereClause ) | |
4323 | "; | |
4324 | CRM_Core_DAO::executeQuery($sql); | |
4325 | } | |
6a488035 | 4326 | } |
5c7dad85 | 4327 | |
9c9908e9 | 4328 | /** |
4329 | * Add relationship permission criteria to where clause. | |
4330 | * | |
4331 | * @param string $grouping | |
4332 | * @param array $where Array to add "where" criteria to, in case you are generating a temp table. | |
4333 | * Not the main query. | |
4334 | */ | |
f871c3a9 AS |
4335 | public function addRelationshipPermissionClauses($grouping, &$where) { |
4336 | $relPermission = $this->getWhereValues('relation_permission', $grouping); | |
4337 | if ($relPermission) { | |
f85f95ba NM |
4338 | if (!is_array($relPermission[2])) { |
4339 | // this form value was scalar in previous versions of Civi | |
be2fb01f | 4340 | $relPermission[2] = [$relPermission[2]]; |
f85f95ba | 4341 | } |
f871c3a9 AS |
4342 | $where[$grouping][] = "(civicrm_relationship.is_permission_a_b IN (" . implode(",", $relPermission[2]) . "))"; |
4343 | ||
4344 | $allRelationshipPermissions = CRM_Contact_BAO_Relationship::buildOptions('is_permission_a_b'); | |
9c9908e9 | 4345 | |
4346 | $relPermNames = array_intersect_key($allRelationshipPermissions, array_flip($relPermission[2])); | |
4347 | $this->_qill[$grouping][] = ts('Permissioned Relationships') . ' - ' . implode(' OR ', $relPermNames); | |
f871c3a9 AS |
4348 | } |
4349 | } | |
4350 | ||
5c7dad85 TO |
4351 | /** |
4352 | * Add start & end date criteria in | |
4353 | * @param string $grouping | |
4354 | * @param array $where | |
4355 | * = array to add where clauses to, in case you are generating a temp table. | |
4356 | * not the main query. | |
4357 | */ | |
4358 | public function addRelationshipDateClauses($grouping, &$where) { | |
41b8dd1d | 4359 | foreach (['start_date', 'end_date'] as $dateField) { |
4360 | $dateValueLow = $this->getWhereValues('relationship_' . $dateField . '_low', $grouping); | |
4361 | $dateValueHigh = $this->getWhereValues('relationship_' . $dateField . '_high', $grouping); | |
5c7dad85 | 4362 | if (!empty($dateValueLow)) { |
ad682134 | 4363 | $date = date('Ymd', strtotime($dateValueLow[2])); |
7c96365b | 4364 | $where[$grouping][] = "civicrm_relationship.$dateField >= $date"; |
c8e625fc | 4365 | $this->_qill[$grouping][] = ($dateField == 'end_date' ? ts('Relationship Ended on or After') : ts('Relationship Recorded Start Date On or After')) . " " . CRM_Utils_Date::customFormat($date); |
ad682134 | 4366 | } |
5c7dad85 | 4367 | if (!empty($dateValueHigh)) { |
ad682134 | 4368 | $date = date('Ymd', strtotime($dateValueHigh[2])); |
7c96365b | 4369 | $where[$grouping][] = "civicrm_relationship.$dateField <= $date"; |
c8e625fc | 4370 | $this->_qill[$grouping][] = ($dateField == 'end_date' ? ts('Relationship Ended on or Before') : ts('Relationship Recorded Start Date On or Before')) . " " . CRM_Utils_Date::customFormat($date); |
ad682134 | 4371 | } |
4372 | } | |
4373 | } | |
77b97be7 | 4374 | |
eea5db81 | 4375 | /** |
4376 | * Add start & end active period criteria in | |
4377 | * @param string $grouping | |
4378 | * @param array $where | |
4379 | * = array to add where clauses to, in case you are generating a temp table. | |
4380 | * not the main query. | |
4381 | */ | |
4382 | public function addRelationshipActivePeriodClauses($grouping, &$where) { | |
be2fb01f | 4383 | $dateValues = []; |
eea5db81 | 4384 | $dateField = 'active_period_date'; |
4385 | ||
4386 | $dateValueLow = $this->getWhereValues('relation_active_period_date_low', $grouping); | |
4387 | $dateValueHigh = $this->getWhereValues('relation_active_period_date_high', $grouping); | |
02633eb0 | 4388 | $dateValueLowFormated = $dateValueHighFormated = NULL; |
eea5db81 | 4389 | if (!empty($dateValueLow) && !empty($dateValueHigh)) { |
4390 | $dateValueLowFormated = date('Ymd', strtotime($dateValueLow[2])); | |
4391 | $dateValueHighFormated = date('Ymd', strtotime($dateValueHigh[2])); | |
eea5db81 | 4392 | $this->_qill[$grouping][] = (ts('Relationship was active between')) . " " . CRM_Utils_Date::customFormat($dateValueLowFormated) . " and " . CRM_Utils_Date::customFormat($dateValueHighFormated); |
4393 | } | |
4394 | elseif (!empty($dateValueLow)) { | |
4395 | $dateValueLowFormated = date('Ymd', strtotime($dateValueLow[2])); | |
eea5db81 | 4396 | $this->_qill[$grouping][] = (ts('Relationship was active after')) . " " . CRM_Utils_Date::customFormat($dateValueLowFormated); |
4397 | } | |
4398 | elseif (!empty($dateValueHigh)) { | |
4399 | $dateValueHighFormated = date('Ymd', strtotime($dateValueHigh[2])); | |
eea5db81 | 4400 | $this->_qill[$grouping][] = (ts('Relationship was active before')) . " " . CRM_Utils_Date::customFormat($dateValueHighFormated); |
4401 | } | |
02633eb0 FBB |
4402 | |
4403 | if ($activePeriodClauses = self::getRelationshipActivePeriodClauses($dateValueLowFormated, $dateValueHighFormated, TRUE)) { | |
4404 | $where[$grouping][] = $activePeriodClauses; | |
4405 | } | |
eea5db81 | 4406 | } |
4407 | ||
4408 | /** | |
4409 | * Get start & end active period criteria | |
b832662c | 4410 | * |
4411 | * @param $from | |
4412 | * @param $to | |
4413 | * @param $forceTableName | |
4414 | * | |
4415 | * @return string | |
eea5db81 | 4416 | */ |
4417 | public static function getRelationshipActivePeriodClauses($from, $to, $forceTableName) { | |
4418 | $tableName = $forceTableName ? 'civicrm_relationship.' : ''; | |
4419 | if (!is_null($from) && !is_null($to)) { | |
4420 | return '(((' . $tableName . 'start_date >= ' . $from . ' AND ' . $tableName . 'start_date <= ' . $to . ') OR | |
4421 | (' . $tableName . 'end_date >= ' . $from . ' AND ' . $tableName . 'end_date <= ' . $to . ') OR | |
4422 | (' . $tableName . 'start_date <= ' . $from . ' AND ' . $tableName . 'end_date >= ' . $to . ' )) OR | |
4423 | (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR | |
4424 | (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date >= ' . $from . ') OR | |
4425 | (' . $tableName . 'end_date IS NULL AND ' . $tableName . 'start_date <= ' . $to . '))'; | |
4426 | } | |
4427 | elseif (!is_null($from)) { | |
4428 | return '((' . $tableName . 'start_date >= ' . $from . ') OR | |
4429 | (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR | |
4430 | (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date >= ' . $from . '))'; | |
4431 | } | |
4432 | elseif (!is_null($to)) { | |
4433 | return '((' . $tableName . 'start_date <= ' . $to . ') OR | |
4434 | (' . $tableName . 'start_date IS NULL AND ' . $tableName . 'end_date IS NULL) OR | |
4435 | (' . $tableName . 'end_date IS NULL AND ' . $tableName . 'start_date <= ' . $to . '))'; | |
4436 | } | |
4437 | } | |
4438 | ||
6a488035 | 4439 | /** |
fe482240 | 4440 | * Default set of return properties. |
6a488035 | 4441 | * |
77b97be7 EM |
4442 | * @param int $mode |
4443 | * | |
a6c01b45 CW |
4444 | * @return array |
4445 | * derault return properties | |
6a488035 | 4446 | */ |
00be9182 | 4447 | public static function &defaultReturnProperties($mode = 1) { |
6a488035 | 4448 | if (!isset(self::$_defaultReturnProperties)) { |
be2fb01f | 4449 | self::$_defaultReturnProperties = []; |
6a488035 TO |
4450 | } |
4451 | ||
4452 | if (!isset(self::$_defaultReturnProperties[$mode])) { | |
4453 | // add activity return properties | |
4454 | if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) { | |
4455 | self::$_defaultReturnProperties[$mode] = CRM_Activity_BAO_Query::defaultReturnProperties($mode, FALSE); | |
4456 | } | |
4457 | else { | |
4458 | self::$_defaultReturnProperties[$mode] = CRM_Core_Component::defaultReturnProperties($mode, FALSE); | |
4459 | } | |
4460 | ||
4461 | if (empty(self::$_defaultReturnProperties[$mode])) { | |
be2fb01f | 4462 | self::$_defaultReturnProperties[$mode] = [ |
6a488035 TO |
4463 | 'image_URL' => 1, |
4464 | 'legal_identifier' => 1, | |
4465 | 'external_identifier' => 1, | |
4466 | 'contact_type' => 1, | |
4467 | 'contact_sub_type' => 1, | |
4468 | 'sort_name' => 1, | |
4469 | 'display_name' => 1, | |
4470 | 'preferred_mail_format' => 1, | |
4471 | 'nick_name' => 1, | |
4472 | 'first_name' => 1, | |
4473 | 'middle_name' => 1, | |
4474 | 'last_name' => 1, | |
04ffef8d CW |
4475 | 'prefix_id' => 1, |
4476 | 'suffix_id' => 1, | |
e171748b | 4477 | 'formal_title' => 1, |
aa62b355 | 4478 | 'communication_style_id' => 1, |
6a488035 | 4479 | 'birth_date' => 1, |
04ffef8d | 4480 | 'gender_id' => 1, |
6a488035 TO |
4481 | 'street_address' => 1, |
4482 | 'supplemental_address_1' => 1, | |
4483 | 'supplemental_address_2' => 1, | |
207f62c6 | 4484 | 'supplemental_address_3' => 1, |
6a488035 TO |
4485 | 'city' => 1, |
4486 | 'postal_code' => 1, | |
4487 | 'postal_code_suffix' => 1, | |
4488 | 'state_province' => 1, | |
4489 | 'country' => 1, | |
4490 | 'world_region' => 1, | |
4491 | 'geo_code_1' => 1, | |
4492 | 'geo_code_2' => 1, | |
4493 | 'email' => 1, | |
4494 | 'on_hold' => 1, | |
4495 | 'phone' => 1, | |
4496 | 'im' => 1, | |
4497 | 'household_name' => 1, | |
4498 | 'organization_name' => 1, | |
4499 | 'deceased_date' => 1, | |
4500 | 'is_deceased' => 1, | |
4501 | 'job_title' => 1, | |
4502 | 'legal_name' => 1, | |
4503 | 'sic_code' => 1, | |
4504 | 'current_employer' => 1, | |
4505 | // FIXME: should we use defaultHierReturnProperties() for the below? | |
4506 | 'do_not_email' => 1, | |
4507 | 'do_not_mail' => 1, | |
4508 | 'do_not_sms' => 1, | |
4509 | 'do_not_phone' => 1, | |
4510 | 'do_not_trade' => 1, | |
4511 | 'is_opt_out' => 1, | |
4512 | 'contact_is_deleted' => 1, | |
b725488f CW |
4513 | 'preferred_communication_method' => 1, |
4514 | 'preferred_language' => 1, | |
be2fb01f | 4515 | ]; |
6a488035 TO |
4516 | } |
4517 | } | |
4518 | return self::$_defaultReturnProperties[$mode]; | |
4519 | } | |
4520 | ||
4521 | /** | |
fe482240 | 4522 | * Get primary condition for a sql clause. |
6a488035 TO |
4523 | * |
4524 | * @param int $value | |
4525 | * | |
fffe9ee1 | 4526 | * @return string|NULL |
6a488035 | 4527 | */ |
00be9182 | 4528 | public static function getPrimaryCondition($value) { |
6a488035 TO |
4529 | if (is_numeric($value)) { |
4530 | $value = (int ) $value; | |
4531 | return ($value == 1) ? 'is_primary = 1' : 'is_primary = 0'; | |
4532 | } | |
4533 | return NULL; | |
4534 | } | |
4535 | ||
4536 | /** | |
fe482240 | 4537 | * Wrapper for a simple search query. |
6a488035 TO |
4538 | * |
4539 | * @param array $params | |
4540 | * @param array $returnProperties | |
4e6072d5 | 4541 | * @param bool $count |
6a488035 | 4542 | * |
4e6072d5 | 4543 | * @return string |
b832662c | 4544 | * @throws \CRM_Core_Exception |
6a488035 | 4545 | */ |
00be9182 | 4546 | public static function getQuery($params = NULL, $returnProperties = NULL, $count = FALSE) { |
6a488035 TO |
4547 | $query = new CRM_Contact_BAO_Query($params, $returnProperties); |
4548 | list($select, $from, $where, $having) = $query->query(); | |
3875e6b6 | 4549 | $groupBy = ($query->_useGroupBy) ? 'GROUP BY contact_a.id' : ''; |
6a488035 | 4550 | |
c9c5c58d AS |
4551 | $query = "$select $from $where $groupBy $having"; |
4552 | return $query; | |
6a488035 TO |
4553 | } |
4554 | ||
4555 | /** | |
4556 | * These are stub comments as this function needs more explanation - particularly in terms of how it | |
4557 | * relates to $this->searchQuery and why it replicates rather than calles $this->searchQuery. | |
4558 | * | |
4559 | * This function was originally written as a wrapper for the api query but is called from multiple places | |
4560 | * in the core code directly so the name is misleading. This function does not use the searchQuery function | |
4561 | * but it is unclear as to whehter that is historical or there is a reason | |
4562 | * CRM-11290 led to the permissioning action being extracted from searchQuery & shared with this function | |
4563 | * | |
837c4660 E |
4564 | * @param array $params |
4565 | * @param array $returnProperties | |
4566 | * @param null $fields | |
6a488035 | 4567 | * @param string $sort |
837c4660 E |
4568 | * @param int $offset |
4569 | * @param int $row_count | |
77c5b619 TO |
4570 | * @param bool $smartGroupCache |
4571 | * ?? update smart group cache?. | |
4572 | * @param bool $count | |
4573 | * Return count obnly. | |
4574 | * @param bool $skipPermissions | |
4575 | * Should permissions be ignored or should the logged in user's permissions be applied. | |
9ae25b56 | 4576 | * @param int $mode |
4577 | * This basically correlates to the component. | |
8f165fa5 | 4578 | * @param string $apiEntity |
4579 | * The api entity being called. | |
4580 | * This sort-of duplicates $mode in a confusing way. Probably not by design. | |
837c4660 | 4581 | * |
0606198b | 4582 | * @param bool|null $primaryLocationOnly |
b832662c | 4583 | * |
b725488f | 4584 | * @return array |
b832662c | 4585 | * @throws \CRM_Core_Exception |
6a488035 | 4586 | */ |
59f4c9ee | 4587 | public static function apiQuery( |
6a488035 TO |
4588 | $params = NULL, |
4589 | $returnProperties = NULL, | |
4590 | $fields = NULL, | |
4591 | $sort = NULL, | |
4592 | $offset = 0, | |
4593 | $row_count = 25, | |
4594 | $smartGroupCache = TRUE, | |
4595 | $count = FALSE, | |
9ae25b56 | 4596 | $skipPermissions = TRUE, |
3123273f | 4597 | $mode = CRM_Contact_BAO_Query::MODE_CONTACTS, |
0606198b | 4598 | $apiEntity = NULL, |
4599 | $primaryLocationOnly = NULL | |
6a488035 TO |
4600 | ) { |
4601 | ||
4602 | $query = new CRM_Contact_BAO_Query( | |
4603 | $params, $returnProperties, | |
9ae25b56 | 4604 | NULL, TRUE, FALSE, $mode, |
6a488035 | 4605 | $skipPermissions, |
66670e4d | 4606 | TRUE, $smartGroupCache, |
4607 | NULL, 'AND', | |
0606198b | 4608 | $apiEntity, $primaryLocationOnly |
6a488035 TO |
4609 | ); |
4610 | ||
5c7dad85 TO |
4611 | //this should add a check for view deleted if permissions are enabled |
4612 | if ($skipPermissions) { | |
4613 | $query->_skipDeleteClause = TRUE; | |
4614 | } | |
6a488035 | 4615 | $query->generatePermissionClause(FALSE, $count); |
d9ab802d PJ |
4616 | |
4617 | // note : this modifies _fromClause and _simpleFromClause | |
4618 | $query->includePseudoFieldsJoin($sort); | |
4619 | ||
6a488035 TO |
4620 | list($select, $from, $where, $having) = $query->query($count); |
4621 | ||
5c7dad85 | 4622 | if (!empty($query->_permissionWhereClause)) { |
21c6be28 SL |
4623 | if (!empty($query->_permissionFromClause) && !stripos($from, 'aclContactCache')) { |
4624 | $from .= " $query->_permissionFromClause"; | |
4625 | } | |
6a488035 TO |
4626 | if (empty($where)) { |
4627 | $where = "WHERE $query->_permissionWhereClause"; | |
4628 | } | |
4629 | else { | |
4630 | $where = "$where AND $query->_permissionWhereClause"; | |
4631 | } | |
4632 | } | |
4633 | ||
4634 | $sql = "$select $from $where $having"; | |
4635 | ||
783144b0 | 4636 | // add group by only when API action is not getcount |
4637 | // otherwise query fetches incorrect count | |
4638 | if ($query->_useGroupBy && !$count) { | |
e5cceea5 | 4639 | $sql .= self::getGroupByFromSelectColumns($query->_select, 'contact_a.id'); |
6a488035 TO |
4640 | } |
4641 | if (!empty($sort)) { | |
bd9a3dd5 | 4642 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
4643 | $sql .= " ORDER BY $sort "; |
4644 | } | |
4645 | if ($row_count > 0 && $offset >= 0) { | |
bf00d1b6 | 4646 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
9ae25b56 | 4647 | $row_count = CRM_Utils_Type::escape($row_count, 'Int'); |
6a488035 TO |
4648 | $sql .= " LIMIT $offset, $row_count "; |
4649 | } | |
4650 | ||
4651 | $dao = CRM_Core_DAO::executeQuery($sql); | |
4652 | ||
9ae25b56 | 4653 | // @todo derive this from the component class rather than hard-code two options. |
4654 | $entityIDField = ($mode == CRM_Contact_BAO_Query::MODE_CONTRIBUTE) ? 'contribution_id' : 'contact_id'; | |
4655 | ||
be2fb01f | 4656 | $values = []; |
6a488035 TO |
4657 | while ($dao->fetch()) { |
4658 | if ($count) { | |
4659 | $noRows = $dao->rowCount; | |
be2fb01f | 4660 | return [$noRows, NULL]; |
6a488035 | 4661 | } |
d9ab802d | 4662 | $val = $query->store($dao); |
86ab13b7 | 4663 | $convertedVals = $query->convertToPseudoNames($dao, TRUE, TRUE); |
d9ab802d PJ |
4664 | |
4665 | if (!empty($convertedVals)) { | |
e3571aca | 4666 | $val = array_replace_recursive($val, $convertedVals); |
d9ab802d | 4667 | } |
9ae25b56 | 4668 | $values[$dao->$entityIDField] = $val; |
6a488035 | 4669 | } |
6cba576f | 4670 | return [$values]; |
6a488035 TO |
4671 | } |
4672 | ||
bb05da0c | 4673 | /** |
4674 | * Get the actual custom field name by stripping off the appended string. | |
4675 | * | |
4676 | * The string could be _relative, _from, or _to | |
4677 | * | |
4678 | * @todo use metadata rather than convention to do this. | |
4679 | * | |
4680 | * @param string $parameterName | |
4681 | * The name of the parameter submitted to the form. | |
4682 | * e.g | |
4683 | * custom_3_relative | |
4684 | * custom_3_from | |
4685 | * | |
4686 | * @return string | |
4687 | */ | |
4688 | public static function getCustomFieldName($parameterName) { | |
4689 | if (substr($parameterName, -5, 5) == '_from') { | |
4690 | return substr($parameterName, 0, strpos($parameterName, '_from')); | |
4691 | } | |
4692 | if (substr($parameterName, -9, 9) == '_relative') { | |
4693 | return substr($parameterName, 0, strpos($parameterName, '_relative')); | |
4694 | } | |
4695 | if (substr($parameterName, -3, 3) == '_to') { | |
4696 | return substr($parameterName, 0, strpos($parameterName, '_to')); | |
4697 | } | |
4698 | } | |
4699 | ||
4700 | /** | |
4c2fe77b | 4701 | * Convert submitted values for relative custom fields to query object format. |
bb05da0c | 4702 | * |
4703 | * The query will support the sqlOperator format so convert to that format. | |
4704 | * | |
4705 | * @param array $formValues | |
4706 | * Submitted values. | |
4707 | * @param array $params | |
4708 | * Converted parameters for the query object. | |
4709 | * @param string $values | |
4710 | * Submitted value. | |
4711 | * @param string $fieldName | |
4712 | * Submitted field name. (Matches form field not DB field.) | |
4713 | */ | |
4c2fe77b | 4714 | protected static function convertCustomRelativeFields(&$formValues, &$params, $values, $fieldName) { |
bb05da0c | 4715 | if (empty($values)) { |
4716 | // e.g we might have relative set & from & to empty. The form flow is a bit funky & | |
4717 | // this function gets called again after they fields have been converted which can get ugly. | |
4718 | return; | |
4719 | } | |
4720 | $customFieldName = self::getCustomFieldName($fieldName); | |
4721 | ||
4722 | if (substr($fieldName, -9, 9) == '_relative') { | |
4723 | list($from, $to) = CRM_Utils_Date::getFromTo($values, NULL, NULL); | |
4724 | } | |
4725 | else { | |
f3acfdd9 | 4726 | if ($fieldName == $customFieldName . '_to' && !empty($formValues[$customFieldName . '_from'])) { |
bb05da0c | 4727 | // Both to & from are set. We only need to acton one, choosing from. |
4728 | return; | |
4729 | } | |
4730 | ||
9c1bc317 CW |
4731 | $from = $formValues[$customFieldName . '_from'] ?? NULL; |
4732 | $to = $formValues[$customFieldName . '_to'] ?? NULL; | |
4c2fe77b | 4733 | |
2fe91f9d | 4734 | if (self::isCustomDateField($customFieldName)) { |
4c2fe77b | 4735 | list($from, $to) = CRM_Utils_Date::getFromTo(NULL, $from, $to); |
4736 | } | |
bb05da0c | 4737 | } |
4738 | ||
4739 | if ($from) { | |
4740 | if ($to) { | |
be2fb01f | 4741 | $relativeFunction = ['BETWEEN' => [$from, $to]]; |
bb05da0c | 4742 | } |
4743 | else { | |
be2fb01f | 4744 | $relativeFunction = ['>=' => $from]; |
bb05da0c | 4745 | } |
4746 | } | |
4747 | else { | |
be2fb01f | 4748 | $relativeFunction = ['<=' => $to]; |
bb05da0c | 4749 | } |
be2fb01f | 4750 | $params[] = [ |
bb05da0c | 4751 | $customFieldName, |
4752 | '=', | |
4753 | $relativeFunction, | |
4754 | 0, | |
4755 | 0, | |
be2fb01f | 4756 | ]; |
bb05da0c | 4757 | } |
4758 | ||
2fe91f9d | 4759 | /** |
4760 | * Are we dealing with custom field of type date. | |
4761 | * | |
4762 | * @param $fieldName | |
4763 | * | |
4764 | * @return bool | |
7d3553e2 | 4765 | * @throws Exception |
2fe91f9d | 4766 | */ |
4767 | public static function isCustomDateField($fieldName) { | |
4768 | if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($fieldName)) == FALSE) { | |
4769 | return FALSE; | |
4770 | } | |
6d8d4c91 | 4771 | try { |
7d3553e2 VP |
4772 | $customFieldData = CRM_Core_BAO_CustomField::getFieldObject($customFieldID); |
4773 | $customFieldDataType = $customFieldData->data_type; | |
6d8d4c91 SL |
4774 | if ('Date' == $customFieldDataType) { |
4775 | return TRUE; | |
4776 | } | |
4777 | } | |
7d3553e2 | 4778 | catch (Exception $e) { |
2fe91f9d | 4779 | } |
4780 | return FALSE; | |
4781 | } | |
4782 | ||
df60621b | 4783 | /** |
4784 | * Has this field already been reformatting to Query object syntax. | |
4785 | * | |
4786 | * The form layer passed formValues to this function in preProcess & postProcess. Reason unknown. This seems | |
4787 | * to come with associated double queries & is possibly damaging performance. | |
4788 | * | |
4789 | * However, here we add a tested function to ensure convertFormValues identifies pre-processed fields & returns | |
4790 | * them as they are. | |
4791 | * | |
4792 | * @param mixed $values | |
4793 | * Value in formValues for the field. | |
4794 | * | |
4795 | * @return bool; | |
4796 | */ | |
06d67d53 | 4797 | public static function isAlreadyProcessedForQueryFormat($values) { |
df60621b | 4798 | if (!is_array($values)) { |
4799 | return FALSE; | |
4800 | } | |
4801 | if (($operator = CRM_Utils_Array::value(1, $values)) == FALSE) { | |
4802 | return FALSE; | |
4803 | } | |
4804 | return in_array($operator, CRM_Core_DAO::acceptedSQLOperators()); | |
4805 | } | |
4806 | ||
20306bb8 | 4807 | /** |
4808 | * If the state and country are passed remove state. | |
4809 | * | |
4810 | * Country is implicit from the state, but including both results in | |
4811 | * a poor query as there is no combined index on state AND country. | |
4812 | * | |
0e480632 | 4813 | * @see https://issues.civicrm.org/jira/browse/CRM-18125 |
20306bb8 | 4814 | * |
4815 | * @param array $formValues | |
4816 | */ | |
4817 | public static function filterCountryFromValuesIfStateExists(&$formValues) { | |
e4001cf0 | 4818 | if (!empty($formValues['country']) && !empty($formValues['state_province'])) { |
4819 | // The use of array map sanitises the data by ensuring we are dealing with integers. | |
4820 | $states = implode(', ', array_map('intval', $formValues['state_province'])); | |
4821 | $countryList = CRM_Core_DAO::singleValueQuery( | |
4822 | "SELECT GROUP_CONCAT(country_id) FROM civicrm_state_province WHERE id IN ($states)" | |
4823 | ); | |
4824 | if ($countryList == $formValues['country']) { | |
4825 | unset($formValues['country']); | |
20306bb8 | 4826 | } |
4827 | } | |
4828 | } | |
4829 | ||
36d2f4d5 | 4830 | /** |
4831 | * For some special cases, grouping by subset of select fields becomes mandatory. | |
4832 | * Hence, full_group_by mode is handled by appending any_value | |
4833 | * keyword to select fields not present in groupBy | |
4834 | * | |
4835 | * @param array $selectClauses | |
4836 | * @param array $groupBy - Columns already included in GROUP By clause. | |
0ad26017 | 4837 | * @param string $aggregateFunction |
36d2f4d5 | 4838 | * |
4839 | * @return string | |
4840 | */ | |
0ad26017 | 4841 | public static function appendAnyValueToSelect($selectClauses, $groupBy, $aggregateFunction = 'ANY_VALUE') { |
b07c960a | 4842 | if (!CRM_Utils_SQL::disableFullGroupByMode()) { |
36d2f4d5 | 4843 | $groupBy = array_map('trim', (array) $groupBy); |
f8a02ee1 | 4844 | $aggregateFunctions = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN|IF)[[:blank:]]*\(/i'; |
36d2f4d5 | 4845 | foreach ($selectClauses as $key => &$val) { |
4846 | list($selectColumn, $alias) = array_pad(preg_split('/ as /i', $val), 2, NULL); | |
4847 | // append ANY_VALUE() keyword | |
4848 | if (!in_array($selectColumn, $groupBy) && preg_match($aggregateFunctions, trim($selectColumn)) !== 1) { | |
0ad26017 | 4849 | $val = ($aggregateFunction == 'GROUP_CONCAT') ? |
4850 | str_replace($selectColumn, "$aggregateFunction(DISTINCT {$selectColumn})", $val) : | |
4851 | str_replace($selectColumn, "$aggregateFunction({$selectColumn})", $val); | |
36d2f4d5 | 4852 | } |
4853 | } | |
4854 | } | |
4855 | ||
4856 | return "SELECT " . implode(', ', $selectClauses) . " "; | |
4857 | } | |
4858 | ||
b07c960a | 4859 | /** |
4860 | * For some special cases, where if non-aggregate ORDER BY columns are not present in GROUP BY | |
4861 | * on full_group_by mode, then append the those missing columns to GROUP BY clause | |
4862 | * keyword to select fields not present in groupBy | |
4863 | * | |
e5167553 | 4864 | * @param string $groupBy - GROUP BY clause where missing ORDER BY columns will be appended if not present |
b07c960a | 4865 | * @param array $orderBys - ORDER BY sub-clauses |
4866 | * | |
4867 | */ | |
4868 | public static function getGroupByFromOrderBy(&$groupBy, $orderBys) { | |
0ad26017 | 4869 | if (!CRM_Utils_SQL::disableFullGroupByMode()) { |
b07c960a | 4870 | foreach ($orderBys as $orderBy) { |
69078420 SL |
4871 | // remove sort syntax from ORDER BY clauses if present |
4872 | $orderBy = str_ireplace([' DESC', ' ASC', '`'], '', $orderBy); | |
b07c960a | 4873 | // if ORDER BY column is not present in GROUP BY then append it to end |
e5167553 | 4874 | if (preg_match('/(MAX|MIN)\(/i', trim($orderBy)) !== 1 && !strstr($groupBy, $orderBy)) { |
4875 | $groupBy .= ", {$orderBy}"; | |
b07c960a | 4876 | } |
4877 | } | |
4878 | } | |
4879 | } | |
4880 | ||
e5cceea5 | 4881 | /** |
960ba386 | 4882 | * Include select columns in groupBy clause. |
e5cceea5 | 4883 | * |
dc852c7b | 4884 | * @param array $selectClauses |
960ba386 | 4885 | * @param array|string|null $groupBy - Columns already included in GROUP By clause. |
e5cceea5 | 4886 | * |
4887 | * @return string | |
4888 | */ | |
4889 | public static function getGroupByFromSelectColumns($selectClauses, $groupBy = NULL) { | |
b708c08d | 4890 | $groupBy = (array) $groupBy; |
bad98dd5 | 4891 | $sqlMode = CRM_Core_DAO::singleValueQuery('SELECT @@sql_mode'); |
b708c08d | 4892 | |
4893 | //return if ONLY_FULL_GROUP_BY is not enabled. | |
712e729f | 4894 | if (CRM_Utils_SQL::supportsFullGroupBy() && !empty($sqlMode) && in_array('ONLY_FULL_GROUP_BY', explode(',', $sqlMode))) { |
f8a02ee1 | 4895 | $regexToExclude = '/(ROUND|AVG|COUNT|GROUP_CONCAT|SUM|MAX|MIN|IF)[[:blank:]]*\(/i'; |
dc852c7b | 4896 | foreach ($selectClauses as $key => $val) { |
4897 | $aliasArray = preg_split('/ as /i', $val); | |
4898 | // if more than 1 alias we need to split by ','. | |
4899 | if (count($aliasArray) > 2) { | |
4900 | $aliasArray = preg_split('/,/', $val); | |
4901 | foreach ($aliasArray as $key => $value) { | |
4902 | $alias = current(preg_split('/ as /i', $value)); | |
4903 | if (!in_array($alias, $groupBy) && preg_match($regexToExclude, trim($alias)) !== 1) { | |
4904 | $groupBy[] = $alias; | |
4905 | } | |
e5cceea5 | 4906 | } |
4907 | } | |
dc852c7b | 4908 | else { |
4909 | list($selectColumn, $alias) = array_pad($aliasArray, 2, NULL); | |
4910 | $dateRegex = '/^(DATE_FORMAT|DATE_ADD|CASE)/i'; | |
4911 | $tableName = current(explode('.', $selectColumn)); | |
4912 | $primaryKey = "{$tableName}.id"; | |
4913 | // exclude columns which are already included in groupBy and aggregate functions from select | |
4914 | // CRM-18439 - Also exclude the columns which are functionally dependent on columns in $groupBy (MySQL 5.7+) | |
4915 | if (!in_array($selectColumn, $groupBy) && !in_array($primaryKey, $groupBy) && preg_match($regexToExclude, trim($selectColumn)) !== 1) { | |
4916 | if (!empty($alias) && preg_match($dateRegex, trim($selectColumn))) { | |
4917 | $groupBy[] = $alias; | |
4918 | } | |
4919 | else { | |
4920 | $groupBy[] = $selectColumn; | |
4921 | } | |
e5cceea5 | 4922 | } |
4923 | } | |
4924 | } | |
4925 | } | |
4926 | ||
b708c08d | 4927 | if (!empty($groupBy)) { |
4928 | return " GROUP BY " . implode(', ', $groupBy); | |
e5cceea5 | 4929 | } |
4930 | return ''; | |
4931 | } | |
4932 | ||
6a488035 | 4933 | /** |
fe482240 | 4934 | * Create and query the db for an contact search. |
6a488035 | 4935 | * |
77c5b619 TO |
4936 | * @param int $offset |
4937 | * The offset for the query. | |
4938 | * @param int $rowCount | |
4939 | * The number of rows to return. | |
db54ba06 | 4940 | * @param string|CRM_Utils_Sort $sort |
77c5b619 TO |
4941 | * The order by string. |
4942 | * @param bool $count | |
4943 | * Is this a count only query ?. | |
4944 | * @param bool $includeContactIds | |
4945 | * Should we include contact ids?. | |
4946 | * @param bool $sortByChar | |
4947 | * If true returns the distinct array of first characters for search results. | |
4948 | * @param bool $groupContacts | |
4949 | * If true, return only the contact ids. | |
4950 | * @param bool $returnQuery | |
4951 | * Should we return the query as a string. | |
4952 | * @param string $additionalWhereClause | |
4953 | * If the caller wants to further restrict the search (used for components). | |
77b97be7 | 4954 | * @param null $sortOrder |
77c5b619 TO |
4955 | * @param string $additionalFromClause |
4956 | * Should be clause with proper joins, effective to reduce where clause load. | |
77b97be7 EM |
4957 | * |
4958 | * @param bool $skipOrderAndLimit | |
6a488035 | 4959 | * |
e0f5b841 | 4960 | * @return string|null|CRM_Core_DAO |
6a488035 | 4961 | */ |
59f4c9ee | 4962 | public function searchQuery( |
6a488035 TO |
4963 | $offset = 0, $rowCount = 0, $sort = NULL, |
4964 | $count = FALSE, $includeContactIds = FALSE, | |
4965 | $sortByChar = FALSE, $groupContacts = FALSE, | |
4966 | $returnQuery = FALSE, | |
4967 | $additionalWhereClause = NULL, $sortOrder = NULL, | |
4968 | $additionalFromClause = NULL, $skipOrderAndLimit = FALSE | |
4969 | ) { | |
4970 | ||
89d7bb76 | 4971 | $query = $this->getSearchSQL($offset, $rowCount, $sort, $count, $includeContactIds, $sortByChar, $groupContacts, $additionalWhereClause, $sortOrder, $additionalFromClause, $skipOrderAndLimit); |
6a488035 TO |
4972 | |
4973 | if ($returnQuery) { | |
4974 | return $query; | |
4975 | } | |
6a488035 TO |
4976 | if ($count) { |
4977 | return CRM_Core_DAO::singleValueQuery($query); | |
4978 | } | |
4979 | ||
6a488035 | 4980 | $dao = CRM_Core_DAO::executeQuery($query); |
d567ed10 | 4981 | |
89d7bb76 | 4982 | // We can always call this - it will only re-enable if it was originally enabled. |
4983 | CRM_Core_DAO::reenableFullGroupByMode(); | |
d567ed10 | 4984 | |
6a488035 | 4985 | if ($groupContacts) { |
be2fb01f | 4986 | $ids = []; |
6a488035 TO |
4987 | while ($dao->fetch()) { |
4988 | $ids[] = $dao->id; | |
4989 | } | |
4990 | return implode(',', $ids); | |
4991 | } | |
4992 | ||
4993 | return $dao; | |
4994 | } | |
4995 | ||
52cda5dc | 4996 | /** |
caefed7d | 4997 | * Create and query the db for the list of all first letters used by contacts |
52cda5dc MWMC |
4998 | * |
4999 | * @return CRM_Core_DAO | |
5000 | */ | |
5001 | public function alphabetQuery() { | |
caefed7d MWMC |
5002 | $sqlParts = $this->getSearchSQLParts(NULL, NULL, NULL, FALSE, FALSE, TRUE); |
5003 | $query = "SELECT DISTINCT LEFT(contact_a.sort_name, 1) as sort_name | |
def88c52 | 5004 | {$sqlParts['from']} |
9790d61c | 5005 | {$sqlParts['where']}"; |
52cda5dc | 5006 | $dao = CRM_Core_DAO::executeQuery($query); |
52cda5dc MWMC |
5007 | return $dao; |
5008 | } | |
5009 | ||
4243847f | 5010 | /** |
2ca46d4d | 5011 | * Fetch a list of contacts for displaying a search results page |
4243847f | 5012 | * |
2ca46d4d TO |
5013 | * @param array $cids |
5014 | * List of contact IDs | |
77c5b619 | 5015 | * @param bool $includeContactIds |
4243847f CW |
5016 | * @return CRM_Core_DAO |
5017 | */ | |
2ca46d4d | 5018 | public function getCachedContacts($cids, $includeContactIds) { |
00a03110 | 5019 | CRM_Core_DAO::disableFullGroupByMode(); |
2ca46d4d | 5020 | CRM_Utils_Type::validateAll($cids, 'Positive'); |
4243847f | 5021 | $this->_includeContactIds = $includeContactIds; |
be2fb01f | 5022 | $onlyDeleted = in_array(['deleted_contacts', '=', '1', '0', '0'], $this->_params); |
f6364403 | 5023 | list($select, $from, $where) = $this->query(FALSE, FALSE, FALSE, $onlyDeleted); |
2ca46d4d TO |
5024 | $select .= sprintf(", (%s) AS _wgt", $this->createSqlCase('contact_a.id', $cids)); |
5025 | $where .= sprintf(' AND contact_a.id IN (%s)', implode(',', $cids)); | |
5026 | $order = 'ORDER BY _wgt'; | |
1d8fcdc5 | 5027 | $groupBy = $this->_useGroupBy ? ' GROUP BY contact_a.id' : ''; |
2ca46d4d | 5028 | $limit = ''; |
9b5914f2 | 5029 | $query = "$select $from $where $groupBy $order $limit"; |
23379b49 | 5030 | |
00a03110 | 5031 | $result = CRM_Core_DAO::executeQuery($query); |
5032 | CRM_Core_DAO::reenableFullGroupByMode(); | |
5033 | return $result; | |
4243847f CW |
5034 | } |
5035 | ||
2ca46d4d TO |
5036 | /** |
5037 | * Construct a SQL CASE expression. | |
5038 | * | |
5039 | * @param string $idCol | |
5040 | * The name of a column with ID's (eg 'contact_a.id'). | |
5041 | * @param array $cids | |
5042 | * Array(int $weight => int $id). | |
5043 | * @return string | |
5044 | * CASE WHEN id=123 THEN 1 WHEN id=456 THEN 2 END | |
5045 | */ | |
5046 | private function createSqlCase($idCol, $cids) { | |
5047 | $buf = "CASE\n"; | |
5048 | foreach ($cids as $weight => $cid) { | |
5049 | $buf .= " WHEN $idCol = $cid THEN $weight \n"; | |
5050 | } | |
5051 | $buf .= "END\n"; | |
5052 | return $buf; | |
5053 | } | |
5054 | ||
6a488035 TO |
5055 | /** |
5056 | * Populate $this->_permissionWhereClause with permission related clause and update other | |
5057 | * query related properties. | |
5058 | * | |
5059 | * Function calls ACL permission class and hooks to filter the query appropriately | |
5060 | * | |
5061 | * Note that these 2 params were in the code when extracted from another function | |
5062 | * and a second round extraction would be to make them properties of the class | |
5063 | * | |
77c5b619 TO |
5064 | * @param bool $onlyDeleted |
5065 | * Only get deleted contacts. | |
5066 | * @param bool $count | |
5067 | * Return Count only. | |
6a488035 | 5068 | */ |
00be9182 | 5069 | public function generatePermissionClause($onlyDeleted = FALSE, $count = FALSE) { |
6a488035 | 5070 | if (!$this->_skipPermission) { |
21c6be28 SL |
5071 | $permissionClauses = CRM_Contact_BAO_Contact_Permission::cacheClause(); |
5072 | $this->_permissionWhereClause = $permissionClauses[1]; | |
5073 | $this->_permissionFromClause = $permissionClauses[0]; | |
6a488035 | 5074 | |
21b8bcb5 SL |
5075 | if (CRM_Core_Permission::check('access deleted contacts')) { |
5076 | if (!$onlyDeleted) { | |
99827266 | 5077 | $this->_permissionWhereClause .= ' AND (contact_a.is_deleted = 0)'; |
21b8bcb5 SL |
5078 | } |
5079 | else { | |
99827266 | 5080 | $this->_permissionWhereClause .= " AND (contact_a.is_deleted) "; |
21b8bcb5 | 5081 | } |
54b8719b SL |
5082 | } |
5083 | ||
d1d108ee | 5084 | if (isset($this->_tables['civicrm_activity'])) { |
5085 | $bao = new CRM_Activity_BAO_Activity(); | |
5086 | $clauses = $subclauses = []; | |
5087 | foreach ((array) $bao->addSelectWhereClause() as $field => $vals) { | |
5088 | if ($vals && $field !== 'id') { | |
5089 | $clauses[] = $bao->tableName() . ".$field " . $vals; | |
5090 | } | |
5091 | elseif ($vals) { | |
5092 | $subclauses[] = "$field " . implode(" AND $field ", (array) $vals); | |
5093 | } | |
5094 | } | |
5095 | if ($subclauses) { | |
5096 | $clauses[] = $bao->tableName() . '.`id` IN (SELECT `id` FROM `' . $bao->tableName() . '` WHERE ' . implode(' AND ', $subclauses) . ')'; | |
5097 | } | |
5098 | if (!empty($clauses) && $this->_permissionWhereClause) { | |
5099 | $this->_permissionWhereClause .= ' AND (' . implode(' AND ', $clauses) . ')'; | |
5100 | } | |
5101 | elseif (!empty($clauses)) { | |
5102 | $this->_permissionWhereClause .= '(' . implode(' AND ', $clauses) . ')'; | |
5103 | } | |
5104 | } | |
6a488035 TO |
5105 | } |
5106 | else { | |
5107 | // add delete clause if needed even if we are skipping permission | |
5108 | // CRM-7639 | |
5109 | if (!$this->_skipDeleteClause) { | |
5110 | if (CRM_Core_Permission::check('access deleted contacts') and $onlyDeleted) { | |
5111 | $this->_permissionWhereClause = '(contact_a.is_deleted)'; | |
5112 | } | |
5113 | else { | |
5114 | // CRM-6181 | |
5115 | $this->_permissionWhereClause = '(contact_a.is_deleted = 0)'; | |
5116 | } | |
5117 | } | |
5118 | } | |
5119 | } | |
5120 | ||
86538308 EM |
5121 | /** |
5122 | * @param $val | |
5123 | */ | |
00be9182 | 5124 | public function setSkipPermission($val) { |
6a488035 TO |
5125 | $this->_skipPermission = $val; |
5126 | } | |
5127 | ||
86538308 EM |
5128 | /** |
5129 | * @param null $context | |
5130 | * | |
5131 | * @return array | |
b832662c | 5132 | * @throws \CRM_Core_Exception |
86538308 | 5133 | */ |
faba82fc | 5134 | public function summaryContribution($context = NULL) { |
d7803ec1 | 5135 | list($innerselect, $from, $where, $having) = $this->query(TRUE); |
21c6be28 SL |
5136 | if (!empty($this->_permissionFromClause) && !stripos($from, 'aclContactCache')) { |
5137 | $from .= " $this->_permissionFromClause"; | |
5138 | } | |
8ef6eb78 | 5139 | if ($this->_permissionWhereClause) { |
5140 | $where .= " AND " . $this->_permissionWhereClause; | |
5141 | } | |
5142 | if ($context == 'search') { | |
5143 | $where .= " AND contact_a.is_deleted = 0 "; | |
5144 | } | |
6a488035 | 5145 | |
8ef6eb78 | 5146 | $this->appendFinancialTypeWhereAndFromToQueryStrings($where, $from); |
dbaa9d7d | 5147 | |
d54658f6 | 5148 | $summary = ['total' => [], 'soft_credit' => ['count' => 0, 'avg' => 0, 'amount' => 0]]; |
8ef6eb78 | 5149 | $this->addBasicStatsToSummary($summary, $where, $from); |
d03881d3 | 5150 | |
6d68fd76 | 5151 | if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) { |
5152 | $this->addBasicSoftCreditStatsToStats($summary, $where, $from); | |
5153 | } | |
5154 | ||
386fce20 | 5155 | $this->addBasicCancelStatsToSummary($summary, $where, $from); |
6a488035 TO |
5156 | |
5157 | return $summary; | |
5158 | } | |
5159 | ||
5384a978 | 5160 | /** |
dbaa9d7d | 5161 | * Append financial ACL limits to the query from & where clauses, if applicable. |
5384a978 | 5162 | * |
dbaa9d7d | 5163 | * @param string $where |
5164 | * @param string $from | |
5384a978 | 5165 | */ |
dbaa9d7d | 5166 | public function appendFinancialTypeWhereAndFromToQueryStrings(&$where, &$from) { |
5167 | if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { | |
5168 | return; | |
5384a978 VR |
5169 | } |
5170 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); | |
5171 | if (!empty($financialTypes)) { | |
5172 | $where .= " AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ") AND li.id IS NULL"; | |
5173 | $from .= " LEFT JOIN civicrm_line_item li | |
5174 | ON civicrm_contribution.id = li.contribution_id AND | |
5175 | li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") "; | |
5176 | } | |
5177 | else { | |
5178 | $where .= " AND civicrm_contribution.financial_type_id IN (0)"; | |
5179 | } | |
5384a978 VR |
5180 | } |
5181 | ||
6a488035 | 5182 | /** |
fe482240 | 5183 | * Getter for the qill object. |
6a488035 | 5184 | * |
b832662c | 5185 | * @return array |
6a488035 | 5186 | */ |
00be9182 | 5187 | public function qill() { |
6a488035 TO |
5188 | return $this->_qill; |
5189 | } | |
5190 | ||
5191 | /** | |
fe482240 | 5192 | * Default set of return default hier return properties. |
6a488035 | 5193 | * |
fffe9ee1 | 5194 | * @return array |
6a488035 | 5195 | */ |
00be9182 | 5196 | public static function &defaultHierReturnProperties() { |
6a488035 | 5197 | if (!isset(self::$_defaultHierReturnProperties)) { |
be2fb01f | 5198 | self::$_defaultHierReturnProperties = [ |
6a488035 TO |
5199 | 'home_URL' => 1, |
5200 | 'image_URL' => 1, | |
5201 | 'legal_identifier' => 1, | |
5202 | 'external_identifier' => 1, | |
5203 | 'contact_type' => 1, | |
5204 | 'contact_sub_type' => 1, | |
5205 | 'sort_name' => 1, | |
5206 | 'display_name' => 1, | |
5207 | 'nick_name' => 1, | |
5208 | 'first_name' => 1, | |
5209 | 'middle_name' => 1, | |
5210 | 'last_name' => 1, | |
67744c4e CW |
5211 | 'prefix_id' => 1, |
5212 | 'suffix_id' => 1, | |
e171748b | 5213 | 'formal_title' => 1, |
aa62b355 | 5214 | 'communication_style_id' => 1, |
6a488035 TO |
5215 | 'email_greeting' => 1, |
5216 | 'postal_greeting' => 1, | |
5217 | 'addressee' => 1, | |
5218 | 'birth_date' => 1, | |
67744c4e | 5219 | 'gender_id' => 1, |
6a488035 TO |
5220 | 'preferred_communication_method' => 1, |
5221 | 'do_not_phone' => 1, | |
5222 | 'do_not_email' => 1, | |
5223 | 'do_not_mail' => 1, | |
5224 | 'do_not_sms' => 1, | |
5225 | 'do_not_trade' => 1, | |
be2fb01f CW |
5226 | 'location' => [ |
5227 | '1' => [ | |
59f4c9ee TO |
5228 | 'location_type' => 1, |
5229 | 'street_address' => 1, | |
5230 | 'city' => 1, | |
5231 | 'state_province' => 1, | |
5232 | 'postal_code' => 1, | |
5233 | 'postal_code_suffix' => 1, | |
5234 | 'country' => 1, | |
5235 | 'phone-Phone' => 1, | |
5236 | 'phone-Mobile' => 1, | |
5237 | 'phone-Fax' => 1, | |
5238 | 'phone-1' => 1, | |
5239 | 'phone-2' => 1, | |
5240 | 'phone-3' => 1, | |
5241 | 'im-1' => 1, | |
5242 | 'im-2' => 1, | |
5243 | 'im-3' => 1, | |
5244 | 'email-1' => 1, | |
5245 | 'email-2' => 1, | |
5246 | 'email-3' => 1, | |
be2fb01f CW |
5247 | ], |
5248 | '2' => [ | |
59f4c9ee TO |
5249 | 'location_type' => 1, |
5250 | 'street_address' => 1, | |
5251 | 'city' => 1, | |
5252 | 'state_province' => 1, | |
5253 | 'postal_code' => 1, | |
5254 | 'postal_code_suffix' => 1, | |
5255 | 'country' => 1, | |
5256 | 'phone-Phone' => 1, | |
5257 | 'phone-Mobile' => 1, | |
5258 | 'phone-1' => 1, | |
5259 | 'phone-2' => 1, | |
5260 | 'phone-3' => 1, | |
5261 | 'im-1' => 1, | |
5262 | 'im-2' => 1, | |
5263 | 'im-3' => 1, | |
5264 | 'email-1' => 1, | |
5265 | 'email-2' => 1, | |
5266 | 'email-3' => 1, | |
be2fb01f CW |
5267 | ], |
5268 | ], | |
5269 | ]; | |
6a488035 TO |
5270 | } |
5271 | return self::$_defaultHierReturnProperties; | |
5272 | } | |
5273 | ||
86538308 | 5274 | /** |
c037736a | 5275 | * Build query for a date field. |
5276 | * | |
5277 | * @param array $values | |
100fef9d CW |
5278 | * @param string $tableName |
5279 | * @param string $fieldName | |
5280 | * @param string $dbFieldName | |
c037736a | 5281 | * @param string $fieldTitle |
86538308 | 5282 | * @param bool $appendTimeStamp |
d51e2817 | 5283 | * @param string $dateFormat |
936ededf | 5284 | * @param string|null $highDBFieldName |
5285 | * Optional field name for when the 'high' part of the calculation uses a different field than the 'low' part. | |
5286 | * This is an obscure situation & one we don't want to do more of but supporting them here is the only way for now. | |
5287 | * Examples are event date & relationship active date -in both cases we are looking for things greater than the start | |
5288 | * date & less than the end date. | |
5289 | * | |
5290 | * @throws \CRM_Core_Exception | |
86538308 | 5291 | */ |
59f4c9ee | 5292 | public function dateQueryBuilder( |
33a17d7e | 5293 | $values, $tableName, $fieldName, |
6a488035 | 5294 | $dbFieldName, $fieldTitle, |
d51e2817 | 5295 | $appendTimeStamp = TRUE, |
936ededf | 5296 | $dateFormat = 'YmdHis', |
5297 | $highDBFieldName = NULL | |
6a488035 | 5298 | ) { |
5b5ea9b6 | 5299 | // @todo - remove dateFormat - pretty sure it's never passed in... |
6a488035 | 5300 | list($name, $op, $value, $grouping, $wildcard) = $values; |
56d1630d | 5301 | if ($name !== $fieldName && $name !== "{$fieldName}_low" && $name !== "{$fieldName}_high") { |
5302 | CRM_Core_Error::deprecatedFunctionWarning('Date query builder called unexpectedly'); | |
5303 | return; | |
5304 | } | |
80b5c9f1 | 5305 | if ($tableName === 'civicrm_contact') { |
5306 | // Special handling for contact table as it has a known alias in advanced search. | |
5307 | $tableName = 'contact_a'; | |
5308 | } | |
2206409b | 5309 | if ($name === "{$fieldName}_low" || |
5310 | $name === "{$fieldName}_high" | |
6a488035 | 5311 | ) { |
efb88612 | 5312 | if (isset($this->_rangeCache[$fieldName]) || !$value) { |
6a488035 TO |
5313 | return; |
5314 | } | |
5315 | $this->_rangeCache[$fieldName] = 1; | |
5316 | ||
5317 | $secondOP = $secondPhrase = $secondValue = $secondDate = $secondDateFormat = NULL; | |
5318 | ||
5319 | if ($name == $fieldName . '_low') { | |
5320 | $firstOP = '>='; | |
228917cd | 5321 | $firstPhrase = ts('greater than or equal to'); |
d51e2817 | 5322 | $firstDate = CRM_Utils_Date::processDate($value, NULL, FALSE, $dateFormat); |
6a488035 TO |
5323 | |
5324 | $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping); | |
228917cd | 5325 | if (!empty($secondValues) && $secondValues[2]) { |
6a488035 | 5326 | $secondOP = '<='; |
228917cd | 5327 | $secondPhrase = ts('less than or equal to'); |
6a488035 TO |
5328 | $secondValue = $secondValues[2]; |
5329 | ||
228917cd | 5330 | if ($appendTimeStamp && strlen($secondValue) == 10) { |
6a488035 TO |
5331 | $secondValue .= ' 23:59:59'; |
5332 | } | |
d51e2817 | 5333 | $secondDate = CRM_Utils_Date::processDate($secondValue, NULL, FALSE, $dateFormat); |
6a488035 TO |
5334 | } |
5335 | } | |
5336 | elseif ($name == $fieldName . '_high') { | |
5337 | $firstOP = '<='; | |
228917cd | 5338 | $firstPhrase = ts('less than or equal to'); |
6a488035 | 5339 | |
228917cd | 5340 | if ($appendTimeStamp && strlen($value) == 10) { |
6a488035 TO |
5341 | $value .= ' 23:59:59'; |
5342 | } | |
d51e2817 | 5343 | $firstDate = CRM_Utils_Date::processDate($value, NULL, FALSE, $dateFormat); |
6a488035 TO |
5344 | |
5345 | $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping); | |
228917cd | 5346 | if (!empty($secondValues) && $secondValues[2]) { |
6a488035 | 5347 | $secondOP = '>='; |
228917cd | 5348 | $secondPhrase = ts('greater than or equal to'); |
6a488035 | 5349 | $secondValue = $secondValues[2]; |
d51e2817 | 5350 | $secondDate = CRM_Utils_Date::processDate($secondValue, NULL, FALSE, $dateFormat); |
6a488035 TO |
5351 | } |
5352 | } | |
5353 | ||
5354 | if (!$appendTimeStamp) { | |
5355 | $firstDate = substr($firstDate, 0, 8); | |
5356 | } | |
5357 | $firstDateFormat = CRM_Utils_Date::customFormat($firstDate); | |
5358 | ||
5359 | if ($secondDate) { | |
5360 | if (!$appendTimeStamp) { | |
5361 | $secondDate = substr($secondDate, 0, 8); | |
5362 | } | |
5363 | $secondDateFormat = CRM_Utils_Date::customFormat($secondDate); | |
5364 | } | |
5365 | ||
6a488035 | 5366 | if ($secondDate) { |
936ededf | 5367 | $highDBFieldName = $highDBFieldName ?? $dbFieldName; |
6a488035 TO |
5368 | $this->_where[$grouping][] = " |
5369 | ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND | |
936ededf | 5370 | ( {$tableName}.{$highDBFieldName} $secondOP '$secondDate' ) |
6a488035 TO |
5371 | "; |
5372 | $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\" " . ts('AND') . " $secondPhrase \"$secondDateFormat\""; | |
5373 | } | |
5374 | else { | |
5375 | $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'"; | |
5376 | $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\""; | |
5377 | } | |
5378 | } | |
5379 | ||
5380 | if ($name == $fieldName) { | |
d820f4e8 | 5381 | //In Get API, for operators other then '=' the $value is in array(op => value) format |
5382 | if (is_array($value) && !empty($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { | |
5383 | $op = key($value); | |
5384 | $value = $value[$op]; | |
5385 | } | |
6a488035 | 5386 | |
efb88612 | 5387 | $date = $format = NULL; |
fbc6a4d4 | 5388 | if (strstr($op, 'IN')) { |
be2fb01f | 5389 | $format = []; |
ee657a1c | 5390 | foreach ($value as &$date) { |
d51e2817 | 5391 | $date = CRM_Utils_Date::processDate($date, NULL, FALSE, $dateFormat); |
ee657a1c | 5392 | if (!$appendTimeStamp) { |
5393 | $date = substr($date, 0, 8); | |
5394 | } | |
fbc6a4d4 | 5395 | $format[] = CRM_Utils_Date::customFormat($date); |
ee657a1c | 5396 | } |
5397 | $date = "('" . implode("','", $value) . "')"; | |
fbc6a4d4 | 5398 | $format = implode(', ', $format); |
ee657a1c | 5399 | } |
0a74debb | 5400 | elseif ($value && (!strstr($op, 'NULL') && !strstr($op, 'EMPTY'))) { |
d51e2817 | 5401 | $date = CRM_Utils_Date::processDate($value, NULL, FALSE, $dateFormat); |
ee657a1c | 5402 | if (!$appendTimeStamp) { |
5403 | $date = substr($date, 0, 8); | |
5404 | } | |
fbc6a4d4 | 5405 | $format = CRM_Utils_Date::customFormat($date); |
efb88612 | 5406 | $date = "'$date'"; |
6a488035 | 5407 | } |
6a488035 TO |
5408 | |
5409 | if ($date) { | |
f1339cf2 | 5410 | $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op $date"; |
6a488035 TO |
5411 | } |
5412 | else { | |
515293b2 | 5413 | $this->_where[$grouping][] = self::buildClause("{$tableName}.{$dbFieldName}", $op); |
6a488035 | 5414 | } |
efb88612 | 5415 | |
d820f4e8 | 5416 | $op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op); |
efb88612 | 5417 | $this->_qill[$grouping][] = "$fieldTitle $op $format"; |
6a488035 | 5418 | } |
56d1630d | 5419 | |
5420 | // Ensure the tables are set, but don't whomp anything. | |
5421 | $this->_tables[$tableName] = $this->_tables[$tableName] ?? 1; | |
5422 | $this->_whereTables[$tableName] = $this->_whereTables[$tableName] ?? 1; | |
6a488035 TO |
5423 | } |
5424 | ||
86538308 EM |
5425 | /** |
5426 | * @param $values | |
100fef9d CW |
5427 | * @param string $tableName |
5428 | * @param string $fieldName | |
5429 | * @param string $dbFieldName | |
86538308 EM |
5430 | * @param $fieldTitle |
5431 | * @param null $options | |
5432 | */ | |
59f4c9ee | 5433 | public function numberRangeBuilder( |
51ccfbbe TO |
5434 | &$values, |
5435 | $tableName, $fieldName, | |
5436 | $dbFieldName, $fieldTitle, | |
5437 | $options = NULL | |
6a488035 TO |
5438 | ) { |
5439 | list($name, $op, $value, $grouping, $wildcard) = $values; | |
5440 | ||
5441 | if ($name == "{$fieldName}_low" || | |
5442 | $name == "{$fieldName}_high" | |
5443 | ) { | |
5444 | if (isset($this->_rangeCache[$fieldName])) { | |
5445 | return; | |
5446 | } | |
5447 | $this->_rangeCache[$fieldName] = 1; | |
5448 | ||
5449 | $secondOP = $secondPhrase = $secondValue = NULL; | |
5450 | ||
5451 | if ($name == "{$fieldName}_low") { | |
5452 | $firstOP = '>='; | |
228917cd | 5453 | $firstPhrase = ts('greater than'); |
6a488035 TO |
5454 | |
5455 | $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping); | |
5456 | if (!empty($secondValues)) { | |
5457 | $secondOP = '<='; | |
228917cd | 5458 | $secondPhrase = ts('less than'); |
6a488035 TO |
5459 | $secondValue = $secondValues[2]; |
5460 | } | |
5461 | } | |
5462 | else { | |
5463 | $firstOP = '<='; | |
228917cd | 5464 | $firstPhrase = ts('less than'); |
6a488035 TO |
5465 | |
5466 | $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping); | |
5467 | if (!empty($secondValues)) { | |
5468 | $secondOP = '>='; | |
228917cd | 5469 | $secondPhrase = ts('greater than'); |
6a488035 TO |
5470 | $secondValue = $secondValues[2]; |
5471 | } | |
5472 | } | |
5473 | ||
5474 | if ($secondOP) { | |
5475 | $this->_where[$grouping][] = " | |
5476 | ( {$tableName}.{$dbFieldName} $firstOP {$value} ) AND | |
5477 | ( {$tableName}.{$dbFieldName} $secondOP {$secondValue} ) | |
5478 | "; | |
5479 | $displayValue = $options ? $options[$value] : $value; | |
5480 | $secondDisplayValue = $options ? $options[$secondValue] : $secondValue; | |
5481 | ||
59f4c9ee TO |
5482 | $this->_qill[$grouping][] |
5483 | = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\""; | |
6a488035 TO |
5484 | } |
5485 | else { | |
5486 | $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP {$value}"; | |
5487 | $displayValue = $options ? $options[$value] : $value; | |
5488 | $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\""; | |
5489 | } | |
5490 | $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1; | |
5491 | ||
5492 | return; | |
5493 | } | |
5494 | ||
5495 | if ($name == $fieldName) { | |
5496 | $op = '='; | |
5497 | $phrase = '='; | |
5498 | ||
5499 | $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op {$value}"; | |
5500 | ||
5501 | $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1; | |
5502 | $displayValue = $options ? $options[$value] : $value; | |
5503 | $this->_qill[$grouping][] = "$fieldTitle - $phrase \"$displayValue\""; | |
5504 | } | |
6a488035 TO |
5505 | } |
5506 | ||
c4a7c967 | 5507 | /** |
5508 | * @param $values | |
5509 | * @param string $tableName | |
5510 | * @param string $fieldName | |
5511 | * @param string $dbFieldName | |
5512 | * @param $fieldTitle | |
5513 | * @param null $options | |
5514 | */ | |
5515 | public function ageRangeQueryBuilder( | |
5516 | &$values, | |
5517 | $tableName, $fieldName, | |
5518 | $dbFieldName, $fieldTitle, | |
5519 | $options = NULL | |
5520 | ) { | |
5521 | list($name, $op, $value, $grouping, $wildcard) = $values; | |
5522 | ||
5523 | $asofDateValues = $this->getWhereValues("{$fieldName}_asof_date", $grouping); | |
69078420 SL |
5524 | // will be treated as current day |
5525 | $asofDate = NULL; | |
c4a7c967 | 5526 | if ($asofDateValues) { |
5527 | $asofDate = CRM_Utils_Date::processDate($asofDateValues[2]); | |
5528 | $asofDateFormat = CRM_Utils_Date::customFormat(substr($asofDate, 0, 8)); | |
5529 | $fieldTitle .= ' ' . ts('as of') . ' ' . $asofDateFormat; | |
5530 | } | |
5531 | ||
5532 | if ($name == "{$fieldName}_low" || | |
5533 | $name == "{$fieldName}_high" | |
5534 | ) { | |
5535 | if (isset($this->_rangeCache[$fieldName])) { | |
5536 | return; | |
5537 | } | |
5538 | $this->_rangeCache[$fieldName] = 1; | |
5539 | ||
5540 | $secondOP = $secondPhrase = $secondValue = NULL; | |
5541 | ||
5542 | if ($name == "{$fieldName}_low") { | |
5543 | $firstPhrase = ts('greater than or equal to'); | |
5544 | // NB: age > X means date of birth < Y | |
5545 | $firstOP = '<='; | |
5546 | $firstDate = self::calcDateFromAge($asofDate, $value, 'min'); | |
5547 | ||
5548 | $secondValues = $this->getWhereValues("{$fieldName}_high", $grouping); | |
5549 | if (!empty($secondValues)) { | |
5550 | $secondOP = '>='; | |
5551 | $secondPhrase = ts('less than or equal to'); | |
5552 | $secondValue = $secondValues[2]; | |
5553 | $secondDate = self::calcDateFromAge($asofDate, $secondValue, 'max'); | |
5554 | } | |
5555 | } | |
5556 | else { | |
5557 | $firstOP = '>='; | |
5558 | $firstPhrase = ts('less than or equal to'); | |
5559 | $firstDate = self::calcDateFromAge($asofDate, $value, 'max'); | |
5560 | ||
5561 | $secondValues = $this->getWhereValues("{$fieldName}_low", $grouping); | |
5562 | if (!empty($secondValues)) { | |
5563 | $secondOP = '<='; | |
5564 | $secondPhrase = ts('greater than or equal to'); | |
5565 | $secondValue = $secondValues[2]; | |
5566 | $secondDate = self::calcDateFromAge($asofDate, $secondValue, 'min'); | |
5567 | } | |
5568 | } | |
5569 | ||
c4a7c967 | 5570 | if ($secondOP) { |
5571 | $this->_where[$grouping][] = " | |
5572 | ( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND | |
5573 | ( {$tableName}.{$dbFieldName} $secondOP '$secondDate' ) | |
5574 | "; | |
5575 | $displayValue = $options ? $options[$value] : $value; | |
5576 | $secondDisplayValue = $options ? $options[$secondValue] : $secondValue; | |
5577 | ||
5578 | $this->_qill[$grouping][] | |
5579 | = "$fieldTitle - $firstPhrase \"$displayValue\" " . ts('AND') . " $secondPhrase \"$secondDisplayValue\""; | |
5580 | } | |
5581 | else { | |
5582 | $this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'"; | |
5583 | $displayValue = $options ? $options[$value] : $value; | |
5584 | $this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$displayValue\""; | |
5585 | } | |
5586 | $this->_tables[$tableName] = $this->_whereTables[$tableName] = 1; | |
5587 | return; | |
5588 | } | |
5589 | } | |
5590 | ||
70599df6 | 5591 | /** |
5592 | * Calculate date from age. | |
5593 | * | |
5594 | * @param string $asofDate | |
5595 | * @param int $age | |
5596 | * @param string $type | |
5597 | * | |
5598 | * @return string | |
b832662c | 5599 | * @throws \Exception |
70599df6 | 5600 | */ |
c4a7c967 | 5601 | public static function calcDateFromAge($asofDate, $age, $type) { |
5602 | $date = new DateTime($asofDate); | |
5603 | if ($type == "min") { | |
5604 | // minimum age is $age: dob <= date - age "235959" | |
5605 | $date->sub(new DateInterval("P" . $age . "Y")); | |
5606 | return $date->format('Ymd') . "235959"; | |
69c53a43 | 5607 | } |
5608 | else { | |
c4a7c967 | 5609 | // max age is $age: dob >= date - (age + 1y) + 1d "000000" |
5610 | $date->sub(new DateInterval("P" . ($age + 1) . "Y"))->add(new DateInterval("P1D")); | |
5611 | return $date->format('Ymd') . "000000"; | |
5612 | } | |
5613 | } | |
5614 | ||
6a488035 TO |
5615 | /** |
5616 | * Given the field name, operator, value & its data type | |
5617 | * builds the where Clause for the query | |
5618 | * used for handling 'IS NULL'/'IS NOT NULL' operators | |
5619 | * | |
77c5b619 TO |
5620 | * @param string $field |
5621 | * Fieldname. | |
5622 | * @param string $op | |
5623 | * Operator. | |
5624 | * @param string $value | |
5625 | * Value. | |
5626 | * @param string $dataType | |
5627 | * Data type of the field. | |
6a488035 | 5628 | * |
a6c01b45 | 5629 | * @return string |
59f4c9ee | 5630 | * Where clause for the query. |
b832662c | 5631 | * @throws \CRM_Core_Exception |
6a488035 | 5632 | */ |
00be9182 | 5633 | public static function buildClause($field, $op, $value = NULL, $dataType = NULL) { |
6a488035 TO |
5634 | $op = trim($op); |
5635 | $clause = "$field $op"; | |
5636 | ||
5637 | switch ($op) { | |
5638 | case 'IS NULL': | |
5639 | case 'IS NOT NULL': | |
5640 | return $clause; | |
5641 | ||
5642 | case 'IS EMPTY': | |
89d4a22f | 5643 | $clause = ($dataType == 'Date') ? " $field IS NULL " : " (NULLIF($field, '') IS NULL) "; |
6a488035 TO |
5644 | return $clause; |
5645 | ||
5646 | case 'IS NOT EMPTY': | |
89d4a22f | 5647 | $clause = ($dataType == 'Date') ? " $field IS NOT NULL " : " (NULLIF($field, '') IS NOT NULL) "; |
6a488035 TO |
5648 | return $clause; |
5649 | ||
a0475688 | 5650 | case 'RLIKE': |
0800bc59 | 5651 | return " CAST({$field} AS BINARY) RLIKE BINARY '{$value}' "; |
a0475688 | 5652 | |
e2b0fbec | 5653 | case 'IN': |
5654 | case 'NOT IN': | |
715277e7 | 5655 | // I feel like this would be escaped properly if passed through $queryString = CRM_Core_DAO::createSqlFilter. |
46b3417a | 5656 | if (!empty($value) && (!is_array($value) || !array_key_exists($op, $value))) { |
be2fb01f | 5657 | $value = [$op => (array) $value]; |
e2b0fbec | 5658 | } |
5659 | ||
6a488035 | 5660 | default: |
89d4a22f | 5661 | if (empty($dataType) || $dataType == 'Date') { |
6a488035 TO |
5662 | $dataType = 'String'; |
5663 | } | |
461c9a60 EM |
5664 | if (is_array($value)) { |
5665 | //this could have come from the api - as in the restWhere section we potentially use the api operator syntax which is becoming more | |
5666 | // widely used and consistent across the codebase | |
5667 | // adding this here won't accept the search functions which don't submit an array | |
b832662c | 5668 | if (($queryString = CRM_Core_DAO::createSQLFilter($field, $value, $dataType)) != FALSE) { |
6e23130a | 5669 | |
461c9a60 EM |
5670 | return $queryString; |
5671 | } | |
ccabbc44 | 5672 | if (!empty($value[0]) && $op === 'BETWEEN') { |
5673 | CRM_Core_Error::deprecatedFunctionWarning('Fix search input params'); | |
b832662c | 5674 | if (($queryString = CRM_Core_DAO::createSQLFilter($field, [$op => $value], $dataType)) != FALSE) { |
6e23130a EM |
5675 | return $queryString; |
5676 | } | |
5677 | } | |
ccabbc44 | 5678 | throw new CRM_Core_Exception(ts('Failed to interpret input for search')); |
461c9a60 | 5679 | } |
9787e1bb SL |
5680 | $emojiWhere = CRM_Utils_SQL::handleEmojiInQuery($value); |
5681 | if ($emojiWhere === '0 = 1') { | |
5682 | $value = $emojiWhere; | |
5683 | } | |
6a488035 | 5684 | $value = CRM_Utils_Type::escape($value, $dataType); |
a75c13cc | 5685 | // if we don't have a dataType we should assume |
85bdc94e | 5686 | if ($dataType == 'String' || $dataType == 'Text') { |
2fc64082 | 5687 | $value = "'" . $value . "'"; |
6a488035 TO |
5688 | } |
5689 | return "$clause $value"; | |
5690 | } | |
5691 | } | |
5692 | ||
86538308 EM |
5693 | /** |
5694 | * @param bool $reset | |
5695 | * | |
5696 | * @return array | |
5697 | */ | |
00be9182 | 5698 | public function openedSearchPanes($reset = FALSE) { |
6a488035 TO |
5699 | if (!$reset || empty($this->_whereTables)) { |
5700 | return self::$_openedPanes; | |
5701 | } | |
5702 | ||
5703 | // pane name to table mapper | |
be2fb01f | 5704 | $panesMapper = [ |
6a488035 TO |
5705 | ts('Contributions') => 'civicrm_contribution', |
5706 | ts('Memberships') => 'civicrm_membership', | |
5707 | ts('Events') => 'civicrm_participant', | |
5708 | ts('Relationships') => 'civicrm_relationship', | |
5709 | ts('Activities') => 'civicrm_activity', | |
5710 | ts('Pledges') => 'civicrm_pledge', | |
5711 | ts('Cases') => 'civicrm_case', | |
6a488035 TO |
5712 | ts('Address Fields') => 'civicrm_address', |
5713 | ts('Notes') => 'civicrm_note', | |
5714 | ts('Change Log') => 'civicrm_log', | |
974ae237 | 5715 | ts('Mailings') => 'civicrm_mailing', |
be2fb01f | 5716 | ]; |
066b4d4a | 5717 | CRM_Contact_BAO_Query_Hook::singleton()->getPanesMapper($panesMapper); |
6a488035 TO |
5718 | |
5719 | foreach (array_keys($this->_whereTables) as $table) { | |
5720 | if ($panName = array_search($table, $panesMapper)) { | |
5721 | self::$_openedPanes[$panName] = TRUE; | |
5722 | } | |
5723 | } | |
5724 | ||
5725 | return self::$_openedPanes; | |
5726 | } | |
5727 | ||
86538308 EM |
5728 | /** |
5729 | * @param $operator | |
5730 | */ | |
00be9182 | 5731 | public function setOperator($operator) { |
be2fb01f | 5732 | $validOperators = ['AND', 'OR']; |
6a488035 TO |
5733 | if (!in_array($operator, $validOperators)) { |
5734 | $operator = 'AND'; | |
5735 | } | |
5736 | $this->_operator = $operator; | |
5737 | } | |
5738 | ||
86538308 EM |
5739 | /** |
5740 | * @return string | |
5741 | */ | |
00be9182 | 5742 | public function getOperator() { |
6a488035 TO |
5743 | return $this->_operator; |
5744 | } | |
5745 | ||
86538308 EM |
5746 | /** |
5747 | * @param $from | |
5748 | * @param $where | |
5749 | * @param $having | |
5750 | */ | |
00be9182 | 5751 | public function filterRelatedContacts(&$from, &$where, &$having) { |
2bedfb3f | 5752 | if (!isset(Civi::$statics[__CLASS__]['related_contacts_filter'])) { |
be2fb01f | 5753 | Civi::$statics[__CLASS__]['related_contacts_filter'] = []; |
2bedfb3f HA |
5754 | } |
5755 | $_rTempCache =& Civi::$statics[__CLASS__]['related_contacts_filter']; | |
68f8975b | 5756 | // since there only can be one instance of this filter in every query |
2bedfb3f HA |
5757 | // skip if filter has already applied |
5758 | foreach ($_rTempCache as $acache) { | |
68f8975b HA |
5759 | foreach ($acache['queries'] as $aqcache) { |
5760 | if (strpos($from, $aqcache['from']) !== FALSE) { | |
5761 | $having = NULL; | |
5762 | return; | |
5763 | } | |
2bedfb3f HA |
5764 | } |
5765 | } | |
5766 | $arg_sig = sha1("$from $where $having"); | |
5767 | if (isset($_rTempCache[$arg_sig])) { | |
5768 | $cache = $_rTempCache[$arg_sig]; | |
5769 | } | |
5770 | else { | |
6a488035 | 5771 | // create temp table with contact ids |
68f8975b | 5772 | |
def88c52 | 5773 | $tableName = CRM_Utils_SQL_TempTable::build()->createWithColumns('contact_id int primary key')->setMemory(TRUE)->getName(); |
6a488035 TO |
5774 | |
5775 | $sql = " | |
5776 | REPLACE INTO $tableName ( contact_id ) | |
5777 | SELECT contact_a.id | |
5778 | $from | |
5779 | $where | |
5780 | $having | |
5781 | "; | |
5782 | CRM_Core_DAO::executeQuery($sql); | |
5783 | ||
be2fb01f | 5784 | $cache = ['tableName' => $tableName, 'queries' => []]; |
68f8975b HA |
5785 | $_rTempCache[$arg_sig] = $cache; |
5786 | } | |
5787 | // upsert the query depending on relationship type | |
5788 | if (isset($cache['queries'][$this->_displayRelationshipType])) { | |
5789 | $qcache = $cache['queries'][$this->_displayRelationshipType]; | |
5790 | } | |
5791 | else { | |
5792 | $tableName = $cache['tableName']; | |
be2fb01f | 5793 | $qcache = [ |
68f8975b HA |
5794 | "from" => "", |
5795 | "where" => "", | |
be2fb01f | 5796 | ]; |
6a488035 | 5797 | $rTypes = CRM_Core_PseudoConstant::relationshipType(); |
6a488035 TO |
5798 | if (is_numeric($this->_displayRelationshipType)) { |
5799 | $relationshipTypeLabel = $rTypes[$this->_displayRelationshipType]['label_a_b']; | |
68f8975b | 5800 | $qcache['from'] = " |
6a488035 TO |
5801 | INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id OR displayRelType.contact_id_b = contact_a.id ) |
5802 | INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a OR transform_temp.contact_id = displayRelType.contact_id_b ) | |
5803 | "; | |
68f8975b | 5804 | $qcache['where'] = " |
d7a05de1 | 5805 | WHERE displayRelType.relationship_type_id = {$this->_displayRelationshipType} |
6a488035 TO |
5806 | AND displayRelType.is_active = 1 |
5807 | "; | |
5808 | } | |
5809 | else { | |
5810 | list($relType, $dirOne, $dirTwo) = explode('_', $this->_displayRelationshipType); | |
5811 | if ($dirOne == 'a') { | |
5812 | $relationshipTypeLabel = $rTypes[$relType]['label_a_b']; | |
68f8975b | 5813 | $qcache['from'] .= " |
6a488035 TO |
5814 | INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id ) |
5815 | INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_b ) | |
5816 | "; | |
5817 | } | |
5818 | else { | |
5819 | $relationshipTypeLabel = $rTypes[$relType]['label_b_a']; | |
68f8975b | 5820 | $qcache['from'] .= " |
6a488035 TO |
5821 | INNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_b = contact_a.id ) |
5822 | INNER JOIN $tableName transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a ) | |
5823 | "; | |
5824 | } | |
68f8975b | 5825 | $qcache['where'] = " |
d7a05de1 | 5826 | WHERE displayRelType.relationship_type_id = $relType |
6a488035 TO |
5827 | AND displayRelType.is_active = 1 |
5828 | "; | |
5829 | } | |
68f8975b HA |
5830 | $qcache['relTypeLabel'] = $relationshipTypeLabel; |
5831 | $_rTempCache[$arg_sig]['queries'][$this->_displayRelationshipType] = $qcache; | |
6a488035 | 5832 | } |
68f8975b HA |
5833 | $qillMessage = ts('Contacts with a Relationship Type of: '); |
5834 | $iqill = $qillMessage . "'" . $qcache['relTypeLabel'] . "'"; | |
5835 | if (!is_array($this->_qill[0]) || !in_array($iqill, $this->_qill[0])) { | |
5836 | $this->_qill[0][] = $iqill; | |
5837 | } | |
5838 | if (strpos($from, $qcache['from']) === FALSE) { | |
83cffc1e | 5839 | if (strpos($from, "INNER JOIN") !== FALSE) { |
5adc0da9 JJ |
5840 | // lets replace all the INNER JOIN's in the $from so we dont exclude other data |
5841 | // this happens when we have an event_type in the quert (CRM-7969) | |
5842 | $from = str_replace("INNER JOIN", "LEFT JOIN", $from); | |
5843 | // Make sure the relationship join right after the FROM and other joins afterwards. | |
5844 | // This gives us the possibility to change the join on civicrm case. | |
5845 | $from = preg_replace("/LEFT JOIN/", $qcache['from'] . " LEFT JOIN", $from, 1); | |
83cffc1e JJ |
5846 | } |
5847 | else { | |
5adc0da9 JJ |
5848 | $from .= $qcache['from']; |
5849 | } | |
d7a05de1 | 5850 | $where = $qcache['where']; |
5adc0da9 JJ |
5851 | if (!empty($this->_tables['civicrm_case'])) { |
5852 | // Change the join on CiviCRM case so that it joins on the right contac from the relationship. | |
5853 | $from = str_replace("ON civicrm_case_contact.contact_id = contact_a.id", "ON civicrm_case_contact.contact_id = transform_temp.contact_id", $from); | |
5854 | $where .= " AND displayRelType.case_id = civicrm_case_contact.case_id "; | |
5855 | } | |
21c6be28 SL |
5856 | if (!empty($this->_permissionFromClause) && !stripos($from, 'aclContactCache')) { |
5857 | $from .= " $this->_permissionFromClause"; | |
5858 | } | |
2bedfb3f HA |
5859 | if (!empty($this->_permissionWhereClause)) { |
5860 | $where .= "AND $this->_permissionWhereClause"; | |
5861 | } | |
6a488035 TO |
5862 | } |
5863 | ||
5864 | $having = NULL; | |
5865 | } | |
5866 | ||
86538308 | 5867 | /** |
30415e03 | 5868 | * See CRM-19811 for why this is database hurty without apparent benefit. |
5869 | * | |
86538308 EM |
5870 | * @param $op |
5871 | * | |
5872 | * @return bool | |
5873 | */ | |
5c7dad85 | 5874 | public static function caseImportant($op) { |
91768280 | 5875 | return !in_array($op, ['LIKE', 'IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY']); |
6a488035 TO |
5876 | } |
5877 | ||
86538308 EM |
5878 | /** |
5879 | * @param $returnProperties | |
5880 | * @param $prefix | |
5881 | * | |
5882 | * @return bool | |
5883 | */ | |
5c7dad85 TO |
5884 | public static function componentPresent(&$returnProperties, $prefix) { |
5885 | foreach ($returnProperties as $name => $dontCare) { | |
6a488035 TO |
5886 | if (substr($name, 0, strlen($prefix)) == $prefix) { |
5887 | return TRUE; | |
5888 | } | |
5889 | } | |
5890 | return FALSE; | |
5891 | } | |
5892 | ||
5893 | /** | |
c037736a | 5894 | * Builds the necessary structures for all fields that are similar to option value look-ups. |
6a488035 | 5895 | * |
5a4f6742 CW |
5896 | * @param string $name |
5897 | * the name of the field. | |
5898 | * @param string $op | |
5899 | * the sql operator, this function should handle ALL SQL operators. | |
5900 | * @param string $value | |
5901 | * depends on the operator and who's calling the query builder. | |
5902 | * @param int $grouping | |
5903 | * the index where to place the where clause. | |
3bdf1f3a | 5904 | * @param string $daoName |
5905 | * DAO Name. | |
5a4f6742 CW |
5906 | * @param array $field |
5907 | * an array that contains various properties of the field identified by $name. | |
5908 | * @param string $label | |
5909 | * The label for this field element. | |
5910 | * @param string $dataType | |
b832662c | 5911 | * |
5912 | * @throws \CRM_Core_Exception | |
6a488035 | 5913 | */ |
59f4c9ee | 5914 | public function optionValueQuery( |
6a488035 TO |
5915 | $name, |
5916 | $op, | |
5917 | $value, | |
5918 | $grouping, | |
e9cde327 | 5919 | $daoName, |
6a488035 TO |
5920 | $field, |
5921 | $label, | |
bf0f0d19 | 5922 | $dataType = 'String' |
6a488035 | 5923 | ) { |
bd3728fb | 5924 | |
be2fb01f | 5925 | $pseudoFields = [ |
5c7dad85 TO |
5926 | 'email_greeting', |
5927 | 'postal_greeting', | |
5928 | 'addressee', | |
be2fb01f | 5929 | ]; |
d9ab802d | 5930 | |
bf0f0d19 | 5931 | list($tableName, $fieldName) = explode('.', $field['where'], 2); |
5932 | if ($tableName == 'civicrm_contact') { | |
5933 | $wc = "contact_a.$fieldName"; | |
5934 | } | |
5935 | else { | |
5936 | // Special handling for on_hold, so that we actually use the 'where' | |
5937 | // property in order to limit the query by the on_hold status of the email, | |
5938 | // instead of using email.id which would be nonsensical. | |
5939 | if ($field['name'] === 'on_hold') { | |
5940 | $wc = $field['where']; | |
9df8a11d | 5941 | } |
8b695e91 | 5942 | else { |
bf0f0d19 | 5943 | $wc = "$tableName.id"; |
8b695e91 | 5944 | } |
9df8a11d | 5945 | } |
bf0f0d19 | 5946 | |
d9ab802d | 5947 | if (in_array($name, $pseudoFields)) { |
9724097e | 5948 | $wc = "contact_a.{$name}_id"; |
d9ab802d PJ |
5949 | $dataType = 'Positive'; |
5950 | $value = (!$value) ? 0 : $value; | |
5951 | } | |
728eefa0 | 5952 | if ($name == "world_region") { |
5953 | $field['name'] = $name; | |
5954 | } | |
d9ab802d | 5955 | |
9b183452 | 5956 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($daoName, $field['name'], $value, $op); |
be2fb01f | 5957 | $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $label, 2 => $qillop, 3 => $qillVal]); |
d9ab802d | 5958 | $this->_where[$grouping][] = self::buildClause($wc, $op, $value, $dataType); |
6a488035 TO |
5959 | } |
5960 | ||
a30e57e0 | 5961 | /** |
100fef9d | 5962 | * Check and explode a user defined numeric string into an array |
6a488035 TO |
5963 | * this was the protocol used by search builder in the old old days before we had |
5964 | * super nice js widgets to do the hard work | |
5965 | * | |
4e6072d5 | 5966 | * @param string $string |
77c5b619 TO |
5967 | * @param string $dataType |
5968 | * The dataType we should check for the values, default integer. | |
6a488035 | 5969 | * |
72b3a70c CW |
5970 | * @return bool|array |
5971 | * false if string does not match the pattern | |
5972 | * array of numeric values if string does match the pattern | |
6a488035 | 5973 | */ |
00be9182 | 5974 | public static function parseSearchBuilderString($string, $dataType = 'Integer') { |
6a488035 TO |
5975 | $string = trim($string); |
5976 | if (substr($string, 0, 1) != '(' || substr($string, -1, 1) != ')') { | |
69078420 | 5977 | return FALSE; |
6a488035 TO |
5978 | } |
5979 | ||
5980 | $string = substr($string, 1, -1); | |
5981 | $values = explode(',', $string); | |
5982 | if (empty($values)) { | |
5983 | return FALSE; | |
5984 | } | |
5985 | ||
be2fb01f | 5986 | $returnValues = []; |
6a488035 | 5987 | foreach ($values as $v) { |
5c7dad85 | 5988 | if ($dataType == 'Integer' && !is_numeric($v)) { |
6a488035 TO |
5989 | return FALSE; |
5990 | } | |
4c9b6178 | 5991 | elseif ($dataType == 'String' && !is_string($v)) { |
6a488035 TO |
5992 | return FALSE; |
5993 | } | |
5994 | $returnValues[] = trim($v); | |
5995 | } | |
5996 | ||
5997 | if (empty($returnValues)) { | |
5998 | return FALSE; | |
5999 | } | |
6000 | ||
6001 | return $returnValues; | |
6002 | } | |
6a488035 | 6003 | |
d9ab802d | 6004 | /** |
100fef9d | 6005 | * Convert the pseudo constants id's to their names |
837c4660 | 6006 | * |
59f4c9ee | 6007 | * @param CRM_Core_DAO $dao |
837c4660 | 6008 | * @param bool $return |
a5e77b37 | 6009 | * @param bool $usedForAPI |
837c4660 | 6010 | * |
59f4c9ee | 6011 | * @return array|NULL |
d9ab802d | 6012 | */ |
a5e77b37 | 6013 | public function convertToPseudoNames(&$dao, $return = FALSE, $usedForAPI = FALSE) { |
d9ab802d | 6014 | if (empty($this->_pseudoConstantsSelect)) { |
59f4c9ee | 6015 | return NULL; |
d9ab802d | 6016 | } |
be2fb01f | 6017 | $values = []; |
d9ab802d | 6018 | foreach ($this->_pseudoConstantsSelect as $key => $value) { |
a7488080 | 6019 | if (!empty($this->_pseudoConstantsSelect[$key]['sorting'])) { |
d9ab802d PJ |
6020 | continue; |
6021 | } | |
6022 | ||
9db71ff2 | 6023 | if (is_object($dao) && property_exists($dao, $value['idCol'])) { |
6ef04c72 | 6024 | $val = $dao->{$value['idCol']}; |
3875e6b6 | 6025 | if ($key == 'groups') { |
6026 | $dao->groups = $this->convertGroupIDStringToLabelString($dao, $val); | |
86ab13b7 | 6027 | continue; |
3875e6b6 | 6028 | } |
d9ab802d PJ |
6029 | |
6030 | if (CRM_Utils_System::isNull($val)) { | |
6031 | $dao->$key = NULL; | |
d9ab802d | 6032 | } |
86ab13b7 | 6033 | elseif (!empty($value['pseudoconstant'])) { |
6034 | // If pseudoconstant is set that is kind of defacto for 'we have a bit more info about this' | |
6035 | // and we can use the metadata to figure it out. | |
6036 | // ideally this bit of IF will absorb & replace all the rest in time as we move to | |
6037 | // more metadata based choices. | |
6038 | if (strpos($val, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) { | |
6039 | $dbValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($val, CRM_Core_DAO::VALUE_SEPARATOR)); | |
6040 | foreach ($dbValues as $pseudoValue) { | |
6041 | $convertedValues[] = CRM_Core_PseudoConstant::getLabel($value['bao'], $value['idCol'], $pseudoValue); | |
6042 | } | |
6043 | ||
6044 | $dao->$key = ($usedForAPI) ? $convertedValues : implode(', ', $convertedValues); | |
9c1bc317 | 6045 | $realFieldName = $this->_pseudoConstantsSelect[$key]['field_name'] ?? NULL; |
86ab13b7 | 6046 | if ($usedForAPI && $realFieldName) { |
6047 | // normally we would see 2 fields returned for pseudoConstants. An exception is | |
6048 | // preferred_communication_method where there is no id-variant. | |
6049 | // For the api we prioritise getting the real data returned. | |
6050 | // over the resolved version | |
6051 | $dao->$realFieldName = $dbValues; | |
6052 | } | |
6053 | ||
6054 | } | |
6055 | else { | |
6056 | // This is basically the same as the default but since we have the bao we can use | |
6057 | // a cached function. | |
6058 | $dao->$key = CRM_Core_PseudoConstant::getLabel($value['bao'], $value['idCol'], $val); | |
6059 | } | |
6060 | } | |
b719f000 | 6061 | elseif ($baoName = CRM_Utils_Array::value('bao', $value, NULL)) { |
827659b2 | 6062 | //preserve id value |
6063 | $idColumn = "{$key}_id"; | |
6064 | $dao->$idColumn = $val; | |
b105067c | 6065 | |
6066 | if ($key == 'state_province_name') { | |
144d0efe | 6067 | $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant::stateProvince($val); |
b105067c | 6068 | } |
6069 | else { | |
6ef04c72 | 6070 | $dao->{$value['pseudoField']} = $dao->$key = CRM_Core_PseudoConstant::getLabel($baoName, $value['pseudoField'], $val); |
b105067c | 6071 | } |
d9ab802d PJ |
6072 | } |
6073 | elseif ($value['pseudoField'] == 'state_province_abbreviation') { | |
41d9d3cf IK |
6074 | // old bad code: |
6075 | // $dao->$key = CRM_Core_PseudoConstant::stateProvinceAbbreviation($val); | |
6076 | // iank: Return abbreviation if its a province from a country that uses abbreviations, | |
6077 | // otherwise return the name of the province. | |
6078 | // https://lab.civicrm.org/dev/core/issues/724 | |
6079 | if (is_numeric(CRM_Core_PseudoConstant::stateProvinceAbbreviation($val))) { | |
6080 | $dao->$key = CRM_Core_PseudoConstant::stateProvince($val); | |
6081 | } else { | |
6082 | $dao->$key = CRM_Core_PseudoConstant::stateProvinceAbbreviation($val); | |
6083 | } | |
d9ab802d | 6084 | } |
86ab13b7 | 6085 | // @todo handle this in the section above for pseudoconstants. |
be2fb01f | 6086 | elseif (in_array($value['pseudoField'], ['participant_role_id', 'participant_role'])) { |
3af96592 | 6087 | // @todo define bao on this & merge into the above condition. |
348b1fda | 6088 | $viewValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $val); |
6089 | ||
6090 | if ($value['pseudoField'] == 'participant_role') { | |
6091 | $pseudoOptions = CRM_Core_PseudoConstant::get('CRM_Event_DAO_Participant', 'role_id'); | |
6092 | foreach ($viewValues as $k => $v) { | |
6093 | $viewValues[$k] = $pseudoOptions[$v]; | |
6094 | } | |
62fd7fe8 | 6095 | } |
a5e77b37 | 6096 | $dao->$key = ($usedForAPI && count($viewValues) > 1) ? $viewValues : implode(', ', $viewValues); |
62fd7fe8 | 6097 | } |
d9ab802d PJ |
6098 | else { |
6099 | $labels = CRM_Core_OptionGroup::values($value['pseudoField']); | |
6100 | $dao->$key = $labels[$val]; | |
6101 | } | |
b719f000 PJ |
6102 | |
6103 | // return converted values in array format | |
6104 | if ($return) { | |
6105 | if (strpos($key, '-') !== FALSE) { | |
6106 | $keyVal = explode('-', $key); | |
6107 | $current = &$values; | |
6108 | $lastElement = array_pop($keyVal); | |
6109 | foreach ($keyVal as $v) { | |
6110 | if (!array_key_exists($v, $current)) { | |
be2fb01f | 6111 | $current[$v] = []; |
b719f000 PJ |
6112 | } |
6113 | $current = &$current[$v]; | |
6114 | } | |
6115 | $current[$lastElement] = $dao->$key; | |
6116 | } | |
6117 | else { | |
6118 | $values[$key] = $dao->$key; | |
6119 | } | |
6120 | } | |
d9ab802d PJ |
6121 | } |
6122 | } | |
86ab13b7 | 6123 | if (!$usedForAPI) { |
714ab070 | 6124 | foreach ($this->legacyHackedFields as $realField => $labelField) { |
86ab13b7 | 6125 | // This is a temporary routine for handling these fields while |
6126 | // we figure out how to handled them based on metadata in | |
6127 | /// export and search builder. CRM-19815, CRM-19830. | |
f02f3e59 | 6128 | if (isset($dao->$realField) && is_numeric($dao->$realField) && isset($dao->$labelField)) { |
86ab13b7 | 6129 | $dao->$realField = $dao->$labelField; |
6130 | } | |
6131 | } | |
6132 | } | |
d9ab802d PJ |
6133 | return $values; |
6134 | } | |
6135 | ||
837c4660 | 6136 | /** |
fe482240 | 6137 | * Include pseudo fields LEFT JOIN. |
77c5b619 | 6138 | * @param string|array $sort can be a object or string |
d9ab802d | 6139 | * |
59f4c9ee | 6140 | * @return array|NULL |
d9ab802d | 6141 | */ |
00be9182 | 6142 | public function includePseudoFieldsJoin($sort) { |
d9ab802d | 6143 | if (!$sort || empty($this->_pseudoConstantsSelect)) { |
59f4c9ee | 6144 | return NULL; |
d9ab802d PJ |
6145 | } |
6146 | $sort = is_string($sort) ? $sort : $sort->orderBy(); | |
be2fb01f | 6147 | $present = []; |
d9ab802d PJ |
6148 | |
6149 | foreach ($this->_pseudoConstantsSelect as $name => $value) { | |
a7488080 | 6150 | if (!empty($value['table'])) { |
d9ab802d PJ |
6151 | $regex = "/({$value['table']}\.|{$name})/"; |
6152 | if (preg_match($regex, $sort)) { | |
6153 | $this->_elemnt[$value['element']] = 1; | |
6154 | $this->_select[$value['element']] = $value['select']; | |
6155 | $this->_pseudoConstantsSelect[$name]['sorting'] = 1; | |
6156 | $present[$value['table']] = $value['join']; | |
6157 | } | |
6158 | } | |
6159 | } | |
6160 | $presentSimpleFrom = $present; | |
6161 | ||
6162 | if (array_key_exists('civicrm_worldregion', $this->_whereTables) && | |
5c7dad85 TO |
6163 | array_key_exists('civicrm_country', $presentSimpleFrom) |
6164 | ) { | |
d9ab802d PJ |
6165 | unset($presentSimpleFrom['civicrm_country']); |
6166 | } | |
6167 | if (array_key_exists('civicrm_worldregion', $this->_tables) && | |
5c7dad85 TO |
6168 | array_key_exists('civicrm_country', $present) |
6169 | ) { | |
d9ab802d PJ |
6170 | unset($present['civicrm_country']); |
6171 | } | |
6172 | ||
6173 | $presentClause = $presentSimpleFromClause = NULL; | |
6174 | if (!empty($present)) { | |
6175 | $presentClause = implode(' ', $present); | |
6176 | } | |
6177 | if (!empty($presentSimpleFrom)) { | |
6178 | $presentSimpleFromClause = implode(' ', $presentSimpleFrom); | |
6179 | } | |
6180 | ||
6181 | $this->_fromClause = $this->_fromClause . $presentClause; | |
6182 | $this->_simpleFromClause = $this->_simpleFromClause . $presentSimpleFromClause; | |
6183 | ||
be2fb01f | 6184 | return [$presentClause, $presentSimpleFromClause]; |
d9ab802d | 6185 | } |
9ab34172 | 6186 | |
0e2e76cf EM |
6187 | /** |
6188 | * Build qill for field. | |
6189 | * | |
6190 | * Qill refers to the query detail visible on the UI. | |
6191 | * | |
bb05da0c | 6192 | * @param string $daoName |
6193 | * @param string $fieldName | |
6194 | * @param mixed $fieldValue | |
6195 | * @param string $op | |
6196 | * @param array $pseudoExtraParam | |
6197 | * @param int $type | |
6198 | * Type of the field per CRM_Utils_Type | |
0e2e76cf EM |
6199 | * |
6200 | * @return array | |
6201 | */ | |
bb05da0c | 6202 | public static function buildQillForFieldValue( |
6203 | $daoName, | |
6204 | $fieldName, | |
6205 | $fieldValue, | |
6206 | $op, | |
be2fb01f | 6207 | $pseudoExtraParam = [], |
bb05da0c | 6208 | $type = CRM_Utils_Type::T_STRING |
6209 | ) { | |
afa0b07c | 6210 | $qillOperators = CRM_Core_SelectValues::getSearchBuilderOperators(); |
97c1cf60 | 6211 | |
3086e282 | 6212 | //API usually have fieldValue format as array(operator => array(values)), |
6213 | //so we need to separate operator out of fieldValue param | |
6214 | if (is_array($fieldValue) && in_array(key($fieldValue), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { | |
6215 | $op = key($fieldValue); | |
6216 | $fieldValue = $fieldValue[$op]; | |
6217 | } | |
6218 | ||
9b183452 | 6219 | // if Operator chosen is NULL/EMPTY then |
6220 | if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) { | |
be2fb01f | 6221 | return [CRM_Utils_Array::value($op, $qillOperators, $op), '']; |
9b183452 | 6222 | } |
6223 | ||
3fc41a5d | 6224 | // @todo - if the right BAO is passed in special handling for the below |
6225 | // fields should not be required. testQillOptions. | |
6226 | if ($fieldName == 'country_id') { | |
e4001cf0 | 6227 | $pseudoOptions = CRM_Core_PseudoConstant::country(); |
9b183452 | 6228 | } |
6229 | elseif ($fieldName == 'county_id') { | |
e4001cf0 | 6230 | $pseudoOptions = CRM_Core_PseudoConstant::county(); |
9b183452 | 6231 | } |
728eefa0 | 6232 | elseif ($fieldName == 'world_region') { |
e4001cf0 | 6233 | $pseudoOptions = CRM_Core_PseudoConstant::worldRegion(); |
728eefa0 | 6234 | } |
97c1cf60 | 6235 | elseif ($daoName == 'CRM_Event_DAO_Event' && $fieldName == 'id') { |
450fcddb JP |
6236 | $checkPermission = CRM_Utils_Array::value('check_permission', $pseudoExtraParam, TRUE); |
6237 | $pseudoOptions = CRM_Event_BAO_Event::getEvents(0, $fieldValue, TRUE, $checkPermission, TRUE); | |
97c1cf60 | 6238 | } |
6ffab5b7 | 6239 | elseif ($fieldName == 'contribution_product_id') { |
bb05da0c | 6240 | $pseudoOptions = CRM_Contribute_PseudoConstant::products(); |
6ffab5b7 | 6241 | } |
e8401ae2 | 6242 | elseif ($daoName == 'CRM_Contact_DAO_Group' && $fieldName == 'id') { |
bb05da0c | 6243 | $pseudoOptions = CRM_Core_PseudoConstant::group(); |
e8401ae2 | 6244 | } |
3086e282 | 6245 | elseif ($daoName == 'CRM_Batch_BAO_EntityBatch' && $fieldName == 'batch_id') { |
6246 | $pseudoOptions = CRM_Contribute_PseudoConstant::batch(); | |
6247 | } | |
efb88612 | 6248 | elseif ($daoName) { |
bb05da0c | 6249 | $pseudoOptions = CRM_Core_PseudoConstant::get($daoName, $fieldName, $pseudoExtraParam); |
97c1cf60 | 6250 | } |
9ab34172 | 6251 | |
efb88612 | 6252 | if (is_array($fieldValue)) { |
be2fb01f | 6253 | $qillString = []; |
bb05da0c | 6254 | if (!empty($pseudoOptions)) { |
efb88612 | 6255 | foreach ((array) $fieldValue as $val) { |
bb05da0c | 6256 | $qillString[] = CRM_Utils_Array::value($val, $pseudoOptions, $val); |
efb88612 | 6257 | } |
6258 | $fieldValue = implode(', ', $qillString); | |
9ab34172 | 6259 | } |
efb88612 | 6260 | else { |
bb05da0c | 6261 | if ($type == CRM_Utils_Type::T_DATE) { |
6262 | foreach ($fieldValue as $index => $value) { | |
6263 | $fieldValue[$index] = CRM_Utils_Date::customFormat($value); | |
6264 | } | |
6265 | } | |
6266 | $separator = ', '; | |
6267 | // @todo - this is a bit specific (one operator). | |
6268 | // However it is covered by a unit test so can be altered later with | |
6269 | // some confidence. | |
9de9a433 | 6270 | if ($op === 'BETWEEN') { |
bb05da0c | 6271 | $separator = ' AND '; |
6272 | } | |
6273 | $fieldValue = implode($separator, $fieldValue); | |
9ab34172 | 6274 | } |
9ab34172 | 6275 | } |
bb05da0c | 6276 | elseif (!empty($pseudoOptions) && array_key_exists($fieldValue, $pseudoOptions)) { |
6277 | $fieldValue = $pseudoOptions[$fieldValue]; | |
6278 | } | |
6279 | elseif ($type === CRM_Utils_Type::T_DATE) { | |
6280 | $fieldValue = CRM_Utils_Date::customFormat($fieldValue); | |
9ab34172 | 6281 | } |
afa0b07c | 6282 | |
be2fb01f | 6283 | return [CRM_Utils_Array::value($op, $qillOperators, $op), $fieldValue]; |
9ab34172 | 6284 | } |
96025800 | 6285 | |
1a7029b8 MD |
6286 | /** |
6287 | * Get the qill (search description for field) for the specified field. | |
6288 | * | |
6289 | * @param string $daoName | |
6290 | * @param string $name | |
6291 | * @param string $value | |
6292 | * @param string|array $op | |
6293 | * @param string $label | |
6294 | * | |
6295 | * @return string | |
6296 | */ | |
6297 | public static function getQillValue($daoName, string $name, $value, $op, string $label) { | |
6298 | list($op, $value) = self::buildQillForFieldValue($daoName, $name, $value, $op); | |
6299 | return ts('%1 %2 %3', [1 => $label, 2 => $op, 3 => $value]); | |
6300 | } | |
6301 | ||
836eb043 | 6302 | /** |
6303 | * Alter value to reflect wildcard settings. | |
6304 | * | |
6305 | * The form will have tried to guess whether this is a good field to wildcard but there is | |
6306 | * also a site-wide setting that specifies whether it is OK to append the wild card to the beginning | |
6307 | * or only the end of the string | |
6308 | * | |
6309 | * @param bool $wildcard | |
6310 | * This is a bool made on an assessment 'elsewhere' on whether this is a good field to wildcard. | |
6311 | * @param string $op | |
6312 | * Generally '=' or 'LIKE'. | |
6313 | * @param string $value | |
6314 | * The search string. | |
6315 | * | |
6316 | * @return string | |
6317 | */ | |
0b38e8f1 | 6318 | public static function getWildCardedValue($wildcard, $op, $value) { |
9de9a433 | 6319 | if ($wildcard && $op === 'LIKE') { |
836eb043 | 6320 | if (CRM_Core_Config::singleton()->includeWildCardInName && (substr($value, 0, 1) != '%')) { |
ff494d25 | 6321 | $value = "%$value"; |
836eb043 | 6322 | } |
ff494d25 BS |
6323 | if (substr($value, -1, 1) != '%') { |
6324 | $value = "$value%"; | |
836eb043 | 6325 | } |
6326 | } | |
ff494d25 | 6327 | return "$value"; |
836eb043 | 6328 | } |
6329 | ||
0b38e8f1 | 6330 | /** |
6331 | * Process special fields of Search Form in OK (Operator in Key) format | |
6332 | * | |
6333 | * @param array $formValues | |
6334 | * @param array $specialFields | |
6335 | * Special params to be processed | |
6336 | * @param array $changeNames | |
6337 | * Array of fields whose name should be changed | |
6338 | */ | |
be2fb01f | 6339 | public static function processSpecialFormValue(&$formValues, $specialFields, $changeNames = []) { |
3086e282 | 6340 | // Array of special fields whose value are considered only for NULL or EMPTY operators |
be2fb01f | 6341 | $nullableFields = ['contribution_batch_id']; |
3086e282 | 6342 | |
0b38e8f1 | 6343 | foreach ($specialFields as $element) { |
9c1bc317 | 6344 | $value = $formValues[$element] ?? NULL; |
0b38e8f1 | 6345 | if ($value) { |
6346 | if (is_array($value)) { | |
c3f7ab62 | 6347 | if (array_key_exists($element, $changeNames)) { |
0b38e8f1 | 6348 | unset($formValues[$element]); |
6349 | $element = $changeNames[$element]; | |
6350 | } | |
be2fb01f | 6351 | $formValues[$element] = ['IN' => $value]; |
0b38e8f1 | 6352 | } |
be2fb01f CW |
6353 | elseif (in_array($value, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) { |
6354 | $formValues[$element] = [$value => 1]; | |
3086e282 | 6355 | } |
6356 | elseif (!in_array($element, $nullableFields)) { | |
0b38e8f1 | 6357 | // if wildcard is already present return searchString as it is OR append and/or prepend with wildcard |
6358 | $isWilcard = strstr($value, '%') ? FALSE : CRM_Core_Config::singleton()->includeWildCardInName; | |
be2fb01f | 6359 | $formValues[$element] = ['LIKE' => self::getWildCardedValue($isWilcard, 'LIKE', $value)]; |
0b38e8f1 | 6360 | } |
6361 | } | |
6362 | } | |
6363 | } | |
6364 | ||
db54ba06 TO |
6365 | /** |
6366 | * Parse and assimilate the various sort options. | |
6367 | * | |
6368 | * Side-effect: if sorting on a common column from a related table (`city`, `postal_code`, | |
6369 | * `email`), the related table may be joined automatically. | |
6370 | * | |
6371 | * At time of writing, this code is deeply flawed and should be rewritten. For the moment, | |
6372 | * it's been extracted to a standalone function. | |
6373 | * | |
6374 | * @param string|CRM_Utils_Sort $sort | |
6375 | * The order by string. | |
db54ba06 TO |
6376 | * @param null $sortOrder |
6377 | * Who knows? Hu knows. He who knows Hu knows who. | |
59733b26 | 6378 | * |
b832662c | 6379 | * @return string |
db54ba06 | 6380 | * list(string $orderByClause, string $additionalFromClause). |
afbe25c1 | 6381 | * |
b832662c | 6382 | * @throws \CRM_Core_Exception |
db54ba06 | 6383 | */ |
59733b26 | 6384 | protected function prepareOrderBy($sort, $sortOrder) { |
fbf68a81 | 6385 | $orderByArray = []; |
6386 | $orderBy = ''; | |
6387 | ||
6388 | if (CRM_Core_Config::singleton()->includeOrderByClause || | |
db54ba06 TO |
6389 | isset($this->_distinctComponentClause) |
6390 | ) { | |
6391 | if ($sort) { | |
6392 | if (is_string($sort)) { | |
6393 | $orderBy = $sort; | |
6394 | } | |
6395 | else { | |
6396 | $orderBy = trim($sort->orderBy()); | |
6397 | } | |
6398 | // Deliberately remove the backticks again, as they mess up the evil | |
6399 | // string munging below. This balanced by re-escaping before use. | |
6400 | $orderBy = str_replace('`', '', $orderBy); | |
6401 | ||
6402 | if (!empty($orderBy)) { | |
6403 | // this is special case while searching for | |
6404 | // change log CRM-1718 | |
6405 | if (preg_match('/sort_name/i', $orderBy)) { | |
6406 | $orderBy = str_replace('sort_name', 'contact_a.sort_name', $orderBy); | |
6407 | } | |
6408 | ||
db54ba06 | 6409 | if ($sortOrder) { |
fbf68a81 | 6410 | $orderBy .= " $sortOrder"; |
db54ba06 TO |
6411 | } |
6412 | ||
6413 | // always add contact_a.id to the ORDER clause | |
6414 | // so the order is deterministic | |
fbf68a81 | 6415 | if (strpos('contact_a.id', $orderBy) === FALSE) { |
6416 | $orderBy .= ", contact_a.id"; | |
db54ba06 TO |
6417 | } |
6418 | } | |
6419 | } | |
db54ba06 | 6420 | else { |
fbf68a81 | 6421 | $orderBy = " contact_a.sort_name ASC, contact_a.id"; |
db54ba06 TO |
6422 | } |
6423 | } | |
52cda5dc | 6424 | if (!$orderBy) { |
59733b26 | 6425 | return NULL; |
252f0858 | 6426 | } |
6427 | // Remove this here & add it at the end for simplicity. | |
fbf68a81 | 6428 | $order = trim($orderBy); |
52cda5dc | 6429 | $orderByArray = explode(',', $order); |
db54ba06 | 6430 | |
9d5c7f14 | 6431 | foreach ($orderByArray as $orderByClause) { |
6432 | $orderByClauseParts = explode(' ', trim($orderByClause)); | |
6433 | $field = $orderByClauseParts[0]; | |
2e1f50d6 | 6434 | $direction = $orderByClauseParts[1] ?? 'asc'; |
7f594311 | 6435 | $fieldSpec = $this->getMetadataForRealField($field); |
252f0858 | 6436 | |
708ce91f | 6437 | // This is a hacky add-in for primary address joins. Feel free to iterate as it is unit tested. |
6438 | // @todo much more cleanup on location handling in addHierarchical elements. Potentially | |
6439 | // add keys to $this->fields to represent the actual keys for locations. | |
6440 | if (empty($fieldSpec) && substr($field, 0, 2) === '1-') { | |
6441 | $fieldSpec = $this->getMetadataForField(substr($field, 2)); | |
6442 | $this->addAddressTable('1-' . str_replace('civicrm_', '', $fieldSpec['table_name']), 'is_primary = 1'); | |
6443 | } | |
6444 | ||
7f594311 | 6445 | if ($this->_returnProperties === []) { |
6446 | if (!empty($fieldSpec['table_name']) && !isset($this->_tables[$fieldSpec['table_name']])) { | |
6447 | $this->_tables[$fieldSpec['table_name']] = 1; | |
6448 | $order = $fieldSpec['where'] . ' ' . $direction; | |
6449 | } | |
252f0858 | 6450 | |
7f594311 | 6451 | } |
1ddfb09e | 6452 | $cfID = CRM_Core_BAO_CustomField::getKeyID($field); |
6453 | // add to cfIDs array if not present | |
6454 | if (!empty($cfID) && !array_key_exists($cfID, $this->_cfIDs)) { | |
be2fb01f | 6455 | $this->_cfIDs[$cfID] = []; |
1ddfb09e | 6456 | $this->_customQuery = new CRM_Core_BAO_CustomQuery($this->_cfIDs, TRUE, $this->_locationSpecificCustomFields); |
6457 | $this->_customQuery->query(); | |
6458 | $this->_select = array_merge($this->_select, $this->_customQuery->_select); | |
6459 | $this->_tables = array_merge($this->_tables, $this->_customQuery->_tables); | |
6460 | } | |
6461 | ||
6462 | // By replacing the join to the option value table with the mysql construct | |
6463 | // ORDER BY field('contribution_status_id', 2,1,4) | |
6464 | // we can remove a join. In the case of the option value join it is | |
6465 | /// a join known to cause slow queries. | |
6466 | // @todo cover other pseudoconstant types. Limited to option group ones & Foreign keys | |
6467 | // matching an id+name parrern in the | |
6468 | // first instance for scope reasons. They require slightly different handling as the column (label) | |
6469 | // is not declared for them. | |
6470 | // @todo so far only integer fields are being handled. If we add string fields we need to look at | |
6471 | // escaping. | |
6472 | $pseudoConstantMetadata = CRM_Utils_Array::value('pseudoconstant', $fieldSpec, FALSE); | |
6473 | if (!empty($pseudoConstantMetadata) | |
6474 | ) { | |
6475 | if (!empty($pseudoConstantMetadata['optionGroupName']) | |
6476 | || $this->isPseudoFieldAnFK($fieldSpec) | |
6477 | ) { | |
1dd6ae65 D |
6478 | // dev/core#1305 @todo this is not the right thing to do but for now avoid fatal error |
6479 | if (empty($fieldSpec['bao'])) { | |
6480 | continue; | |
6481 | } | |
1c2683cd CW |
6482 | $sortedOptions = $fieldSpec['bao']::buildOptions($fieldSpec['name']); |
6483 | natcasesort($sortedOptions); | |
1ddfb09e | 6484 | $fieldIDsInOrder = implode(',', array_keys($sortedOptions)); |
6485 | // Pretty sure this validation ALSO happens in the order clause & this can't be reached but... | |
6486 | // this might give some early warning. | |
6487 | CRM_Utils_Type::validate($fieldIDsInOrder, 'CommaSeparatedIntegers'); | |
452a21f7 PF |
6488 | // use where if it's set to fully qualify ambiguous column names |
6489 | // i.e. civicrm_contribution.contribution_status_id instead of contribution_status_id | |
6490 | $pseudoColumnName = $fieldSpec['where'] ?? $fieldSpec['name']; | |
6491 | $order = str_replace("$field", "field($pseudoColumnName,$fieldIDsInOrder)", $order); | |
1ddfb09e | 6492 | } |
6493 | //CRM-12565 add "`" around $field if it is a pseudo constant | |
6494 | // This appears to be for 'special' fields like locations with appended numbers or hyphens .. maybe. | |
6495 | if (!empty($pseudoConstantMetadata['element']) && $pseudoConstantMetadata['element'] == $field) { | |
6496 | $order = str_replace($field, "`{$field}`", $order); | |
6497 | } | |
db54ba06 TO |
6498 | } |
6499 | } | |
6500 | ||
9d5c7f14 | 6501 | $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode); |
6502 | $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, $this->_primaryLocation, $this->_mode); | |
6503 | ||
db54ba06 TO |
6504 | // The above code relies on crazy brittle string manipulation of a peculiarly-encoded ORDER BY |
6505 | // clause. But this magic helper which forgivingly reescapes ORDER BY. | |
9d5c7f14 | 6506 | if ($order) { |
252f0858 | 6507 | $order = CRM_Utils_Type::escape($order, 'MysqlOrderBy'); |
59733b26 | 6508 | return ' ORDER BY ' . $order; |
db54ba06 | 6509 | } |
db54ba06 TO |
6510 | } |
6511 | ||
3875e6b6 | 6512 | /** |
6513 | * Convert a string of group IDs to a string of group labels. | |
6514 | * | |
6515 | * The original string may include duplicates and groups the user does not have | |
6516 | * permission to see. | |
6517 | * | |
6518 | * @param CRM_Core_DAO $dao | |
6519 | * @param string $val | |
6520 | * | |
6521 | * @return string | |
6522 | */ | |
6523 | public function convertGroupIDStringToLabelString(&$dao, $val) { | |
6524 | $groupIDs = explode(',', $val); | |
3875e6b6 | 6525 | // Note that groups that the user does not have permission to will be excluded (good). |
6d054a8e | 6526 | $groups = array_intersect_key(CRM_Core_PseudoConstant::group(), array_flip($groupIDs)); |
3875e6b6 | 6527 | return implode(', ', $groups); |
962f4484 | 6528 | |
6529 | } | |
6530 | ||
6531 | /** | |
6532 | * Set the qill and where properties for a field. | |
6533 | * | |
6534 | * This function is intended as a short-term function to encourage refactoring | |
6535 | * & re-use - but really we should just have less special-casing. | |
6536 | * | |
6537 | * @param string $name | |
6538 | * @param string $op | |
6539 | * @param string|array $value | |
6540 | * @param string $grouping | |
b832662c | 6541 | * @param array $field |
6542 | * | |
6543 | * @throws \CRM_Core_Exception | |
962f4484 | 6544 | */ |
6545 | public function setQillAndWhere($name, $op, $value, $grouping, $field) { | |
6546 | $this->_where[$grouping][] = self::buildClause("contact_a.{$name}", $op, $value); | |
6547 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op); | |
be2fb01f | 6548 | $this->_qill[$grouping][] = ts("%1 %2 %3", [ |
962f4484 | 6549 | 1 => $field['title'], |
6550 | 2 => $qillop, | |
6551 | 3 => $qillVal, | |
be2fb01f | 6552 | ]); |
3875e6b6 | 6553 | } |
6554 | ||
86ab13b7 | 6555 | /** |
6556 | * Has the pseudoconstant of the field been requested. | |
6557 | * | |
6558 | * For example if the field is payment_instrument_id then it | |
6559 | * has been requested if either payment_instrument_id or payment_instrument | |
6560 | * have been requested. Payment_instrument is the option groun name field value. | |
6561 | * | |
6562 | * @param array $field | |
7bd16b05 | 6563 | * @param string $fieldName |
6564 | * The unique name of the field - ie. the one it will be aliased to in the query. | |
86ab13b7 | 6565 | * |
6566 | * @return bool | |
6567 | */ | |
7bd16b05 | 6568 | private function pseudoConstantNameIsInReturnProperties($field, $fieldName = NULL) { |
124f3c1b | 6569 | $realField = $this->getMetadataForRealField($fieldName); |
6570 | if (!isset($realField['pseudoconstant'])) { | |
6571 | return FALSE; | |
6572 | } | |
6573 | $pseudoConstant = $realField['pseudoconstant']; | |
6574 | if (empty($pseudoConstant['optionGroupName']) && | |
6575 | CRM_Utils_Array::value('labelColumn', $pseudoConstant) !== 'name') { | |
6576 | // We are increasing our pseudoconstant handling - but still very cautiously, | |
6577 | // hence the check for labelColumn === name | |
86ab13b7 | 6578 | return FALSE; |
6579 | } | |
86ab13b7 | 6580 | |
f3acfdd9 | 6581 | if (!empty($pseudoConstant['optionGroupName']) && !empty($this->_returnProperties[$pseudoConstant['optionGroupName']])) { |
86ab13b7 | 6582 | return TRUE; |
6583 | } | |
f3acfdd9 | 6584 | if (!empty($this->_returnProperties[$fieldName])) { |
7bd16b05 | 6585 | return TRUE; |
6586 | } | |
6587 | // Is this still required - the above goes off the unique name. Test with things like | |
5c9e101f | 6588 | // communication_preferences & prefix_id. |
f3acfdd9 | 6589 | if (!empty($this->_returnProperties[$field['name']])) { |
86ab13b7 | 6590 | return TRUE; |
6591 | } | |
6592 | return FALSE; | |
6593 | } | |
6594 | ||
f946d152 | 6595 | /** |
6596 | * Get Select Clause. | |
6597 | * | |
6598 | * @return string | |
6599 | */ | |
6600 | public function getSelect() { | |
b832662c | 6601 | $select = 'SELECT '; |
f946d152 | 6602 | if (isset($this->_distinctComponentClause)) { |
6603 | $select .= "{$this->_distinctComponentClause}, "; | |
6604 | } | |
6605 | $select .= implode(', ', $this->_select); | |
6606 | return $select; | |
6607 | } | |
6608 | ||
8ef6eb78 | 6609 | /** |
6610 | * Add basic statistics to the summary. | |
6611 | * | |
6612 | * @param array $summary | |
6613 | * @param string $where | |
6614 | * @param string $from | |
6615 | * | |
6616 | * @return array | |
041ecc95 | 6617 | * @throws \CRM_Core_Exception |
8ef6eb78 | 6618 | */ |
6619 | protected function addBasicStatsToSummary(&$summary, $where, $from) { | |
fb75fcb0 | 6620 | $summary['total']['count'] = 0; |
6621 | $summary['total']['amount'] = $summary['total']['avg'] = []; | |
8ef6eb78 | 6622 | |
6623 | $query = " | |
6624 | SELECT COUNT( conts.total_amount ) as total_count, | |
6625 | SUM( conts.total_amount ) as total_amount, | |
6626 | AVG( conts.total_amount ) as total_avg, | |
6627 | conts.currency as currency | |
6628 | FROM ( | |
6629 | SELECT civicrm_contribution.total_amount, COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count, | |
6630 | civicrm_contribution.currency | |
6631 | $from | |
6632 | $where AND civicrm_contribution.contribution_status_id = 1 | |
6633 | GROUP BY civicrm_contribution.id | |
6634 | ) as conts | |
6635 | GROUP BY currency"; | |
6636 | ||
6637 | $dao = CRM_Core_DAO::executeQuery($query); | |
6638 | ||
8ef6eb78 | 6639 | while ($dao->fetch()) { |
6640 | $summary['total']['count'] += $dao->total_count; | |
6641 | $summary['total']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency); | |
6642 | $summary['total']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency); | |
6643 | } | |
bfac1665 | 6644 | |
261b72cf | 6645 | if (!empty($summary['total']['amount'])) { |
6646 | $summary['total']['amount'] = implode(', ', $summary['total']['amount']); | |
6647 | $summary['total']['avg'] = implode(', ', $summary['total']['avg']); | |
6648 | } | |
6649 | else { | |
b64b7a9e | 6650 | $summary['total']['amount'] = $summary['total']['avg'] = 0; |
261b72cf | 6651 | } |
8ef6eb78 | 6652 | return $summary; |
6653 | } | |
6654 | ||
6d68fd76 | 6655 | /** |
6656 | * Add basic soft credit statistics to summary array. | |
6657 | * | |
6658 | * @param array $summary | |
6659 | * @param string $where | |
6660 | * @param string $from | |
b832662c | 6661 | * |
6662 | * @throws \CRM_Core_Exception | |
6d68fd76 | 6663 | */ |
6664 | protected function addBasicSoftCreditStatsToStats(&$summary, $where, $from) { | |
6665 | $query = " | |
6666 | SELECT COUNT( conts.total_amount ) as total_count, | |
6667 | SUM( conts.total_amount ) as total_amount, | |
6668 | AVG( conts.total_amount ) as total_avg, | |
6669 | conts.currency as currency | |
6670 | FROM ( | |
6671 | SELECT civicrm_contribution_soft.amount as total_amount, civicrm_contribution_soft.currency | |
6672 | $from | |
6673 | $where AND civicrm_contribution.contribution_status_id = 1 AND civicrm_contribution_soft.id IS NOT NULL | |
6674 | GROUP BY civicrm_contribution_soft.id | |
6675 | ) as conts | |
6676 | GROUP BY currency"; | |
6677 | ||
6678 | $dao = CRM_Core_DAO::executeQuery($query); | |
6679 | $summary['soft_credit']['count'] = 0; | |
6680 | $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = []; | |
6681 | while ($dao->fetch()) { | |
6682 | $summary['soft_credit']['count'] += $dao->total_count; | |
6683 | $summary['soft_credit']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency); | |
6684 | $summary['soft_credit']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency); | |
6685 | } | |
6686 | if (!empty($summary['soft_credit']['amount'])) { | |
6687 | $summary['soft_credit']['amount'] = implode(', ', $summary['soft_credit']['amount']); | |
6688 | $summary['soft_credit']['avg'] = implode(', ', $summary['soft_credit']['avg']); | |
6689 | } | |
6690 | else { | |
6691 | $summary['soft_credit']['amount'] = $summary['soft_credit']['avg'] = 0; | |
6692 | } | |
6693 | } | |
6694 | ||
386fce20 | 6695 | /** |
6696 | * Add basic stats about cancelled contributions to the summary. | |
6697 | * | |
6698 | * @param array $summary | |
6699 | * @param string $where | |
6700 | * @param string $from | |
b832662c | 6701 | * |
6702 | * @throws \CRM_Core_Exception | |
386fce20 | 6703 | */ |
6704 | protected function addBasicCancelStatsToSummary(&$summary, $where, $from) { | |
6705 | $query = " | |
6706 | SELECT COUNT( conts.total_amount ) as cancel_count, | |
6707 | SUM( conts.total_amount ) as cancel_amount, | |
6708 | AVG( conts.total_amount ) as cancel_avg, | |
6709 | conts.currency as currency | |
6710 | FROM ( | |
6711 | SELECT civicrm_contribution.total_amount, civicrm_contribution.currency | |
6712 | $from | |
1a7029b8 | 6713 | $where AND civicrm_contribution.cancel_date IS NOT NULL |
386fce20 | 6714 | GROUP BY civicrm_contribution.id |
6715 | ) as conts | |
6716 | GROUP BY currency"; | |
6717 | ||
6718 | $dao = CRM_Core_DAO::executeQuery($query); | |
d54658f6 | 6719 | $summary['cancel'] = ['count' => 0, 'amount' => 0, 'avg' => 0]; |
386fce20 | 6720 | if ($dao->N <= 1) { |
6721 | if ($dao->fetch()) { | |
6722 | $summary['cancel']['count'] = $dao->cancel_count; | |
6723 | $summary['cancel']['amount'] = CRM_Utils_Money::format($dao->cancel_amount, $dao->currency); | |
6724 | $summary['cancel']['avg'] = CRM_Utils_Money::format($dao->cancel_avg, $dao->currency); | |
6725 | } | |
6726 | } | |
6727 | else { | |
6728 | $summary['cancel']['count'] = 0; | |
6729 | $summary['cancel']['amount'] = $summary['cancel']['avg'] = []; | |
6730 | while ($dao->fetch()) { | |
6731 | $summary['cancel']['count'] += $dao->cancel_count; | |
6732 | $summary['cancel']['amount'][] = CRM_Utils_Money::format($dao->cancel_amount, $dao->currency); | |
6733 | $summary['cancel']['avg'][] = CRM_Utils_Money::format($dao->cancel_avg, $dao->currency); | |
6734 | } | |
6735 | $summary['cancel']['amount'] = implode(', ', $summary['cancel']['amount']); | |
6736 | $summary['cancel']['avg'] = implode(', ', $summary['cancel']['avg']); | |
6737 | } | |
6738 | } | |
6739 | ||
89d7bb76 | 6740 | /** |
89d7bb76 | 6741 | * Create the sql query for an contact search. |
6742 | * | |
6743 | * @param int $offset | |
6744 | * The offset for the query. | |
6745 | * @param int $rowCount | |
6746 | * The number of rows to return. | |
6747 | * @param string|CRM_Utils_Sort $sort | |
6748 | * The order by string. | |
6749 | * @param bool $count | |
6750 | * Is this a count only query ?. | |
6751 | * @param bool $includeContactIds | |
6752 | * Should we include contact ids?. | |
6753 | * @param bool $sortByChar | |
6754 | * If true returns the distinct array of first characters for search results. | |
6755 | * @param bool $groupContacts | |
6756 | * If true, return only the contact ids. | |
6757 | * @param string $additionalWhereClause | |
6758 | * If the caller wants to further restrict the search (used for components). | |
6759 | * @param null $sortOrder | |
6760 | * @param string $additionalFromClause | |
6761 | * Should be clause with proper joins, effective to reduce where clause load. | |
6762 | * | |
6763 | * @param bool $skipOrderAndLimit | |
9de9a433 | 6764 | * |
89d7bb76 | 6765 | * @return string |
9de9a433 | 6766 | * |
6767 | * @throws \CRM_Core_Exception | |
89d7bb76 | 6768 | */ |
6769 | public function getSearchSQL( | |
6770 | $offset = 0, $rowCount = 0, $sort = NULL, | |
6771 | $count = FALSE, $includeContactIds = FALSE, | |
6772 | $sortByChar = FALSE, $groupContacts = FALSE, | |
6773 | $additionalWhereClause = NULL, $sortOrder = NULL, | |
6774 | $additionalFromClause = NULL, $skipOrderAndLimit = FALSE) { | |
fe3416bf | 6775 | |
6776 | $sqlParts = $this->getSearchSQLParts($offset, $rowCount, $sort, $count, $includeContactIds, $sortByChar, $groupContacts, $additionalWhereClause, $sortOrder, $additionalFromClause); | |
6777 | ||
caefed7d MWMC |
6778 | if ($sortByChar) { |
6779 | CRM_Core_Error::deprecatedFunctionWarning('sort by char is deprecated - use alphabetQuery method'); | |
6780 | $sqlParts['order_by'] = 'ORDER BY sort_name asc'; | |
6781 | } | |
6782 | ||
fe3416bf | 6783 | if ($skipOrderAndLimit) { |
6784 | CRM_Core_Error::deprecatedFunctionWarning('skipOrderAndLimit is deprected - call getSearchSQLParts & construct it in the calling function'); | |
6785 | $query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']}"; | |
6786 | } | |
6787 | else { | |
6788 | $query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']} {$sqlParts['order_by']} {$sqlParts['limit']}"; | |
6789 | } | |
6790 | return $query; | |
6791 | } | |
6792 | ||
6793 | /** | |
6794 | * Get the component parts of the search query as an array. | |
6795 | * | |
6796 | * @param int $offset | |
6797 | * The offset for the query. | |
6798 | * @param int $rowCount | |
6799 | * The number of rows to return. | |
6800 | * @param string|CRM_Utils_Sort $sort | |
6801 | * The order by string. | |
6802 | * @param bool $count | |
6803 | * Is this a count only query ?. | |
6804 | * @param bool $includeContactIds | |
6805 | * Should we include contact ids?. | |
6806 | * @param bool $sortByChar | |
6807 | * If true returns the distinct array of first characters for search results. | |
6808 | * @param bool $groupContacts | |
6809 | * If true, return only the contact ids. | |
6810 | * @param string $additionalWhereClause | |
6811 | * If the caller wants to further restrict the search (used for components). | |
6812 | * @param null $sortOrder | |
6813 | * @param string $additionalFromClause | |
6814 | * Should be clause with proper joins, effective to reduce where clause load. | |
6815 | * | |
6816 | * @return array | |
b832662c | 6817 | * @throws \CRM_Core_Exception |
fe3416bf | 6818 | */ |
6819 | public function getSearchSQLParts($offset = 0, $rowCount = 0, $sort = NULL, | |
6820 | $count = FALSE, $includeContactIds = FALSE, | |
6821 | $sortByChar = FALSE, $groupContacts = FALSE, | |
6822 | $additionalWhereClause = NULL, $sortOrder = NULL, | |
6823 | $additionalFromClause = NULL) { | |
89d7bb76 | 6824 | if ($includeContactIds) { |
6825 | $this->_includeContactIds = TRUE; | |
6826 | $this->_whereClause = $this->whereClause(); | |
6827 | } | |
89d7bb76 | 6828 | $onlyDeleted = in_array([ |
6829 | 'deleted_contacts', | |
6830 | '=', | |
6831 | '1', | |
6832 | '0', | |
69078420 | 6833 | '0', |
89d7bb76 | 6834 | ], $this->_params); |
6835 | ||
6836 | // if we’re explicitly looking for a certain contact’s contribs, events, etc. | |
6837 | // and that contact happens to be deleted, set $onlyDeleted to true | |
6838 | foreach ($this->_params as $values) { | |
9c1bc317 CW |
6839 | $name = $values[0] ?? NULL; |
6840 | $op = $values[1] ?? NULL; | |
6841 | $value = $values[2] ?? NULL; | |
9de9a433 | 6842 | if ($name === 'contact_id' and $op === '=') { |
89d7bb76 | 6843 | if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'is_deleted')) { |
6844 | $onlyDeleted = TRUE; | |
6845 | } | |
6846 | break; | |
6847 | } | |
6848 | } | |
6849 | ||
6850 | // building the query string | |
6851 | $groupBy = $groupByCols = NULL; | |
6852 | if (!$count) { | |
6853 | if (isset($this->_groupByComponentClause)) { | |
6854 | $groupByCols = preg_replace('/^GROUP BY /', '', trim($this->_groupByComponentClause)); | |
6855 | $groupByCols = explode(', ', $groupByCols); | |
6856 | } | |
6857 | elseif ($this->_useGroupBy) { | |
6858 | $groupByCols = ['contact_a.id']; | |
6859 | } | |
6860 | } | |
6861 | if ($this->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY && (!$count)) { | |
6862 | $groupByCols = ['civicrm_activity.id']; | |
6863 | } | |
6864 | if (!empty($groupByCols)) { | |
6865 | $groupBy = " GROUP BY " . implode(', ', $groupByCols); | |
6866 | } | |
6867 | ||
fe3416bf | 6868 | $order = $orderBy = ''; |
89d7bb76 | 6869 | if (!$count) { |
caefed7d | 6870 | if (!$sortByChar) { |
59733b26 | 6871 | $order = $this->prepareOrderBy($sort, $sortOrder); |
caefed7d | 6872 | } |
89d7bb76 | 6873 | } |
caefed7d MWMC |
6874 | // Cases where we are disabling FGB (FULL_GROUP_BY_MODE): |
6875 | // 1. When GROUP BY columns are present then disable FGB otherwise it demands to add ORDER BY columns in GROUP BY and eventually in SELECT | |
89d7bb76 | 6876 | // clause. This will impact the search query output. |
caefed7d | 6877 | $disableFullGroupByMode = (!empty($groupBy) || $groupContacts); |
89d7bb76 | 6878 | |
6879 | if ($disableFullGroupByMode) { | |
6880 | CRM_Core_DAO::disableFullGroupByMode(); | |
6881 | } | |
6882 | ||
6883 | // CRM-15231 | |
6884 | $this->_sort = $sort; | |
6885 | ||
6886 | //CRM-15967 | |
6887 | $this->includePseudoFieldsJoin($sort); | |
6888 | ||
6889 | list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted); | |
6890 | ||
6891 | if ($additionalWhereClause) { | |
6892 | $where = $where . ' AND ' . $additionalWhereClause; | |
6893 | } | |
6894 | ||
6895 | //additional from clause should be w/ proper joins. | |
6896 | if ($additionalFromClause) { | |
6897 | $from .= "\n" . $additionalFromClause; | |
6898 | } | |
6899 | ||
6900 | // if we are doing a transform, do it here | |
6901 | // use the $from, $where and $having to get the contact ID | |
6902 | if ($this->_displayRelationshipType) { | |
6903 | $this->filterRelatedContacts($from, $where, $having); | |
6904 | } | |
fe3416bf | 6905 | $limit = (!$count && $rowCount) ? " LIMIT " . CRM_Utils_Type::escape($offset, 'Int') . ", " . CRM_Utils_Type::escape($rowCount, 'Int') : ''; |
6906 | ||
6907 | return [ | |
6908 | 'select' => $select, | |
6909 | 'from' => $from, | |
6910 | 'where' => $where, | |
6911 | 'order_by' => $order, | |
6912 | 'group_by' => $groupBy, | |
6913 | 'having' => $having, | |
6914 | 'limit' => $limit, | |
6915 | ]; | |
89d7bb76 | 6916 | } |
6917 | ||
a0090e6b | 6918 | /** |
6919 | * Get the metadata for a given field. | |
6920 | * | |
6921 | * @param string $fieldName | |
6922 | * | |
6923 | * @return array | |
6924 | */ | |
6925 | protected function getMetadataForField($fieldName) { | |
6926 | if ($fieldName === 'contact_a.id') { | |
6927 | // This seems to be the only anomaly. | |
6928 | $fieldName = 'id'; | |
6929 | } | |
2e1f50d6 CW |
6930 | $pseudoField = $this->_pseudoConstantsSelect[$fieldName] ?? []; |
6931 | $field = $this->_fields[$fieldName] ?? $pseudoField; | |
a0090e6b | 6932 | $field = array_merge($field, $pseudoField); |
6933 | if (!empty($field) && empty($field['name'])) { | |
6934 | // standardising field formatting here - over time we can phase out variants. | |
6935 | // all paths using this currently unit tested | |
124f3c1b | 6936 | $field['name'] = CRM_Utils_Array::value('field_name', $field, CRM_Utils_Array::value('idCol', $field, $fieldName)); |
a0090e6b | 6937 | } |
6938 | return $field; | |
6939 | } | |
6940 | ||
6941 | /** | |
6942 | * Get the metadata for a given field, returning the 'real field' if it is a pseudofield. | |
6943 | * | |
6944 | * @param string $fieldName | |
6945 | * | |
6946 | * @return array | |
6947 | */ | |
6b051312 | 6948 | public function getMetadataForRealField($fieldName) { |
a0090e6b | 6949 | $field = $this->getMetadataForField($fieldName); |
124f3c1b | 6950 | if (!empty($field['is_pseudofield_for'])) { |
6951 | $field = $this->getMetadataForField($field['is_pseudofield_for']); | |
6952 | $field['pseudofield_name'] = $fieldName; | |
6953 | } | |
6954 | elseif (!empty($field['pseudoconstant'])) { | |
6955 | if (!empty($field['pseudoconstant']['optionGroupName'])) { | |
6956 | $field['pseudofield_name'] = $field['pseudoconstant']['optionGroupName']; | |
6957 | if (empty($field['table_name'])) { | |
6958 | if (!empty($field['where'])) { | |
6959 | $field['table_name'] = explode('.', $field['where'])[0]; | |
6960 | } | |
6961 | else { | |
6962 | $field['table_name'] = 'civicrm_contact'; | |
6963 | } | |
6964 | } | |
6965 | } | |
6966 | } | |
6967 | return $field; | |
a0090e6b | 6968 | } |
6969 | ||
8df0c9e0 | 6970 | /** |
6971 | * Get the field datatype, using the type in the database rather than the pseudofield, if a pseudofield. | |
6972 | * | |
6973 | * @param string $fieldName | |
6974 | * | |
6975 | * @return string | |
6976 | */ | |
6977 | public function getDataTypeForRealField($fieldName) { | |
6978 | return CRM_Utils_Type::typeToString($this->getMetadataForRealField($fieldName)['type']); | |
6979 | } | |
6980 | ||
07f3c42b | 6981 | /** |
6982 | * If we have a field that is better rendered via the pseudoconstant handled them here. | |
6983 | * | |
6984 | * Rather than joining in the additional table we render the option value on output. | |
6985 | * | |
6986 | * @todo - so far this applies to a narrow range of pseudocontants. We are adding them | |
6987 | * carefully with test coverage but aim to extend. | |
6988 | * | |
07f3c42b | 6989 | * @param string $name |
07f3c42b | 6990 | */ |
124f3c1b | 6991 | protected function addPseudoconstantFieldToSelect($name) { |
6992 | $field = $this->getMetadataForRealField($name); | |
6993 | $realFieldName = $field['name']; | |
9c1bc317 | 6994 | $pseudoFieldName = $field['pseudofield_name'] ?? NULL; |
124f3c1b | 6995 | if ($pseudoFieldName) { |
6996 | // @todo - we don't really need to build this array now we have metadata more available with getMetadataForField fn. | |
6997 | $this->_pseudoConstantsSelect[$pseudoFieldName] = [ | |
6998 | 'pseudoField' => $pseudoFieldName, | |
6999 | 'idCol' => $realFieldName, | |
7000 | 'field_name' => $field['name'], | |
7001 | 'bao' => $field['bao'], | |
7002 | 'pseudoconstant' => $field['pseudoconstant'], | |
7003 | ]; | |
7004 | } | |
7005 | ||
7006 | $this->_tables[$field['table_name']] = 1; | |
7007 | $this->_element[$realFieldName] = 1; | |
7008 | $this->_select[$field['name']] = str_replace('civicrm_contact.', 'contact_a.', "{$field['where']} as `$realFieldName`"); | |
7009 | } | |
7010 | ||
7011 | /** | |
7012 | * Is this pseudofield a foreign key constraint. | |
7013 | * | |
7014 | * We are trying to cautiously expand our pseudoconstant handling. This check allows us | |
7015 | * to extend to a narrowly defined type (and then only if the pseudofield is in the fields | |
7016 | * array which is done for contributions which are mostly handled as pseudoconstants. | |
7017 | * | |
7018 | * @param $fieldSpec | |
7019 | * | |
7020 | * @return bool | |
7021 | */ | |
7022 | protected function isPseudoFieldAnFK($fieldSpec) { | |
7023 | if (empty($fieldSpec['FKClassName']) | |
7024 | || CRM_Utils_Array::value('keyColumn', $fieldSpec['pseudoconstant']) !== 'id' | |
7025 | || CRM_Utils_Array::value('labelColumn', $fieldSpec['pseudoconstant']) !== 'name') { | |
7026 | return FALSE; | |
7027 | } | |
7028 | return TRUE; | |
07f3c42b | 7029 | } |
7030 | ||
8fc9f99a | 7031 | /** |
7032 | * Is the field a relative date field. | |
7033 | * | |
7034 | * @param string $fieldName | |
7035 | * | |
7036 | * @return bool | |
7037 | */ | |
7038 | protected function isARelativeDateField($fieldName) { | |
7039 | if (substr($fieldName, -9, 9) !== '_relative') { | |
7040 | return FALSE; | |
7041 | } | |
7042 | $realField = substr($fieldName, 0, strlen($fieldName) - 9); | |
7043 | return isset($this->_fields[$realField]); | |
7044 | } | |
7045 | ||
33a17d7e | 7046 | /** |
7047 | * Get the specifications for the field, if available. | |
7048 | * | |
7049 | * @param string $fieldName | |
7050 | * Fieldname as displayed on the form. | |
7051 | * | |
7052 | * @return array | |
7053 | */ | |
7054 | public function getFieldSpec($fieldName) { | |
7055 | if (isset($this->_fields[$fieldName])) { | |
6b051312 | 7056 | $fieldSpec = $this->_fields[$fieldName]; |
7057 | if (!empty($fieldSpec['is_pseudofield_for'])) { | |
7058 | $fieldSpec = array_merge($this->_fields[$fieldSpec['is_pseudofield_for']], $this->_fields[$fieldName]); | |
7059 | } | |
7060 | return $fieldSpec; | |
33a17d7e | 7061 | } |
7062 | $lowFieldName = str_replace('_low', '', $fieldName); | |
7063 | if (isset($this->_fields[$lowFieldName])) { | |
7064 | return array_merge($this->_fields[$lowFieldName], ['field_name' => $lowFieldName]); | |
7065 | } | |
7066 | $highFieldName = str_replace('_high', '', $fieldName); | |
7067 | if (isset($this->_fields[$highFieldName])) { | |
7068 | return array_merge($this->_fields[$highFieldName], ['field_name' => $highFieldName]); | |
7069 | } | |
7070 | return []; | |
7071 | } | |
7072 | ||
7073 | public function buildWhereForDate() { | |
7074 | ||
7075 | } | |
7076 | ||
7077 | /** | |
7078 | * Is the field a relative date field. | |
7079 | * | |
7080 | * @param string $fieldName | |
7081 | * | |
7082 | * @return bool | |
7083 | */ | |
7084 | protected function isADateRangeField($fieldName) { | |
7085 | if (substr($fieldName, -4, 4) !== '_low' && substr($fieldName, -5, 5) !== '_high') { | |
7086 | return FALSE; | |
7087 | } | |
7088 | return !empty($this->getFieldSpec($fieldName)); | |
7089 | } | |
7090 | ||
8fc9f99a | 7091 | /** |
7092 | * @param $values | |
7093 | */ | |
7094 | protected function buildRelativeDateQuery(&$values) { | |
9c1bc317 | 7095 | $value = $values[2] ?? NULL; |
8fc9f99a | 7096 | if (empty($value)) { |
7097 | return; | |
7098 | } | |
7099 | $fieldName = substr($values[0], 0, strlen($values[0]) - 9); | |
7100 | $fieldSpec = $this->_fields[$fieldName]; | |
7101 | $tableName = $fieldSpec['table_name']; | |
7102 | $filters = CRM_Core_OptionGroup::values('relative_date_filters'); | |
9c1bc317 | 7103 | $grouping = $values[3] ?? NULL; |
7f175707 | 7104 | // If the table value is already set for a custom field it will be more nuanced than just '1'. |
7105 | $this->_tables[$tableName] = $this->_tables[$tableName] ?? 1; | |
7106 | $this->_whereTables[$tableName] = $this->_whereTables[$tableName] ?? 1; | |
8fc9f99a | 7107 | |
7108 | $dates = CRM_Utils_Date::getFromTo($value, NULL, NULL); | |
936ededf | 7109 | // Where end would be populated only if we are handling one of the weird ones with different from & to fields. |
7110 | $secondWhere = $fieldSpec['where_end'] ?? $fieldSpec['where']; | |
6245de60 JG |
7111 | |
7112 | $where = $fieldSpec['where']; | |
7113 | if ($fieldSpec['table_name'] === 'civicrm_contact') { | |
7114 | // Special handling for contact table as it has a known alias in advanced search. | |
7115 | $where = str_replace('civicrm_contact.', 'contact_a.', $where); | |
7116 | $secondWhere = str_replace('civicrm_contact.', 'contact_a.', $secondWhere); | |
7117 | } | |
7118 | ||
c47a2365 | 7119 | $this->_qill[$grouping][] = $this->getQillForRelativeDateRange($dates[0], $dates[1], $fieldSpec['title'], $filters[$value]); |
7120 | if ($fieldName === 'relation_active_period_date') { | |
7121 | // Hack this to fix regression https://lab.civicrm.org/dev/core/issues/1592 | |
7122 | // Not sure the 'right' fix. | |
7123 | $this->_where[$grouping] = [self::getRelationshipActivePeriodClauses($dates[0], $dates[1], TRUE)]; | |
7124 | return; | |
7125 | } | |
7126 | ||
8fc9f99a | 7127 | if (empty($dates[0])) { |
7128 | // ie. no start date we only have end date | |
936ededf | 7129 | $this->_where[$grouping][] = $secondWhere . " <= '{$dates[1]}'"; |
8fc9f99a | 7130 | } |
7131 | elseif (empty($dates[1])) { | |
936ededf | 7132 | |
8fc9f99a | 7133 | // ie. no end date we only have start date |
6245de60 | 7134 | $this->_where[$grouping][] = $where . " >= '{$dates[0]}'"; |
8fc9f99a | 7135 | } |
7136 | else { | |
7137 | // we have start and end dates. | |
6245de60 JG |
7138 | if ($secondWhere !== $where) { |
7139 | $this->_where[$grouping][] = $where . ">= '{$dates[0]}' AND $secondWhere <='{$dates[1]}'"; | |
936ededf | 7140 | } |
7141 | else { | |
7142 | $this->_where[$grouping][] = $where . " BETWEEN '{$dates[0]}' AND '{$dates[1]}'"; | |
7143 | } | |
8fc9f99a | 7144 | } |
7145 | } | |
7146 | ||
33a17d7e | 7147 | /** |
7148 | * Build the query for a date field if it is a _high or _low field. | |
7149 | * | |
7150 | * @param $values | |
7151 | * | |
7152 | * @return bool | |
936ededf | 7153 | * @throws \CRM_Core_Exception |
33a17d7e | 7154 | */ |
7155 | public function buildDateRangeQuery($values) { | |
7156 | if ($this->isADateRangeField($values[0])) { | |
7157 | $fieldSpec = $this->getFieldSpec($values[0]); | |
7158 | $title = empty($fieldSpec['unique_title']) ? $fieldSpec['title'] : $fieldSpec['unique_title']; | |
7159 | $this->dateQueryBuilder($values, $fieldSpec['table_name'], $fieldSpec['field_name'], $fieldSpec['name'], $title); | |
7160 | return TRUE; | |
7161 | } | |
7162 | return FALSE; | |
7163 | } | |
7164 | ||
708ce91f | 7165 | /** |
7166 | * Add the address table into the query. | |
7167 | * | |
7168 | * @param string $tableKey | |
7169 | * @param string $joinCondition | |
7170 | * | |
7171 | * @return array | |
7172 | * - alias name | |
7173 | * - address join. | |
7174 | */ | |
7175 | protected function addAddressTable($tableKey, $joinCondition) { | |
7176 | $tName = "$tableKey-address"; | |
7177 | $aName = "`$tableKey-address`"; | |
7178 | $this->_select["{$tName}_id"] = "`$tName`.id as `{$tName}_id`"; | |
7179 | $this->_element["{$tName}_id"] = 1; | |
7180 | $addressJoin = "\nLEFT JOIN civicrm_address $aName ON ($aName.contact_id = contact_a.id AND $aName.$joinCondition)"; | |
7181 | $this->_tables[$tName] = $addressJoin; | |
7182 | ||
7183 | return [ | |
7184 | $aName, | |
69078420 | 7185 | $addressJoin, |
708ce91f | 7186 | ]; |
7187 | } | |
7188 | ||
d7570a7f | 7189 | /** |
7190 | * Get the clause for group status. | |
7191 | * | |
7192 | * @param int $grouping | |
7193 | * | |
7194 | * @return string | |
7195 | */ | |
7196 | protected function getGroupStatusClause($grouping) { | |
7197 | $statuses = $this->getSelectedGroupStatuses($grouping); | |
7198 | return "status IN (" . implode(', ', $statuses) . ")"; | |
7199 | } | |
7200 | ||
7201 | /** | |
7202 | * Get an array of the statuses that have been selected. | |
7203 | * | |
7204 | * @param string $grouping | |
7205 | * | |
7206 | * @return array | |
b832662c | 7207 | * |
7208 | * @throws \CRM_Core_Exception | |
d7570a7f | 7209 | */ |
7210 | protected function getSelectedGroupStatuses($grouping) { | |
7211 | $statuses = []; | |
7212 | $gcsValues = $this->getWhereValues('group_contact_status', $grouping); | |
7213 | if ($gcsValues && | |
7214 | is_array($gcsValues[2]) | |
7215 | ) { | |
7216 | foreach ($gcsValues[2] as $k => $v) { | |
7217 | if ($v) { | |
7218 | $statuses[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'"; | |
7219 | } | |
7220 | } | |
7221 | } | |
7222 | else { | |
7223 | $statuses[] = "'Added'"; | |
7224 | } | |
7225 | return $statuses; | |
7226 | } | |
7227 | ||
9178793e | 7228 | /** |
7229 | * Get the qill value for the field. | |
7230 | * | |
a43deb74 | 7231 | * @param string $name |
9178793e | 7232 | * @param array|int|string $value |
a43deb74 | 7233 | * @param string $op |
7234 | * @param array $fieldSpec | |
7235 | * @param string $labelOverride | |
7236 | * Label override, if required. | |
9178793e | 7237 | * |
7238 | * @return string | |
7239 | */ | |
a43deb74 | 7240 | public function getQillForField($name, $value, $op, $fieldSpec = [], $labelOverride = NULL): string { |
7241 | list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($fieldSpec['bao'] ?? NULL, $name, $value, $op); | |
9178793e | 7242 | return (string) ts("%1 %2 %3", [ |
a43deb74 | 7243 | 1 => $labelOverride ?? $fieldSpec['title'], |
9178793e | 7244 | 2 => $qillop, |
7245 | 3 => $qillVal, | |
7246 | ]); | |
7247 | } | |
7248 | ||
a43deb74 | 7249 | /** |
7250 | * Where handling for any field with adequately defined metadata. | |
7251 | * | |
7252 | * @param array $fieldSpec | |
7253 | * @param string $name | |
7254 | * @param string|array|int $value | |
7255 | * @param string $op | |
7256 | * @param string|int $grouping | |
7257 | * | |
7258 | * @throws \CRM_Core_Exception | |
7259 | */ | |
7260 | public function handleWhereFromMetadata($fieldSpec, $name, $value, $op, $grouping = 0) { | |
7261 | $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldSpec['where'], $op, $value, CRM_Utils_Type::typeToString($fieldSpec['type'])); | |
7262 | $this->_qill[$grouping][] = $this->getQillForField($name, $value, $op, $fieldSpec); | |
b4d8c550 | 7263 | if (!isset($this->_tables[$fieldSpec['table_name']])) { |
a43deb74 | 7264 | $this->_tables[$fieldSpec['table_name']] = 1; |
7265 | } | |
b4d8c550 | 7266 | if (!isset($this->_whereTables[$fieldSpec['table_name']])) { |
a43deb74 | 7267 | $this->_whereTables[$fieldSpec['table_name']] = 1; |
7268 | } | |
7269 | } | |
7270 | ||
c47a2365 | 7271 | /** |
7272 | * Get the qill for the relative date range. | |
7273 | * | |
7274 | * @param string|null $from | |
7275 | * @param string|null $to | |
7276 | * @param string $fieldTitle | |
7277 | * @param string $relativeRange | |
7278 | * | |
7279 | * @return string | |
7280 | */ | |
7281 | protected function getQillForRelativeDateRange($from, $to, string $fieldTitle, string $relativeRange): string { | |
7282 | if (!$from) { | |
7283 | return ts('%1 is ', [$fieldTitle]) . $relativeRange . ' (' . ts('to %1', [CRM_Utils_Date::customFormat($to)]) . ')'; | |
7284 | } | |
7285 | if (!$to) { | |
7286 | return ts('%1 is ', [$fieldTitle]) . $relativeRange . ' (' . ts('from %1', [CRM_Utils_Date::customFormat($from)]) . ')'; | |
7287 | } | |
7288 | return ts('%1 is ', [$fieldTitle]) . $relativeRange . ' (' . ts('between %1 and %2', [ | |
7289 | CRM_Utils_Date::customFormat($from), | |
7290 | CRM_Utils_Date::customFormat($to), | |
7291 | ]) . ')'; | |
7292 | } | |
7293 | ||
e7830dbe | 7294 | } |