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;
139 * @param array $params
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
;
214 * This method returns the links that are given for each search row.
217 * @param bool $editLink
218 * @param bool $ufLink
224 static function &links($map = FALSE, $editLink = FALSE, $ufLink = FALSE, $gids = NULL) {
225 if (!self
::$_links) {
226 self
::$_links = array();
228 $viewPermission = TRUE;
230 // check view permission for each profile id, in case multiple profile ids are rendered
231 // then view action is disabled if any profile returns false
232 foreach ($gids as $profileId) {
233 $viewPermission = CRM_Core_Permission
::ufGroupValid($profileId, CRM_Core_Permission
::VIEW
);
234 if (!$viewPermission) {
240 if ($viewPermission) {
241 self
::$_links[CRM_Core_Action
::VIEW
] = array(
242 'name' => ts('View'),
243 'url' => 'civicrm/profile/view',
244 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%',
245 'title' => ts('View Profile Details'),
250 self
::$_links[CRM_Core_Action
::UPDATE
] = array(
251 'name' => ts('Edit'),
252 'url' => 'civicrm/profile/edit',
253 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%',
254 'title' => ts('Edit'),
259 self
::$_links[CRM_Core_Action
::PROFILE
] = array(
260 'name' => ts('Website Profile'),
261 'url' => 'user/%%ufID%%',
263 'title' => ts('View Website Profile'),
268 self
::$_links[CRM_Core_Action
::MAP
] = array(
270 'url' => 'civicrm/profile/map',
271 'qs' => 'reset=1&cid=%%id%%&gid=%%gid%%',
272 'title' => ts('Map'),
276 return self
::$_links;
280 * Getter for array of the parameters required for creating pager.
283 * @param array $params
287 function getPagerParams($action, &$params) {
289 CRM_Utils_System
::isNull($this->_multiRecordTableName
) ?
ts('Contact %%StatusMessage%%') : ts('Contact Multi Records %%StatusMessage%%');
290 $params['status'] = $status;
291 $params['csvString'] = NULL;
292 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
294 $params['buttonTop'] = 'PagerTopButton';
295 $params['buttonBottom'] = 'PagerBottomButton';
299 * Returns the column headers as an array of tuples:
300 * (name, sortName (key to the sort array))
302 * @param string $action the action being performed
303 * @param enum $output what should the result set include (web/email/csv)
305 * @return array the column headers that need to be displayed
308 function &getColumnHeaders($action = NULL, $output = NULL) {
309 static $skipFields = array('group', 'tag');
310 $multipleFields = array('url');
311 $direction = CRM_Utils_Sort
::ASCENDING
;
313 if (!isset(self
::$_columnHeaders)) {
314 self
::$_columnHeaders = array(array('name' => ''),
316 'name' => ts('Name'),
317 'sort' => 'sort_name',
318 'direction' => CRM_Utils_Sort
::ASCENDING
,
319 'field_name' => 'sort_name',
323 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
325 foreach ($this->_fields
as $name => $field) {
326 // skip pseudo fields
327 if (substr($name, 0, 9) == 'phone_ext') {
331 if (!empty($field['in_selector']) &&
332 !in_array($name, $skipFields)
335 if (strpos($name, '-') !== FALSE) {
336 $value = explode('-', $name);
337 $fieldName = CRM_Utils_Array
::value(0, $value);
338 $lType = CRM_Utils_Array
::value(1, $value);
339 $type = CRM_Utils_Array
::value(2, $value);
341 if (!in_array($fieldName, $multipleFields)) {
342 if ($lType == 'Primary') {
343 $locationTypeName = 1;
346 $locationTypeName = $locationTypes[$lType];
349 if (in_array($fieldName, array(
350 'phone', 'im', 'email'))) {
352 $name = "`$locationTypeName-$fieldName-$type`";
355 $name = "`$locationTypeName-$fieldName`";
359 $name = "`$locationTypeName-$fieldName`";
363 $name = "website-{$lType}-{$fieldName}";
367 self
::$_columnHeaders[] = array(
368 'name' => $field['title'],
370 'direction' => $direction,
371 'field_name' => CRM_Core_BAO_UFField
::isValidFieldName($name) ?
$name : $fieldName,
374 $direction = CRM_Utils_Sort
::DONTCARE
;
379 // if we dont have any valid columns, dont add the implicit ones
380 // this allows the template to check on emptiness of column headers
382 self
::$_columnHeaders = array();
385 self
::$_columnHeaders[] = array('desc' => ts('Actions'));
388 return self
::$_columnHeaders;
392 * Returns total number of rows for the query.
396 * @return int Total number of rows
399 function getTotalCount($action) {
400 $additionalWhereClause = 'contact_a.is_deleted = 0';
401 $additionalFromClause = NULL;
404 if ($this->_multiRecordTableName
&&
405 !array_key_exists($this->_multiRecordTableName
, $this->_query
->_whereTables
)) {
406 $additionalFromClause = CRM_Utils_Array
::value($this->_multiRecordTableName
, $this->_query
->_tables
);
410 $countVal = $this->_query
->searchQuery(0, 0, NULL, TRUE, NULL, NULL, NULL,
411 $returnQuery, $additionalWhereClause, NULL, $additionalFromClause
419 $sql = preg_replace('/DISTINCT/', '', $countVal);
420 return CRM_Core_DAO
::singleValueQuery($sql);
425 * Return the qill for this selector
431 return $this->_query
->qill();
435 * Returns all the rows in the given offset and rowCount
437 * @param enum $action the action being performed
438 * @param int $offset the row number to start from
439 * @param int $rowCount the number of rows to return
440 * @param string $sort the sql string that describes the sort order
441 * @param enum $output what should the result set include (web/email/csv)
443 * @return int the total number of rows for this action
445 function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $extraWhereClause = NULL) {
447 $multipleFields = array('url');
448 //$sort object processing for location fields
450 $vars = $sort->_vars
;
452 foreach ($vars as $key => $field) {
453 $field = $vars[$key];
454 $fieldArray = explode('-', $field['name']);
455 $fieldType = CRM_Utils_Array
::value('2', $fieldArray);
456 if (is_numeric(CRM_Utils_Array
::value('1', $fieldArray))) {
457 if (!in_array($fieldType, $multipleFields)) {
458 $locationType = new CRM_Core_DAO_LocationType();
459 $locationType->id
= $fieldArray[1];
460 $locationType->find(TRUE);
461 if ($fieldArray[0] == 'email' ||
$fieldArray[0] == 'im' ||
$fieldArray[0] == 'phone') {
462 $field['name'] = "`" . $locationType->name
. "-" . $fieldArray[0] . "-1`";
465 $field['name'] = "`" . $locationType->name
. "-" . $fieldArray[0] . "`";
469 $field['name'] = "`website-" . $fieldArray[1] . "-{$fieldType}`";
472 $varArray[$key] = $field;
474 $sort->_vars
= $varArray;
477 $additionalWhereClause = 'contact_a.is_deleted = 0';
479 if ($extraWhereClause) {
480 $additionalWhereClause .= " AND {$extraWhereClause}";
484 if ($this->_multiRecordTableName
) {
487 $this->_query
->_useGroupBy
= TRUE;
488 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, NULL, NULL,
489 NULL, NULL, $returnQuery, $additionalWhereClause
493 $resQuery = preg_replace('/GROUP BY contact_a.id[\s]+ORDER BY/', ' ORDER BY', $result);
494 $result = CRM_Core_DAO
::executeQuery($resQuery);
497 // process the result of the query
500 // check if edit is configured in profile settings
502 $editLink = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'is_edit_link');
505 //FIXME : make sure to handle delete separately. CRM-4418
506 $mask = CRM_Core_Action
::mask(array(CRM_Core_Permission
::getPermission()));
507 if ($editLink && ($mask & CRM_Core_Permission
::EDIT
)) {
508 // do not allow edit for anon users in joomla frontend, CRM-4668
509 $config = CRM_Core_Config
::singleton();
510 if (!$config->userFrameworkFrontend
) {
511 $this->_editLink
= TRUE;
514 $links = self
::links($this->_map
, $this->_editLink
, $this->_linkToUF
, $this->_profileIds
);
516 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
519 static $skipFields = array('group', 'tag');
521 foreach ($this->_fields
as $key => $field) {
522 // skip pseudo fields
523 if (substr($key, 0, 9) == 'phone_ext') {
527 if (!empty($field['in_selector']) &&
528 !in_array($key, $skipFields)
530 if (strpos($key, '-') !== FALSE) {
531 $value = explode('-', $key);
532 $fieldName = CRM_Utils_Array
::value(0, $value);
533 $id = CRM_Utils_Array
::value(1, $value);
534 $type = CRM_Utils_Array
::value(2, $value);
536 if (!in_array($fieldName, $multipleFields)) {
537 $locationTypeName = NULL;
538 if (is_numeric($id)) {
539 $locationTypeName = CRM_Utils_Array
::value($id, $locationTypes);
542 if ($id == 'Primary') {
543 $locationTypeName = 1;
547 if (!$locationTypeName) {
550 $locationTypeName = str_replace(' ', '_', $locationTypeName);
551 if (in_array($fieldName, array(
552 'phone', 'im', 'email'))) {
554 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
557 $names[] = "{$locationTypeName}-{$fieldName}";
561 $names[] = "{$locationTypeName}-{$fieldName}";
565 $names[] = "website-{$id}-{$fieldName}";
568 elseif ($field['name'] == 'id') {
569 $names[] = 'contact_id';
572 $names[] = $field['name'];
578 $multipleSelectFields = array('preferred_communication_method' => 1);
579 $multiRecordTableId = NULL;
580 if ($this->_multiRecordTableName
) {
581 $multiRecordTableId = "{$this->_multiRecordTableName}_id";
584 // we need to determine of overlay profile should be shown
585 $showProfileOverlay = CRM_Core_BAO_UFGroup
::showOverlayProfile();
587 while ($result->fetch()) {
588 $this->_query
->convertToPseudoNames($result);
590 if (isset($result->country
)) {
591 // the query returns the untranslated country name
592 $i18n = CRM_Core_I18n
::singleton();
593 $result->country
= $i18n->translate($result->country
);
597 $row[] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
598 $result->contact_sub_type
: $result->contact_type
,
603 if ($result->sort_name
) {
604 $row[] = $result->sort_name
;
611 foreach ($names as $name) {
612 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
613 $row[] = CRM_Core_BAO_CustomField
::getDisplayValue($result->$name,
619 elseif (substr($name, -4) == '-url' &&
620 !empty($result->$name)
622 $url = CRM_Utils_System
::fixURL($result->$name);
623 $typeId = substr($name, 0, -4) . "-website_type_id";
624 $typeName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId);
626 $row[] = "<a href=\"$url\">{$result->$name} (${typeName})</a>";
629 $row[] = "<a href=\"$url\">{$result->$name}</a>";
632 elseif ($name == 'preferred_language') {
633 $row[] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name);
635 elseif ($multipleSelectFields &&
636 array_key_exists($name, $multipleSelectFields)
638 $paramsNew = array($name => $result->$name);
639 $name = array($name => array('newName' => $name, 'groupName' => $name));
641 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
642 $row[] = $paramsNew[$key];
644 elseif (strpos($name, '-im')) {
645 if (!empty($result->$name)) {
646 $providerId = $name . "-provider_id";
647 $providerName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->$providerId);
648 $row[] = $result->$name . " ({$providerName})";
654 elseif (strpos($name, '-phone-')) {
655 $phoneExtField = str_replace('phone', 'phone_ext', $name);
656 if (isset($result->$phoneExtField)) {
657 $row[] = $result->$name . " (" . $result->$phoneExtField . ")";
660 $row[] = $result->$name;
663 elseif (in_array($name, array(
664 'addressee', 'email_greeting', 'postal_greeting'))) {
665 $dname = $name . '_display';
666 $row[] = $result->$dname;
668 elseif (in_array($name, array(
669 'birth_date', 'deceased_date'))) {
670 $row[] = CRM_Utils_Date
::customFormat($result->$name);
672 elseif (isset($result->$name)) {
673 $row[] = $result->$name;
679 if (!empty($result->$name)) {
686 'id' => $result->contact_id
,
687 'gid' => implode(',', $this->_profileIds
),
690 // pass record id param to view url for multi record view
691 if ($multiRecordTableId && $newLinks) {
692 if ($result->$multiRecordTableId){
693 if ($newLinks[CRM_Core_Action
::VIEW
]['url'] == 'civicrm/profile/view') {
694 $newLinks[CRM_Core_Action
::VIEW
]['qs'] .= "&multiRecord=view&recordId=%%recordId%%&allFields=1";
695 $params['recordId'] = $result->$multiRecordTableId;
700 if ($this->_linkToUF
) {
701 $ufID = CRM_Core_BAO_UFMatch
::getUFId($result->contact_id
);
703 unset($newLinks[CRM_Core_Action
::PROFILE
]);
706 $params['ufID'] = $ufID;
710 $row[] = CRM_Core_Action
::formLink($newLinks,
715 'profile.selector.row',
728 * Name of export file.
730 * @param string $output type of output
732 * @return string name of the file
734 function getExportFileName($output = 'csv') {
735 return ts('CiviCRM Profile Listings');
739 * set the _multiRecordTableName to display the result set
740 * according to multi record custom field values
742 function setMultiRecordTableName($fields) {
743 $customGroupId = $multiRecordTableName = NULL;
744 $selectorSet = FALSE;
746 foreach ($fields as $field => $properties) {
747 if (!CRM_Core_BAO_CustomField
::getKeyID($field)) {
750 if ($cgId = CRM_Core_BAO_CustomField
::isMultiRecordField($field)) {
751 $customGroupId = CRM_Utils_System
::isNull($customGroupId) ?
$cgId : $customGroupId;
753 //if the field is submitted set multiRecordTableName
754 if ($customGroupId) {
755 $isSubmitted = FALSE;
756 foreach ($this->_query
->_params
as $key => $value) {
757 //check the query params 'where' element
758 if ($value[0] == $field) {
765 $this->_multiRecordTableName
= $multiRecordTableName =
766 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');
767 if ($multiRecordTableName) {
772 if (!empty($properties['in_selector'])) {
779 if (!isset($customGroupId) ||
!$customGroupId) {
783 //if the field is in selector and not a searchable field
784 //get the proper customvalue table name
786 $this->_multiRecordTableName
= $multiRecordTableName =
787 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');