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 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}",
240 'title' => ts('View Contact Details'),
241 'ref' => 'view-contact',
243 CRM_Core_Action
::UPDATE
=> array(
244 'name' => ts('Edit'),
245 'url' => 'civicrm/contact/add',
246 'qs' => "reset=1&action=update&cid=%%id%%{$searchContext}{$extraParams}",
247 'title' => ts('Edit Contact Details'),
248 'ref' => 'edit-contact',
252 $config = CRM_Core_Config
::singleton();
253 if ($config->mapAPIKey
&& $config->mapProvider
) {
254 self
::$_links[CRM_Core_Action
::MAP
] = array(
256 'url' => 'civicrm/contact/map',
257 'qs' => "reset=1&cid=%%id%%{$searchContext}{$extraParams}",
258 'title' => ts('Map Contact'),
262 // Adding Context Menu Links in more action
265 foreach ($contextMenu as $key => $value) {
266 $contextVal = '&context=' . $value['key'];
267 if ($value['key'] == 'delete') {
268 $contextVal = $searchContext;
271 $url = "civicrm/contact/view/{$value['key']}";
272 $qs = "reset=1&action=add&cid=%%id%%{$contextVal}{$extraParams}";
273 if ($value['key'] == 'activity') {
274 $qs = "action=browse&selectedChild=activity&reset=1&cid=%%id%%{$extraParams}";
276 elseif ($value['key'] == 'email') {
277 $url = "civicrm/contact/view/activity";
278 $qs = "atype=3&action=add&reset=1&cid=%%id%%{$extraParams}";
281 self
::$_links[$counter++
] = array(
282 'name' => $value['title'],
285 'title' => $value['title'],
286 'ref' => $value['ref'],
291 return self
::$_links;
296 * getter for array of the parameters required for creating pager.
301 function getPagerParams($action, &$params) {
302 $params['status'] = ts('Contact %%StatusMessage%%');
303 $params['csvString'] = NULL;
304 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
306 $params['buttonTop'] = 'PagerTopButton';
307 $params['buttonBottom'] = 'PagerBottomButton';
311 function &getColHeads($action = NULL, $output = NULL) {
312 $colHeads = self
::_getColumnHeaders();
313 $colHeads[] = array('desc' => ts('Actions'), 'name' => ts('Action'));
318 * returns the column headers as an array of tuples:
319 * (name, sortName (key to the sort array))
321 * @param string $action the action being performed
322 * @param enum $output what should the result set include (web/email/csv)
324 * @return array the column headers that need to be displayed
327 function &getColumnHeaders($action = NULL, $output = NULL) {
329 if ($output == CRM_Core_Selector_Controller
::EXPORT
) {
330 $csvHeaders = array(ts('Contact Id'), ts('Contact Type'));
331 foreach ($this->getColHeads($action, $output) as $column) {
332 if (array_key_exists('name', $column)) {
333 $csvHeaders[] = $column['name'];
336 $headers = $csvHeaders;
338 elseif ($output == CRM_Core_Selector_Controller
::SCREEN
) {
339 $csvHeaders = array(ts('Name'));
340 foreach ($this->getColHeads($action, $output) as $key => $column) {
341 if (array_key_exists('name', $column) &&
343 $column['name'] != ts('Name')
345 $csvHeaders[$key] = $column['name'];
348 $headers = $csvHeaders;
350 elseif ($this->_ufGroupID
) {
351 // we dont use the cached value of column headers
352 // since it potentially changed because of the profile selected
353 static $skipFields = array('group', 'tag');
354 $direction = CRM_Utils_Sort
::ASCENDING
;
356 if (!self
::$_columnHeaders) {
357 self
::$_columnHeaders = array(array('name' => ''),
359 'name' => ts('Name'),
360 'sort' => 'sort_name',
361 'direction' => CRM_Utils_Sort
::ASCENDING
,
365 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
367 foreach ($this->_fields
as $name => $field) {
368 if (CRM_Utils_Array
::value('in_selector', $field) &&
369 !in_array($name, $skipFields)
371 if (strpos($name, '-') !== FALSE) {
372 list($fieldName, $lType, $type) = CRM_Utils_System
::explode('-', $name, 3);
374 if ($lType == 'Primary') {
375 $locationTypeName = 1;
378 $locationTypeName = $locationTypes[$lType];
381 if (in_array($fieldName, array(
382 'phone', 'im', 'email'))) {
384 $name = "`$locationTypeName-$fieldName-$type`";
387 $name = "`$locationTypeName-$fieldName`";
391 $name = "`$locationTypeName-$fieldName`";
394 //to handle sort key for Internal contactId.CRM-2289
396 $name = 'contact_id';
399 self
::$_columnHeaders[] = array(
400 'name' => $field['title'],
402 'direction' => $direction,
404 $direction = CRM_Utils_Sort
::DONTCARE
;
409 // if we dont have any valid columns, dont add the implicit ones
410 // this allows the template to check on emptiness of column headers
412 self
::$_columnHeaders = array();
415 self
::$_columnHeaders[] = array('desc' => ts('Actions'), 'name' => ts('Action'));
418 $headers = self
::$_columnHeaders;
420 elseif (!empty($this->_returnProperties
)) {
421 self
::$_columnHeaders = array(array('name' => ''),
423 'name' => ts('Name'),
424 'sort' => 'sort_name',
425 'direction' => CRM_Utils_Sort
::ASCENDING
,
428 $properties = self
::makeProperties($this->_returnProperties
);
430 foreach ($properties as $prop) {
431 if ($prop == 'contact_type' ||
$prop == 'contact_sub_type' ||
$prop == 'sort_name') {
435 if (strpos($prop, '-')) {
436 list($loc, $fld, $phoneType) = CRM_Utils_System
::explode('-', $prop, 3);
437 $title = $this->_query
->_fields
[$fld]['title'];
438 if (trim($phoneType) && !is_numeric($phoneType) && strtolower($phoneType) != $fld) {
439 $title .= "-{$phoneType}";
443 elseif (isset($this->_query
->_fields
[$prop]) && isset($this->_query
->_fields
[$prop]['title'])) {
444 $title = $this->_query
->_fields
[$prop]['title'];
449 self
::$_columnHeaders[] = array('name' => $title, 'sort' => $prop);
451 self
::$_columnHeaders[] = array('name' => ts('Actions'));
452 $headers = self
::$_columnHeaders;
455 $headers = $this->getColHeads($action, $output);
462 * Returns total number of rows for the query.
466 * @return int Total number of rows
469 function getTotalCount($action) {
470 // Use count from cache during paging/sorting
471 if (!empty($_GET['crmPID']) ||
!empty($_GET['crmSID'])) {
472 $count = CRM_Core_BAO_Cache
::getItem('Search Results Count', $this->_key
);
475 $count = $this->_query
->searchQuery(0, 0, NULL, TRUE);
476 CRM_Core_BAO_Cache
::setItem($count, 'Search Results Count', $this->_key
);
482 * returns all the rows in the given offset and rowCount
484 * @param enum $action the action being performed
485 * @param int $offset the row number to start from
486 * @param int $rowCount the number of rows to return
487 * @param string $sort the sql string that describes the sort order
488 * @param enum $output what should the result set include (web/email/csv)
490 * @return int the total number of rows for this action
492 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
493 $config = CRM_Core_Config
::singleton();
495 if (($output == CRM_Core_Selector_Controller
::EXPORT ||
496 $output == CRM_Core_Selector_Controller
::SCREEN
498 $this->_formValues
['radio_ts'] == 'ts_sel'
500 $includeContactIds = TRUE;
503 $includeContactIds = FALSE;
506 // note the formvalues were given by CRM_Contact_Form_Search to us
507 // and contain the search criteria (parameters)
508 // note that the default action is basic
510 $cacheKey = $this->buildPrevNextCache($sort);
511 $result = $this->_query
->getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds);
514 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
517 // process the result of the query
519 $permissions = array(CRM_Core_Permission
::getPermission());
520 if (CRM_Core_Permission
::check('delete contacts')) {
521 $permissions[] = CRM_Core_Permission
::DELETE
;
523 $mask = CRM_Core_Action
::mask($permissions);
525 // mask value to hide map link if there are not lat/long
526 $mapMask = $mask & 4095;
528 if ($this->_searchContext
== 'smog') {
529 $gc = CRM_Core_SelectValues
::groupContactStatus();
532 if ($this->_ufGroupID
) {
533 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
536 static $skipFields = array('group', 'tag');
537 foreach ($this->_fields
as $key => $field) {
539 CRM_Utils_Array
::value('in_selector', $field) &&
540 !in_array($key, $skipFields)
542 if (strpos($key, '-') !== FALSE) {
543 list($fieldName, $id, $type) = CRM_Utils_System
::explode('-', $key, 3);
545 if ($id == 'Primary') {
546 $locationTypeName = 1;
549 $locationTypeName = CRM_Utils_Array
::value($id, $locationTypes);
550 if (!$locationTypeName) {
555 $locationTypeName = str_replace(' ', '_', $locationTypeName);
556 if (in_array($fieldName, array(
557 'phone', 'im', 'email'))) {
559 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
562 $names[] = "{$locationTypeName}-{$fieldName}";
566 $names[] = "{$locationTypeName}-{$fieldName}";
570 $names[] = $field['name'];
577 elseif (!empty($this->_returnProperties
)) {
578 $names = self
::makeProperties($this->_returnProperties
);
581 $names = self
::$_properties;
584 $multipleSelectFields = array('preferred_communication_method' => 1);
586 $links = self
::links($this->_context
, $this->_contextMenu
, $this->_key
);
588 //check explicitly added contact to a Smart Group.
589 $groupID = CRM_Utils_Array
::key('1', $this->_formValues
['group']);
591 $pseudoconstants = array();
592 // for CRM-3157 purposes
593 if (in_array('world_region', $names)) {
594 $pseudoconstants['world_region'] = array(
595 'dbName' => 'world_region_id',
596 'values' => CRM_Core_PseudoConstant
::worldRegion()
601 while ($result->fetch()) {
603 $this->_query
->convertToPseudoNames($result);
605 // the columns we are interested in
606 foreach ($names as $property) {
607 if ($property == 'status') {
610 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($property)) {
611 $row[$property] = CRM_Core_BAO_CustomField
::getDisplayValue(
619 $multipleSelectFields &&
620 array_key_exists($property, $multipleSelectFields)
623 $paramsNew = array($key => $result->$property);
624 $name = array($key => array('newName' => $key, 'groupName' => $key));
626 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
627 $row[$key] = $paramsNew[$key];
629 elseif (strpos($property, '-im')) {
630 $row[$property] = $result->$property;
631 if (!empty($result->$property)) {
632 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
633 $providerId = $property . "-provider_id";
634 $providerName = $imProviders[$result->$providerId];
635 $row[$property] = $result->$property . " ({$providerName})";
638 elseif (in_array($property, array(
639 'addressee', 'email_greeting', 'postal_greeting'))) {
640 $greeting = $property . '_display';
641 $row[$property] = $result->$greeting;
643 elseif ($property == 'state_province') {
644 $row[$property] = $result->state_province_name
;
646 elseif (isset($pseudoconstants[$property])) {
647 $row[$property] = CRM_Utils_Array
::value(
648 $result->{$pseudoconstants[$property]['dbName']},
649 $pseudoconstants[$property]['values']
652 elseif (strpos($property, '-url') !== FALSE) {
654 $websiteKey = 'website-1';
655 $propertyArray = explode('-', $property);
656 $websiteFld = $websiteKey . '-' . array_pop($propertyArray);
657 if (!empty($result->$websiteFld)) {
658 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
659 $websiteType = $websiteTypes[$result->{"$websiteKey-website_type_id"}];
660 $websiteValue = $result->$websiteFld;
661 $websiteUrl = "<a href=\"{$websiteValue}\">{$websiteValue} ({$websiteType})</a>";
663 $row[$property] = $websiteUrl;
666 $row[$property] = isset($result->$property) ?
$result->$property : NULL;
670 if (!empty($result->postal_code_suffix
)) {
671 $row['postal_code'] .= "-" . $result->postal_code_suffix
;
674 if ($output != CRM_Core_Selector_Controller
::EXPORT
&&
675 $this->_searchContext
== 'smog'
677 if (empty($result->status
) &&
680 $contactID = $result->contact_id
;
683 'contact_id' => $contactID,
684 'group_id' => $groupID,
687 $gcDefaults = array();
688 CRM_Core_DAO
::commonRetrieve('CRM_Contact_DAO_GroupContact', $gcParams, $gcDefaults);
690 if (empty($gcDefaults)) {
691 $row['status'] = ts('Smart');
694 $row['status'] = $gc[$gcDefaults['status']];
698 $row['status'] = NULL;
702 $row['status'] = $gc[$result->status
];
706 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
707 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contact_id
;
709 if (CRM_Utils_Array
::value('deleted_contacts', $this->_formValues
)
710 && CRM_Core_Permission
::check('access deleted contacts')
714 'name' => ts('View'),
715 'url' => 'civicrm/contact/view',
716 'qs' => 'reset=1&cid=%%id%%',
717 'title' => ts('View Contact Details'),
720 'name' => ts('Restore'),
721 'url' => 'civicrm/contact/view/delete',
722 'qs' => 'reset=1&cid=%%id%%&restore=1',
723 'title' => ts('Restore Contact'),
726 if (CRM_Core_Permission
::check('delete contacts')) {
728 'name' => ts('Delete Permanently'),
729 'url' => 'civicrm/contact/view/delete',
730 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
731 'title' => ts('Permanently Delete Contact'),
734 $row['action'] = CRM_Core_Action
::formLink(
737 array('id' => $result->contact_id
),
740 'contact.selector.row',
745 elseif ((is_numeric(CRM_Utils_Array
::value('geo_code_1', $row))) ||
746 ($config->mapGeoCoding
&&
747 CRM_Utils_Array
::value('city', $row) &&
748 CRM_Utils_Array
::value('state_province', $row)
751 $row['action'] = CRM_Core_Action
::formLink(
754 array('id' => $result->contact_id
),
757 'contact.selector.row',
763 $row['action'] = CRM_Core_Action
::formLink(
766 array('id' => $result->contact_id
),
769 'contact.selector.row',
775 // allow components to add more actions
776 CRM_Core_Component
::searchAction($row, $result->contact_id
);
778 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
779 $result->contact_sub_type
: $result->contact_type
,
784 $row['contact_type_orig'] = $result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
;
785 $row['contact_sub_type'] = $result->contact_sub_type ?
786 CRM_Contact_BAO_ContactType
::contactTypePairs(FALSE, $result->contact_sub_type
, ', ') : $result->contact_sub_type
;
787 $row['contact_id'] = $result->contact_id
;
788 $row['sort_name'] = $result->sort_name
;
789 if (array_key_exists('id', $row)) {
790 $row['id'] = $result->contact_id
;
795 if (in_array($row['contact_id'], $seenIDs) === FALSE) {
796 $seenIDs[] = $row['contact_id'];
804 function buildPrevNextCache($sort) {
805 $cacheKey = 'civicrm search ' . $this->_key
;
807 // Get current page requested
808 $pageNum = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
809 // When starting from scratch, clear any old cache
811 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKey, 'civicrm_contact');
815 $pageSize = CRM_Utils_Request
::retrieve('crmRowCount', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, 50);
816 $firstRecord = ($pageNum - 1) * $pageSize;
818 //for alphabetic pagination selection save
819 $sortByCharacter = CRM_Utils_Request
::retrieve('sortByCharacter', 'String', CRM_Core_DAO
::$_nullObject);
821 //for text field pagination selection save
822 $countRow = CRM_Core_BAO_PrevNextCache
::getCount($cacheKey, NULL, "entity_table = 'civicrm_contact'");
824 // $sortByCharacter triggers a refresh in the prevNext cache
825 if ($sortByCharacter && $sortByCharacter != 'all') {
826 $cacheKey .= "_alphabet";
827 $this->fillupPrevNextCache($sort, $cacheKey);
829 elseif ($firstRecord >= $countRow) {
830 $this->fillupPrevNextCache($sort, $cacheKey, $countRow, $firstRecord +
500);
835 function addActions(&$rows) {
836 $config = CRM_Core_Config
::singleton();
838 $permissions = array(CRM_Core_Permission
::getPermission());
839 if (CRM_Core_Permission
::check('delete contacts')) {
840 $permissions[] = CRM_Core_Permission
::DELETE
;
842 $mask = CRM_Core_Action
::mask($permissions);
843 // mask value to hide map link if there are not lat/long
844 $mapMask = $mask & 4095;
846 // mask value to hide map link if there are not lat/long
847 $mapMask = $mask & 4095;
849 $links = self
::links($this->_context
, $this->_contextMenu
, $this->_key
);
852 foreach ($rows as $id => & $row) {
853 if (CRM_Utils_Array
::value('deleted_contacts', $this->_formValues
)
854 && CRM_Core_Permission
::check('access deleted contacts')
858 'name' => ts('View'),
859 'url' => 'civicrm/contact/view',
860 'qs' => 'reset=1&cid=%%id%%',
861 'title' => ts('View Contact Details'),
864 'name' => ts('Restore'),
865 'url' => 'civicrm/contact/view/delete',
866 'qs' => 'reset=1&cid=%%id%%&restore=1',
867 'title' => ts('Restore Contact'),
870 if (CRM_Core_Permission
::check('delete contacts')) {
872 'name' => ts('Delete Permanently'),
873 'url' => 'civicrm/contact/view/delete',
874 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
875 'title' => ts('Permanently Delete Contact'),
878 $row['action'] = CRM_Core_Action
::formLink(
881 array('id' => $row['contact_id']),
884 'contact.selector.actions',
889 elseif ((is_numeric(CRM_Utils_Array
::value('geo_code_1', $row))) ||
890 ($config->mapGeoCoding
&&
891 CRM_Utils_Array
::value('city', $row) &&
892 CRM_Utils_Array
::value('state_province', $row)
895 $row['action'] = CRM_Core_Action
::formLink(
898 array('id' => $row['contact_id']),
901 'contact.selector.actions',
907 $row['action'] = CRM_Core_Action
::formLink(
910 array('id' => $row['contact_id']),
913 'contact.selector.actions',
919 // allow components to add more actions
920 CRM_Core_Component
::searchAction($row, $row['contact_id']);
922 if (!empty($row['contact_type_orig'])) {
923 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($row['contact_type_orig'],
924 FALSE, $row['contact_id']);
929 function removeActions(&$rows) {
930 foreach ($rows as $rid => & $rValue) {
931 unset($rValue['contact_type']);
932 unset($rValue['action']);
937 * @param object $sort
938 * @param string $cacheKey
942 function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = 500) {
944 // For custom searches, use the contactIDs method
945 if (is_a($this, 'CRM_Contact_Selector_Custom')) {
946 $sql = $this->_search
->contactIDs($start, $end, $sort, TRUE);
947 $replaceSQL = "SELECT contact_a.id as contact_id";
949 // For core searches use the searchQuery method
951 $sql = $this->_query
->searchQuery(
954 FALSE, TRUE, TRUE, NULL
956 $replaceSQL = "SELECT contact_a.id as id";
960 // due to limitations in our search query writer, the above query does not work
961 // in cases where the query is being sorted on a non-contact table
962 // this results in a fatal error :(
963 // see below for the gross hack of trapping the error and not filling
964 // the prev next cache in this situation
965 // the other alternative of running the FULL query will just be incredibly inefficient
966 // and slow things down way too much on large data sets / complex queries
969 INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
970 SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
973 $sql = str_replace($replaceSQL, $insertSQL, $sql);
975 CRM_Core_Error
::ignoreException();
976 $result = CRM_Core_DAO
::executeQuery($sql);
977 CRM_Core_Error
::setCallback();
979 if (is_a($result, 'DB_Error')) {
980 // oops the above query failed, so lets just ignore it
982 // we print a sorry cant figure it out on view page
986 // also record an entry in the cache key table, so we can delete it periodically
987 CRM_Core_BAO_Cache
::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
991 * Given the current formValues, gets the query in local
995 reference) $formValues submitted formValues
997 * @return array $qill which contains an array of strings
1001 // the current internationalisation is bad, but should more or less work
1002 // for most of "European" languages
1003 public function getQILL() {
1004 return $this->_query
->qill();
1008 * name of export file.
1010 * @param string $output type of output
1012 * @return string name of the file
1014 function getExportFileName($output = 'csv') {
1015 return ts('CiviCRM Contact Search');
1019 * get colunmn headers for search selector
1022 * @return array $_columnHeaders
1025 private static function &_getColumnHeaders() {
1026 if (!isset(self
::$_columnHeaders)) {
1027 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
1028 'address_options', TRUE, NULL, TRUE
1031 self
::$_columnHeaders = array(
1032 'contact_type' => array('desc' => ts('Contact Type')),
1033 'sort_name' => array(
1034 'name' => ts('Name'),
1035 'sort' => 'sort_name',
1036 'direction' => CRM_Utils_Sort
::ASCENDING
,
1040 $defaultAddress = array(
1041 'street_address' => array('name' => ts('Address')),
1043 'name' => ts('City'),
1045 'direction' => CRM_Utils_Sort
::DONTCARE
,
1047 'state_province' => array(
1048 'name' => ts('State'),
1049 'sort' => 'state_province',
1050 'direction' => CRM_Utils_Sort
::DONTCARE
,
1052 'postal_code' => array(
1053 'name' => ts('Postal'),
1054 'sort' => 'postal_code',
1055 'direction' => CRM_Utils_Sort
::DONTCARE
,
1058 'name' => ts('Country'),
1059 'sort' => 'country',
1060 'direction' => CRM_Utils_Sort
::DONTCARE
,
1064 foreach ($defaultAddress as $columnName => $column) {
1065 if (CRM_Utils_Array
::value($columnName, $addressOptions)) {
1066 self
::$_columnHeaders[$columnName] = $column;
1070 self
::$_columnHeaders['email'] = array(
1071 'name' => ts('Email'),
1073 'direction' => CRM_Utils_Sort
::DONTCARE
,
1076 self
::$_columnHeaders['phone'] = array('name' => ts('Phone'));
1078 return self
::$_columnHeaders;
1081 function &getQuery() {
1082 return $this->_query
;
1085 function alphabetQuery() {
1086 return $this->_query
->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
1089 function contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
1090 $sortOrder = &$this->getSortOrder($this->_action
);
1091 $sort = new CRM_Utils_Sort($sortOrder, $sortID);
1093 // rectify params to what proximity search expects if there is a value for prox_distance
1094 // CRM-7021 CRM-7905
1095 if (!empty($params)) {
1096 CRM_Contact_BAO_ProximityQuery
::fixInputParams($params);
1099 if (!$displayRelationshipType) {
1100 $query = new CRM_Contact_BAO_Query($params,
1101 $this->_returnProperties
,
1102 NULL, FALSE, FALSE, 1,
1103 FALSE, TRUE, TRUE, NULL,
1108 $query = new CRM_Contact_BAO_Query($params, $this->_returnProperties
,
1109 NULL, FALSE, FALSE, 1,
1110 FALSE, TRUE, TRUE, $displayRelationshipType,
1114 $value = $query->searchQuery(0, 0, $sort,
1115 FALSE, FALSE, FALSE,
1121 function &makeProperties(&$returnProperties) {
1122 $properties = array();
1123 foreach ($returnProperties as $name => $value) {
1124 if ($name != 'location') {
1125 // special handling for group and tag
1126 if (in_array($name, array('group', 'tag'))) {
1130 // special handling for notes
1131 if (in_array($name, array('note', 'note_subject', 'note_body'))) {
1135 $properties[] = $name;
1138 // extract all the location stuff
1139 foreach ($value as $n => $v) {
1140 foreach ($v as $n1 => $v1) {
1141 if (!strpos('_id', $n1) && $n1 != 'location_type') {
1142 $properties[] = "{$n}-{$n1}";