3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
68 * The public visible fields to be shown to the user
75 * The custom fields for this domain
79 protected $_customFields;
82 * Cache the query object
89 * Cache the expanded options list if any
96 * The group id that we are editing
103 * Do we enable mapping of users
110 * Do we enable edit link
114 protected $_editLink;
117 * Should we link to the UF Profile
121 protected $_linkToUF;
124 * Store profile ids if multiple profile ids are passed using comma separated.
125 * Currently lets implement this functionality only for dialog mode
127 protected $_profileIds = array();
129 protected $_multiRecordTableName = NULL;
134 * @param array $params
135 * @param $customFields
136 * @param string params the params for the where clause
139 * @param bool $editLink
140 * @param bool $linkToUF
142 * @return \CRM_Profile_Selector_Listings
145 function __construct(
153 $this->_params
= $params;
155 if (is_array($ufGroupIds)) {
156 $this->_profileIds
= $ufGroupIds;
157 $this->_gid
= $ufGroupIds[0];
160 $this->_profileIds
= array($ufGroupIds);
161 $this->_gid
= $ufGroupIds;
165 $this->_editLink
= $editLink;
166 $this->_linkToUF
= $linkToUF;
168 //get the details of the uf group
170 $groupId = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_UFGroup',
171 $this->_gid
, 'limit_listings_group_id'
175 // add group id to params if a uf group belong to a any group
177 if (!empty($this->_params
['group'])) {
178 $this->_params
['group'][$groupId] = 1;
181 $this->_params
['group'] = array($groupId => 1);
185 $this->_fields
= CRM_Core_BAO_UFGroup
::getListingFields(CRM_Core_Action
::VIEW
,
186 CRM_Core_BAO_UFGroup
::PUBLIC_VISIBILITY |
187 CRM_Core_BAO_UFGroup
::LISTINGS_VISIBILITY
,
188 FALSE, $this->_profileIds
191 $this->_customFields
= &$customFields;
193 $returnProperties = CRM_Contact_BAO_Contact
::makeHierReturnProperties($this->_fields
);
194 $returnProperties['contact_type'] = 1;
195 $returnProperties['contact_sub_type'] = 1;
196 $returnProperties['sort_name'] = 1;
198 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($this->_params
, 1);
199 $this->_query
= new CRM_Contact_BAO_Query($queryParams, $returnProperties, $this->_fields
);
201 //the below is done for query building for multirecord custom field listing
202 //to show all the custom field multi valued records of a particular contact
203 $this->setMultiRecordTableName($this->_fields
);
205 $this->_options
= &$this->_query
->_options
;
209 * This method returns the links that are given for each search row.
212 * @param bool $editLink
213 * @param bool $ufLink
218 public static function &links($map = FALSE, $editLink = FALSE, $ufLink = FALSE, $gids = NULL) {
219 if (!self
::$_links) {
220 self
::$_links = array();
222 $viewPermission = TRUE;
224 // check view permission for each profile id, in case multiple profile ids are rendered
225 // then view action is disabled if any profile returns false
226 foreach ($gids as $profileId) {
227 $viewPermission = CRM_Core_Permission
::ufGroupValid($profileId, CRM_Core_Permission
::VIEW
);
228 if (!$viewPermission) {
234 if ($viewPermission) {
235 self
::$_links[CRM_Core_Action
::VIEW
] = array(
236 'name' => ts('View'),
237 'url' => 'civicrm/profile/view',
238 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%',
239 'title' => ts('View Profile Details'),
244 self
::$_links[CRM_Core_Action
::UPDATE
] = array(
245 'name' => ts('Edit'),
246 'url' => 'civicrm/profile/edit',
247 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%',
248 'title' => ts('Edit'),
253 self
::$_links[CRM_Core_Action
::PROFILE
] = array(
254 'name' => ts('Website Profile'),
255 'url' => 'user/%%ufID%%',
257 'title' => ts('View Website Profile'),
262 self
::$_links[CRM_Core_Action
::MAP
] = array(
264 'url' => 'civicrm/profile/map',
265 'qs' => 'reset=1&cid=%%id%%&gid=%%gid%%',
266 'title' => ts('Map'),
270 return self
::$_links;
274 * Getter for array of the parameters required for creating pager.
277 * @param array $params
279 public function getPagerParams($action, &$params) {
281 CRM_Utils_System
::isNull($this->_multiRecordTableName
) ?
ts('Contact %%StatusMessage%%') : ts('Contact Multi Records %%StatusMessage%%');
282 $params['status'] = $status;
283 $params['csvString'] = NULL;
284 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
286 $params['buttonTop'] = 'PagerTopButton';
287 $params['buttonBottom'] = 'PagerBottomButton';
291 * Returns the column headers as an array of tuples:
292 * (name, sortName (key to the sort array))
294 * @param string $action
295 * The action being performed.
296 * @param string $output
297 * What should the result set include (web/email/csv).
300 * the column headers that need to be displayed
302 public function &getColumnHeaders($action = NULL, $output = NULL) {
303 static $skipFields = array('group', 'tag');
304 $multipleFields = array('url');
305 $direction = CRM_Utils_Sort
::ASCENDING
;
307 if (!isset(self
::$_columnHeaders)) {
308 self
::$_columnHeaders = array(
311 'name' => ts('Name'),
312 'sort' => 'sort_name',
313 'direction' => CRM_Utils_Sort
::ASCENDING
,
314 'field_name' => 'sort_name',
318 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
320 foreach ($this->_fields
as $name => $field) {
321 // skip pseudo fields
322 if (substr($name, 0, 9) == 'phone_ext') {
326 if (!empty($field['in_selector']) &&
327 !in_array($name, $skipFields)
330 if (strpos($name, '-') !== FALSE) {
331 $value = explode('-', $name);
332 $fieldName = CRM_Utils_Array
::value(0, $value);
333 $lType = CRM_Utils_Array
::value(1, $value);
334 $type = CRM_Utils_Array
::value(2, $value);
336 if (!in_array($fieldName, $multipleFields)) {
337 if ($lType == 'Primary') {
338 $locationTypeName = 1;
341 $locationTypeName = $locationTypes[$lType];
344 if (in_array($fieldName, array(
350 $name = "`$locationTypeName-$fieldName-$type`";
353 $name = "`$locationTypeName-$fieldName`";
357 $name = "`$locationTypeName-$fieldName`";
361 $name = "website-{$lType}-{$fieldName}";
365 self
::$_columnHeaders[] = array(
366 'name' => $field['title'],
368 'direction' => $direction,
369 'field_name' => CRM_Core_BAO_UFField
::isValidFieldName($name) ?
$name : $fieldName,
372 $direction = CRM_Utils_Sort
::DONTCARE
;
377 // if we dont have any valid columns, dont add the implicit ones
378 // this allows the template to check on emptiness of column headers
380 self
::$_columnHeaders = array();
383 self
::$_columnHeaders[] = array('desc' => ts('Actions'));
386 return self
::$_columnHeaders;
390 * Returns total number of rows for the query.
395 * Total number of rows
397 public function getTotalCount($action) {
398 $additionalWhereClause = 'contact_a.is_deleted = 0';
399 $additionalFromClause = NULL;
402 if ($this->_multiRecordTableName
&&
403 !array_key_exists($this->_multiRecordTableName
, $this->_query
->_whereTables
)
405 $additionalFromClause = CRM_Utils_Array
::value($this->_multiRecordTableName
, $this->_query
->_tables
);
409 $countVal = $this->_query
->searchQuery(0, 0, NULL, TRUE, NULL, NULL, NULL,
410 $returnQuery, $additionalWhereClause, NULL, $additionalFromClause
418 $sql = preg_replace('/DISTINCT/', '', $countVal);
419 return CRM_Core_DAO
::singleValueQuery($sql);
424 * Return the qill for this selector
428 public function getQill() {
429 return $this->_query
->qill();
433 * Returns all the rows in the given offset and rowCount
435 * @param string $action
436 * The action being performed.
438 * The row number to start from.
439 * @param int $rowCount
440 * The number of rows to return.
441 * @param string $sort
442 * The sql string that describes the sort order.
443 * @param string $output
444 * What should the result set include (web/email/csv).
447 * the total number of rows for this action
449 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $extraWhereClause = 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 ($extraWhereClause) {
484 $additionalWhereClause .= " AND {$extraWhereClause}";
488 if ($this->_multiRecordTableName
) {
491 $this->_query
->_useGroupBy
= TRUE;
492 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, NULL, NULL,
493 NULL, NULL, $returnQuery, $additionalWhereClause
497 $resQuery = preg_replace('/GROUP BY contact_a.id[\s]+ORDER BY/', ' ORDER BY', $result);
498 $result = CRM_Core_DAO
::executeQuery($resQuery);
501 // process the result of the query
504 // check if edit is configured in profile settings
506 $editLink = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'is_edit_link');
509 //FIXME : make sure to handle delete separately. CRM-4418
510 $mask = CRM_Core_Action
::mask(array(CRM_Core_Permission
::getPermission()));
511 if ($editLink && ($mask & CRM_Core_Permission
::EDIT
)) {
512 // do not allow edit for anon users in joomla frontend, CRM-4668
513 $config = CRM_Core_Config
::singleton();
514 if (!$config->userFrameworkFrontend
) {
515 $this->_editLink
= TRUE;
518 $links = self
::links($this->_map
, $this->_editLink
, $this->_linkToUF
, $this->_profileIds
);
520 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
523 static $skipFields = array('group', 'tag');
525 foreach ($this->_fields
as $key => $field) {
526 // skip pseudo fields
527 if (substr($key, 0, 9) == 'phone_ext') {
531 if (!empty($field['in_selector']) &&
532 !in_array($key, $skipFields)
534 if (strpos($key, '-') !== FALSE) {
535 $value = explode('-', $key);
536 $fieldName = CRM_Utils_Array
::value(0, $value);
537 $id = CRM_Utils_Array
::value(1, $value);
538 $type = CRM_Utils_Array
::value(2, $value);
540 if (!in_array($fieldName, $multipleFields)) {
541 $locationTypeName = NULL;
542 if (is_numeric($id)) {
543 $locationTypeName = CRM_Utils_Array
::value($id, $locationTypes);
546 if ($id == 'Primary') {
547 $locationTypeName = 1;
551 if (!$locationTypeName) {
554 $locationTypeName = str_replace(' ', '_', $locationTypeName);
555 if (in_array($fieldName, array(
561 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
564 $names[] = "{$locationTypeName}-{$fieldName}";
568 $names[] = "{$locationTypeName}-{$fieldName}";
572 $names[] = "website-{$id}-{$fieldName}";
575 elseif ($field['name'] == 'id') {
576 $names[] = 'contact_id';
579 $names[] = $field['name'];
584 $multipleSelectFields = array('preferred_communication_method' => 1);
585 $multiRecordTableId = NULL;
586 if ($this->_multiRecordTableName
) {
587 $multiRecordTableId = "{$this->_multiRecordTableName}_id";
590 // we need to determine of overlay profile should be shown
591 $showProfileOverlay = CRM_Core_BAO_UFGroup
::showOverlayProfile();
593 while ($result->fetch()) {
594 $this->_query
->convertToPseudoNames($result);
596 if (isset($result->country
)) {
597 // the query returns the untranslated country name
598 $i18n = CRM_Core_I18n
::singleton();
599 $result->country
= $i18n->translate($result->country
);
603 $row[] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
,
608 if ($result->sort_name
) {
609 $row[] = $result->sort_name
;
616 foreach ($names as $name) {
617 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
618 $row[] = CRM_Core_BAO_CustomField
::getDisplayValue($result->$name,
624 elseif (substr($name, -4) == '-url' &&
625 !empty($result->$name)
627 $url = CRM_Utils_System
::fixURL($result->$name);
628 $typeId = substr($name, 0, -4) . "-website_type_id";
629 $typeName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId);
631 $row[] = "<a href=\"$url\">{$result->$name} (${typeName})</a>";
634 $row[] = "<a href=\"$url\">{$result->$name}</a>";
637 elseif ($name == 'preferred_language') {
638 $row[] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name);
640 elseif ($multipleSelectFields &&
641 array_key_exists($name, $multipleSelectFields)
643 $paramsNew = array($name => $result->$name);
644 $name = array($name => array('newName' => $name, 'groupName' => $name));
646 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
647 $row[] = $paramsNew[$key];
649 elseif (strpos($name, '-im')) {
650 if (!empty($result->$name)) {
651 $providerId = $name . "-provider_id";
652 $providerName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->$providerId);
653 $row[] = $result->$name . " ({$providerName})";
659 elseif (strpos($name, '-phone-')) {
660 $phoneExtField = str_replace('phone', 'phone_ext', $name);
661 if (isset($result->$phoneExtField)) {
662 $row[] = $result->$name . " (" . $result->$phoneExtField . ")";
665 $row[] = $result->$name;
668 elseif (in_array($name, array(
673 $dname = $name . '_display';
674 $row[] = $result->$dname;
676 elseif (in_array($name, array(
680 $row[] = CRM_Utils_Date
::customFormat($result->$name);
682 elseif (isset($result->$name)) {
683 $row[] = $result->$name;
689 if (!empty($result->$name)) {
696 'id' => $result->contact_id
,
697 'gid' => implode(',', $this->_profileIds
),
700 // pass record id param to view url for multi record view
701 if ($multiRecordTableId && $newLinks) {
702 if ($result->$multiRecordTableId) {
703 if ($newLinks[CRM_Core_Action
::VIEW
]['url'] == 'civicrm/profile/view') {
704 $newLinks[CRM_Core_Action
::VIEW
]['qs'] .= "&multiRecord=view&recordId=%%recordId%%&allFields=1";
705 $params['recordId'] = $result->$multiRecordTableId;
710 if ($this->_linkToUF
) {
711 $ufID = CRM_Core_BAO_UFMatch
::getUFId($result->contact_id
);
713 unset($newLinks[CRM_Core_Action
::PROFILE
]);
716 $params['ufID'] = $ufID;
720 $row[] = CRM_Core_Action
::formLink($newLinks,
725 'profile.selector.row',
738 * Name of export file.
740 * @param string $output
746 public function getExportFileName($output = 'csv') {
747 return ts('CiviCRM Profile Listings');
751 * set the _multiRecordTableName to display the result set
752 * according to multi record custom field values
754 public function setMultiRecordTableName($fields) {
755 $customGroupId = $multiRecordTableName = NULL;
756 $selectorSet = FALSE;
758 foreach ($fields as $field => $properties) {
759 if (!CRM_Core_BAO_CustomField
::getKeyID($field)) {
762 if ($cgId = CRM_Core_BAO_CustomField
::isMultiRecordField($field)) {
763 $customGroupId = CRM_Utils_System
::isNull($customGroupId) ?
$cgId : $customGroupId;
765 //if the field is submitted set multiRecordTableName
766 if ($customGroupId) {
767 $isSubmitted = FALSE;
768 foreach ($this->_query
->_params
as $key => $value) {
769 //check the query params 'where' element
770 if ($value[0] == $field) {
777 $this->_multiRecordTableName
= $multiRecordTableName =
778 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');
779 if ($multiRecordTableName) {
784 if (!empty($properties['in_selector'])) {
791 if (!isset($customGroupId) ||
!$customGroupId) {
795 //if the field is in selector and not a searchable field
796 //get the proper customvalue table name
798 $this->_multiRecordTableName
= $multiRecordTableName =
799 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');