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