Merge pull request #16135 from eileenmcnaughton/pay_total
[civicrm-core.git] / CRM / Contact / Selector.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
95cdcc0f 19 * Class is to retrieve and display a range of contacts that match the given criteria.
6a488035 20 *
95cdcc0f 21 * It is specifically for results of advanced search options.
6a488035
TO
22 */
23class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
24
450941a2
JK
25 const CACHE_SIZE = 500;
26
6a488035
TO
27 /**
28 * This defines two actions- View and Edit.
29 *
30 * @var array
6a488035 31 */
69078420 32 public static $_links = NULL;
6a488035
TO
33
34 /**
100fef9d 35 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
36 *
37 * @var array
6a488035 38 */
69078420 39 public static $_columnHeaders;
6a488035
TO
40
41 /**
42 * Properties of contact we're interested in displaying
43 * @var array
6a488035 44 */
69078420 45 public static $_properties = [
353ffa53
TO
46 'contact_id',
47 'contact_type',
48 'contact_sub_type',
49 'sort_name',
50 'street_address',
51 'city',
52 'state_province',
53 'postal_code',
54 'country',
55 'geo_code_1',
56 'geo_code_2',
57 'is_deceased',
58 'email',
59 'on_hold',
60 'phone',
61 'status',
62 'do_not_email',
63 'do_not_phone',
64 'do_not_mail',
be2fb01f 65 ];
6a488035
TO
66
67 /**
100fef9d 68 * FormValues is the array returned by exportValues called on
6a488035
TO
69 * the HTML_QuickForm_Controller for that page.
70 *
71 * @var array
6a488035
TO
72 */
73 public $_formValues;
74
75 /**
76 * The contextMenu
77 *
78 * @var array
6a488035
TO
79 */
80 protected $_contextMenu;
81
82 /**
100fef9d 83 * Params is the array in a value used by the search query creator
6a488035
TO
84 *
85 * @var array
6a488035
TO
86 */
87 public $_params;
88
89 /**
90 * The return properties used for search
91 *
92 * @var array
6a488035
TO
93 */
94 protected $_returnProperties;
95
96 /**
100fef9d 97 * Represent the type of selector
6a488035
TO
98 *
99 * @var int
6a488035
TO
100 */
101 protected $_action;
102
103 protected $_searchContext;
104
836eb043 105 /**
106 * Query object for this selector.
107 *
108 * @var CRM_Contact_BAO_Query
109 */
6a488035
TO
110 protected $_query;
111
836eb043 112 /**
113 * Get the query object for this selector.
114 *
115 * @return CRM_Contact_BAO_Query
116 */
117 public function getQueryObject() {
118 return $this->_query;
119 }
120
6a488035 121 /**
100fef9d 122 * Group id
6a488035
TO
123 *
124 * @var int
125 */
126 protected $_ufGroupID;
127
128 /**
100fef9d 129 * The public visible fields to be shown to the user
6a488035
TO
130 *
131 * @var array
6a488035
TO
132 */
133 protected $_fields;
134
135 /**
fe482240 136 * Class constructor.
6a488035 137 *
77b97be7 138 * @param $customSearchClass
77c5b619
TO
139 * @param array $formValues
140 * Array of form values imported.
141 * @param array $params
142 * Array of parameters for query.
77b97be7
EM
143 * @param null $returnProperties
144 * @param \const|int $action - action of search basic or advanced.
145 *
146 * @param bool $includeContactIds
147 * @param bool $searchDescendentGroups
148 * @param string $searchContext
149 * @param null $contextMenu
6a488035
TO
150 *
151 * @return CRM_Contact_Selector
6a488035 152 */
79d7553f 153 public function __construct(
6a488035
TO
154 $customSearchClass,
155 $formValues = NULL,
156 $params = NULL,
157 $returnProperties = NULL,
158 $action = CRM_Core_Action::NONE,
159 $includeContactIds = FALSE,
160 $searchDescendentGroups = TRUE,
161 $searchContext = 'search',
162 $contextMenu = NULL
163 ) {
164 //don't build query constructor, if form is not submitted
a3d827a7 165 $force = CRM_Utils_Request::retrieve('force', 'Boolean');
6a488035
TO
166 if (empty($formValues) && !$force) {
167 return;
168 }
169
170 // submitted form values
171 $this->_formValues = &$formValues;
172 $this->_params = &$params;
173 $this->_returnProperties = &$returnProperties;
174 $this->_contextMenu = &$contextMenu;
175 $this->_context = $searchContext;
176
177 // type of selector
178 $this->_action = $action;
179
180 $this->_searchContext = $searchContext;
181
182 $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $this->_formValues);
183
184 if ($this->_ufGroupID) {
185 $this->_fields = CRM_Core_BAO_UFGroup::getListingFields(CRM_Core_Action::VIEW,
186 CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY |
187 CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY,
188 FALSE, $this->_ufGroupID
189 );
190 self::$_columnHeaders = NULL;
191
192 $this->_customFields = CRM_Core_BAO_CustomField::getFieldsForImport('Individual');
193
194 $this->_returnProperties = CRM_Contact_BAO_Contact::makeHierReturnProperties($this->_fields);
195 $this->_returnProperties['contact_type'] = 1;
196 $this->_returnProperties['contact_sub_type'] = 1;
197 $this->_returnProperties['sort_name'] = 1;
7906396a
JP
198 if (!empty($this->_returnProperties['location']) && is_array($this->_returnProperties['location'])) {
199 foreach ($this->_returnProperties['location'] as $key => $property) {
200 if (!empty($property['email'])) {
201 $this->_returnProperties['location'][$key]['on_hold'] = 1;
202 }
203 }
204 }
6a488035
TO
205 }
206
207 $displayRelationshipType = CRM_Utils_Array::value('display_relationship_type', $this->_formValues);
208 $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND');
209
210 // rectify params to what proximity search expects if there is a value for prox_distance
211 // CRM-7021
212 if (!empty($this->_params)) {
213 CRM_Contact_BAO_ProximityQuery::fixInputParams($this->_params);
214 }
215
216 $this->_query = new CRM_Contact_BAO_Query(
217 $this->_params,
218 $this->_returnProperties,
219 NULL,
220 $includeContactIds,
221 FALSE,
222 CRM_Contact_BAO_Query::MODE_CONTACTS,
223 FALSE,
224 $searchDescendentGroups,
225 FALSE,
226 $displayRelationshipType,
227 $operator
228 );
229
230 $this->_options = &$this->_query->_options;
231 }
6a488035 232
2eb3ff3e 233 /**
234 * This method set cache key, later used in test environment
235 *
236 * @param string $key
237 */
238 public function setKey($key) {
239 $this->_key = $key;
240 }
241
6a488035
TO
242 /**
243 * This method returns the links that are given for each search row.
244 * currently the links added for each row are
245 *
246 * - View
247 * - Edit
248 *
249 * @return array
6a488035 250 */
00be9182 251 public static function &links() {
6a488035
TO
252 list($context, $contextMenu, $key) = func_get_args();
253 $extraParams = ($key) ? "&key={$key}" : NULL;
254 $searchContext = ($context) ? "&context=$context" : NULL;
255
256 if (!(self::$_links)) {
be2fb01f
CW
257 self::$_links = [
258 CRM_Core_Action::VIEW => [
6a488035
TO
259 'name' => ts('View'),
260 'url' => 'civicrm/contact/view',
8e4ec1f5 261 'class' => 'no-popup',
6a488035
TO
262 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}",
263 'title' => ts('View Contact Details'),
264 'ref' => 'view-contact',
be2fb01f
CW
265 ],
266 CRM_Core_Action::UPDATE => [
6a488035
TO
267 'name' => ts('Edit'),
268 'url' => 'civicrm/contact/add',
8e4ec1f5 269 'class' => 'no-popup',
6a488035
TO
270 'qs' => "reset=1&action=update&cid=%%id%%{$searchContext}{$extraParams}",
271 'title' => ts('Edit Contact Details'),
272 'ref' => 'edit-contact',
be2fb01f
CW
273 ],
274 ];
6a488035
TO
275
276 $config = CRM_Core_Config::singleton();
030c6f6b
SB
277 //CRM-16552: mapAPIKey is not mandatory as google no longer requires an API Key
278 if ($config->mapProvider && ($config->mapAPIKey || $config->mapProvider == 'Google')) {
be2fb01f 279 self::$_links[CRM_Core_Action::MAP] = [
6a488035
TO
280 'name' => ts('Map'),
281 'url' => 'civicrm/contact/map',
282 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}",
283 'title' => ts('Map Contact'),
be2fb01f 284 ];
6a488035
TO
285 }
286
287 // Adding Context Menu Links in more action
288 if ($contextMenu) {
289 $counter = 7000;
290 foreach ($contextMenu as $key => $value) {
291 $contextVal = '&context=' . $value['key'];
292 if ($value['key'] == 'delete') {
293 $contextVal = $searchContext;
294 }
6a488035
TO
295 $url = "civicrm/contact/view/{$value['key']}";
296 $qs = "reset=1&action=add&cid=%%id%%{$contextVal}{$extraParams}";
297 if ($value['key'] == 'activity') {
298 $qs = "action=browse&selectedChild=activity&reset=1&cid=%%id%%{$extraParams}";
299 }
300 elseif ($value['key'] == 'email') {
301 $url = "civicrm/contact/view/activity";
302 $qs = "atype=3&action=add&reset=1&cid=%%id%%{$extraParams}";
303 }
304
be2fb01f 305 self::$_links[$counter++] = [
6a488035
TO
306 'name' => $value['title'],
307 'url' => $url,
308 'qs' => $qs,
309 'title' => $value['title'],
310 'ref' => $value['ref'],
8e4ec1f5 311 'class' => CRM_Utils_Array::value('class', $value),
be2fb01f 312 ];
6a488035
TO
313 }
314 }
315 }
316 return self::$_links;
317 }
6a488035
TO
318
319 /**
100fef9d 320 * Getter for array of the parameters required for creating pager.
6a488035 321 *
77b97be7 322 * @param $action
c490a46a 323 * @param array $params
6a488035 324 */
00be9182 325 public function getPagerParams($action, &$params) {
353ffa53 326 $params['status'] = ts('Contact %%StatusMessage%%');
6a488035 327 $params['csvString'] = NULL;
353ffa53 328 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
6a488035
TO
329
330 $params['buttonTop'] = 'PagerTopButton';
331 $params['buttonBottom'] = 'PagerBottomButton';
332 }
6a488035 333
86538308
EM
334 /**
335 * @param null $action
336 * @param null $output
337 *
338 * @return array
339 */
00be9182 340 public function &getColHeads($action = NULL, $output = NULL) {
6a488035 341 $colHeads = self::_getColumnHeaders();
be2fb01f 342 $colHeads[] = ['desc' => ts('Actions'), 'name' => ts('Action')];
6a488035
TO
343 return $colHeads;
344 }
345
346 /**
100fef9d 347 * Returns the column headers as an array of tuples:
6a488035
TO
348 * (name, sortName (key to the sort array))
349 *
77c5b619
TO
350 * @param string $action
351 * The action being performed.
3f8d2862 352 * @param string $output
77c5b619 353 * What should the result set include (web/email/csv).
6a488035 354 *
a6c01b45
CW
355 * @return array
356 * the column headers that need to be displayed
6a488035 357 */
00be9182 358 public function &getColumnHeaders($action = NULL, $output = NULL) {
6a488035 359 $headers = NULL;
b3eeb853 360
361 // unset return property elements that we don't care
362 if (!empty($this->_returnProperties)) {
be2fb01f 363 $doNotCareElements = [
b3eeb853 364 'contact_type',
365 'contact_sub_type',
366 'sort_name',
be2fb01f 367 ];
481a74f4 368 foreach ($doNotCareElements as $value) {
b3eeb853 369 unset($this->_returnProperties[$value]);
370 }
371 }
372
6a488035 373 if ($output == CRM_Core_Selector_Controller::EXPORT) {
be2fb01f 374 $csvHeaders = [ts('Contact ID'), ts('Contact Type')];
6a488035
TO
375 foreach ($this->getColHeads($action, $output) as $column) {
376 if (array_key_exists('name', $column)) {
377 $csvHeaders[] = $column['name'];
378 }
379 }
380 $headers = $csvHeaders;
381 }
382 elseif ($output == CRM_Core_Selector_Controller::SCREEN) {
be2fb01f 383 $csvHeaders = [ts('Name')];
6a488035
TO
384 foreach ($this->getColHeads($action, $output) as $key => $column) {
385 if (array_key_exists('name', $column) &&
386 $column['name'] &&
387 $column['name'] != ts('Name')
388 ) {
389 $csvHeaders[$key] = $column['name'];
390 }
391 }
392 $headers = $csvHeaders;
393 }
394 elseif ($this->_ufGroupID) {
395 // we dont use the cached value of column headers
396 // since it potentially changed because of the profile selected
be2fb01f 397 static $skipFields = ['group', 'tag'];
6a488035
TO
398 $direction = CRM_Utils_Sort::ASCENDING;
399 $empty = TRUE;
400 if (!self::$_columnHeaders) {
be2fb01f
CW
401 self::$_columnHeaders = [
402 ['name' => ''],
403 [
6a488035
TO
404 'name' => ts('Name'),
405 'sort' => 'sort_name',
406 'direction' => CRM_Utils_Sort::ASCENDING,
be2fb01f
CW
407 ],
408 ];
6a488035 409
f9ba4d01 410 $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
6a488035
TO
411
412 foreach ($this->_fields as $name => $field) {
a7488080 413 if (!empty($field['in_selector']) &&
6a488035
TO
414 !in_array($name, $skipFields)
415 ) {
416 if (strpos($name, '-') !== FALSE) {
417 list($fieldName, $lType, $type) = CRM_Utils_System::explode('-', $name, 3);
418
419 if ($lType == 'Primary') {
420 $locationTypeName = 1;
421 }
422 else {
423 $locationTypeName = $locationTypes[$lType];
424 }
425
be2fb01f 426 if (in_array($fieldName, [
353ffa53
TO
427 'phone',
428 'im',
79d7553f 429 'email',
be2fb01f 430 ])) {
6a488035
TO
431 if ($type) {
432 $name = "`$locationTypeName-$fieldName-$type`";
433 }
434 else {
435 $name = "`$locationTypeName-$fieldName`";
436 }
437 }
438 else {
439 $name = "`$locationTypeName-$fieldName`";
440 }
441 }
442 //to handle sort key for Internal contactId.CRM-2289
443 if ($name == 'id') {
444 $name = 'contact_id';
445 }
446
be2fb01f 447 self::$_columnHeaders[] = [
6a488035
TO
448 'name' => $field['title'],
449 'sort' => $name,
450 'direction' => $direction,
be2fb01f 451 ];
6a488035
TO
452 $direction = CRM_Utils_Sort::DONTCARE;
453 $empty = FALSE;
454 }
455 }
456
457 // if we dont have any valid columns, dont add the implicit ones
458 // this allows the template to check on emptiness of column headers
459 if ($empty) {
be2fb01f 460 self::$_columnHeaders = [];
6a488035
TO
461 }
462 else {
be2fb01f 463 self::$_columnHeaders[] = ['desc' => ts('Actions'), 'name' => ts('Action')];
6a488035
TO
464 }
465 }
466 $headers = self::$_columnHeaders;
467 }
468 elseif (!empty($this->_returnProperties)) {
be2fb01f
CW
469 self::$_columnHeaders = [
470 ['name' => ''],
471 [
6a488035
TO
472 'name' => ts('Name'),
473 'sort' => 'sort_name',
474 'direction' => CRM_Utils_Sort::ASCENDING,
be2fb01f
CW
475 ],
476 ];
6a488035
TO
477 $properties = self::makeProperties($this->_returnProperties);
478
479 foreach ($properties as $prop) {
6a488035
TO
480 if (strpos($prop, '-')) {
481 list($loc, $fld, $phoneType) = CRM_Utils_System::explode('-', $prop, 3);
482 $title = $this->_query->_fields[$fld]['title'];
483 if (trim($phoneType) && !is_numeric($phoneType) && strtolower($phoneType) != $fld) {
484 $title .= "-{$phoneType}";
485 }
f9ba4d01 486 // fetch Location type label from name as $loc, which will be later used in column header
487 $title .= sprintf(" (%s)",
488 CRM_Core_PseudoConstant::getLabel(
489 'CRM_Core_DAO_Address',
490 'location_type_id',
491 CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Address', 'location_type_id', $loc)
492 )
493 );
7f594311 494
6a488035
TO
495 }
496 elseif (isset($this->_query->_fields[$prop]) && isset($this->_query->_fields[$prop]['title'])) {
497 $title = $this->_query->_fields[$prop]['title'];
b3eeb853 498 }
9d5c7f14 499 elseif (isset($this->_query->_pseudoConstantsSelect[$prop]) && isset($this->_query->_pseudoConstantsSelect[$prop]['pseudoconstant']['optionGroupName'])) {
500 $title = CRM_Core_BAO_OptionGroup::getTitleByName($this->_query->_pseudoConstantsSelect[$prop]['pseudoconstant']['optionGroupName']);
501 }
b3eeb853 502 else {
6a488035
TO
503 $title = '';
504 }
505
be2fb01f 506 self::$_columnHeaders[] = ['name' => $title, 'sort' => $prop];
6a488035 507 }
be2fb01f 508 self::$_columnHeaders[] = ['name' => ts('Actions')];
6a488035
TO
509 $headers = self::$_columnHeaders;
510 }
511 else {
512 $headers = $this->getColHeads($action, $output);
513 }
514
515 return $headers;
516 }
517
518 /**
519 * Returns total number of rows for the query.
520 *
54957108 521 * @param int $action
6a488035 522 *
a6c01b45
CW
523 * @return int
524 * Total number of rows
6a488035 525 */
00be9182 526 public function getTotalCount($action) {
f0c8c107
CW
527 // Use count from cache during paging/sorting
528 if (!empty($_GET['crmPID']) || !empty($_GET['crmSID'])) {
45b25a1b 529 $count = Civi::cache('long')->get("Search Results Count $this->_key");
f0c8c107
CW
530 }
531 if (empty($count)) {
532 $count = $this->_query->searchQuery(0, 0, NULL, TRUE);
45b25a1b 533 Civi::cache('long')->set("Search Results Count $this->_key", $count);
f0c8c107
CW
534 }
535 return $count;
6a488035
TO
536 }
537
538 /**
fe482240 539 * Returns all the rows in the given offset and rowCount.
6a488035 540 *
3f8d2862 541 * @param string $action
77c5b619
TO
542 * The action being performed.
543 * @param int $offset
544 * The row number to start from.
545 * @param int $rowCount
546 * The number of rows to return.
547 * @param string $sort
548 * The sql string that describes the sort order.
3f8d2862 549 * @param string $output
77c5b619 550 * What should the result set include (web/email/csv).
6a488035 551 *
a6c01b45
CW
552 * @return int
553 * the total number of rows for this action
6a488035 554 */
00be9182 555 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
556 if (($output == CRM_Core_Selector_Controller::EXPORT ||
557 $output == CRM_Core_Selector_Controller::SCREEN
558 ) &&
559 $this->_formValues['radio_ts'] == 'ts_sel'
560 ) {
561 $includeContactIds = TRUE;
562 }
563 else {
564 $includeContactIds = FALSE;
565 }
566
567 // note the formvalues were given by CRM_Contact_Form_Search to us
568 // and contain the search criteria (parameters)
569 // note that the default action is basic
4243847f 570 if ($rowCount) {
2ca46d4d
TO
571 /** @var CRM_Core_PrevNextCache_Interface $prevNext */
572 $prevNext = Civi::service('prevnext');
4243847f 573 $cacheKey = $this->buildPrevNextCache($sort);
2ca46d4d
TO
574 $cids = $prevNext->fetch($cacheKey, $offset, $rowCount);
575 $resultSet = empty($cids) ? [] : $this->_query->getCachedContacts($cids, $includeContactIds)->fetchGenerator();
4243847f
CW
576 }
577 else {
fbef7ddc 578 $resultSet = $this->_query->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds)->fetchGenerator();
4243847f 579 }
6a488035
TO
580
581 // process the result of the query
be2fb01f
CW
582 $rows = [];
583 $permissions = [CRM_Core_Permission::getPermission()];
6a488035
TO
584 if (CRM_Core_Permission::check('delete contacts')) {
585 $permissions[] = CRM_Core_Permission::DELETE;
586 }
587 $mask = CRM_Core_Action::mask($permissions);
588
589 // mask value to hide map link if there are not lat/long
590 $mapMask = $mask & 4095;
591
592 if ($this->_searchContext == 'smog') {
593 $gc = CRM_Core_SelectValues::groupContactStatus();
594 }
595
596 if ($this->_ufGroupID) {
b2b0530a 597 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035 598
be2fb01f
CW
599 $names = [];
600 static $skipFields = ['group', 'tag'];
6a488035 601 foreach ($this->_fields as $key => $field) {
a7488080 602 if (!empty($field['in_selector']) &&
6a488035
TO
603 !in_array($key, $skipFields)
604 ) {
605 if (strpos($key, '-') !== FALSE) {
606 list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $key, 3);
607
608 if ($id == 'Primary') {
609 $locationTypeName = 1;
610 }
a32c8655
SB
611 elseif ($fieldName == 'url') {
612 $locationTypeName = "website-{$id}";
613 }
6a488035
TO
614 else {
615 $locationTypeName = CRM_Utils_Array::value($id, $locationTypes);
616 if (!$locationTypeName) {
617 continue;
618 }
619 }
620
621 $locationTypeName = str_replace(' ', '_', $locationTypeName);
be2fb01f 622 if (in_array($fieldName, [
353ffa53
TO
623 'phone',
624 'im',
79d7553f 625 'email',
be2fb01f 626 ])) {
6a488035
TO
627 if ($type) {
628 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
629 }
630 else {
631 $names[] = "{$locationTypeName}-{$fieldName}";
632 }
633 }
634 else {
635 $names[] = "{$locationTypeName}-{$fieldName}";
636 }
637 }
638 else {
639 $names[] = $field['name'];
640 }
641 }
642 }
643
644 $names[] = "status";
645 }
646 elseif (!empty($this->_returnProperties)) {
647 $names = self::makeProperties($this->_returnProperties);
648 }
649 else {
650 $names = self::$_properties;
651 }
652
6a488035
TO
653 $links = self::links($this->_context, $this->_contextMenu, $this->_key);
654
655 //check explicitly added contact to a Smart Group.
06d67d53 656 $groupID = CRM_Utils_Array::value('group', $this->_formValues);
6a488035 657
be2fb01f 658 $pseudoconstants = [];
6a488035 659 // for CRM-3157 purposes
6a488035 660 if (in_array('world_region', $names)) {
be2fb01f 661 $pseudoconstants['world_region'] = [
7cc09daf 662 'dbName' => 'worldregion_id',
21dfd5f5 663 'values' => CRM_Core_PseudoConstant::worldRegion(),
be2fb01f 664 ];
6a488035
TO
665 }
666
fbef7ddc 667 foreach ($resultSet as $result) {
be2fb01f 668 $row = [];
d9ab802d 669 $this->_query->convertToPseudoNames($result);
6a488035
TO
670 // the columns we are interested in
671 foreach ($names as $property) {
672 if ($property == 'status') {
673 continue;
674 }
675 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($property)) {
8cee0c70 676 $row[$property] = CRM_Core_BAO_CustomField::displayValue(
0c145cc0 677 $result->$property,
6a488035 678 $cfID,
6a488035
TO
679 $result->contact_id
680 );
681 }
6a488035
TO
682 elseif (strpos($property, '-im')) {
683 $row[$property] = $result->$property;
684 if (!empty($result->$property)) {
353ffa53
TO
685 $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
686 $providerId = $property . "-provider_id";
687 $providerName = $imProviders[$result->$providerId];
6a488035
TO
688 $row[$property] = $result->$property . " ({$providerName})";
689 }
690 }
be2fb01f 691 elseif (in_array($property, [
353ffa53
TO
692 'addressee',
693 'email_greeting',
79d7553f 694 'postal_greeting',
be2fb01f 695 ])) {
6a488035
TO
696 $greeting = $property . '_display';
697 $row[$property] = $result->$greeting;
698 }
0c145cc0
DL
699 elseif (isset($pseudoconstants[$property])) {
700 $row[$property] = CRM_Utils_Array::value(
701 $result->{$pseudoconstants[$property]['dbName']},
702 $pseudoconstants[$property]['values']
703 );
6a488035
TO
704 }
705 elseif (strpos($property, '-url') !== FALSE) {
706 $websiteUrl = '';
a32c8655 707 $websiteKey = str_replace('-url', '', $property);
6a488035
TO
708 $propertyArray = explode('-', $property);
709 $websiteFld = $websiteKey . '-' . array_pop($propertyArray);
710 if (!empty($result->$websiteFld)) {
cbf48754 711 $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
353ffa53 712 $websiteType = $websiteTypes[$result->{"$websiteKey-website_type_id"}];
6a488035 713 $websiteValue = $result->$websiteFld;
353ffa53 714 $websiteUrl = "<a href=\"{$websiteValue}\">{$websiteValue} ({$websiteType})</a>";
6a488035
TO
715 }
716 $row[$property] = $websiteUrl;
717 }
7906396a
JP
718 elseif (strpos($property, '-email') !== FALSE) {
719 list($locType) = explode("-email", $property);
720 $onholdProperty = "{$locType}-on_hold";
721
722 $row[$property] = isset($result->$property) ? $result->$property : NULL;
723 if (!empty($row[$property]) && !empty($result->$onholdProperty)) {
724 $row[$property] .= " (On Hold)";
725 }
726 }
6a488035
TO
727 else {
728 $row[$property] = isset($result->$property) ? $result->$property : NULL;
729 }
730 }
731
732 if (!empty($result->postal_code_suffix)) {
733 $row['postal_code'] .= "-" . $result->postal_code_suffix;
734 }
735
736 if ($output != CRM_Core_Selector_Controller::EXPORT &&
737 $this->_searchContext == 'smog'
738 ) {
739 if (empty($result->status) &&
740 $groupID
741 ) {
742 $contactID = $result->contact_id;
743 if ($contactID) {
be2fb01f 744 $gcParams = [
6a488035
TO
745 'contact_id' => $contactID,
746 'group_id' => $groupID,
be2fb01f 747 ];
6a488035 748
be2fb01f 749 $gcDefaults = [];
6a488035
TO
750 CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_GroupContact', $gcParams, $gcDefaults);
751
752 if (empty($gcDefaults)) {
753 $row['status'] = ts('Smart');
754 }
755 else {
756 $row['status'] = $gc[$gcDefaults['status']];
757 }
758 }
759 else {
760 $row['status'] = NULL;
761 }
762 }
763 else {
764 $row['status'] = $gc[$result->status];
765 }
766 }
767
768 if ($output != CRM_Core_Selector_Controller::EXPORT) {
769 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id;
770
a7488080 771 if (!empty($this->_formValues['deleted_contacts']) && CRM_Core_Permission::check('access deleted contacts')
6a488035 772 ) {
be2fb01f
CW
773 $links = [
774 [
6a488035
TO
775 'name' => ts('View'),
776 'url' => 'civicrm/contact/view',
777 'qs' => 'reset=1&cid=%%id%%',
8e4ec1f5 778 'class' => 'no-popup',
6a488035 779 'title' => ts('View Contact Details'),
be2fb01f
CW
780 ],
781 [
6a488035
TO
782 'name' => ts('Restore'),
783 'url' => 'civicrm/contact/view/delete',
784 'qs' => 'reset=1&cid=%%id%%&restore=1',
785 'title' => ts('Restore Contact'),
be2fb01f
CW
786 ],
787 ];
6a488035 788 if (CRM_Core_Permission::check('delete contacts')) {
be2fb01f 789 $links[] = [
6a488035
TO
790 'name' => ts('Delete Permanently'),
791 'url' => 'civicrm/contact/view/delete',
792 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
793 'title' => ts('Permanently Delete Contact'),
be2fb01f 794 ];
6a488035 795 }
87dab4a4
AH
796 $row['action'] = CRM_Core_Action::formLink(
797 $links,
798 NULL,
be2fb01f 799 ['id' => $result->contact_id],
87dab4a4
AH
800 ts('more'),
801 FALSE,
802 'contact.selector.row',
803 'Contact',
804 $result->contact_id
805 );
6a488035
TO
806 }
807 elseif ((is_numeric(CRM_Utils_Array::value('geo_code_1', $row))) ||
f425e142 808 (!empty($row['city']) &&
6a488035
TO
809 CRM_Utils_Array::value('state_province', $row)
810 )
811 ) {
87dab4a4
AH
812 $row['action'] = CRM_Core_Action::formLink(
813 $links,
814 $mask,
be2fb01f 815 ['id' => $result->contact_id],
87dab4a4
AH
816 ts('more'),
817 FALSE,
818 'contact.selector.row',
819 'Contact',
820 $result->contact_id
821 );
6a488035
TO
822 }
823 else {
87dab4a4
AH
824 $row['action'] = CRM_Core_Action::formLink(
825 $links,
826 $mapMask,
be2fb01f 827 ['id' => $result->contact_id],
87dab4a4
AH
828 ts('more'),
829 FALSE,
830 'contact.selector.row',
831 'Contact',
832 $result->contact_id
833 );
6a488035
TO
834 }
835
836 // allow components to add more actions
837 CRM_Core_Component::searchAction($row, $result->contact_id);
838
ce80b209 839 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type,
6a488035
TO
840 FALSE,
841 $result->contact_id
842 );
843
2bbddafc 844 $row['contact_type_orig'] = $result->contact_sub_type ? $result->contact_sub_type : $result->contact_type;
6a488035
TO
845 $row['contact_id'] = $result->contact_id;
846 $row['sort_name'] = $result->sort_name;
98445ac5 847 // Surely this if should be if NOT - otherwise it's just wierd.
6a488035
TO
848 if (array_key_exists('id', $row)) {
849 $row['id'] = $result->contact_id;
850 }
851 }
852
98445ac5 853 $rows[$row['contact_id']] = $row;
6a488035
TO
854 }
855
6a488035
TO
856 return $rows;
857 }
858
86538308 859 /**
c490a46a 860 * @param CRM_Utils_Sort $sort
86538308
EM
861 *
862 * @return string
863 */
00be9182 864 public function buildPrevNextCache($sort) {
4243847f
CW
865 $cacheKey = 'civicrm search ' . $this->_key;
866
ddf14bea 867 // We should clear the cache in following conditions:
868 // 1. when starting from scratch, i.e new search
869 // 2. if records are sorted
870
871 // get current page requested
a3d827a7 872 $pageNum = CRM_Utils_Request::retrieve('crmPID', 'Integer');
ddf14bea 873
874 // get the current sort order
a3d827a7 875 $currentSortID = CRM_Utils_Request::retrieve('crmSID', 'String');
ddf14bea 876
877 $session = CRM_Core_Session::singleton();
878
879 // get previous sort id
880 $previousSortID = $session->get('previousSortID');
881
882 // check for current != previous to ensure cache is not reset if paging is done without changing
883 // sort criteria
481a74f4 884 if (!$pageNum || (!empty($currentSortID) && $currentSortID != $previousSortID)) {
744b4e34 885 Civi::service('prevnext')->deleteItem(NULL, $cacheKey, 'civicrm_contact');
ddf14bea 886 // this means it's fresh search, so set pageNum=1
887 if (!$pageNum) {
888 $pageNum = 1;
889 }
890 }
891
892 // set the current sort as previous sort
893 if (!empty($currentSortID)) {
894 $session->set('previousSortID', $currentSortID);
4243847f 895 }
6a488035 896
64951b63
CW
897 $pageSize = CRM_Utils_Request::retrieve('crmRowCount', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, 50);
898 $firstRecord = ($pageNum - 1) * $pageSize;
6a488035
TO
899
900 //for alphabetic pagination selection save
a3d827a7 901 $sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String');
6a488035
TO
902
903 //for text field pagination selection save
c93f3d19 904 $countRow = Civi::service('prevnext')->getCount($cacheKey);
64951b63 905 // $sortByCharacter triggers a refresh in the prevNext cache
4243847f 906 if ($sortByCharacter && $sortByCharacter != 'all') {
450941a2 907 $this->fillupPrevNextCache($sort, $cacheKey, 0, max(self::CACHE_SIZE, $pageSize));
6a488035 908 }
65e49ebf 909 elseif (($firstRecord + $pageSize) >= $countRow) {
450941a2 910 $this->fillupPrevNextCache($sort, $cacheKey, $countRow, max(self::CACHE_SIZE, $pageSize) + $firstRecord - $countRow);
64951b63 911 }
4243847f 912 return $cacheKey;
6a488035
TO
913 }
914
86538308
EM
915 /**
916 * @param $rows
917 */
00be9182 918 public function addActions(&$rows) {
6a488035 919
be2fb01f 920 $basicPermissions = CRM_Core_Permission::check('delete contacts') ? [CRM_Core_Permission::DELETE] : [];
6a488035 921
730afb46 922 // get permissions on an individual level (CRM-12645)
98445ac5 923 // @todo look at storing this to the session as this is called twice during search results render.
730afb46 924 $can_edit_list = CRM_Contact_BAO_Contact_Permission::allowList(array_keys($rows), CRM_Core_Permission::EDIT);
925
926 $links_template = self::links($this->_context, $this->_contextMenu, $this->_key);
927
6a488035 928 foreach ($rows as $id => & $row) {
730afb46 929 $links = $links_template;
98445ac5 930 if (in_array($id, $can_edit_list)) {
be2fb01f 931 $mask = CRM_Core_Action::mask(array_merge([CRM_Core_Permission::EDIT], $basicPermissions));
98445ac5 932 }
933 else {
be2fb01f 934 $mask = CRM_Core_Action::mask(array_merge([CRM_Core_Permission::VIEW], $basicPermissions));
98445ac5 935 }
730afb46 936
98445ac5 937 if ((!is_numeric(CRM_Utils_Array::value('geo_code_1', $row))) &&
938 (empty($row['city']) ||
939 !CRM_Utils_Array::value('state_province', $row)
940 )
941 ) {
942 $mask = $mask & 4095;
730afb46 943 }
944
a7488080 945 if (!empty($this->_formValues['deleted_contacts']) && CRM_Core_Permission::check('access deleted contacts')
6a488035 946 ) {
be2fb01f
CW
947 $links = [
948 [
6a488035
TO
949 'name' => ts('View'),
950 'url' => 'civicrm/contact/view',
951 'qs' => 'reset=1&cid=%%id%%',
f6364403 952 'class' => 'no-popup',
6a488035 953 'title' => ts('View Contact Details'),
be2fb01f
CW
954 ],
955 [
6a488035
TO
956 'name' => ts('Restore'),
957 'url' => 'civicrm/contact/view/delete',
958 'qs' => 'reset=1&cid=%%id%%&restore=1',
959 'title' => ts('Restore Contact'),
be2fb01f
CW
960 ],
961 ];
6a488035 962 if (CRM_Core_Permission::check('delete contacts')) {
be2fb01f 963 $links[] = [
6a488035
TO
964 'name' => ts('Delete Permanently'),
965 'url' => 'civicrm/contact/view/delete',
966 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
967 'title' => ts('Permanently Delete Contact'),
be2fb01f 968 ];
6a488035 969 }
87dab4a4
AH
970 $row['action'] = CRM_Core_Action::formLink(
971 $links,
ce80b209 972 NULL,
be2fb01f 973 ['id' => $row['contact_id']],
87dab4a4
AH
974 ts('more'),
975 FALSE,
976 'contact.selector.actions',
977 'Contact',
978 $row['contact_id']
979 );
6a488035 980 }
6a488035 981 else {
87dab4a4
AH
982 $row['action'] = CRM_Core_Action::formLink(
983 $links,
98445ac5 984 $mask,
be2fb01f 985 ['id' => $row['contact_id']],
87dab4a4
AH
986 ts('more'),
987 FALSE,
988 'contact.selector.actions',
989 'Contact',
990 $row['contact_id']
991 );
6a488035
TO
992 }
993
994 // allow components to add more actions
995 CRM_Core_Component::searchAction($row, $row['contact_id']);
996
2bbddafc
CW
997 if (!empty($row['contact_type_orig'])) {
998 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($row['contact_type_orig'],
6a488035
TO
999 FALSE, $row['contact_id']);
1000 }
1001 }
1002 }
1003
86538308
EM
1004 /**
1005 * @param $rows
1006 */
00be9182 1007 public function removeActions(&$rows) {
6a488035
TO
1008 foreach ($rows as $rid => & $rValue) {
1009 unset($rValue['contact_type']);
1010 unset($rValue['action']);
1011 }
1012 }
1013
64951b63 1014 /**
c490a46a 1015 * @param CRM_Utils_Sort $sort
64951b63
CW
1016 * @param string $cacheKey
1017 * @param int $start
1018 * @param int $end
1aa104c5 1019 *
1020 * @throws \CRM_Core_Exception
64951b63 1021 */
450941a2 1022 public function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = self::CACHE_SIZE) {
778a10cb 1023 $coreSearch = TRUE;
64951b63 1024 // For custom searches, use the contactIDs method
6a488035 1025 if (is_a($this, 'CRM_Contact_Selector_Custom')) {
64951b63 1026 $sql = $this->_search->contactIDs($start, $end, $sort, TRUE);
778a10cb 1027 $coreSearch = FALSE;
6a488035 1028 }
64951b63 1029 // For core searches use the searchQuery method
6a488035 1030 else {
63ae2e7b 1031 $sql = $this->_query->getSearchSQL($start, $end, $sort, FALSE, $this->_query->_includeContactIds,
1032 FALSE, TRUE);
6a488035
TO
1033 }
1034
1035 // CRM-9096
1036 // due to limitations in our search query writer, the above query does not work
1037 // in cases where the query is being sorted on a non-contact table
1038 // this results in a fatal error :(
1039 // see below for the gross hack of trapping the error and not filling
1040 // the prev next cache in this situation
1041 // the other alternative of running the FULL query will just be incredibly inefficient
1042 // and slow things down way too much on large data sets / complex queries
1043
94a33a7a 1044 $selectSQL = CRM_Core_DAO::composeQuery("SELECT DISTINCT %1, contact_a.id, contact_a.sort_name", [1 => [$cacheKey, 'String']]);
6a488035 1045
1aa104c5 1046 $sql = str_ireplace(['SELECT contact_a.id as contact_id', 'SELECT contact_a.id as id'], $selectSQL, $sql);
1047 $sql = str_ireplace('ORDER BY `contact_id`', 'ORDER BY `id`', $sql, $sql);
1048
f946d152 1049 try {
94a33a7a 1050 Civi::service('prevnext')->fillWithSql($cacheKey, $sql);
f946d152 1051 }
1052 catch (CRM_Core_Exception $e) {
778a10cb 1053 if ($coreSearch) {
1054 // in the case of error, try rebuilding cache using full sql which is used for search selector display
1055 // this fixes the bugs reported in CRM-13996 & CRM-14438
1056 $this->rebuildPreNextCache($start, $end, $sort, $cacheKey);
1057 }
1058 else {
dcb44c9b 1059 CRM_Core_Error::deprecatedFunctionWarning('Custom searches should return sql capable of filling the prevnext cache.');
4caab828 1060 // This will always show for CiviRules :-( as a) it orders by 'rule_label'
1061 // which is not available in the query & b) it uses contact not contact_a
1062 // as an alias.
1063 // CRM_Core_Session::setStatus(ts('Query Failed'));
778a10cb 1064 return;
1065 }
6a488035
TO
1066 }
1067
e37c68e1
TO
1068 if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) {
1069 // SQL-backed prevnext cache uses an extra record for pruning the cache.
435fc552
SL
1070 // Also ensure that caches stay alive for 2 days as per previous code
1071 Civi::cache('prevNextCache')->set($cacheKey, $cacheKey, 60 * 60 * 24 * CRM_Core_PrevNextCache_Sql::cacheDays);
e37c68e1 1072 }
6a488035
TO
1073 }
1074
778a10cb 1075 /**
dc195289 1076 * called to rebuild prev next cache using full sql in case of core search ( excluding custom search)
778a10cb 1077 *
77c5b619
TO
1078 * @param int $start
1079 * Start for limit clause.
1080 * @param int $end
1081 * End for limit clause.
c490a46a 1082 * @param CRM_Utils_Sort $sort
77c5b619
TO
1083 * @param string $cacheKey
1084 * Cache key.
778a10cb 1085 */
00be9182 1086 public function rebuildPreNextCache($start, $end, $sort, $cacheKey) {
778a10cb 1087 // generate full SQL
1088 $sql = $this->_query->searchQuery($start, $end, $sort, FALSE, $this->_query->_includeContactIds,
1089 FALSE, FALSE, TRUE);
1090
1091 $dao = CRM_Core_DAO::executeQuery($sql);
1092
450941a2 1093 // build insert query, note that currently we build cache for 500 (self::CACHE_SIZE) contact records at a time, hence below approach
83068a64 1094 $rows = [];
22e263ad 1095 while ($dao->fetch()) {
83068a64
TO
1096 $rows[] = [
1097 'entity_table' => 'civicrm_contact',
1098 'entity_id1' => $dao->contact_id,
1099 'entity_id2' => $dao->contact_id,
1100 'data' => $dao->sort_name,
1101 ];
778a10cb 1102 }
1103
83068a64 1104 Civi::service('prevnext')->fillWithArray($cacheKey, $rows);
778a10cb 1105 }
1106
6a488035 1107 /**
1054415f 1108 * @inheritDoc
6a488035 1109 */
6a488035
TO
1110 public function getQILL() {
1111 return $this->_query->qill();
1112 }
1113
1114 /**
100fef9d 1115 * Name of export file.
6a488035 1116 *
77c5b619
TO
1117 * @param string $output
1118 * Type of output.
6a488035 1119 *
a6c01b45
CW
1120 * @return string
1121 * name of the file
6a488035 1122 */
00be9182 1123 public function getExportFileName($output = 'csv') {
6a488035
TO
1124 return ts('CiviCRM Contact Search');
1125 }
1126
1127 /**
fe482240 1128 * Get colunmn headers for search selector.
6a488035 1129 *
a6c01b45 1130 * @return array
6a488035
TO
1131 */
1132 private static function &_getColumnHeaders() {
1133 if (!isset(self::$_columnHeaders)) {
1134 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
1135 'address_options', TRUE, NULL, TRUE
1136 );
1137
be2fb01f
CW
1138 self::$_columnHeaders = [
1139 'contact_type' => ['desc' => ts('Contact Type')],
1140 'sort_name' => [
6a488035
TO
1141 'name' => ts('Name'),
1142 'sort' => 'sort_name',
1143 'direction' => CRM_Utils_Sort::ASCENDING,
be2fb01f
CW
1144 ],
1145 ];
6a488035 1146
be2fb01f
CW
1147 $defaultAddress = [
1148 'street_address' => ['name' => ts('Address')],
1149 'city' => [
5defa2fd 1150 'name' => ts('City'),
6a488035
TO
1151 'sort' => 'city',
1152 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
1153 ],
1154 'state_province' => [
5defa2fd 1155 'name' => ts('State'),
6a488035
TO
1156 'sort' => 'state_province',
1157 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
1158 ],
1159 'postal_code' => [
5defa2fd 1160 'name' => ts('Postal'),
6a488035
TO
1161 'sort' => 'postal_code',
1162 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
1163 ],
1164 'country' => [
5defa2fd 1165 'name' => ts('Country'),
6a488035
TO
1166 'sort' => 'country',
1167 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
1168 ],
1169 ];
6a488035
TO
1170
1171 foreach ($defaultAddress as $columnName => $column) {
a7488080 1172 if (!empty($addressOptions[$columnName])) {
6a488035
TO
1173 self::$_columnHeaders[$columnName] = $column;
1174 }
1175 }
1176
be2fb01f 1177 self::$_columnHeaders['email'] = [
5defa2fd 1178 'name' => ts('Email'),
6a488035
TO
1179 'sort' => 'email',
1180 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f 1181 ];
6a488035 1182
be2fb01f 1183 self::$_columnHeaders['phone'] = ['name' => ts('Phone')];
6a488035
TO
1184 }
1185 return self::$_columnHeaders;
1186 }
1187
86538308
EM
1188 /**
1189 * @return CRM_Contact_BAO_Query
1190 */
3875e6b6 1191 public function getQuery() {
6a488035
TO
1192 return $this->_query;
1193 }
1194
86538308
EM
1195 /**
1196 * @return CRM_Contact_DAO_Contact
1197 */
00be9182 1198 public function alphabetQuery() {
52cda5dc 1199 return $this->_query->alphabetQuery();
6a488035
TO
1200 }
1201
86538308 1202 /**
c490a46a 1203 * @param array $params
100fef9d 1204 * @param int $sortID
86538308
EM
1205 * @param null $displayRelationshipType
1206 * @param string $queryOperator
1207 *
1208 * @return CRM_Contact_DAO_Contact
1209 */
ae066a2b 1210 public function contactIDQuery($params, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
6a488035
TO
1211 $sortOrder = &$this->getSortOrder($this->_action);
1212 $sort = new CRM_Utils_Sort($sortOrder, $sortID);
1213
1214 // rectify params to what proximity search expects if there is a value for prox_distance
1215 // CRM-7021 CRM-7905
1216 if (!empty($params)) {
1217 CRM_Contact_BAO_ProximityQuery::fixInputParams($params);
1218 }
1219
1220 if (!$displayRelationshipType) {
1221 $query = new CRM_Contact_BAO_Query($params,
33092c89 1222 CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES,
336ca64c 1223 NULL, FALSE, FALSE, 1,
6a488035
TO
1224 FALSE, TRUE, TRUE, NULL,
1225 $queryOperator
1226 );
1227 }
1228 else {
33092c89
SB
1229 $query = new CRM_Contact_BAO_Query($params,
1230 CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES,
336ca64c 1231 NULL, FALSE, FALSE, 1,
6a488035
TO
1232 FALSE, TRUE, TRUE, $displayRelationshipType,
1233 $queryOperator
1234 );
1235 }
708ce91f 1236 return $query->searchQuery(0, 0, $sort);
6a488035
TO
1237 }
1238
86538308
EM
1239 /**
1240 * @param $returnProperties
1241 *
1242 * @return array
1243 */
00be9182 1244 public function &makeProperties(&$returnProperties) {
be2fb01f 1245 $properties = [];
6a488035
TO
1246 foreach ($returnProperties as $name => $value) {
1247 if ($name != 'location') {
c6ff5b0d 1248 // special handling for group and tag
be2fb01f 1249 if (in_array($name, ['group', 'tag'])) {
fd18baa6
KJ
1250 $name = "{$name}s";
1251 }
c6ff5b0d
KJ
1252
1253 // special handling for notes
be2fb01f 1254 if (in_array($name, ['note', 'note_subject', 'note_body'])) {
c6ff5b0d
KJ
1255 $name = "notes";
1256 }
1257
6a488035
TO
1258 $properties[] = $name;
1259 }
1260 else {
1261 // extract all the location stuff
1262 foreach ($value as $n => $v) {
1263 foreach ($v as $n1 => $v1) {
1264 if (!strpos('_id', $n1) && $n1 != 'location_type') {
140e25ee 1265 $n = str_replace(' ', '_', $n);
6a488035
TO
1266 $properties[] = "{$n}-{$n1}";
1267 }
1268 }
1269 }
1270 }
1271 }
1272 return $properties;
1273 }
96025800 1274
6a488035 1275}