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