3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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';
310 function &getColHeads($action = NULL, $output = NULL) {
312 $colHeads = self
::_getColumnHeaders();
314 $colHeads[] = array('desc' => ts('Actions'), 'name' => ts('Action'));
319 * returns the column headers as an array of tuples:
320 * (name, sortName (key to the sort array))
322 * @param string $action the action being performed
323 * @param enum $output what should the result set include (web/email/csv)
325 * @return array the column headers that need to be displayed
328 function &getColumnHeaders($action = NULL, $output = NULL) {
330 if ($output == CRM_Core_Selector_Controller
::EXPORT
) {
331 $csvHeaders = array(ts('Contact Id'), ts('Contact Type'));
332 foreach ($this->getColHeads($action, $output) as $column) {
333 if (array_key_exists('name', $column)) {
334 $csvHeaders[] = $column['name'];
337 $headers = $csvHeaders;
339 elseif ($output == CRM_Core_Selector_Controller
::SCREEN
) {
340 $csvHeaders = array(ts('Name'));
341 foreach ($this->getColHeads($action, $output) as $key => $column) {
342 if (array_key_exists('name', $column) &&
344 $column['name'] != ts('Name')
346 $csvHeaders[$key] = $column['name'];
349 $headers = $csvHeaders;
351 elseif ($this->_ufGroupID
) {
352 // we dont use the cached value of column headers
353 // since it potentially changed because of the profile selected
354 static $skipFields = array('group', 'tag');
355 $direction = CRM_Utils_Sort
::ASCENDING
;
357 if (!self
::$_columnHeaders) {
358 self
::$_columnHeaders = array(array('name' => ''),
360 'name' => ts('Name'),
361 'sort' => 'sort_name',
362 'direction' => CRM_Utils_Sort
::ASCENDING
,
366 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
368 foreach ($this->_fields
as $name => $field) {
369 if (CRM_Utils_Array
::value('in_selector', $field) &&
370 !in_array($name, $skipFields)
372 if (strpos($name, '-') !== FALSE) {
373 list($fieldName, $lType, $type) = CRM_Utils_System
::explode('-', $name, 3);
375 if ($lType == 'Primary') {
376 $locationTypeName = 1;
379 $locationTypeName = $locationTypes[$lType];
382 if (in_array($fieldName, array(
383 'phone', 'im', 'email'))) {
385 $name = "`$locationTypeName-$fieldName-$type`";
388 $name = "`$locationTypeName-$fieldName`";
392 $name = "`$locationTypeName-$fieldName`";
395 //to handle sort key for Internal contactId.CRM-2289
397 $name = 'contact_id';
400 self
::$_columnHeaders[] = array(
401 'name' => $field['title'],
403 'direction' => $direction,
405 $direction = CRM_Utils_Sort
::DONTCARE
;
410 // if we dont have any valid columns, dont add the implicit ones
411 // this allows the template to check on emptiness of column headers
413 self
::$_columnHeaders = array();
416 self
::$_columnHeaders[] = array('desc' => ts('Actions'), 'name' => ts('Action'));
419 $headers = self
::$_columnHeaders;
421 elseif (!empty($this->_returnProperties
)) {
422 self
::$_columnHeaders = array(array('name' => ''),
424 'name' => ts('Name'),
425 'sort' => 'sort_name',
426 'direction' => CRM_Utils_Sort
::ASCENDING
,
429 $properties = self
::makeProperties($this->_returnProperties
);
431 foreach ($properties as $prop) {
432 if ($prop == 'contact_type' ||
$prop == 'contact_sub_type' ||
$prop == 'sort_name') {
436 if (strpos($prop, '-')) {
437 list($loc, $fld, $phoneType) = CRM_Utils_System
::explode('-', $prop, 3);
438 $title = $this->_query
->_fields
[$fld]['title'];
439 if (trim($phoneType) && !is_numeric($phoneType) && strtolower($phoneType) != $fld) {
440 $title .= "-{$phoneType}";
444 elseif (isset($this->_query
->_fields
[$prop]) && isset($this->_query
->_fields
[$prop]['title'])) {
445 $title = $this->_query
->_fields
[$prop]['title'];
450 self
::$_columnHeaders[] = array('name' => $title, 'sort' => $prop);
452 self
::$_columnHeaders[] = array('name' => ts('Actions'));
453 $headers = self
::$_columnHeaders;
456 $headers = $this->getColHeads($action, $output);
463 * Returns total number of rows for the query.
467 * @return int Total number of rows
470 function getTotalCount($action) {
471 return $this->_query
->searchQuery(0, 0, NULL, TRUE);
475 * returns all the rows in the given offset and rowCount
477 * @param enum $action the action being performed
478 * @param int $offset the row number to start from
479 * @param int $rowCount the number of rows to return
480 * @param string $sort the sql string that describes the sort order
481 * @param enum $output what should the result set include (web/email/csv)
483 * @return int the total number of rows for this action
485 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
486 $config = CRM_Core_Config
::singleton();
488 if (($output == CRM_Core_Selector_Controller
::EXPORT ||
489 $output == CRM_Core_Selector_Controller
::SCREEN
491 $this->_formValues
['radio_ts'] == 'ts_sel'
493 $includeContactIds = TRUE;
496 $includeContactIds = FALSE;
499 // note the formvalues were given by CRM_Contact_Form_Search to us
500 // and contain the search criteria (parameters)
501 // note that the default action is basic
502 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
504 // process the result of the query
506 $permissions = array(CRM_Core_Permission
::getPermission());
507 if (CRM_Core_Permission
::check('delete contacts')) {
508 $permissions[] = CRM_Core_Permission
::DELETE
;
510 $mask = CRM_Core_Action
::mask($permissions);
512 // mask value to hide map link if there are not lat/long
513 $mapMask = $mask & 4095;
515 if ($this->_searchContext
== 'smog') {
516 $gc = CRM_Core_SelectValues
::groupContactStatus();
519 if ($this->_ufGroupID
) {
520 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
523 static $skipFields = array('group', 'tag');
524 foreach ($this->_fields
as $key => $field) {
526 CRM_Utils_Array
::value('in_selector', $field) &&
527 !in_array($key, $skipFields)
529 if (strpos($key, '-') !== FALSE) {
530 list($fieldName, $id, $type) = CRM_Utils_System
::explode('-', $key, 3);
532 if ($id == 'Primary') {
533 $locationTypeName = 1;
536 $locationTypeName = CRM_Utils_Array
::value($id, $locationTypes);
537 if (!$locationTypeName) {
542 $locationTypeName = str_replace(' ', '_', $locationTypeName);
543 if (in_array($fieldName, array(
544 'phone', 'im', 'email'))) {
546 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
549 $names[] = "{$locationTypeName}-{$fieldName}";
553 $names[] = "{$locationTypeName}-{$fieldName}";
557 $names[] = $field['name'];
564 elseif (!empty($this->_returnProperties
)) {
565 $names = self
::makeProperties($this->_returnProperties
);
568 $names = self
::$_properties;
571 $multipleSelectFields = array('preferred_communication_method' => 1);
573 $links = self
::links($this->_context
, $this->_contextMenu
, $this->_key
);
575 //check explicitly added contact to a Smart Group.
576 $groupID = CRM_Utils_Array
::key('1', $this->_formValues
['group']);
578 $pseudoconstants = array();
579 if (!empty($this->_fields
)) {
580 // get all the pseudoconstant values
581 foreach ($this->_fields
as $name => $values) {
582 if (isset($this->_fields
[$name]['pseudoconstant'])) {
583 $pseudoconstants[$name] =
585 'dbName' => $this->_fields
[$name]['name'],
586 'values' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', $name),
592 // for CRM-3157 purposes
593 if (in_array('country', $names)) {
594 $pseudoconstants['country'] = array(
595 'dbName' => 'country_id',
596 'values' => CRM_Core_PseudoConstant
::country()
600 if (in_array('state_province', $names)) {
601 $pseudoconstants['state_province'] = array(
602 'dbName' => 'state_province_id',
603 'values' => CRM_Core_PseudoConstant
::stateProvince()
607 if (in_array('world_region', $names)) {
608 $pseudoconstants['world_region'] = array(
609 'dbName' => 'world_region_id',
610 'values' => CRM_Core_PseudoConstant
::worldRegion()
615 while ($result->fetch()) {
618 // the columns we are interested in
619 foreach ($names as $property) {
620 if ($property == 'status') {
623 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($property)) {
624 $row[$property] = CRM_Core_BAO_CustomField
::getDisplayValue(
632 $multipleSelectFields &&
633 array_key_exists($property, $multipleSelectFields)
636 $paramsNew = array($key => $result->$property);
637 $name = array($key => array('newName' => $key, 'groupName' => $key));
639 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
640 $row[$key] = $paramsNew[$key];
642 elseif (strpos($property, '-im')) {
643 $row[$property] = $result->$property;
644 if (!empty($result->$property)) {
645 $imProviders = CRM_Core_PseudoConstant
::IMProvider();
646 $providerId = $property . "-provider_id";
647 $providerName = $imProviders[$result->$providerId];
648 $row[$property] = $result->$property . " ({$providerName})";
651 elseif (in_array($property, array(
652 'addressee', 'email_greeting', 'postal_greeting'))) {
653 $greeting = $property . '_display';
654 $row[$property] = $result->$greeting;
656 elseif (isset($pseudoconstants[$property])) {
657 $row[$property] = CRM_Utils_Array
::value(
658 $result->{$pseudoconstants[$property]['dbName']},
659 $pseudoconstants[$property]['values']
662 elseif (strpos($property, '-url') !== FALSE) {
664 $websiteKey = 'website-1';
665 $propertyArray = explode('-', $property);
666 $websiteFld = $websiteKey . '-' . array_pop($propertyArray);
667 if (!empty($result->$websiteFld)) {
668 $websiteTypes = CRM_Core_PseudoConstant
::websiteType();
669 $websiteType = $websiteTypes[$result->{"$websiteKey-website_type_id"}];
670 $websiteValue = $result->$websiteFld;
671 $websiteUrl = "<a href=\"{$websiteValue}\">{$websiteValue} ({$websiteType})</a>";
673 $row[$property] = $websiteUrl;
676 $row[$property] = isset($result->$property) ?
$result->$property : NULL;
680 if (!empty($result->postal_code_suffix
)) {
681 $row['postal_code'] .= "-" . $result->postal_code_suffix
;
684 if ($output != CRM_Core_Selector_Controller
::EXPORT
&&
685 $this->_searchContext
== 'smog'
687 if (empty($result->status
) &&
690 $contactID = $result->contact_id
;
693 'contact_id' => $contactID,
694 'group_id' => $groupID,
697 $gcDefaults = array();
698 CRM_Core_DAO
::commonRetrieve('CRM_Contact_DAO_GroupContact', $gcParams, $gcDefaults);
700 if (empty($gcDefaults)) {
701 $row['status'] = ts('Smart');
704 $row['status'] = $gc[$gcDefaults['status']];
708 $row['status'] = NULL;
712 $row['status'] = $gc[$result->status
];
716 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
717 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contact_id
;
719 if (CRM_Utils_Array
::value('deleted_contacts', $this->_formValues
)
720 && CRM_Core_Permission
::check('access deleted contacts')
724 'name' => ts('View'),
725 'url' => 'civicrm/contact/view',
726 'qs' => 'reset=1&cid=%%id%%',
727 'title' => ts('View Contact Details'),
730 'name' => ts('Restore'),
731 'url' => 'civicrm/contact/view/delete',
732 'qs' => 'reset=1&cid=%%id%%&restore=1',
733 'title' => ts('Restore Contact'),
736 if (CRM_Core_Permission
::check('delete contacts')) {
738 'name' => ts('Delete Permanently'),
739 'url' => 'civicrm/contact/view/delete',
740 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
741 'title' => ts('Permanently Delete Contact'),
744 $row['action'] = CRM_Core_Action
::formLink($links, NULL, array('id' => $result->contact_id
));
746 elseif ((is_numeric(CRM_Utils_Array
::value('geo_code_1', $row))) ||
747 ($config->mapGeoCoding
&&
748 CRM_Utils_Array
::value('city', $row) &&
749 CRM_Utils_Array
::value('state_province', $row)
752 $row['action'] = CRM_Core_Action
::formLink($links, $mask, array('id' => $result->contact_id
));
755 $row['action'] = CRM_Core_Action
::formLink($links, $mapMask, array('id' => $result->contact_id
));
758 // allow components to add more actions
759 CRM_Core_Component
::searchAction($row, $result->contact_id
);
761 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
762 $result->contact_sub_type
: $result->contact_type
,
767 $row['contact_type_orig'] = $result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
;
768 $row['contact_sub_type'] = $result->contact_sub_type ?
769 CRM_Contact_BAO_ContactType
::contactTypePairs(FALSE, $result->contact_sub_type
, ', ') : $result->contact_sub_type
;
770 $row['contact_id'] = $result->contact_id
;
771 $row['sort_name'] = $result->sort_name
;
772 if (array_key_exists('id', $row)) {
773 $row['id'] = $result->contact_id
;
778 if (in_array($row['contact_id'], $seenIDs) === FALSE) {
779 $seenIDs[] = $row['contact_id'];
784 $this->buildPrevNextCache($sort);
789 function buildPrevNextCache($sort) {
790 $cacheKey = CRM_Utils_Array
::value('qfKey', $this->_formValues
);
792 //for prev/next pagination
793 $crmPID = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
795 //for alphabetic pagination selection save
796 $sortByCharacter = CRM_Utils_Request
::retrieve('sortByCharacter', 'String', CRM_Core_DAO
::$_nullObject);
798 //for text field pagination selection save
799 $countRow = CRM_Core_BAO_PrevNextCache
::getCount("%civicrm search {$cacheKey}%", NULL, "entity_table = 'civicrm_contact'", "LIKE");
801 if (!$crmPID && $countRow == 0 && !$sortByCharacter) {
802 $this->fillupPrevNextCache($sort);
804 elseif ($sortByCharacter) {
805 $cacheKeyCharacter = "civicrm search {$cacheKey}_alphabet";
806 if ($sortByCharacter == 'all') {
807 //delete the alphabet key corresponding records in prevnext_cache
808 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKeyCharacter, 'civicrm_contact');
809 $cacheKeyCharacter = NULL;
811 $this->fillupPrevNextCache($sort, $cacheKeyCharacter);
815 function addActions(&$rows) {
816 $config = CRM_Core_Config
::singleton();
818 $permissions = array(CRM_Core_Permission
::getPermission());
819 if (CRM_Core_Permission
::check('delete contacts')) {
820 $permissions[] = CRM_Core_Permission
::DELETE
;
822 $mask = CRM_Core_Action
::mask($permissions);
823 // mask value to hide map link if there are not lat/long
824 $mapMask = $mask & 4095;
826 // mask value to hide map link if there are not lat/long
827 $mapMask = $mask & 4095;
829 $links = self
::links($this->_context
, $this->_contextMenu
, $this->_key
);
832 foreach ($rows as $id => & $row) {
833 if (CRM_Utils_Array
::value('deleted_contacts', $this->_formValues
)
834 && CRM_Core_Permission
::check('access deleted contacts')
838 'name' => ts('View'),
839 'url' => 'civicrm/contact/view',
840 'qs' => 'reset=1&cid=%%id%%',
841 'title' => ts('View Contact Details'),
844 'name' => ts('Restore'),
845 'url' => 'civicrm/contact/view/delete',
846 'qs' => 'reset=1&cid=%%id%%&restore=1',
847 'title' => ts('Restore Contact'),
850 if (CRM_Core_Permission
::check('delete contacts')) {
852 'name' => ts('Delete Permanently'),
853 'url' => 'civicrm/contact/view/delete',
854 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1',
855 'title' => ts('Permanently Delete Contact'),
858 $row['action'] = CRM_Core_Action
::formLink($links, NULL, array('id' => $row['contact_id']));
860 elseif ((is_numeric(CRM_Utils_Array
::value('geo_code_1', $row))) ||
861 ($config->mapGeoCoding
&&
862 CRM_Utils_Array
::value('city', $row) &&
863 CRM_Utils_Array
::value('state_province', $row)
866 $row['action'] = CRM_Core_Action
::formLink($links, $mask, array('id' => $row['contact_id']));
869 $row['action'] = CRM_Core_Action
::formLink($links, $mapMask, array('id' => $row['contact_id']));
872 // allow components to add more actions
873 CRM_Core_Component
::searchAction($row, $row['contact_id']);
875 if (!empty($row['contact_type_orig'])) {
876 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($row['contact_type_orig'],
877 FALSE, $row['contact_id']);
882 function removeActions(&$rows) {
883 foreach ($rows as $rid => & $rValue) {
884 unset($rValue['contact_type']);
885 unset($rValue['action']);
889 function fillupPrevNextCache($sort, $cacheKey = NULL) {
891 $cacheKey = "civicrm search {$this->_key}";
894 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKey, 'civicrm_contact');
896 // lets fill up the prev next cache here, so view can scroll thru
897 if (is_a($this, 'CRM_Contact_Selector_Custom')) {
898 $sql = $this->_search
->contactIDs(0, 0, $sort, TRUE);
899 $replaceSQL = "SELECT contact_a.id as contact_id";
902 $sql = $this->_query
->searchQuery(
905 FALSE, TRUE, TRUE, NULL
907 $replaceSQL = "SELECT contact_a.id as id";
911 // due to limitations in our search query writer, the above query does not work
912 // in cases where the query is being sorted on a non-contact table
913 // this results in a fatal error :(
914 // see below for the gross hack of trapping the error and not filling
915 // the prev next cache in this situation
916 // the other alternative of running the FULL query will just be incredibly inefficient
917 // and slow things down way too much on large data sets / complex queries
920 INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
921 SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
924 $sql = str_replace($replaceSQL, $insertSQL, $sql);
927 CRM_Core_Error
::ignoreException();
928 $result = CRM_Core_DAO
::executeQuery($sql);
929 CRM_Core_Error
::setCallback();
931 if (is_a($result, 'DB_Error')) {
932 // oops the above query failed, so lets just ignore it
934 // we print a sorry cant figure it out on view page
938 // also record an entry in the cache key table, so we can delete it periodically
939 CRM_Core_BAO_Cache
::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
943 * Given the current formValues, gets the query in local
947 reference) $formValues submitted formValues
949 * @return array $qill which contains an array of strings
953 // the current internationalisation is bad, but should more or less work
954 // for most of "European" languages
955 public function getQILL() {
956 return $this->_query
->qill();
960 * name of export file.
962 * @param string $output type of output
964 * @return string name of the file
966 function getExportFileName($output = 'csv') {
967 return ts('CiviCRM Contact Search');
971 * get colunmn headers for search selector
974 * @return array $_columnHeaders
977 private static function &_getColumnHeaders() {
978 if (!isset(self
::$_columnHeaders)) {
979 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
980 'address_options', TRUE, NULL, TRUE
983 self
::$_columnHeaders = array('contact_type' => array('desc' => ts('Contact Type')),
984 'sort_name' => array(
985 'name' => ts('Name'),
986 'sort' => 'sort_name',
987 'direction' => CRM_Utils_Sort
::ASCENDING
,
991 $defaultAddress = array('street_address' => array('name' => ts('Address')),
992 'city' => array('name' => ts('City'),
994 'direction' => CRM_Utils_Sort
::DONTCARE
,
996 'state_province' => array('name' => ts('State'),
997 'sort' => 'state_province',
998 'direction' => CRM_Utils_Sort
::DONTCARE
,
1000 'postal_code' => array('name' => ts('Postal'),
1001 'sort' => 'postal_code',
1002 'direction' => CRM_Utils_Sort
::DONTCARE
,
1004 'country' => array('name' => ts('Country'),
1005 'sort' => 'country',
1006 'direction' => CRM_Utils_Sort
::DONTCARE
,
1010 foreach ($defaultAddress as $columnName => $column) {
1011 if (CRM_Utils_Array
::value($columnName, $addressOptions)) {
1012 self
::$_columnHeaders[$columnName] = $column;
1016 self
::$_columnHeaders['email'] = array('name' => ts('Email'),
1018 'direction' => CRM_Utils_Sort
::DONTCARE
,
1021 self
::$_columnHeaders['phone'] = array('name' => ts('Phone'));
1023 return self
::$_columnHeaders;
1026 function &getQuery() {
1027 return $this->_query
;
1030 function alphabetQuery() {
1031 return $this->_query
->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
1034 function contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
1035 $sortOrder = &$this->getSortOrder($this->_action
);
1036 $sort = new CRM_Utils_Sort($sortOrder, $sortID);
1038 // rectify params to what proximity search expects if there is a value for prox_distance
1039 // CRM-7021 CRM-7905
1040 if (!empty($params)) {
1041 CRM_Contact_BAO_ProximityQuery
::fixInputParams($params);
1044 if (!$displayRelationshipType) {
1045 $query = new CRM_Contact_BAO_Query($params,
1046 $this->_returnProperties
,
1047 NULL, FALSE, FALSE, 1,
1048 FALSE, TRUE, TRUE, NULL,
1053 $query = new CRM_Contact_BAO_Query($params, $this->_returnProperties
,
1054 NULL, FALSE, FALSE, 1,
1055 FALSE, TRUE, TRUE, $displayRelationshipType,
1059 $value = $query->searchQuery(0, 0, $sort,
1060 FALSE, FALSE, FALSE,
1066 function &makeProperties(&$returnProperties) {
1067 $properties = array();
1068 foreach ($returnProperties as $name => $value) {
1069 if ($name != 'location') {
1070 $properties[] = $name;
1073 // extract all the location stuff
1074 foreach ($value as $n => $v) {
1075 foreach ($v as $n1 => $v1) {
1076 if (!strpos('_id', $n1) && $n1 != 'location_type') {
1077 $properties[] = "{$n}-{$n1}";