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