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