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_Profile_Selector_Listings
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
45 * array of supported links, currenly 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 * The sql params we use to get the list of contacts
69 * the public visible fields to be shown to the user
77 * the custom fields for this domain
82 protected $_customFields;
85 * cache the query object
93 * cache the expanded options list if any
101 * The group id that we are editing
108 * Do we enable mapping of users
115 * Do we enable edit link
119 protected $_editLink;
122 * Should we link to the UF Profile
126 protected $_linkToUF;
129 * Store profile ids if multiple profile ids are passed using comma separated.
130 * Currently lets implement this functionality only for dialog mode
132 protected $_profileIds = array();
134 protected $_multiRecordTableName = NULL;
140 * @param $customFields
141 * @param string params the params for the where clause
144 * @param bool $editLink
145 * @param bool $linkToUF
147 * @return \CRM_Profile_Selector_Listings
150 function __construct(
158 $this->_params
= $params;
160 if (is_array($ufGroupIds)) {
161 $this->_profileIds
= $ufGroupIds;
162 $this->_gid
= $ufGroupIds[0];
165 $this->_profileIds
= array($ufGroupIds);
166 $this->_gid
= $ufGroupIds;
170 $this->_editLink
= $editLink;
171 $this->_linkToUF
= $linkToUF;
173 //get the details of the uf group
175 $groupId = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_UFGroup',
176 $this->_gid
, 'limit_listings_group_id'
180 // add group id to params if a uf group belong to a any group
182 if (!empty($this->_params
['group'])) {
183 $this->_params
['group'][$groupId] = 1;
186 $this->_params
['group'] = array($groupId => 1);
190 $this->_fields
= CRM_Core_BAO_UFGroup
::getListingFields(CRM_Core_Action
::VIEW
,
191 CRM_Core_BAO_UFGroup
::PUBLIC_VISIBILITY |
192 CRM_Core_BAO_UFGroup
::LISTINGS_VISIBILITY
,
193 FALSE, $this->_profileIds
196 $this->_customFields
= &$customFields;
198 $returnProperties = CRM_Contact_BAO_Contact
::makeHierReturnProperties($this->_fields
);
199 $returnProperties['contact_type'] = 1;
200 $returnProperties['contact_sub_type'] = 1;
201 $returnProperties['sort_name'] = 1;
203 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($this->_params
, 1);
204 $this->_query
= new CRM_Contact_BAO_Query($queryParams, $returnProperties, $this->_fields
);
206 //the below is done for query building for multirecord custom field listing
207 //to show all the custom field multi valued records of a particular contact
208 $this->setMultiRecordTableName($this->_fields
);
210 $this->_options
= &$this->_query
->_options
;
215 * This method returns the links that are given for each search row.
218 * @param bool $editLink
219 * @param bool $ufLink
225 static function &links($map = FALSE, $editLink = FALSE, $ufLink = FALSE, $gids = NULL) {
226 if (!self
::$_links) {
227 self
::$_links = array();
229 $viewPermission = TRUE;
231 // check view permission for each profile id, in case multiple profile ids are rendered
232 // then view action is disabled if any profile returns false
233 foreach ($gids as $profileId) {
234 $viewPermission = CRM_Core_Permission
::ufGroupValid($profileId, CRM_Core_Permission
::VIEW
);
235 if (!$viewPermission) {
241 if ($viewPermission) {
242 self
::$_links[CRM_Core_Action
::VIEW
] = array(
243 'name' => ts('View'),
244 'url' => 'civicrm/profile/view',
245 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%',
246 'title' => ts('View Profile Details'),
251 self
::$_links[CRM_Core_Action
::UPDATE
] = array(
252 'name' => ts('Edit'),
253 'url' => 'civicrm/profile/edit',
254 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%',
255 'title' => ts('Edit'),
260 self
::$_links[CRM_Core_Action
::PROFILE
] = array(
261 'name' => ts('Website Profile'),
262 'url' => 'user/%%ufID%%',
264 'title' => ts('View Website Profile'),
269 self
::$_links[CRM_Core_Action
::MAP
] = array(
271 'url' => 'civicrm/profile/map',
272 'qs' => 'reset=1&cid=%%id%%&gid=%%gid%%',
273 'title' => ts('Map'),
277 return self
::$_links;
282 * getter for array of the parameters required for creating pager.
290 function getPagerParams($action, &$params) {
292 CRM_Utils_System
::isNull($this->_multiRecordTableName
) ?
ts('Contact %%StatusMessage%%') : ts('Contact Multi Records %%StatusMessage%%');
293 $params['status'] = $status;
294 $params['csvString'] = NULL;
295 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
297 $params['buttonTop'] = 'PagerTopButton';
298 $params['buttonBottom'] = 'PagerBottomButton';
303 * returns the column headers as an array of tuples:
304 * (name, sortName (key to the sort array))
306 * @param string $action the action being performed
307 * @param enum $output what should the result set include (web/email/csv)
309 * @return array the column headers that need to be displayed
312 function &getColumnHeaders($action = NULL, $output = NULL) {
313 static $skipFields = array('group', 'tag');
314 $multipleFields = array('url');
315 $direction = CRM_Utils_Sort
::ASCENDING
;
317 if (!isset(self
::$_columnHeaders)) {
318 self
::$_columnHeaders = array(array('name' => ''),
320 'name' => ts('Name'),
321 'sort' => 'sort_name',
322 'direction' => CRM_Utils_Sort
::ASCENDING
,
323 'field_name' => 'sort_name',
327 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
329 foreach ($this->_fields
as $name => $field) {
330 // skip pseudo fields
331 if (substr($name, 0, 9) == 'phone_ext') {
335 if (!empty($field['in_selector']) &&
336 !in_array($name, $skipFields)
339 if (strpos($name, '-') !== FALSE) {
340 $value = explode('-', $name);
341 $fieldName = CRM_Utils_Array
::value(0, $value);
342 $lType = CRM_Utils_Array
::value(1, $value);
343 $type = CRM_Utils_Array
::value(2, $value);
345 if (!in_array($fieldName, $multipleFields)) {
346 if ($lType == 'Primary') {
347 $locationTypeName = 1;
350 $locationTypeName = $locationTypes[$lType];
353 if (in_array($fieldName, array(
354 'phone', 'im', 'email'))) {
356 $name = "`$locationTypeName-$fieldName-$type`";
359 $name = "`$locationTypeName-$fieldName`";
363 $name = "`$locationTypeName-$fieldName`";
367 $name = "website-{$lType}-{$fieldName}";
371 self
::$_columnHeaders[] = array(
372 'name' => $field['title'],
374 'direction' => $direction,
375 'field_name' => CRM_Core_BAO_UFField
::isValidFieldName($name) ?
$name : $fieldName,
378 $direction = CRM_Utils_Sort
::DONTCARE
;
383 // if we dont have any valid columns, dont add the implicit ones
384 // this allows the template to check on emptiness of column headers
386 self
::$_columnHeaders = array();
389 self
::$_columnHeaders[] = array('desc' => ts('Actions'));
392 return self
::$_columnHeaders;
396 * Returns total number of rows for the query.
400 * @return int Total number of rows
403 function getTotalCount($action) {
404 $additionalWhereClause = 'contact_a.is_deleted = 0';
405 $additionalFromClause = NULL;
408 if ($this->_multiRecordTableName
&&
409 !array_key_exists($this->_multiRecordTableName
, $this->_query
->_whereTables
)) {
410 $additionalFromClause = CRM_Utils_Array
::value($this->_multiRecordTableName
, $this->_query
->_tables
);
414 $countVal = $this->_query
->searchQuery(0, 0, NULL, TRUE, NULL, NULL, NULL,
415 $returnQuery, $additionalWhereClause, NULL, $additionalFromClause
423 $sql = preg_replace('/DISTINCT/', '', $countVal);
424 return CRM_Core_DAO
::singleValueQuery($sql);
429 * Return the qill for this selector
435 return $this->_query
->qill();
439 * returns all the rows in the given offset and rowCount
441 * @param enum $action the action being performed
442 * @param int $offset the row number to start from
443 * @param int $rowCount the number of rows to return
444 * @param string $sort the sql string that describes the sort order
445 * @param enum $output what should the result set include (web/email/csv)
447 * @return int the total number of rows for this action
449 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
451 $multipleFields = array('url');
452 //$sort object processing for location fields
454 $vars = $sort->_vars
;
456 foreach ($vars as $key => $field) {
457 $field = $vars[$key];
458 $fieldArray = explode('-', $field['name']);
459 $fieldType = CRM_Utils_Array
::value('2', $fieldArray);
460 if (is_numeric(CRM_Utils_Array
::value('1', $fieldArray))) {
461 if (!in_array($fieldType, $multipleFields)) {
462 $locationType = new CRM_Core_DAO_LocationType();
463 $locationType->id
= $fieldArray[1];
464 $locationType->find(TRUE);
465 if ($fieldArray[0] == 'email' ||
$fieldArray[0] == 'im' ||
$fieldArray[0] == 'phone') {
466 $field['name'] = "`" . $locationType->name
. "-" . $fieldArray[0] . "-1`";
469 $field['name'] = "`" . $locationType->name
. "-" . $fieldArray[0] . "`";
473 $field['name'] = "`website-" . $fieldArray[1] . "-{$fieldType}`";
476 $varArray[$key] = $field;
478 $sort->_vars
= $varArray;
481 $additionalWhereClause = 'contact_a.is_deleted = 0';
483 if ($this->_multiRecordTableName
) {
486 $this->_query
->_useGroupBy
= TRUE;
487 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, NULL, NULL,
488 NULL, NULL, $returnQuery, $additionalWhereClause
492 $resQuery = preg_replace('/GROUP BY contact_a.id[\s]+ORDER BY/', ' ORDER BY', $result);
493 $result = CRM_Core_DAO
::executeQuery($resQuery);
496 // process the result of the query
499 // check if edit is configured in profile settings
501 $editLink = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'is_edit_link');
504 //FIXME : make sure to handle delete separately. CRM-4418
505 $mask = CRM_Core_Action
::mask(array(CRM_Core_Permission
::getPermission()));
506 if ($editLink && ($mask & CRM_Core_Permission
::EDIT
)) {
507 // do not allow edit for anon users in joomla frontend, CRM-4668
508 $config = CRM_Core_Config
::singleton();
509 if (!$config->userFrameworkFrontend
) {
510 $this->_editLink
= TRUE;
513 $links = self
::links($this->_map
, $this->_editLink
, $this->_linkToUF
, $this->_profileIds
);
515 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
518 static $skipFields = array('group', 'tag');
520 foreach ($this->_fields
as $key => $field) {
521 // skip pseudo fields
522 if (substr($key, 0, 9) == 'phone_ext') {
526 if (!empty($field['in_selector']) &&
527 !in_array($key, $skipFields)
529 if (strpos($key, '-') !== FALSE) {
530 $value = explode('-', $key);
531 $fieldName = CRM_Utils_Array
::value(0, $value);
532 $id = CRM_Utils_Array
::value(1, $value);
533 $type = CRM_Utils_Array
::value(2, $value);
535 if (!in_array($fieldName, $multipleFields)) {
536 $locationTypeName = NULL;
537 if (is_numeric($id)) {
538 $locationTypeName = CRM_Utils_Array
::value($id, $locationTypes);
541 if ($id == 'Primary') {
542 $locationTypeName = 1;
546 if (!$locationTypeName) {
549 $locationTypeName = str_replace(' ', '_', $locationTypeName);
550 if (in_array($fieldName, array(
551 'phone', 'im', 'email'))) {
553 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
556 $names[] = "{$locationTypeName}-{$fieldName}";
560 $names[] = "{$locationTypeName}-{$fieldName}";
564 $names[] = "website-{$id}-{$fieldName}";
567 elseif ($field['name'] == 'id') {
568 $names[] = 'contact_id';
571 $names[] = $field['name'];
577 $multipleSelectFields = array('preferred_communication_method' => 1);
578 $multiRecordTableId = NULL;
579 if ($this->_multiRecordTableName
) {
580 $multiRecordTableId = "{$this->_multiRecordTableName}_id";
583 // we need to determine of overlay profile should be shown
584 $showProfileOverlay = CRM_Core_BAO_UFGroup
::showOverlayProfile();
586 while ($result->fetch()) {
587 $this->_query
->convertToPseudoNames($result);
589 if (isset($result->country
)) {
590 // the query returns the untranslated country name
591 $i18n = CRM_Core_I18n
::singleton();
592 $result->country
= $i18n->translate($result->country
);
596 $row[] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
597 $result->contact_sub_type
: $result->contact_type
,
602 if ($result->sort_name
) {
603 $row[] = $result->sort_name
;
610 foreach ($names as $name) {
611 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
612 $row[] = CRM_Core_BAO_CustomField
::getDisplayValue($result->$name,
618 elseif (substr($name, -4) == '-url' &&
619 !empty($result->$name)
621 $url = CRM_Utils_System
::fixURL($result->$name);
622 $typeId = substr($name, 0, -4) . "-website_type_id";
623 $typeName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId);
625 $row[] = "<a href=\"$url\">{$result->$name} (${typeName})</a>";
628 $row[] = "<a href=\"$url\">{$result->$name}</a>";
631 elseif ($name == 'preferred_language') {
632 $row[] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name);
634 elseif ($multipleSelectFields &&
635 array_key_exists($name, $multipleSelectFields)
637 $paramsNew = array($name => $result->$name);
638 $name = array($name => array('newName' => $name, 'groupName' => $name));
640 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
641 $row[] = $paramsNew[$key];
643 elseif (strpos($name, '-im')) {
644 if (!empty($result->$name)) {
645 $providerId = $name . "-provider_id";
646 $providerName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->$providerId);
647 $row[] = $result->$name . " ({$providerName})";
653 elseif (strpos($name, '-phone-')) {
654 $phoneExtField = str_replace('phone', 'phone_ext', $name);
655 if (isset($result->$phoneExtField)) {
656 $row[] = $result->$name . " (" . $result->$phoneExtField . ")";
659 $row[] = $result->$name;
662 elseif (in_array($name, array(
663 'addressee', 'email_greeting', 'postal_greeting'))) {
664 $dname = $name . '_display';
665 $row[] = $result->$dname;
667 elseif (in_array($name, array(
668 'birth_date', 'deceased_date'))) {
669 $row[] = CRM_Utils_Date
::customFormat($result->$name);
671 elseif (isset($result->$name)) {
672 $row[] = $result->$name;
678 if (!empty($result->$name)) {
685 'id' => $result->contact_id
,
686 'gid' => implode(',', $this->_profileIds
),
689 // pass record id param to view url for multi record view
690 if ($multiRecordTableId && $newLinks) {
691 if ($result->$multiRecordTableId){
692 if ($newLinks[CRM_Core_Action
::VIEW
]['url'] == 'civicrm/profile/view') {
693 $newLinks[CRM_Core_Action
::VIEW
]['qs'] .= "&multiRecord=view&recordId=%%recordId%%&allFields=1";
694 $params['recordId'] = $result->$multiRecordTableId;
699 if ($this->_linkToUF
) {
700 $ufID = CRM_Core_BAO_UFMatch
::getUFId($result->contact_id
);
702 unset($newLinks[CRM_Core_Action
::PROFILE
]);
705 $params['ufID'] = $ufID;
709 $row[] = CRM_Core_Action
::formLink($newLinks,
714 'profile.selector.row',
727 * name of export file.
729 * @param string $output type of output
731 * @return string name of the file
733 function getExportFileName($output = 'csv') {
734 return ts('CiviCRM Profile Listings');
738 * set the _multiRecordTableName to display the result set
739 * according to multi record custom field values
741 function setMultiRecordTableName($fields) {
742 $customGroupId = $multiRecordTableName = NULL;
743 $selectorSet = FALSE;
745 foreach ($fields as $field => $properties) {
746 if (!CRM_Core_BAO_CustomField
::getKeyID($field)) {
749 if ($cgId = CRM_Core_BAO_CustomField
::isMultiRecordField($field)) {
750 $customGroupId = CRM_Utils_System
::isNull($customGroupId) ?
$cgId : $customGroupId;
752 //if the field is submitted set multiRecordTableName
753 if ($customGroupId) {
754 $isSubmitted = FALSE;
755 foreach ($this->_query
->_params
as $key => $value) {
756 //check the query params 'where' element
757 if ($value[0] == $field) {
764 $this->_multiRecordTableName
= $multiRecordTableName =
765 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');
766 if ($multiRecordTableName) {
771 if (!empty($properties['in_selector'])) {
778 if (!isset($customGroupId) ||
!$customGroupId) {
782 //if the field is in selector and not a searchable field
783 //get the proper customvalue table name
785 $this->_multiRecordTableName
= $multiRecordTableName =
786 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');