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 enum $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(array('name' => ''),
310 'name' => ts('Name'),
311 'sort' => 'sort_name',
312 'direction' => CRM_Utils_Sort
::ASCENDING
,
313 'field_name' => 'sort_name',
317 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
319 foreach ($this->_fields
as $name => $field) {
320 // skip pseudo fields
321 if (substr($name, 0, 9) == 'phone_ext') {
325 if (!empty($field['in_selector']) &&
326 !in_array($name, $skipFields)
329 if (strpos($name, '-') !== FALSE) {
330 $value = explode('-', $name);
331 $fieldName = CRM_Utils_Array
::value(0, $value);
332 $lType = CRM_Utils_Array
::value(1, $value);
333 $type = CRM_Utils_Array
::value(2, $value);
335 if (!in_array($fieldName, $multipleFields)) {
336 if ($lType == 'Primary') {
337 $locationTypeName = 1;
340 $locationTypeName = $locationTypes[$lType];
343 if (in_array($fieldName, array(
344 'phone', 'im', 'email'))) {
346 $name = "`$locationTypeName-$fieldName-$type`";
349 $name = "`$locationTypeName-$fieldName`";
353 $name = "`$locationTypeName-$fieldName`";
357 $name = "website-{$lType}-{$fieldName}";
361 self
::$_columnHeaders[] = array(
362 'name' => $field['title'],
364 'direction' => $direction,
365 'field_name' => CRM_Core_BAO_UFField
::isValidFieldName($name) ?
$name : $fieldName,
368 $direction = CRM_Utils_Sort
::DONTCARE
;
373 // if we dont have any valid columns, dont add the implicit ones
374 // this allows the template to check on emptiness of column headers
376 self
::$_columnHeaders = array();
379 self
::$_columnHeaders[] = array('desc' => ts('Actions'));
382 return self
::$_columnHeaders;
386 * Returns total number of rows for the query.
391 * Total number of rows
393 public function getTotalCount($action) {
394 $additionalWhereClause = 'contact_a.is_deleted = 0';
395 $additionalFromClause = NULL;
398 if ($this->_multiRecordTableName
&&
399 !array_key_exists($this->_multiRecordTableName
, $this->_query
->_whereTables
)) {
400 $additionalFromClause = CRM_Utils_Array
::value($this->_multiRecordTableName
, $this->_query
->_tables
);
404 $countVal = $this->_query
->searchQuery(0, 0, NULL, TRUE, NULL, NULL, NULL,
405 $returnQuery, $additionalWhereClause, NULL, $additionalFromClause
413 $sql = preg_replace('/DISTINCT/', '', $countVal);
414 return CRM_Core_DAO
::singleValueQuery($sql);
419 * Return the qill for this selector
423 public function getQill() {
424 return $this->_query
->qill();
428 * Returns all the rows in the given offset and rowCount
430 * @param enum $action
431 * The action being performed.
433 * The row number to start from.
434 * @param int $rowCount
435 * The number of rows to return.
436 * @param string $sort
437 * The sql string that describes the sort order.
438 * @param enum $output
439 * What should the result set include (web/email/csv).
442 * the total number of rows for this action
444 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $extraWhereClause = NULL) {
446 $multipleFields = array('url');
447 //$sort object processing for location fields
449 $vars = $sort->_vars
;
451 foreach ($vars as $key => $field) {
452 $field = $vars[$key];
453 $fieldArray = explode('-', $field['name']);
454 $fieldType = CRM_Utils_Array
::value('2', $fieldArray);
455 if (is_numeric(CRM_Utils_Array
::value('1', $fieldArray))) {
456 if (!in_array($fieldType, $multipleFields)) {
457 $locationType = new CRM_Core_DAO_LocationType();
458 $locationType->id
= $fieldArray[1];
459 $locationType->find(TRUE);
460 if ($fieldArray[0] == 'email' ||
$fieldArray[0] == 'im' ||
$fieldArray[0] == 'phone') {
461 $field['name'] = "`" . $locationType->name
. "-" . $fieldArray[0] . "-1`";
464 $field['name'] = "`" . $locationType->name
. "-" . $fieldArray[0] . "`";
468 $field['name'] = "`website-" . $fieldArray[1] . "-{$fieldType}`";
471 $varArray[$key] = $field;
473 $sort->_vars
= $varArray;
476 $additionalWhereClause = 'contact_a.is_deleted = 0';
478 if ($extraWhereClause) {
479 $additionalWhereClause .= " AND {$extraWhereClause}";
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'];
576 $multipleSelectFields = array('preferred_communication_method' => 1);
577 $multiRecordTableId = NULL;
578 if ($this->_multiRecordTableName
) {
579 $multiRecordTableId = "{$this->_multiRecordTableName}_id";
582 // we need to determine of overlay profile should be shown
583 $showProfileOverlay = CRM_Core_BAO_UFGroup
::showOverlayProfile();
585 while ($result->fetch()) {
586 $this->_query
->convertToPseudoNames($result);
588 if (isset($result->country
)) {
589 // the query returns the untranslated country name
590 $i18n = CRM_Core_I18n
::singleton();
591 $result->country
= $i18n->translate($result->country
);
595 $row[] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
,
600 if ($result->sort_name
) {
601 $row[] = $result->sort_name
;
608 foreach ($names as $name) {
609 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
610 $row[] = CRM_Core_BAO_CustomField
::getDisplayValue($result->$name,
616 elseif (substr($name, -4) == '-url' &&
617 !empty($result->$name)
619 $url = CRM_Utils_System
::fixURL($result->$name);
620 $typeId = substr($name, 0, -4) . "-website_type_id";
621 $typeName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId);
623 $row[] = "<a href=\"$url\">{$result->$name} (${typeName})</a>";
626 $row[] = "<a href=\"$url\">{$result->$name}</a>";
629 elseif ($name == 'preferred_language') {
630 $row[] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name);
632 elseif ($multipleSelectFields &&
633 array_key_exists($name, $multipleSelectFields)
635 $paramsNew = array($name => $result->$name);
636 $name = array($name => array('newName' => $name, 'groupName' => $name));
638 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
639 $row[] = $paramsNew[$key];
641 elseif (strpos($name, '-im')) {
642 if (!empty($result->$name)) {
643 $providerId = $name . "-provider_id";
644 $providerName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->$providerId);
645 $row[] = $result->$name . " ({$providerName})";
651 elseif (strpos($name, '-phone-')) {
652 $phoneExtField = str_replace('phone', 'phone_ext', $name);
653 if (isset($result->$phoneExtField)) {
654 $row[] = $result->$name . " (" . $result->$phoneExtField . ")";
657 $row[] = $result->$name;
660 elseif (in_array($name, array(
661 'addressee', 'email_greeting', 'postal_greeting'))) {
662 $dname = $name . '_display';
663 $row[] = $result->$dname;
665 elseif (in_array($name, array(
666 'birth_date', 'deceased_date'))) {
667 $row[] = CRM_Utils_Date
::customFormat($result->$name);
669 elseif (isset($result->$name)) {
670 $row[] = $result->$name;
676 if (!empty($result->$name)) {
683 'id' => $result->contact_id
,
684 'gid' => implode(',', $this->_profileIds
),
687 // pass record id param to view url for multi record view
688 if ($multiRecordTableId && $newLinks) {
689 if ($result->$multiRecordTableId) {
690 if ($newLinks[CRM_Core_Action
::VIEW
]['url'] == 'civicrm/profile/view') {
691 $newLinks[CRM_Core_Action
::VIEW
]['qs'] .= "&multiRecord=view&recordId=%%recordId%%&allFields=1";
692 $params['recordId'] = $result->$multiRecordTableId;
697 if ($this->_linkToUF
) {
698 $ufID = CRM_Core_BAO_UFMatch
::getUFId($result->contact_id
);
700 unset($newLinks[CRM_Core_Action
::PROFILE
]);
703 $params['ufID'] = $ufID;
707 $row[] = CRM_Core_Action
::formLink($newLinks,
712 'profile.selector.row',
725 * Name of export file.
727 * @param string $output
733 public 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 public 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');