3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
42 class CRM_Contact_Selector
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
45 * This defines two actions- View and Edit.
50 static $_links = NULL;
53 * we use desc to remind us what that column is, name is used in the tpl
58 static $_columnHeaders;
61 * Properties of contact we're interested in displaying
65 static $_properties = array(
66 'contact_id', 'contact_type', 'contact_sub_type',
67 'sort_name', 'street_address',
68 'city', 'state_province', 'postal_code', 'country',
69 'geo_code_1', 'geo_code_2', 'is_deceased',
70 'email', 'on_hold', 'phone', 'status',
71 'do_not_email', 'do_not_phone', 'do_not_mail',
75 * formValues is the array returned by exportValues called on
76 * the HTML_QuickForm_Controller for that page.
89 protected $_contextMenu;
92 * params is the array in a value used by the search query creator
100 * The return properties used for search
105 protected $_returnProperties;
108 * represent the type of selector
115 protected $_searchContext;
124 protected $_ufGroupID;
127 * the public visible fields to be shown to the user
137 * @param $customSearchClass
138 * @param array $formValues array of form values imported
139 * @param array $params array of parameters for query
140 * @param null $returnProperties
141 * @param \const|int $action - action of search basic or advanced.
143 * @param bool $includeContactIds
144 * @param bool $searchDescendentGroups
145 * @param string $searchContext
146 * @param null $contextMenu
148 * @return CRM_Contact_Selector
151 function __construct(
155 $returnProperties = NULL,
156 $action = CRM_Core_Action
::NONE
,
157 $includeContactIds = FALSE,
158 $searchDescendentGroups = TRUE,
159 $searchContext = 'search',
162 //don't build query constructor, if form is not submitted
163 $force = CRM_Utils_Request
::retrieve('force', 'Boolean', CRM_Core_DAO
::$_nullObject);
164 if (empty($formValues) && !$force) {
168 // submitted form values
169 $this->_formValues
= &$formValues;
170 $this->_params
= &$params;
171 $this->_returnProperties
= &$returnProperties;
172 $this->_contextMenu
= &$contextMenu;
173 $this->_context
= $searchContext;
176 $this->_action
= $action;
178 $this->_searchContext
= $searchContext;
180 $this->_ufGroupID
= CRM_Utils_Array
::value('uf_group_id', $this->_formValues
);
182 if ($this->_ufGroupID
) {
183 $this->_fields
= CRM_Core_BAO_UFGroup
::getListingFields(CRM_Core_Action
::VIEW
,
184 CRM_Core_BAO_UFGroup
::PUBLIC_VISIBILITY |
185 CRM_Core_BAO_UFGroup
::LISTINGS_VISIBILITY
,
186 FALSE, $this->_ufGroupID
188 self
::$_columnHeaders = NULL;
190 $this->_customFields
= CRM_Core_BAO_CustomField
::getFieldsForImport('Individual');
192 $this->_returnProperties
= CRM_Contact_BAO_Contact
::makeHierReturnProperties($this->_fields
);
193 $this->_returnProperties
['contact_type'] = 1;
194 $this->_returnProperties
['contact_sub_type'] = 1;
195 $this->_returnProperties
['sort_name'] = 1;
198 $displayRelationshipType = CRM_Utils_Array
::value('display_relationship_type', $this->_formValues
);
199 $operator = CRM_Utils_Array
::value('operator', $this->_formValues
, 'AND');
201 // rectify params to what proximity search expects if there is a value for prox_distance
203 if (!empty($this->_params
)) {
204 CRM_Contact_BAO_ProximityQuery
::fixInputParams($this->_params
);
207 $this->_query
= new CRM_Contact_BAO_Query(
209 $this->_returnProperties
,
213 CRM_Contact_BAO_Query
::MODE_CONTACTS
,
215 $searchDescendentGroups,
217 $displayRelationshipType,
221 $this->_options
= &$this->_query
->_options
;
226 * This method returns the links that are given for each search row.
227 * currently the links added for each row are
236 static function &links() {
237 list($context, $contextMenu, $key) = func_get_args();
238 $extraParams = ($key) ?
"&key={$key}" : NULL;
239 $searchContext = ($context) ?
"&context=$context" : NULL;
241 if (!(self
::$_links)) {
242 self
::$_links = array(
243 CRM_Core_Action
::VIEW
=> array(
244 'name' => ts('View'),
245 'url' => 'civicrm/contact/view',
246 'class' => 'no-popup',
247 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}",
248 'title' => ts('View Contact Details'),
249 'ref' => 'view-contact',
251 CRM_Core_Action
::UPDATE
=> array(
252 'name' => ts('Edit'),
253 'url' => 'civicrm/contact/add',
254 'class' => 'no-popup',
255 'qs' => "reset=1&action=update&cid=%%id%%{$searchContext}{$extraParams}",
256 'title' => ts('Edit Contact Details'),
257 'ref' => 'edit-contact',
261 $config = CRM_Core_Config
::singleton();
262 if ($config->mapAPIKey
&& $config->mapProvider
) {
263 self
::$_links[CRM_Core_Action
::MAP
] = array(
265 'url' => 'civicrm/contact/map',
266 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}",
267 'title' => ts('Map Contact'),
271 // Adding Context Menu Links in more action
274 foreach ($contextMenu as $key => $value) {
275 $contextVal = '&context=' . $value['key'];
276 if ($value['key'] == 'delete') {
277 $contextVal = $searchContext;
279 $url = "civicrm/contact/view/{$value['key']}";
280 $qs = "reset=1&action=add&cid=%%id%%{$contextVal}{$extraParams}";
281 if ($value['key'] == 'activity') {
282 $qs = "action=browse&selectedChild=activity&reset=1&cid=%%id%%{$extraParams}";
284 elseif ($value['key'] == 'email') {
285 $url = "civicrm/contact/view/activity";
286 $qs = "atype=3&action=add&reset=1&cid=%%id%%{$extraParams}";
289 self
::$_links[$counter++
] = array(
290 'name' => $value['title'],
293 'title' => $value['title'],
294 'ref' => $value['ref'],
295 'class' => CRM_Utils_Array
::value('class', $value),
300 return self
::$_links;
305 * getter for array of the parameters required for creating pager.
313 function getPagerParams($action, &$params) {
314 $params['status'] = ts('Contact %%StatusMessage%%');
315 $params['csvString'] = NULL;
316 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
318 $params['buttonTop'] = 'PagerTopButton';
319 $params['buttonBottom'] = 'PagerBottomButton';
324 * @param null $action
325 * @param null $output
329 function &getColHeads($action = NULL, $output = NULL) {
330 $colHeads = self
::_getColumnHeaders();
331 $colHeads[] = array('desc' => ts('Actions'), 'name' => ts('Action'));
336 * returns the column headers as an array of tuples:
337 * (name, sortName (key to the sort array))
339 * @param string $action the action being performed
340 * @param enum $output what should the result set include (web/email/csv)
342 * @return array the column headers that need to be displayed
345 function &getColumnHeaders($action = NULL, $output = NULL) {
348 // unset return property elements that we don't care
349 if (!empty($this->_returnProperties
)) {
350 $doNotCareElements = array(
355 foreach ( $doNotCareElements as $value) {
356 unset($this->_returnProperties
[$value]);
360 if ($output == CRM_Core_Selector_Controller
::EXPORT
) {
361 $csvHeaders = array(ts('Contact ID'), ts('Contact Type'));
362 foreach ($this->getColHeads($action, $output) as $column) {
363 if (array_key_exists('name', $column)) {
364 $csvHeaders[] = $column['name'];
367 $headers = $csvHeaders;
369 elseif ($output == CRM_Core_Selector_Controller
::SCREEN
) {
370 $csvHeaders = array(ts('Name'));
371 foreach ($this->getColHeads($action, $output) as $key => $column) {
372 if (array_key_exists('name', $column) &&
374 $column['name'] != ts('Name')
376 $csvHeaders[$key] = $column['name'];
379 $headers = $csvHeaders;
381 elseif ($this->_ufGroupID
) {
382 // we dont use the cached value of column headers
383 // since it potentially changed because of the profile selected
384 static $skipFields = array('group', 'tag');
385 $direction = CRM_Utils_Sort
::ASCENDING
;
387 if (!self
::$_columnHeaders) {
388 self
::$_columnHeaders = array(array('name' => ''),
390 'name' => ts('Name'),
391 'sort' => 'sort_name',
392 'direction' => CRM_Utils_Sort
::ASCENDING
,
396 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
398 foreach ($this->_fields
as $name => $field) {
399 if (!empty($field['in_selector']) &&
400 !in_array($name, $skipFields)
402 if (strpos($name, '-') !== FALSE) {
403 list($fieldName, $lType, $type) = CRM_Utils_System
::explode('-', $name, 3);
405 if ($lType == 'Primary') {
406 $locationTypeName = 1;
409 $locationTypeName = $locationTypes[$lType];
412 if (in_array($fieldName, array(
413 'phone', 'im', 'email'))) {
415 $name = "`$locationTypeName-$fieldName-$type`";
418 $name = "`$locationTypeName-$fieldName`";
422 $name = "`$locationTypeName-$fieldName`";
425 //to handle sort key for Internal contactId.CRM-2289
427 $name = 'contact_id';
430 self
::$_columnHeaders[] = array(
431 'name' => $field['title'],
433 'direction' => $direction,
435 $direction = CRM_Utils_Sort
::DONTCARE
;
440 // if we dont have any valid columns, dont add the implicit ones
441 // this allows the template to check on emptiness of column headers
443 self
::$_columnHeaders = array();
446 self
::$_columnHeaders[] = array('desc' => ts('Actions'), 'name' => ts('Action'));
449 $headers = self
::$_columnHeaders;
451 elseif (!empty($this->_returnProperties
)) {
452 self
::$_columnHeaders = array(array('name' => ''),
454 'name' => ts('Name'),
455 'sort' => 'sort_name',
456 'direction' => CRM_Utils_Sort
::ASCENDING
,
459 $properties = self
::makeProperties($this->_returnProperties
);
461 foreach ($properties as $prop) {
462 if (strpos($prop, '-')) {
463 list($loc, $fld, $phoneType) = CRM_Utils_System
::explode('-', $prop, 3);
464 $title = $this->_query
->_fields
[$fld]['title'];
465 if (trim($phoneType) && !is_numeric($phoneType) && strtolower($phoneType) != $fld) {
466 $title .= "-{$phoneType}";
470 elseif (isset($this->_query
->_fields
[$prop]) && isset($this->_query
->_fields
[$prop]['title'])) {
471 $title = $this->_query
->_fields
[$prop]['title'];
477 self
::$_columnHeaders[] = array('name' => $title, 'sort' => $prop);
479 self
::$_columnHeaders[] = array('name' => ts('Actions'));
480 $headers = self
::$_columnHeaders;
483 $headers = $this->getColHeads($action, $output);
490 * Returns total number of rows for the query.
494 * @return int Total number of rows
497 function getTotalCount($action) {
498 // Use count from cache during paging/sorting
499 if (!empty($_GET['crmPID']) ||
!empty($_GET['crmSID'])) {
500 $count = CRM_Core_BAO_Cache
::getItem('Search Results Count', $this->_key
);
503 $count = $this->_query
->searchQuery(0, 0, NULL, TRUE);
504 CRM_Core_BAO_Cache
::setItem($count, 'Search Results Count', $this->_key
);
510 * returns all the rows in the given offset and rowCount
512 * @param enum $action the action being performed
513 * @param int $offset the row number to start from
514 * @param int $rowCount the number of rows to return
515 * @param string $sort the sql string that describes the sort order
516 * @param enum $output what should the result set include (web/email/csv)
518 * @return int the total number of rows for this action
520 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
521 $config = CRM_Core_Config
::singleton();
523 if (($output == CRM_Core_Selector_Controller
::EXPORT ||
524 $output == CRM_Core_Selector_Controller
::SCREEN
526 $this->_formValues
['radio_ts'] == 'ts_sel'
528 $includeContactIds = TRUE;
531 $includeContactIds = FALSE;
534 // note the formvalues were given by CRM_Contact_Form_Search to us
535 // and contain the search criteria (parameters)
536 // note that the default action is basic
538 $cacheKey = $this->buildPrevNextCache($sort);
539 $result = $this->_query
->getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds);
542 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
545 // process the result of the query
547 $permissions = array(CRM_Core_Permission
::getPermission());
548 if (CRM_Core_Permission
::check('delete contacts')) {
549 $permissions[] = CRM_Core_Permission
::DELETE
;
551 $mask = CRM_Core_Action
::mask($permissions);
553 // mask value to hide map link if there are not lat/long
554 $mapMask = $mask & 4095;
556 if ($this->_searchContext
== 'smog') {
557 $gc = CRM_Core_SelectValues
::groupContactStatus();
560 if ($this->_ufGroupID
) {
561 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
564 static $skipFields = array('group', 'tag');
565 foreach ($this->_fields
as $key => $field) {
566 if (!empty($field['in_selector']) &&
567 !in_array($key, $skipFields)
569 if (strpos($key, '-') !== FALSE) {
570 list($fieldName, $id, $type) = CRM_Utils_System
::explode('-', $key, 3);
572 if ($id == 'Primary') {
573 $locationTypeName = 1;
576 $locationTypeName = CRM_Utils_Array
::value($id, $locationTypes);
577 if (!$locationTypeName) {
582 $locationTypeName = str_replace(' ', '_', $locationTypeName);
583 if (in_array($fieldName, array(
584 'phone', 'im', 'email'))) {
586 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
589 $names[] = "{$locationTypeName}-{$fieldName}";
593 $names[] = "{$locationTypeName}-{$fieldName}";
597 $names[] = $field['name'];
604 elseif (!empty($this->_returnProperties
)) {
605 $names = self
::makeProperties($this->_returnProperties
);
608 $names = self
::$_properties;
611 $multipleSelectFields = array('preferred_communication_method' => 1);
613 $links = self
::links($this->_context
, $this->_contextMenu
, $this->_key
);
615 //check explicitly added contact to a Smart Group.
616 $groupID = CRM_Utils_Array
::key('1', $this->_formValues
['group']);
618 $pseudoconstants = array();
619 // for CRM-3157 purposes
620 if (in_array('world_region', $names)) {
621 $pseudoconstants['world_region'] = array(
622 'dbName' => 'world_region_id',
623 'values' => CRM_Core_PseudoConstant
::worldRegion()
628 while ($result->fetch()) {
630 $this->_query
->convertToPseudoNames($result);
632 // the columns we are interested in
633 foreach ($names as $property) {
634 if ($property == 'status') {
637 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($property)) {
638 $row[$property] = CRM_Core_BAO_CustomField
::getDisplayValue(
646 $multipleSelectFields &&
647 array_key_exists($property, $multipleSelectFields)
650 $paramsNew = array($key => $result->$property);
651 $name = array($key => array('newName' => $key, 'groupName' => $key));
653 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
654 $row[$key] = $paramsNew[$key];
656 elseif (strpos($property, '-im')) {
657 $row[$property] = $result->$property;
658 if (!empty($result->$property)) {
659 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
660 $providerId = $property . "-provider_id";
661 $providerName = $imProviders[$result->$providerId];
662 $row[$property] = $result->$property . " ({$providerName})";
665 elseif (in_array($property, array(
666 'addressee', 'email_greeting', 'postal_greeting'))) {
667 $greeting = $property . '_display';
668 $row[$property] = $result->$greeting;
670 elseif (isset($pseudoconstants[$property])) {
671 $row[$property] = CRM_Utils_Array
::value(
672 $result->{$pseudoconstants[$property]['dbName']},
673 $pseudoconstants[$property]['values']
676 elseif (strpos($property, '-url') !== FALSE) {
678 $websiteKey = 'website-1';
679 $propertyArray = explode('-', $property);
680 $websiteFld = $websiteKey . '-' . array_pop($propertyArray);
681 if (!empty($result->$websiteFld)) {
682 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
683 $websiteType = $websiteTypes[$result->{"$websiteKey-website_type_id"}];
684 $websiteValue = $result->$websiteFld;
685 $websiteUrl = "<a href=\"{$websiteValue}\">{$websiteValue} ({$websiteType})</a>";
687 $row[$property] = $websiteUrl;
690 $row[$property] = isset($result->$property) ?
$result->$property : NULL;
694 if (!empty($result->postal_code_suffix
)) {
695 $row['postal_code'] .= "-" . $result->postal_code_suffix
;
698 if ($output != CRM_Core_Selector_Controller
::EXPORT
&&
699 $this->_searchContext
== 'smog'
701 if (empty($result->status
) &&
704 $contactID = $result->contact_id
;
707 'contact_id' => $contactID,
708 'group_id' => $groupID,
711 $gcDefaults = array();
712 CRM_Core_DAO
::commonRetrieve('CRM_Contact_DAO_GroupContact', $gcParams, $gcDefaults);
714 if (empty($gcDefaults)) {
715 $row['status'] = ts('Smart');
718 $row['status'] = $gc[$gcDefaults['status']];
722 $row['status'] = NULL;
726 $row['status'] = $gc[$result->status
];
730 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
731 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contact_id
;
733 if (!empty($this->_formValues
['deleted_contacts']) && CRM_Core_Permission
::check('access deleted contacts')
737 'name' => ts('View'),
738 'url' => 'civicrm/contact/view',
739 'qs' => 'reset=1&cid=%%id%%',
740 'class' => 'no-popup',
741 'title' => ts('View Contact Details'),
744 'name' => ts('Restore'),
745 'url' => 'civicrm/contact/view/delete',
746 'qs' => 'reset=1&cid=%%id%%&restore=1',
747 'title' => ts('Restore Contact'),
750 if (CRM_Core_Permission
::check('delete contacts')) {
752 'name' => ts('Delete Permanently'),
753 'url' => 'civicrm/contact/view/delete',
754 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
755 'title' => ts('Permanently Delete Contact'),
758 $row['action'] = CRM_Core_Action
::formLink(
761 array('id' => $result->contact_id
),
764 'contact.selector.row',
769 elseif ((is_numeric(CRM_Utils_Array
::value('geo_code_1', $row))) ||
770 ($config->mapGeoCoding
&& !empty($row['city']) &&
771 CRM_Utils_Array
::value('state_province', $row)
774 $row['action'] = CRM_Core_Action
::formLink(
777 array('id' => $result->contact_id
),
780 'contact.selector.row',
786 $row['action'] = CRM_Core_Action
::formLink(
789 array('id' => $result->contact_id
),
792 'contact.selector.row',
798 // allow components to add more actions
799 CRM_Core_Component
::searchAction($row, $result->contact_id
);
801 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
802 $result->contact_sub_type
: $result->contact_type
,
807 $row['contact_type_orig'] = $result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
;
808 $row['contact_sub_type'] = $result->contact_sub_type ?
809 CRM_Contact_BAO_ContactType
::contactTypePairs(FALSE, $result->contact_sub_type
, ', ') : $result->contact_sub_type
;
810 $row['contact_id'] = $result->contact_id
;
811 $row['sort_name'] = $result->sort_name
;
812 if (array_key_exists('id', $row)) {
813 $row['id'] = $result->contact_id
;
818 if (in_array($row['contact_id'], $seenIDs) === FALSE) {
819 $seenIDs[] = $row['contact_id'];
832 function buildPrevNextCache($sort) {
833 $cacheKey = 'civicrm search ' . $this->_key
;
835 // We should clear the cache in following conditions:
836 // 1. when starting from scratch, i.e new search
837 // 2. if records are sorted
839 // get current page requested
840 $pageNum = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
842 // get the current sort order
843 $currentSortID = CRM_Utils_Request
::retrieve('crmSID', 'String', CRM_Core_DAO
::$_nullObject);
845 $session = CRM_Core_Session
::singleton();
847 // get previous sort id
848 $previousSortID = $session->get('previousSortID');
850 // check for current != previous to ensure cache is not reset if paging is done without changing
852 if (!$pageNum ||
(!empty($currentSortID) && $currentSortID != $previousSortID) ) {
853 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKey, 'civicrm_contact');
854 // this means it's fresh search, so set pageNum=1
860 // set the current sort as previous sort
861 if (!empty($currentSortID)) {
862 $session->set('previousSortID', $currentSortID);
865 $pageSize = CRM_Utils_Request
::retrieve('crmRowCount', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, 50);
866 $firstRecord = ($pageNum - 1) * $pageSize;
868 //for alphabetic pagination selection save
869 $sortByCharacter = CRM_Utils_Request
::retrieve('sortByCharacter', 'String', CRM_Core_DAO
::$_nullObject);
871 //for text field pagination selection save
872 $countRow = CRM_Core_BAO_PrevNextCache
::getCount($cacheKey, NULL, "entity_table = 'civicrm_contact'");
874 // $sortByCharacter triggers a refresh in the prevNext cache
875 if ($sortByCharacter && $sortByCharacter != 'all') {
876 $cacheKey .= "_alphabet";
877 $this->fillupPrevNextCache($sort, $cacheKey);
879 elseif ($firstRecord >= $countRow) {
880 $this->fillupPrevNextCache($sort, $cacheKey, $countRow, 500);
888 function addActions(&$rows) {
889 $config = CRM_Core_Config
::singleton();
891 $permissions = array(CRM_Core_Permission
::getPermission());
892 if (CRM_Core_Permission
::check('delete contacts')) {
893 $permissions[] = CRM_Core_Permission
::DELETE
;
895 $mask = CRM_Core_Action
::mask($permissions);
896 // mask value to hide map link if there are not lat/long
897 $mapMask = $mask & 4095;
899 // mask value to hide map link if there are not lat/long
900 $mapMask = $mask & 4095;
902 $links = self
::links($this->_context
, $this->_contextMenu
, $this->_key
);
905 foreach ($rows as $id => & $row) {
906 if (!empty($this->_formValues
['deleted_contacts']) && CRM_Core_Permission
::check('access deleted contacts')
910 'name' => ts('View'),
911 'url' => 'civicrm/contact/view',
912 'qs' => 'reset=1&cid=%%id%%',
913 'class' => 'no-popup',
914 'title' => ts('View Contact Details'),
917 'name' => ts('Restore'),
918 'url' => 'civicrm/contact/view/delete',
919 'qs' => 'reset=1&cid=%%id%%&restore=1',
920 'title' => ts('Restore Contact'),
923 if (CRM_Core_Permission
::check('delete contacts')) {
925 'name' => ts('Delete Permanently'),
926 'url' => 'civicrm/contact/view/delete',
927 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
928 'title' => ts('Permanently Delete Contact'),
931 $row['action'] = CRM_Core_Action
::formLink(
934 array('id' => $row['contact_id']),
937 'contact.selector.actions',
942 elseif ((is_numeric(CRM_Utils_Array
::value('geo_code_1', $row))) ||
943 ($config->mapGeoCoding
&& !empty($row['city']) &&
944 CRM_Utils_Array
::value('state_province', $row)
947 $row['action'] = CRM_Core_Action
::formLink(
950 array('id' => $row['contact_id']),
953 'contact.selector.actions',
959 $row['action'] = CRM_Core_Action
::formLink(
962 array('id' => $row['contact_id']),
965 'contact.selector.actions',
971 // allow components to add more actions
972 CRM_Core_Component
::searchAction($row, $row['contact_id']);
974 if (!empty($row['contact_type_orig'])) {
975 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($row['contact_type_orig'],
976 FALSE, $row['contact_id']);
984 function removeActions(&$rows) {
985 foreach ($rows as $rid => & $rValue) {
986 unset($rValue['contact_type']);
987 unset($rValue['action']);
992 * @param object $sort
993 * @param string $cacheKey
997 function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = 500) {
999 // For custom searches, use the contactIDs method
1000 if (is_a($this, 'CRM_Contact_Selector_Custom')) {
1001 $sql = $this->_search
->contactIDs($start, $end, $sort, TRUE);
1002 $replaceSQL = "SELECT contact_a.id as contact_id";
1003 $coreSearch = FALSE;
1005 // For core searches use the searchQuery method
1007 $sql = $this->_query
->searchQuery($start, $end, $sort, FALSE, $this->_query
->_includeContactIds
,
1009 $replaceSQL = "SELECT contact_a.id as id";
1013 // due to limitations in our search query writer, the above query does not work
1014 // in cases where the query is being sorted on a non-contact table
1015 // this results in a fatal error :(
1016 // see below for the gross hack of trapping the error and not filling
1017 // the prev next cache in this situation
1018 // the other alternative of running the FULL query will just be incredibly inefficient
1019 // and slow things down way too much on large data sets / complex queries
1022 INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
1023 SELECT DISTINCT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
1026 $sql = str_replace($replaceSQL, $insertSQL, $sql);
1028 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
1029 $result = CRM_Core_DAO
::executeQuery($sql);
1032 if (is_a($result, 'DB_Error')) {
1033 // check if we get error during core search
1035 // in the case of error, try rebuilding cache using full sql which is used for search selector display
1036 // this fixes the bugs reported in CRM-13996 & CRM-14438
1037 $this->rebuildPreNextCache($start, $end, $sort, $cacheKey);
1040 // return if above query fails
1045 // also record an entry in the cache key table, so we can delete it periodically
1046 CRM_Core_BAO_Cache
::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
1050 * This function is called to rebuild prev next cache using full sql in case of core search ( excluding custom search)
1052 * @param int $start start for limit clause
1053 * @param int $end end for limit clause
1055 * @param string $cacheKey cache key
1057 * @internal param $object $sort sort object
1060 function rebuildPreNextCache($start, $end, $sort, $cacheKey) {
1061 // generate full SQL
1062 $sql = $this->_query
->searchQuery($start, $end, $sort, FALSE, $this->_query
->_includeContactIds
,
1063 FALSE, FALSE, TRUE);
1065 $dao = CRM_Core_DAO
::executeQuery($sql);
1067 // build insert query, note that currently we build cache for 500 contact records at a time, hence below approach
1068 $insertValues = array();
1069 while($dao->fetch()) {
1070 $insertValues[] = "('civicrm_contact', {$dao->contact_id}, {$dao->contact_id}, '{$cacheKey}', '" . CRM_Core_DAO
::escapeString($dao->sort_name
) . "')";
1073 //update pre/next cache using single insert query
1074 if (!empty($insertValues)) {
1075 $sql = 'INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES
1076 '.implode(',', $insertValues);
1078 $result = CRM_Core_DAO
::executeQuery($sql);
1083 * Given the current formValues, gets the query in local
1087 reference) $formValues submitted formValues
1089 * @return array $qill which contains an array of strings
1093 // the current internationalisation is bad, but should more or less work
1094 // for most of "European" languages
1095 public function getQILL() {
1096 return $this->_query
->qill();
1100 * name of export file.
1102 * @param string $output type of output
1104 * @return string name of the file
1106 function getExportFileName($output = 'csv') {
1107 return ts('CiviCRM Contact Search');
1111 * get colunmn headers for search selector
1114 * @return array $_columnHeaders
1117 private static function &_getColumnHeaders() {
1118 if (!isset(self
::$_columnHeaders)) {
1119 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1120 'address_options', TRUE, NULL, TRUE
1123 self
::$_columnHeaders = array(
1124 'contact_type' => array('desc' => ts('Contact Type')),
1125 'sort_name' => array(
1126 'name' => ts('Name'),
1127 'sort' => 'sort_name',
1128 'direction' => CRM_Utils_Sort
::ASCENDING
,
1132 $defaultAddress = array(
1133 'street_address' => array('name' => ts('Address')),
1135 'name' => ts('City'),
1137 'direction' => CRM_Utils_Sort
::DONTCARE
,
1139 'state_province' => array(
1140 'name' => ts('State'),
1141 'sort' => 'state_province',
1142 'direction' => CRM_Utils_Sort
::DONTCARE
,
1144 'postal_code' => array(
1145 'name' => ts('Postal'),
1146 'sort' => 'postal_code',
1147 'direction' => CRM_Utils_Sort
::DONTCARE
,
1150 'name' => ts('Country'),
1151 'sort' => 'country',
1152 'direction' => CRM_Utils_Sort
::DONTCARE
,
1156 foreach ($defaultAddress as $columnName => $column) {
1157 if (!empty($addressOptions[$columnName])) {
1158 self
::$_columnHeaders[$columnName] = $column;
1162 self
::$_columnHeaders['email'] = array(
1163 'name' => ts('Email'),
1165 'direction' => CRM_Utils_Sort
::DONTCARE
,
1168 self
::$_columnHeaders['phone'] = array('name' => ts('Phone'));
1170 return self
::$_columnHeaders;
1174 * @return CRM_Contact_BAO_Query
1176 function &getQuery() {
1177 return $this->_query
;
1181 * @return CRM_Contact_DAO_Contact
1183 function alphabetQuery() {
1184 return $this->_query
->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
1191 * @param null $displayRelationshipType
1192 * @param string $queryOperator
1194 * @return CRM_Contact_DAO_Contact
1196 function contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
1197 $sortOrder = &$this->getSortOrder($this->_action
);
1198 $sort = new CRM_Utils_Sort($sortOrder, $sortID);
1200 // rectify params to what proximity search expects if there is a value for prox_distance
1201 // CRM-7021 CRM-7905
1202 if (!empty($params)) {
1203 CRM_Contact_BAO_ProximityQuery
::fixInputParams($params);
1206 if (!$displayRelationshipType) {
1207 $query = new CRM_Contact_BAO_Query($params,
1208 $this->_returnProperties
,
1209 NULL, FALSE, FALSE, 1,
1210 FALSE, TRUE, TRUE, NULL,
1215 $query = new CRM_Contact_BAO_Query($params, $this->_returnProperties
,
1216 NULL, FALSE, FALSE, 1,
1217 FALSE, TRUE, TRUE, $displayRelationshipType,
1221 $value = $query->searchQuery(0, 0, $sort,
1222 FALSE, FALSE, FALSE,
1229 * @param $returnProperties
1233 function &makeProperties(&$returnProperties) {
1234 $properties = array();
1235 foreach ($returnProperties as $name => $value) {
1236 if ($name != 'location') {
1237 // special handling for group and tag
1238 if (in_array($name, array('group', 'tag'))) {
1242 // special handling for notes
1243 if (in_array($name, array('note', 'note_subject', 'note_body'))) {
1247 $properties[] = $name;
1250 // extract all the location stuff
1251 foreach ($value as $n => $v) {
1252 foreach ($v as $n1 => $v1) {
1253 if (!strpos('_id', $n1) && $n1 != 'location_type') {
1254 $properties[] = "{$n}-{$n1}";