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
280 public function getPagerParams($action, &$params) {
282 CRM_Utils_System
::isNull($this->_multiRecordTableName
) ?
ts('Contact %%StatusMessage%%') : ts('Contact Multi Records %%StatusMessage%%');
283 $params['status'] = $status;
284 $params['csvString'] = NULL;
285 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
287 $params['buttonTop'] = 'PagerTopButton';
288 $params['buttonBottom'] = 'PagerBottomButton';
292 * Returns the column headers as an array of tuples:
293 * (name, sortName (key to the sort array))
295 * @param string $action the action being performed
296 * @param enum $output what should the result set include (web/email/csv)
298 * @return array the column headers that need to be displayed
300 public function &getColumnHeaders($action = NULL, $output = NULL) {
301 static $skipFields = array('group', 'tag');
302 $multipleFields = array('url');
303 $direction = CRM_Utils_Sort
::ASCENDING
;
305 if (!isset(self
::$_columnHeaders)) {
306 self
::$_columnHeaders = array(array('name' => ''),
308 'name' => ts('Name'),
309 'sort' => 'sort_name',
310 'direction' => CRM_Utils_Sort
::ASCENDING
,
311 'field_name' => 'sort_name',
315 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
317 foreach ($this->_fields
as $name => $field) {
318 // skip pseudo fields
319 if (substr($name, 0, 9) == 'phone_ext') {
323 if (!empty($field['in_selector']) &&
324 !in_array($name, $skipFields)
327 if (strpos($name, '-') !== FALSE) {
328 $value = explode('-', $name);
329 $fieldName = CRM_Utils_Array
::value(0, $value);
330 $lType = CRM_Utils_Array
::value(1, $value);
331 $type = CRM_Utils_Array
::value(2, $value);
333 if (!in_array($fieldName, $multipleFields)) {
334 if ($lType == 'Primary') {
335 $locationTypeName = 1;
338 $locationTypeName = $locationTypes[$lType];
341 if (in_array($fieldName, array(
342 'phone', 'im', 'email'))) {
344 $name = "`$locationTypeName-$fieldName-$type`";
347 $name = "`$locationTypeName-$fieldName`";
351 $name = "`$locationTypeName-$fieldName`";
355 $name = "website-{$lType}-{$fieldName}";
359 self
::$_columnHeaders[] = array(
360 'name' => $field['title'],
362 'direction' => $direction,
363 'field_name' => CRM_Core_BAO_UFField
::isValidFieldName($name) ?
$name : $fieldName,
366 $direction = CRM_Utils_Sort
::DONTCARE
;
371 // if we dont have any valid columns, dont add the implicit ones
372 // this allows the template to check on emptiness of column headers
374 self
::$_columnHeaders = array();
377 self
::$_columnHeaders[] = array('desc' => ts('Actions'));
380 return self
::$_columnHeaders;
384 * Returns total number of rows for the query.
388 * @return int Total number of rows
390 public function getTotalCount($action) {
391 $additionalWhereClause = 'contact_a.is_deleted = 0';
392 $additionalFromClause = NULL;
395 if ($this->_multiRecordTableName
&&
396 !array_key_exists($this->_multiRecordTableName
, $this->_query
->_whereTables
)) {
397 $additionalFromClause = CRM_Utils_Array
::value($this->_multiRecordTableName
, $this->_query
->_tables
);
401 $countVal = $this->_query
->searchQuery(0, 0, NULL, TRUE, NULL, NULL, NULL,
402 $returnQuery, $additionalWhereClause, NULL, $additionalFromClause
410 $sql = preg_replace('/DISTINCT/', '', $countVal);
411 return CRM_Core_DAO
::singleValueQuery($sql);
416 * Return the qill for this selector
420 public function getQill() {
421 return $this->_query
->qill();
425 * Returns all the rows in the given offset and rowCount
427 * @param enum $action the action being performed
428 * @param int $offset the row number to start from
429 * @param int $rowCount the number of rows to return
430 * @param string $sort the sql string that describes the sort order
431 * @param enum $output what should the result set include (web/email/csv)
433 * @return int the total number of rows for this action
435 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $extraWhereClause = NULL) {
437 $multipleFields = array('url');
438 //$sort object processing for location fields
440 $vars = $sort->_vars
;
442 foreach ($vars as $key => $field) {
443 $field = $vars[$key];
444 $fieldArray = explode('-', $field['name']);
445 $fieldType = CRM_Utils_Array
::value('2', $fieldArray);
446 if (is_numeric(CRM_Utils_Array
::value('1', $fieldArray))) {
447 if (!in_array($fieldType, $multipleFields)) {
448 $locationType = new CRM_Core_DAO_LocationType();
449 $locationType->id
= $fieldArray[1];
450 $locationType->find(TRUE);
451 if ($fieldArray[0] == 'email' ||
$fieldArray[0] == 'im' ||
$fieldArray[0] == 'phone') {
452 $field['name'] = "`" . $locationType->name
. "-" . $fieldArray[0] . "-1`";
455 $field['name'] = "`" . $locationType->name
. "-" . $fieldArray[0] . "`";
459 $field['name'] = "`website-" . $fieldArray[1] . "-{$fieldType}`";
462 $varArray[$key] = $field;
464 $sort->_vars
= $varArray;
467 $additionalWhereClause = 'contact_a.is_deleted = 0';
469 if ($extraWhereClause) {
470 $additionalWhereClause .= " AND {$extraWhereClause}";
474 if ($this->_multiRecordTableName
) {
477 $this->_query
->_useGroupBy
= TRUE;
478 $result = $this->_query
->searchQuery($offset, $rowCount, $sort, NULL, NULL,
479 NULL, NULL, $returnQuery, $additionalWhereClause
483 $resQuery = preg_replace('/GROUP BY contact_a.id[\s]+ORDER BY/', ' ORDER BY', $result);
484 $result = CRM_Core_DAO
::executeQuery($resQuery);
487 // process the result of the query
490 // check if edit is configured in profile settings
492 $editLink = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'is_edit_link');
495 //FIXME : make sure to handle delete separately. CRM-4418
496 $mask = CRM_Core_Action
::mask(array(CRM_Core_Permission
::getPermission()));
497 if ($editLink && ($mask & CRM_Core_Permission
::EDIT
)) {
498 // do not allow edit for anon users in joomla frontend, CRM-4668
499 $config = CRM_Core_Config
::singleton();
500 if (!$config->userFrameworkFrontend
) {
501 $this->_editLink
= TRUE;
504 $links = self
::links($this->_map
, $this->_editLink
, $this->_linkToUF
, $this->_profileIds
);
506 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
509 static $skipFields = array('group', 'tag');
511 foreach ($this->_fields
as $key => $field) {
512 // skip pseudo fields
513 if (substr($key, 0, 9) == 'phone_ext') {
517 if (!empty($field['in_selector']) &&
518 !in_array($key, $skipFields)
520 if (strpos($key, '-') !== FALSE) {
521 $value = explode('-', $key);
522 $fieldName = CRM_Utils_Array
::value(0, $value);
523 $id = CRM_Utils_Array
::value(1, $value);
524 $type = CRM_Utils_Array
::value(2, $value);
526 if (!in_array($fieldName, $multipleFields)) {
527 $locationTypeName = NULL;
528 if (is_numeric($id)) {
529 $locationTypeName = CRM_Utils_Array
::value($id, $locationTypes);
532 if ($id == 'Primary') {
533 $locationTypeName = 1;
537 if (!$locationTypeName) {
540 $locationTypeName = str_replace(' ', '_', $locationTypeName);
541 if (in_array($fieldName, array(
542 'phone', 'im', 'email'))) {
544 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
547 $names[] = "{$locationTypeName}-{$fieldName}";
551 $names[] = "{$locationTypeName}-{$fieldName}";
555 $names[] = "website-{$id}-{$fieldName}";
558 elseif ($field['name'] == 'id') {
559 $names[] = 'contact_id';
562 $names[] = $field['name'];
568 $multipleSelectFields = array('preferred_communication_method' => 1);
569 $multiRecordTableId = NULL;
570 if ($this->_multiRecordTableName
) {
571 $multiRecordTableId = "{$this->_multiRecordTableName}_id";
574 // we need to determine of overlay profile should be shown
575 $showProfileOverlay = CRM_Core_BAO_UFGroup
::showOverlayProfile();
577 while ($result->fetch()) {
578 $this->_query
->convertToPseudoNames($result);
580 if (isset($result->country
)) {
581 // the query returns the untranslated country name
582 $i18n = CRM_Core_I18n
::singleton();
583 $result->country
= $i18n->translate($result->country
);
587 $row[] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
588 $result->contact_sub_type
: $result->contact_type
,
593 if ($result->sort_name
) {
594 $row[] = $result->sort_name
;
601 foreach ($names as $name) {
602 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
603 $row[] = CRM_Core_BAO_CustomField
::getDisplayValue($result->$name,
609 elseif (substr($name, -4) == '-url' &&
610 !empty($result->$name)
612 $url = CRM_Utils_System
::fixURL($result->$name);
613 $typeId = substr($name, 0, -4) . "-website_type_id";
614 $typeName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId);
616 $row[] = "<a href=\"$url\">{$result->$name} (${typeName})</a>";
619 $row[] = "<a href=\"$url\">{$result->$name}</a>";
622 elseif ($name == 'preferred_language') {
623 $row[] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name);
625 elseif ($multipleSelectFields &&
626 array_key_exists($name, $multipleSelectFields)
628 $paramsNew = array($name => $result->$name);
629 $name = array($name => array('newName' => $name, 'groupName' => $name));
631 CRM_Core_OptionGroup
::lookupValues($paramsNew, $name, FALSE);
632 $row[] = $paramsNew[$key];
634 elseif (strpos($name, '-im')) {
635 if (!empty($result->$name)) {
636 $providerId = $name . "-provider_id";
637 $providerName = CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->$providerId);
638 $row[] = $result->$name . " ({$providerName})";
644 elseif (strpos($name, '-phone-')) {
645 $phoneExtField = str_replace('phone', 'phone_ext', $name);
646 if (isset($result->$phoneExtField)) {
647 $row[] = $result->$name . " (" . $result->$phoneExtField . ")";
650 $row[] = $result->$name;
653 elseif (in_array($name, array(
654 'addressee', 'email_greeting', 'postal_greeting'))) {
655 $dname = $name . '_display';
656 $row[] = $result->$dname;
658 elseif (in_array($name, array(
659 'birth_date', 'deceased_date'))) {
660 $row[] = CRM_Utils_Date
::customFormat($result->$name);
662 elseif (isset($result->$name)) {
663 $row[] = $result->$name;
669 if (!empty($result->$name)) {
676 'id' => $result->contact_id
,
677 'gid' => implode(',', $this->_profileIds
),
680 // pass record id param to view url for multi record view
681 if ($multiRecordTableId && $newLinks) {
682 if ($result->$multiRecordTableId){
683 if ($newLinks[CRM_Core_Action
::VIEW
]['url'] == 'civicrm/profile/view') {
684 $newLinks[CRM_Core_Action
::VIEW
]['qs'] .= "&multiRecord=view&recordId=%%recordId%%&allFields=1";
685 $params['recordId'] = $result->$multiRecordTableId;
690 if ($this->_linkToUF
) {
691 $ufID = CRM_Core_BAO_UFMatch
::getUFId($result->contact_id
);
693 unset($newLinks[CRM_Core_Action
::PROFILE
]);
696 $params['ufID'] = $ufID;
700 $row[] = CRM_Core_Action
::formLink($newLinks,
705 'profile.selector.row',
718 * Name of export file.
720 * @param string $output type of output
722 * @return string name of the file
724 public function getExportFileName($output = 'csv') {
725 return ts('CiviCRM Profile Listings');
729 * set the _multiRecordTableName to display the result set
730 * according to multi record custom field values
732 public function setMultiRecordTableName($fields) {
733 $customGroupId = $multiRecordTableName = NULL;
734 $selectorSet = FALSE;
736 foreach ($fields as $field => $properties) {
737 if (!CRM_Core_BAO_CustomField
::getKeyID($field)) {
740 if ($cgId = CRM_Core_BAO_CustomField
::isMultiRecordField($field)) {
741 $customGroupId = CRM_Utils_System
::isNull($customGroupId) ?
$cgId : $customGroupId;
743 //if the field is submitted set multiRecordTableName
744 if ($customGroupId) {
745 $isSubmitted = FALSE;
746 foreach ($this->_query
->_params
as $key => $value) {
747 //check the query params 'where' element
748 if ($value[0] == $field) {
755 $this->_multiRecordTableName
= $multiRecordTableName =
756 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');
757 if ($multiRecordTableName) {
762 if (!empty($properties['in_selector'])) {
769 if (!isset($customGroupId) ||
!$customGroupId) {
773 //if the field is in selector and not a searchable field
774 //get the proper customvalue table name
776 $this->_multiRecordTableName
= $multiRecordTableName =
777 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');