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