3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 array $formValues array of form values imported
138 * @param array $params array of parameters for query
139 * @param int $action - action of search basic or advanced.
141 * @return CRM_Contact_Selector
144 function __construct(
148 $returnProperties = NULL,
149 $action = CRM_Core_Action
::NONE
,
150 $includeContactIds = FALSE,
151 $searchDescendentGroups = TRUE,
152 $searchContext = 'search',
155 //don't build query constructor, if form is not submitted
156 $force = CRM_Utils_Request
::retrieve('force', 'Boolean', CRM_Core_DAO
::$_nullObject);
157 if (empty($formValues) && !$force) {
161 // submitted form values
162 $this->_formValues
= &$formValues;
163 $this->_params
= &$params;
164 $this->_returnProperties
= &$returnProperties;
165 $this->_contextMenu
= &$contextMenu;
166 $this->_context
= $searchContext;
169 $this->_action
= $action;
171 $this->_searchContext
= $searchContext;
173 $this->_ufGroupID
= CRM_Utils_Array
::value('uf_group_id', $this->_formValues
);
175 if ($this->_ufGroupID
) {
176 $this->_fields
= CRM_Core_BAO_UFGroup
::getListingFields(CRM_Core_Action
::VIEW
,
177 CRM_Core_BAO_UFGroup
::PUBLIC_VISIBILITY |
178 CRM_Core_BAO_UFGroup
::LISTINGS_VISIBILITY
,
179 FALSE, $this->_ufGroupID
181 self
::$_columnHeaders = NULL;
183 $this->_customFields
= CRM_Core_BAO_CustomField
::getFieldsForImport('Individual');
185 $this->_returnProperties
= CRM_Contact_BAO_Contact
::makeHierReturnProperties($this->_fields
);
186 $this->_returnProperties
['contact_type'] = 1;
187 $this->_returnProperties
['contact_sub_type'] = 1;
188 $this->_returnProperties
['sort_name'] = 1;
191 $displayRelationshipType = CRM_Utils_Array
::value('display_relationship_type', $this->_formValues
);
192 $operator = CRM_Utils_Array
::value('operator', $this->_formValues
, 'AND');
194 // rectify params to what proximity search expects if there is a value for prox_distance
196 if (!empty($this->_params
)) {
197 CRM_Contact_BAO_ProximityQuery
::fixInputParams($this->_params
);
200 $this->_query
= new CRM_Contact_BAO_Query(
202 $this->_returnProperties
,
206 CRM_Contact_BAO_Query
::MODE_CONTACTS
,
208 $searchDescendentGroups,
210 $displayRelationshipType,
214 $this->_options
= &$this->_query
->_options
;
219 * This method returns the links that are given for each search row.
220 * currently the links added for each row are
229 static function &links() {
230 list($context, $contextMenu, $key) = func_get_args();
231 $extraParams = ($key) ?
"&key={$key}" : NULL;
232 $searchContext = ($context) ?
"&context=$context" : NULL;
234 if (!(self
::$_links)) {
235 self
::$_links = array(
236 CRM_Core_Action
::VIEW
=> array(
237 'name' => ts('View'),
238 'url' => 'civicrm/contact/view',
239 'class' => 'no-popup',
240 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}",
241 'title' => ts('View Contact Details'),
242 'ref' => 'view-contact',
244 CRM_Core_Action
::UPDATE
=> array(
245 'name' => ts('Edit'),
246 'url' => 'civicrm/contact/add',
247 'class' => 'no-popup',
248 'qs' => "reset=1&action=update&cid=%%id%%{$searchContext}{$extraParams}",
249 'title' => ts('Edit Contact Details'),
250 'ref' => 'edit-contact',
254 $config = CRM_Core_Config
::singleton();
255 if ($config->mapAPIKey
&& $config->mapProvider
) {
256 self
::$_links[CRM_Core_Action
::MAP
] = array(
258 'url' => 'civicrm/contact/map',
259 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}",
260 'title' => ts('Map Contact'),
264 // Adding Context Menu Links in more action
267 foreach ($contextMenu as $key => $value) {
268 $contextVal = '&context=' . $value['key'];
269 if ($value['key'] == 'delete') {
270 $contextVal = $searchContext;
272 $url = "civicrm/contact/view/{$value['key']}";
273 $qs = "reset=1&action=add&cid=%%id%%{$contextVal}{$extraParams}";
274 if ($value['key'] == 'activity') {
275 $qs = "action=browse&selectedChild=activity&reset=1&cid=%%id%%{$extraParams}";
277 elseif ($value['key'] == 'email') {
278 $url = "civicrm/contact/view/activity";
279 $qs = "atype=3&action=add&reset=1&cid=%%id%%{$extraParams}";
282 self
::$_links[$counter++
] = array(
283 'name' => $value['title'],
286 'title' => $value['title'],
287 'ref' => $value['ref'],
288 'class' => CRM_Utils_Array
::value('class', $value),
293 return self
::$_links;
298 * getter for array of the parameters required for creating pager.
303 function getPagerParams($action, &$params) {
304 $params['status'] = ts('Contact %%StatusMessage%%');
305 $params['csvString'] = NULL;
306 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
308 $params['buttonTop'] = 'PagerTopButton';
309 $params['buttonBottom'] = 'PagerBottomButton';
313 function &getColHeads($action = NULL, $output = NULL) {
314 $colHeads = self
::_getColumnHeaders();
315 $colHeads[] = array('desc' => ts('Actions'), 'name' => ts('Action'));
320 * returns the column headers as an array of tuples:
321 * (name, sortName (key to the sort array))
323 * @param string $action the action being performed
324 * @param enum $output what should the result set include (web/email/csv)
326 * @return array the column headers that need to be displayed
329 function &getColumnHeaders($action = NULL, $output = NULL) {
332 // unset return property elements that we don't care
333 if (!empty($this->_returnProperties
)) {
334 $doNotCareElements = array(
339 foreach ( $doNotCareElements as $value) {
340 unset($this->_returnProperties
[$value]);
344 if ($output == CRM_Core_Selector_Controller
::EXPORT
) {
345 $csvHeaders = array(ts('Contact Id'), ts('Contact Type'));
346 foreach ($this->getColHeads($action, $output) as $column) {
347 if (array_key_exists('name', $column)) {
348 $csvHeaders[] = $column['name'];
351 $headers = $csvHeaders;
353 elseif ($output == CRM_Core_Selector_Controller
::SCREEN
) {
354 $csvHeaders = array(ts('Name'));
355 foreach ($this->getColHeads($action, $output) as $key => $column) {
356 if (array_key_exists('name', $column) &&
358 $column['name'] != ts('Name')
360 $csvHeaders[$key] = $column['name'];
363 $headers = $csvHeaders;
365 elseif ($this->_ufGroupID
) {
366 // we dont use the cached value of column headers
367 // since it potentially changed because of the profile selected
368 static $skipFields = array('group', 'tag');
369 $direction = CRM_Utils_Sort
::ASCENDING
;
371 if (!self
::$_columnHeaders) {
372 self
::$_columnHeaders = array(array('name' => ''),
374 'name' => ts('Name'),
375 'sort' => 'sort_name',
376 'direction' => CRM_Utils_Sort
::ASCENDING
,
380 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
382 foreach ($this->_fields
as $name => $field) {
383 if (!empty($field['in_selector']) &&
384 !in_array($name, $skipFields)
386 if (strpos($name, '-') !== FALSE) {
387 list($fieldName, $lType, $type) = CRM_Utils_System
::explode('-', $name, 3);
389 if ($lType == 'Primary') {
390 $locationTypeName = 1;
393 $locationTypeName = $locationTypes[$lType];
396 if (in_array($fieldName, array(
397 'phone', 'im', 'email'))) {
399 $name = "`$locationTypeName-$fieldName-$type`";
402 $name = "`$locationTypeName-$fieldName`";
406 $name = "`$locationTypeName-$fieldName`";
409 //to handle sort key for Internal contactId.CRM-2289
411 $name = 'contact_id';
414 self
::$_columnHeaders[] = array(
415 'name' => $field['title'],
417 'direction' => $direction,
419 $direction = CRM_Utils_Sort
::DONTCARE
;
424 // if we dont have any valid columns, dont add the implicit ones
425 // this allows the template to check on emptiness of column headers
427 self
::$_columnHeaders = array();
430 self
::$_columnHeaders[] = array('desc' => ts('Actions'), 'name' => ts('Action'));
433 $headers = self
::$_columnHeaders;
435 elseif (!empty($this->_returnProperties
)) {
436 self
::$_columnHeaders = array(array('name' => ''),
438 'name' => ts('Name'),
439 'sort' => 'sort_name',
440 'direction' => CRM_Utils_Sort
::ASCENDING
,
443 $properties = self
::makeProperties($this->_returnProperties
);
445 foreach ($properties as $prop) {
446 if (strpos($prop, '-')) {
447 list($loc, $fld, $phoneType) = CRM_Utils_System
::explode('-', $prop, 3);
448 $title = $this->_query
->_fields
[$fld]['title'];
449 if (trim($phoneType) && !is_numeric($phoneType) && strtolower($phoneType) != $fld) {
450 $title .= "-{$phoneType}";
454 elseif (isset($this->_query
->_fields
[$prop]) && isset($this->_query
->_fields
[$prop]['title'])) {
455 $title = $this->_query
->_fields
[$prop]['title'];
461 self
::$_columnHeaders[] = array('name' => $title, 'sort' => $prop);
463 self
::$_columnHeaders[] = array('name' => ts('Actions'));
464 $headers = self
::$_columnHeaders;
467 $headers = $this->getColHeads($action, $output);
474 * Returns total number of rows for the query.
478 * @return int Total number of rows
481 function getTotalCount($action) {
482 // Use count from cache during paging/sorting
483 if (!empty($_GET['crmPID']) ||
!empty($_GET['crmSID'])) {
484 $count = CRM_Core_BAO_Cache
::getItem('Search Results Count', $this->_key
);
487 $count = $this->_query
->searchQuery(0, 0, NULL, TRUE);
488 CRM_Core_BAO_Cache
::setItem($count, 'Search Results Count', $this->_key
);
494 * returns all the rows in the given offset and rowCount
496 * @param enum $action the action being performed
497 * @param int $offset the row number to start from
498 * @param int $rowCount the number of rows to return
499 * @param string $sort the sql string that describes the sort order
500 * @param enum $output what should the result set include (web/email/csv)
502 * @return int the total number of rows for this action
504 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
505 $config = CRM_Core_Config
::singleton();
507 if (($output == CRM_Core_Selector_Controller
::EXPORT ||
508 $output == CRM_Core_Selector_Controller
::SCREEN
510 $this->_formValues
['radio_ts'] == 'ts_sel'
512 $includeContactIds = TRUE;
515 $includeContactIds = FALSE;
518 // note the formvalues were given by CRM_Contact_Form_Search to us
519 // and contain the search criteria (parameters)
520 // note that the default action is basic
522 $cacheKey = $this->buildPrevNextCache($sort);
523 $result = $this->_query
->getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds);
525 // CRM-13996: result is empty when selector columns are sorted. hence we need to run the query again
526 if ( $result->N
== 0) {
527 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
531 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
534 // process the result of the query
536 $permissions = array(CRM_Core_Permission
::getPermission());
537 if (CRM_Core_Permission
::check('delete contacts')) {
538 $permissions[] = CRM_Core_Permission
::DELETE
;
540 $mask = CRM_Core_Action
::mask($permissions);
542 // mask value to hide map link if there are not lat/long
543 $mapMask = $mask & 4095;
545 if ($this->_searchContext
== 'smog') {
546 $gc = CRM_Core_SelectValues
::groupContactStatus();
549 if ($this->_ufGroupID
) {
550 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
553 static $skipFields = array('group', 'tag');
554 foreach ($this->_fields
as $key => $field) {
555 if (!empty($field['in_selector']) &&
556 !in_array($key, $skipFields)
558 if (strpos($key, '-') !== FALSE) {
559 list($fieldName, $id, $type) = CRM_Utils_System
::explode('-', $key, 3);
561 if ($id == 'Primary') {
562 $locationTypeName = 1;
565 $locationTypeName = CRM_Utils_Array
::value($id, $locationTypes);
566 if (!$locationTypeName) {
571 $locationTypeName = str_replace(' ', '_', $locationTypeName);
572 if (in_array($fieldName, array(
573 'phone', 'im', 'email'))) {
575 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
578 $names[] = "{$locationTypeName}-{$fieldName}";
582 $names[] = "{$locationTypeName}-{$fieldName}";
586 $names[] = $field['name'];
593 elseif (!empty($this->_returnProperties
)) {
594 $names = self
::makeProperties($this->_returnProperties
);
597 $names = self
::$_properties;
600 $multipleSelectFields = array('preferred_communication_method' => 1);
602 $links = self
::links($this->_context
, $this->_contextMenu
, $this->_key
);
604 //check explicitly added contact to a Smart Group.
605 $groupID = CRM_Utils_Array
::key('1', $this->_formValues
['group']);
607 $pseudoconstants = array();
608 // for CRM-3157 purposes
609 if (in_array('world_region', $names)) {
610 $pseudoconstants['world_region'] = array(
611 'dbName' => 'world_region_id',
612 'values' => CRM_Core_PseudoConstant
::worldRegion()
617 while ($result->fetch()) {
619 $this->_query
->convertToPseudoNames($result);
621 // the columns we are interested in
622 foreach ($names as $property) {
623 if ($property == 'status') {
626 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($property)) {
627 $row[$property] = CRM_Core_BAO_CustomField
::getDisplayValue(
635 $multipleSelectFields &&
636 array_key_exists($property, $multipleSelectFields)
639 $paramsNew = array($key => $result->$property);
640 $name = array($key => array('newName' => $key, 'groupName' => $key));
642 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
643 $row[$key] = $paramsNew[$key];
645 elseif (strpos($property, '-im')) {
646 $row[$property] = $result->$property;
647 if (!empty($result->$property)) {
648 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
649 $providerId = $property . "-provider_id";
650 $providerName = $imProviders[$result->$providerId];
651 $row[$property] = $result->$property . " ({$providerName})";
654 elseif (in_array($property, array(
655 'addressee', 'email_greeting', 'postal_greeting'))) {
656 $greeting = $property . '_display';
657 $row[$property] = $result->$greeting;
659 elseif ($property == 'state_province') {
660 $row[$property] = $result->state_province_name
;
662 elseif (isset($pseudoconstants[$property])) {
663 $row[$property] = CRM_Utils_Array
::value(
664 $result->{$pseudoconstants[$property]['dbName']},
665 $pseudoconstants[$property]['values']
668 elseif (strpos($property, '-url') !== FALSE) {
670 $websiteKey = 'website-1';
671 $propertyArray = explode('-', $property);
672 $websiteFld = $websiteKey . '-' . array_pop($propertyArray);
673 if (!empty($result->$websiteFld)) {
674 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
675 $websiteType = $websiteTypes[$result->{"$websiteKey-website_type_id"}];
676 $websiteValue = $result->$websiteFld;
677 $websiteUrl = "<a href=\"{$websiteValue}\">{$websiteValue} ({$websiteType})</a>";
679 $row[$property] = $websiteUrl;
682 $row[$property] = isset($result->$property) ?
$result->$property : NULL;
686 if (!empty($result->postal_code_suffix
)) {
687 $row['postal_code'] .= "-" . $result->postal_code_suffix
;
690 if ($output != CRM_Core_Selector_Controller
::EXPORT
&&
691 $this->_searchContext
== 'smog'
693 if (empty($result->status
) &&
696 $contactID = $result->contact_id
;
699 'contact_id' => $contactID,
700 'group_id' => $groupID,
703 $gcDefaults = array();
704 CRM_Core_DAO
::commonRetrieve('CRM_Contact_DAO_GroupContact', $gcParams, $gcDefaults);
706 if (empty($gcDefaults)) {
707 $row['status'] = ts('Smart');
710 $row['status'] = $gc[$gcDefaults['status']];
714 $row['status'] = NULL;
718 $row['status'] = $gc[$result->status
];
722 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
723 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contact_id
;
725 if (!empty($this->_formValues
['deleted_contacts']) && CRM_Core_Permission
::check('access deleted contacts')
729 'name' => ts('View'),
730 'url' => 'civicrm/contact/view',
731 'qs' => 'reset=1&cid=%%id%%',
732 'class' => 'no-popup',
733 'title' => ts('View Contact Details'),
736 'name' => ts('Restore'),
737 'url' => 'civicrm/contact/view/delete',
738 'qs' => 'reset=1&cid=%%id%%&restore=1',
739 'title' => ts('Restore Contact'),
742 if (CRM_Core_Permission
::check('delete contacts')) {
744 'name' => ts('Delete Permanently'),
745 'url' => 'civicrm/contact/view/delete',
746 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
747 'title' => ts('Permanently Delete Contact'),
750 $row['action'] = CRM_Core_Action
::formLink(
753 array('id' => $result->contact_id
),
756 'contact.selector.row',
761 elseif ((is_numeric(CRM_Utils_Array
::value('geo_code_1', $row))) ||
762 ($config->mapGeoCoding
&& !empty($row['city']) &&
763 CRM_Utils_Array
::value('state_province', $row)
766 $row['action'] = CRM_Core_Action
::formLink(
769 array('id' => $result->contact_id
),
772 'contact.selector.row',
778 $row['action'] = CRM_Core_Action
::formLink(
781 array('id' => $result->contact_id
),
784 'contact.selector.row',
790 // allow components to add more actions
791 CRM_Core_Component
::searchAction($row, $result->contact_id
);
793 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
794 $result->contact_sub_type
: $result->contact_type
,
799 $row['contact_type_orig'] = $result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
;
800 $row['contact_sub_type'] = $result->contact_sub_type ?
801 CRM_Contact_BAO_ContactType
::contactTypePairs(FALSE, $result->contact_sub_type
, ', ') : $result->contact_sub_type
;
802 $row['contact_id'] = $result->contact_id
;
803 $row['sort_name'] = $result->sort_name
;
804 if (array_key_exists('id', $row)) {
805 $row['id'] = $result->contact_id
;
810 if (in_array($row['contact_id'], $seenIDs) === FALSE) {
811 $seenIDs[] = $row['contact_id'];
819 function buildPrevNextCache($sort) {
820 $cacheKey = 'civicrm search ' . $this->_key
;
822 // Get current page requested
823 $pageNum = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
824 // When starting from scratch, clear any old cache
826 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKey, 'civicrm_contact');
830 $pageSize = CRM_Utils_Request
::retrieve('crmRowCount', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, 50);
831 $firstRecord = ($pageNum - 1) * $pageSize;
833 //for alphabetic pagination selection save
834 $sortByCharacter = CRM_Utils_Request
::retrieve('sortByCharacter', 'String', CRM_Core_DAO
::$_nullObject);
836 //for text field pagination selection save
837 $countRow = CRM_Core_BAO_PrevNextCache
::getCount($cacheKey, NULL, "entity_table = 'civicrm_contact'");
839 // $sortByCharacter triggers a refresh in the prevNext cache
840 if ($sortByCharacter && $sortByCharacter != 'all') {
841 $cacheKey .= "_alphabet";
842 $this->fillupPrevNextCache($sort, $cacheKey);
844 elseif ($firstRecord >= $countRow) {
845 $this->fillupPrevNextCache($sort, $cacheKey, $countRow, $firstRecord +
500);
850 function addActions(&$rows) {
851 $config = CRM_Core_Config
::singleton();
853 $permissions = array(CRM_Core_Permission
::getPermission());
854 if (CRM_Core_Permission
::check('delete contacts')) {
855 $permissions[] = CRM_Core_Permission
::DELETE
;
857 $mask = CRM_Core_Action
::mask($permissions);
858 // mask value to hide map link if there are not lat/long
859 $mapMask = $mask & 4095;
861 // mask value to hide map link if there are not lat/long
862 $mapMask = $mask & 4095;
864 $links = self
::links($this->_context
, $this->_contextMenu
, $this->_key
);
867 foreach ($rows as $id => & $row) {
868 if (!empty($this->_formValues
['deleted_contacts']) && CRM_Core_Permission
::check('access deleted contacts')
872 'name' => ts('View'),
873 'url' => 'civicrm/contact/view',
874 'qs' => 'reset=1&cid=%%id%%',
875 'title' => ts('View Contact Details'),
878 'name' => ts('Restore'),
879 'url' => 'civicrm/contact/view/delete',
880 'qs' => 'reset=1&cid=%%id%%&restore=1',
881 'title' => ts('Restore Contact'),
884 if (CRM_Core_Permission
::check('delete contacts')) {
886 'name' => ts('Delete Permanently'),
887 'url' => 'civicrm/contact/view/delete',
888 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
889 'title' => ts('Permanently Delete Contact'),
892 $row['action'] = CRM_Core_Action
::formLink(
895 array('id' => $row['contact_id']),
898 'contact.selector.actions',
903 elseif ((is_numeric(CRM_Utils_Array
::value('geo_code_1', $row))) ||
904 ($config->mapGeoCoding
&& !empty($row['city']) &&
905 CRM_Utils_Array
::value('state_province', $row)
908 $row['action'] = CRM_Core_Action
::formLink(
911 array('id' => $row['contact_id']),
914 'contact.selector.actions',
920 $row['action'] = CRM_Core_Action
::formLink(
923 array('id' => $row['contact_id']),
926 'contact.selector.actions',
932 // allow components to add more actions
933 CRM_Core_Component
::searchAction($row, $row['contact_id']);
935 if (!empty($row['contact_type_orig'])) {
936 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($row['contact_type_orig'],
937 FALSE, $row['contact_id']);
942 function removeActions(&$rows) {
943 foreach ($rows as $rid => & $rValue) {
944 unset($rValue['contact_type']);
945 unset($rValue['action']);
950 * @param object $sort
951 * @param string $cacheKey
955 function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = 500) {
957 // For custom searches, use the contactIDs method
958 if (is_a($this, 'CRM_Contact_Selector_Custom')) {
959 $sql = $this->_search
->contactIDs($start, $end, $sort, TRUE);
960 $replaceSQL = "SELECT contact_a.id as contact_id";
962 // For core searches use the searchQuery method
964 $sql = $this->_query
->searchQuery(
967 FALSE, TRUE, TRUE, NULL
969 $replaceSQL = "SELECT contact_a.id as id";
973 // due to limitations in our search query writer, the above query does not work
974 // in cases where the query is being sorted on a non-contact table
975 // this results in a fatal error :(
976 // see below for the gross hack of trapping the error and not filling
977 // the prev next cache in this situation
978 // the other alternative of running the FULL query will just be incredibly inefficient
979 // and slow things down way too much on large data sets / complex queries
982 INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
983 SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
986 $sql = str_replace($replaceSQL, $insertSQL, $sql);
988 CRM_Core_Error
::ignoreException();
989 $result = CRM_Core_DAO
::executeQuery($sql);
990 CRM_Core_Error
::setCallback();
992 if (is_a($result, 'DB_Error')) {
993 // oops the above query failed, so lets just ignore it
995 // we print a sorry cant figure it out on view page
999 // also record an entry in the cache key table, so we can delete it periodically
1000 CRM_Core_BAO_Cache
::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
1004 * Given the current formValues, gets the query in local
1008 reference) $formValues submitted formValues
1010 * @return array $qill which contains an array of strings
1014 // the current internationalisation is bad, but should more or less work
1015 // for most of "European" languages
1016 public function getQILL() {
1017 return $this->_query
->qill();
1021 * name of export file.
1023 * @param string $output type of output
1025 * @return string name of the file
1027 function getExportFileName($output = 'csv') {
1028 return ts('CiviCRM Contact Search');
1032 * get colunmn headers for search selector
1035 * @return array $_columnHeaders
1038 private static function &_getColumnHeaders() {
1039 if (!isset(self
::$_columnHeaders)) {
1040 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1041 'address_options', TRUE, NULL, TRUE
1044 self
::$_columnHeaders = array(
1045 'contact_type' => array('desc' => ts('Contact Type')),
1046 'sort_name' => array(
1047 'name' => ts('Name'),
1048 'sort' => 'sort_name',
1049 'direction' => CRM_Utils_Sort
::ASCENDING
,
1053 $defaultAddress = array(
1054 'street_address' => array('name' => ts('Address')),
1056 'name' => ts('City'),
1058 'direction' => CRM_Utils_Sort
::DONTCARE
,
1060 'state_province' => array(
1061 'name' => ts('State'),
1062 'sort' => 'state_province',
1063 'direction' => CRM_Utils_Sort
::DONTCARE
,
1065 'postal_code' => array(
1066 'name' => ts('Postal'),
1067 'sort' => 'postal_code',
1068 'direction' => CRM_Utils_Sort
::DONTCARE
,
1071 'name' => ts('Country'),
1072 'sort' => 'country',
1073 'direction' => CRM_Utils_Sort
::DONTCARE
,
1077 foreach ($defaultAddress as $columnName => $column) {
1078 if (!empty($addressOptions[$columnName])) {
1079 self
::$_columnHeaders[$columnName] = $column;
1083 self
::$_columnHeaders['email'] = array(
1084 'name' => ts('Email'),
1086 'direction' => CRM_Utils_Sort
::DONTCARE
,
1089 self
::$_columnHeaders['phone'] = array('name' => ts('Phone'));
1091 return self
::$_columnHeaders;
1094 function &getQuery() {
1095 return $this->_query
;
1098 function alphabetQuery() {
1099 return $this->_query
->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
1102 function contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
1103 $sortOrder = &$this->getSortOrder($this->_action
);
1104 $sort = new CRM_Utils_Sort($sortOrder, $sortID);
1106 // rectify params to what proximity search expects if there is a value for prox_distance
1107 // CRM-7021 CRM-7905
1108 if (!empty($params)) {
1109 CRM_Contact_BAO_ProximityQuery
::fixInputParams($params);
1112 if (!$displayRelationshipType) {
1113 $query = new CRM_Contact_BAO_Query($params,
1114 $this->_returnProperties
,
1115 NULL, FALSE, FALSE, 1,
1116 FALSE, TRUE, TRUE, NULL,
1121 $query = new CRM_Contact_BAO_Query($params, $this->_returnProperties
,
1122 NULL, FALSE, FALSE, 1,
1123 FALSE, TRUE, TRUE, $displayRelationshipType,
1127 $value = $query->searchQuery(0, 0, $sort,
1128 FALSE, FALSE, FALSE,
1134 function &makeProperties(&$returnProperties) {
1135 $properties = array();
1136 foreach ($returnProperties as $name => $value) {
1137 if ($name != 'location') {
1138 // special handling for group and tag
1139 if (in_array($name, array('group', 'tag'))) {
1143 // special handling for notes
1144 if (in_array($name, array('note', 'note_subject', 'note_body'))) {
1148 $properties[] = $name;
1151 // extract all the location stuff
1152 foreach ($value as $n => $v) {
1153 foreach ($v as $n1 => $v1) {
1154 if (!strpos('_id', $n1) && $n1 != 'location_type') {
1155 $properties[] = "{$n}-{$n1}";