Merge pull request #9586 from alifrumin/caselinks
[civicrm-core.git] / CRM / Profile / Selector / Listings.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
35 * This class is used to retrieve and display a range of
36 * contacts that match the given criteria (specifically for
37 * results of advanced search options.
38 *
39 */
40class CRM_Profile_Selector_Listings extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
41
42 /**
b44e3f84 43 * Array of supported links, currently view and edit
6a488035
TO
44 *
45 * @var array
6a488035
TO
46 */
47 static $_links = NULL;
48
49 /**
100fef9d 50 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
51 *
52 * @var array
6a488035
TO
53 */
54 static $_columnHeaders;
55
56 /**
fe482240 57 * The sql params we use to get the list of contacts.
6a488035
TO
58 *
59 * @var string
6a488035
TO
60 */
61 protected $_params;
62
63 /**
fe482240 64 * The public visible fields to be shown to the user.
6a488035
TO
65 *
66 * @var array
6a488035
TO
67 */
68 protected $_fields;
69
70 /**
fe482240 71 * The custom fields for this domain.
6a488035
TO
72 *
73 * @var array
6a488035
TO
74 */
75 protected $_customFields;
76
77 /**
fe482240 78 * Cache the query object.
6a488035
TO
79 *
80 * @var object
6a488035
TO
81 */
82 protected $_query;
83
84 /**
fe482240 85 * Cache the expanded options list if any.
6a488035
TO
86 *
87 * @var object
6a488035
TO
88 */
89 protected $_options;
90
91 /**
fe482240 92 * The group id that we are editing.
6a488035
TO
93 *
94 * @var int
95 */
96 protected $_gid;
97
98 /**
fe482240 99 * Do we enable mapping of users.
6a488035
TO
100 *
101 * @var boolean
102 */
103 protected $_map;
104
105 /**
fe482240 106 * Do we enable edit link.
6a488035
TO
107 *
108 * @var boolean
109 */
110 protected $_editLink;
111
112 /**
fe482240 113 * Should we link to the UF Profile.
6a488035
TO
114 *
115 * @var boolean
116 */
117 protected $_linkToUF;
118
119 /**
120 * Store profile ids if multiple profile ids are passed using comma separated.
121 * Currently lets implement this functionality only for dialog mode
122 */
123 protected $_profileIds = array();
124
125 protected $_multiRecordTableName = NULL;
77b97be7 126
6a488035 127 /**
fe482240 128 * Class constructor.
6a488035 129 *
c301f76e 130 * @param array $params the params for the where clause
131 * @param array $customFields
132 * @param array $ufGroupIds
77b97be7
EM
133 * @param bool $map
134 * @param bool $editLink
135 * @param bool $linkToUF
136 *
137 * @return \CRM_Profile_Selector_Listings
6a488035 138 */
c301f76e 139 public function __construct(
6a488035
TO
140 &$params,
141 &$customFields,
142 $ufGroupIds = NULL,
143 $map = FALSE,
144 $editLink = FALSE,
145 $linkToUF = FALSE
146 ) {
147 $this->_params = $params;
148
149 if (is_array($ufGroupIds)) {
150 $this->_profileIds = $ufGroupIds;
151 $this->_gid = $ufGroupIds[0];
152 }
153 else {
154 $this->_profileIds = array($ufGroupIds);
155 $this->_gid = $ufGroupIds;
156 }
157
353ffa53 158 $this->_map = $map;
6a488035
TO
159 $this->_editLink = $editLink;
160 $this->_linkToUF = $linkToUF;
161
162 //get the details of the uf group
163 if ($this->_gid) {
164 $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_UFGroup',
165 $this->_gid, 'limit_listings_group_id'
166 );
167 }
168
169 // add group id to params if a uf group belong to a any group
170 if ($groupId) {
a7488080 171 if (!empty($this->_params['group'])) {
6a488035
TO
172 $this->_params['group'][$groupId] = 1;
173 }
174 else {
175 $this->_params['group'] = array($groupId => 1);
176 }
177 }
178
179 $this->_fields = CRM_Core_BAO_UFGroup::getListingFields(CRM_Core_Action::VIEW,
180 CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY |
181 CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY,
182 FALSE, $this->_profileIds
183 );
184
185 $this->_customFields = &$customFields;
186
187 $returnProperties = CRM_Contact_BAO_Contact::makeHierReturnProperties($this->_fields);
188 $returnProperties['contact_type'] = 1;
189 $returnProperties['contact_sub_type'] = 1;
190 $returnProperties['sort_name'] = 1;
191
353ffa53
TO
192 $queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_params, 1);
193 $this->_query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, $this->_fields);
6a488035
TO
194
195 //the below is done for query building for multirecord custom field listing
196 //to show all the custom field multi valued records of a particular contact
197 $this->setMultiRecordTableName($this->_fields);
198
199 $this->_options = &$this->_query->_options;
200 }
6a488035
TO
201
202 /**
203 * This method returns the links that are given for each search row.
204 *
77b97be7
EM
205 * @param bool $map
206 * @param bool $editLink
207 * @param bool $ufLink
208 * @param null $gids
209 *
6a488035 210 * @return array
6a488035 211 */
00be9182 212 public static function &links($map = FALSE, $editLink = FALSE, $ufLink = FALSE, $gids = NULL) {
6a488035
TO
213 if (!self::$_links) {
214 self::$_links = array();
215
216 $viewPermission = TRUE;
217 if ($gids) {
218 // check view permission for each profile id, in case multiple profile ids are rendered
219 // then view action is disabled if any profile returns false
220 foreach ($gids as $profileId) {
221 $viewPermission = CRM_Core_Permission::ufGroupValid($profileId, CRM_Core_Permission::VIEW);
222 if (!$viewPermission) {
223 break;
224 }
225 }
226 }
227
228 if ($viewPermission) {
229 self::$_links[CRM_Core_Action::VIEW] = array(
230 'name' => ts('View'),
231 'url' => 'civicrm/profile/view',
232 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%',
233 'title' => ts('View Profile Details'),
234 );
235 }
236
237 if ($editLink) {
238 self::$_links[CRM_Core_Action::UPDATE] = array(
239 'name' => ts('Edit'),
240 'url' => 'civicrm/profile/edit',
241 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%',
242 'title' => ts('Edit'),
243 );
244 }
245
246 if ($ufLink) {
247 self::$_links[CRM_Core_Action::PROFILE] = array(
248 'name' => ts('Website Profile'),
249 'url' => 'user/%%ufID%%',
250 'qs' => ' ',
251 'title' => ts('View Website Profile'),
252 );
253 }
254
255 if ($map) {
256 self::$_links[CRM_Core_Action::MAP] = array(
257 'name' => ts('Map'),
258 'url' => 'civicrm/profile/map',
259 'qs' => 'reset=1&cid=%%id%%&gid=%%gid%%',
260 'title' => ts('Map'),
261 );
262 }
263 }
264 return self::$_links;
265 }
6a488035
TO
266
267 /**
100fef9d 268 * Getter for array of the parameters required for creating pager.
6a488035 269 *
77b97be7 270 * @param $action
c490a46a 271 * @param array $params
6a488035 272 */
00be9182 273 public function getPagerParams($action, &$params) {
c301f76e 274 $status = CRM_Utils_System::isNull($this->_multiRecordTableName) ? ts('Contact %%StatusMessage%%') : ts('Contact Multi Records %%StatusMessage%%');
353ffa53 275 $params['status'] = $status;
6a488035 276 $params['csvString'] = NULL;
353ffa53 277 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
6a488035
TO
278
279 $params['buttonTop'] = 'PagerTopButton';
280 $params['buttonBottom'] = 'PagerBottomButton';
281 }
6a488035
TO
282
283 /**
100fef9d 284 * Returns the column headers as an array of tuples:
6a488035
TO
285 * (name, sortName (key to the sort array))
286 *
68c9fb83
TO
287 * @param string $action
288 * The action being performed.
3f8d2862 289 * @param string $output
68c9fb83 290 * What should the result set include (web/email/csv).
6a488035 291 *
a6c01b45
CW
292 * @return array
293 * the column headers that need to be displayed
6a488035 294 */
00be9182 295 public function &getColumnHeaders($action = NULL, $output = NULL) {
6a488035
TO
296 static $skipFields = array('group', 'tag');
297 $multipleFields = array('url');
353ffa53
TO
298 $direction = CRM_Utils_Sort::ASCENDING;
299 $empty = TRUE;
6a488035 300 if (!isset(self::$_columnHeaders)) {
353ffa53
TO
301 self::$_columnHeaders = array(
302 array('name' => ''),
6a488035
TO
303 array(
304 'name' => ts('Name'),
305 'sort' => 'sort_name',
306 'direction' => CRM_Utils_Sort::ASCENDING,
ae94c3e3 307 'field_name' => 'sort_name',
6a488035
TO
308 ),
309 );
310
b2b0530a 311 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
312
313 foreach ($this->_fields as $name => $field) {
314 // skip pseudo fields
315 if (substr($name, 0, 9) == 'phone_ext') {
316 continue;
317 }
318
a7488080 319 if (!empty($field['in_selector']) &&
6a488035
TO
320 !in_array($name, $skipFields)
321 ) {
322
323 if (strpos($name, '-') !== FALSE) {
353ffa53 324 $value = explode('-', $name);
6a488035 325 $fieldName = CRM_Utils_Array::value(0, $value);
353ffa53
TO
326 $lType = CRM_Utils_Array::value(1, $value);
327 $type = CRM_Utils_Array::value(2, $value);
6a488035
TO
328
329 if (!in_array($fieldName, $multipleFields)) {
330 if ($lType == 'Primary') {
331 $locationTypeName = 1;
332 }
333 else {
334 $locationTypeName = $locationTypes[$lType];
335 }
336
337 if (in_array($fieldName, array(
353ffa53
TO
338 'phone',
339 'im',
c301f76e 340 'email',
353ffa53 341 ))) {
6a488035
TO
342 if ($type) {
343 $name = "`$locationTypeName-$fieldName-$type`";
344 }
345 else {
346 $name = "`$locationTypeName-$fieldName`";
347 }
348 }
349 else {
350 $name = "`$locationTypeName-$fieldName`";
351 }
352 }
353 else {
354 $name = "website-{$lType}-{$fieldName}";
355 }
356 }
357
358 self::$_columnHeaders[] = array(
359 'name' => $field['title'],
360 'sort' => $name,
361 'direction' => $direction,
ae94c3e3 362 'field_name' => CRM_Core_BAO_UFField::isValidFieldName($name) ? $name : $fieldName,
6a488035
TO
363 );
364
365 $direction = CRM_Utils_Sort::DONTCARE;
366 $empty = FALSE;
367 }
368 }
369
abdd5e3e 370 // if we don't have any valid columns, don't add the implicit ones
6a488035
TO
371 // this allows the template to check on emptiness of column headers
372 if ($empty) {
373 self::$_columnHeaders = array();
374 }
375 else {
376 self::$_columnHeaders[] = array('desc' => ts('Actions'));
377 }
378 }
379 return self::$_columnHeaders;
380 }
381
382 /**
383 * Returns total number of rows for the query.
384 *
54957108 385 * @param int $action
6a488035 386 *
a6c01b45
CW
387 * @return int
388 * Total number of rows
6a488035 389 */
00be9182 390 public function getTotalCount($action) {
6a488035
TO
391 $additionalWhereClause = 'contact_a.is_deleted = 0';
392 $additionalFromClause = NULL;
393 $returnQuery = NULL;
394
395 if ($this->_multiRecordTableName &&
353ffa53
TO
396 !array_key_exists($this->_multiRecordTableName, $this->_query->_whereTables)
397 ) {
6a488035
TO
398 $additionalFromClause = CRM_Utils_Array::value($this->_multiRecordTableName, $this->_query->_tables);
399 $returnQuery = TRUE;
400 }
401
402 $countVal = $this->_query->searchQuery(0, 0, NULL, TRUE, NULL, NULL, NULL,
403 $returnQuery, $additionalWhereClause, NULL, $additionalFromClause
404 );
405
406 if (!$returnQuery) {
407 return $countVal;
408 }
409
410 if ($returnQuery) {
411 $sql = preg_replace('/DISTINCT/', '', $countVal);
412 return CRM_Core_DAO::singleValueQuery($sql);
413 }
414 }
415
416 /**
fe482240 417 * Return the qill for this selector.
6a488035
TO
418 *
419 * @return string
6a488035 420 */
00be9182 421 public function getQill() {
6a488035
TO
422 return $this->_query->qill();
423 }
424
425 /**
fe482240 426 * Returns all the rows in the given offset and rowCount.
6a488035 427 *
3f8d2862 428 * @param string $action
68c9fb83
TO
429 * The action being performed.
430 * @param int $offset
431 * The row number to start from.
432 * @param int $rowCount
433 * The number of rows to return.
434 * @param string $sort
435 * The sql string that describes the sort order.
3f8d2862 436 * @param string $output
68c9fb83 437 * What should the result set include (web/email/csv).
6a488035 438 *
ea3ddccf 439 * @param string $extraWhereClause
440 *
a6c01b45
CW
441 * @return int
442 * the total number of rows for this action
6a488035 443 */
00be9182 444 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $extraWhereClause = NULL) {
6a488035
TO
445
446 $multipleFields = array('url');
447 //$sort object processing for location fields
448 if ($sort) {
449 $vars = $sort->_vars;
450 $varArray = array();
451 foreach ($vars as $key => $field) {
353ffa53 452 $field = $vars[$key];
6a488035 453 $fieldArray = explode('-', $field['name']);
353ffa53 454 $fieldType = CRM_Utils_Array::value('2', $fieldArray);
6a488035
TO
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`";
462 }
463 else {
464 $field['name'] = "`" . $locationType->name . "-" . $fieldArray[0] . "`";
465 }
466 }
467 else {
468 $field['name'] = "`website-" . $fieldArray[1] . "-{$fieldType}`";
469 }
470 }
471 $varArray[$key] = $field;
472 }
5a7264bd 473 $sort->_vars = $varArray;
6a488035
TO
474 }
475
6a488035 476 $additionalWhereClause = 'contact_a.is_deleted = 0';
a2f03f06
N
477
478 if ($extraWhereClause) {
479 $additionalWhereClause .= " AND {$extraWhereClause}";
480 }
481
6a488035
TO
482 $returnQuery = NULL;
483 if ($this->_multiRecordTableName) {
484 $returnQuery = TRUE;
485 }
5bc63401 486 $this->_query->_useGroupBy = TRUE;
6a488035
TO
487 $result = $this->_query->searchQuery($offset, $rowCount, $sort, NULL, NULL,
488 NULL, NULL, $returnQuery, $additionalWhereClause
489 );
490
491 if ($returnQuery) {
492 $resQuery = preg_replace('/GROUP BY contact_a.id[\s]+ORDER BY/', ' ORDER BY', $result);
353ffa53 493 $result = CRM_Core_DAO::executeQuery($resQuery);
6a488035
TO
494 }
495
496 // process the result of the query
497 $rows = array();
498
499 // check if edit is configured in profile settings
500 if ($this->_gid) {
501 $editLink = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_edit_link');
502 }
503
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;
511 }
512 }
513 $links = self::links($this->_map, $this->_editLink, $this->_linkToUF, $this->_profileIds);
514
b2b0530a 515 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
516
517 $names = array();
518 static $skipFields = array('group', 'tag');
519
520 foreach ($this->_fields as $key => $field) {
521 // skip pseudo fields
522 if (substr($key, 0, 9) == 'phone_ext') {
523 continue;
524 }
525
a7488080 526 if (!empty($field['in_selector']) &&
6a488035
TO
527 !in_array($key, $skipFields)
528 ) {
529 if (strpos($key, '-') !== FALSE) {
353ffa53 530 $value = explode('-', $key);
6a488035 531 $fieldName = CRM_Utils_Array::value(0, $value);
353ffa53
TO
532 $id = CRM_Utils_Array::value(1, $value);
533 $type = CRM_Utils_Array::value(2, $value);
6a488035
TO
534
535 if (!in_array($fieldName, $multipleFields)) {
536 $locationTypeName = NULL;
537 if (is_numeric($id)) {
538 $locationTypeName = CRM_Utils_Array::value($id, $locationTypes);
539 }
540 else {
541 if ($id == 'Primary') {
542 $locationTypeName = 1;
543 }
544 }
545
546 if (!$locationTypeName) {
547 continue;
548 }
549 $locationTypeName = str_replace(' ', '_', $locationTypeName);
550 if (in_array($fieldName, array(
353ffa53
TO
551 'phone',
552 'im',
c301f76e 553 'email',
353ffa53 554 ))) {
6a488035
TO
555 if ($type) {
556 $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
557 }
558 else {
559 $names[] = "{$locationTypeName}-{$fieldName}";
560 }
561 }
562 else {
563 $names[] = "{$locationTypeName}-{$fieldName}";
564 }
565 }
566 else {
567 $names[] = "website-{$id}-{$fieldName}";
568 }
569 }
570 elseif ($field['name'] == 'id') {
571 $names[] = 'contact_id';
572 }
573 else {
574 $names[] = $field['name'];
575 }
576 }
577 }
578
6a488035
TO
579 $multipleSelectFields = array('preferred_communication_method' => 1);
580 $multiRecordTableId = NULL;
581 if ($this->_multiRecordTableName) {
582 $multiRecordTableId = "{$this->_multiRecordTableName}_id";
583 }
584
585 // we need to determine of overlay profile should be shown
586 $showProfileOverlay = CRM_Core_BAO_UFGroup::showOverlayProfile();
587
6a488035 588 while ($result->fetch()) {
d9ab802d
PJ
589 $this->_query->convertToPseudoNames($result);
590
6a488035
TO
591 if (isset($result->country)) {
592 // the query returns the untranslated country name
593 $i18n = CRM_Core_I18n::singleton();
594 $result->country = $i18n->translate($result->country);
595 }
353ffa53 596 $row = array();
6a488035 597 $empty = TRUE;
dc98079b 598 $row[] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type,
6a488035
TO
599 FALSE,
600 $result->contact_id,
601 $showProfileOverlay
602 );
603 if ($result->sort_name) {
ae94c3e3 604 $row[] = $result->sort_name;
6a488035
TO
605 $empty = FALSE;
606 }
607 else {
608 continue;
609 }
610
611 foreach ($names as $name) {
612 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) {
8cee0c70 613 $row[] = CRM_Core_BAO_CustomField::displayValue($result->$name,
6a488035 614 $cfID,
6a488035
TO
615 $result->contact_id
616 );
617 }
618 elseif (substr($name, -4) == '-url' &&
619 !empty($result->$name)
620 ) {
353ffa53
TO
621 $url = CRM_Utils_System::fixURL($result->$name);
622 $typeId = substr($name, 0, -4) . "-website_type_id";
a8c23526 623 $typeName = CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId);
6a488035
TO
624 if ($typeName) {
625 $row[] = "<a href=\"$url\">{$result->$name} (${typeName})</a>";
626 }
627 else {
628 $row[] = "<a href=\"$url\">{$result->$name}</a>";
629 }
630 }
631 elseif ($name == 'preferred_language') {
a8c23526 632 $row[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name);
6a488035
TO
633 }
634 elseif ($multipleSelectFields &&
635 array_key_exists($name, $multipleSelectFields)
636 ) {
916f78e6 637 $paramsNew = array($name => $result->$name);
638 $name = array($name => array('newName' => $name, 'groupName' => $name));
639
6a488035
TO
640 CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE);
641 $row[] = $paramsNew[$key];
642 }
643 elseif (strpos($name, '-im')) {
644 if (!empty($result->$name)) {
353ffa53 645 $providerId = $name . "-provider_id";
a8c23526 646 $providerName = CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->$providerId);
353ffa53 647 $row[] = $result->$name . " ({$providerName})";
6a488035
TO
648 }
649 else {
650 $row[] = '';
651 }
652 }
653 elseif (strpos($name, '-phone-')) {
654 $phoneExtField = str_replace('phone', 'phone_ext', $name);
655 if (isset($result->$phoneExtField)) {
656 $row[] = $result->$name . " (" . $result->$phoneExtField . ")";
657 }
658 else {
659 $row[] = $result->$name;
660 }
661 }
662 elseif (in_array($name, array(
353ffa53
TO
663 'addressee',
664 'email_greeting',
c301f76e 665 'postal_greeting',
353ffa53 666 ))) {
6a488035
TO
667 $dname = $name . '_display';
668 $row[] = $result->$dname;
669 }
670 elseif (in_array($name, array(
353ffa53 671 'birth_date',
c301f76e 672 'deceased_date',
353ffa53 673 ))) {
6a488035
TO
674 $row[] = CRM_Utils_Date::customFormat($result->$name);
675 }
676 elseif (isset($result->$name)) {
677 $row[] = $result->$name;
678 }
679 else {
680 $row[] = '';
681 }
682
683 if (!empty($result->$name)) {
684 $empty = FALSE;
685 }
686 }
687
688 $newLinks = $links;
689 $params = array(
690 'id' => $result->contact_id,
691 'gid' => implode(',', $this->_profileIds),
692 );
693
694 // pass record id param to view url for multi record view
695 if ($multiRecordTableId && $newLinks) {
9b873358 696 if ($result->$multiRecordTableId) {
6a488035
TO
697 if ($newLinks[CRM_Core_Action::VIEW]['url'] == 'civicrm/profile/view') {
698 $newLinks[CRM_Core_Action::VIEW]['qs'] .= "&multiRecord=view&recordId=%%recordId%%&allFields=1";
699 $params['recordId'] = $result->$multiRecordTableId;
700 }
701 }
702 }
703
704 if ($this->_linkToUF) {
705 $ufID = CRM_Core_BAO_UFMatch::getUFId($result->contact_id);
706 if (!$ufID) {
707 unset($newLinks[CRM_Core_Action::PROFILE]);
708 }
709 else {
710 $params['ufID'] = $ufID;
711 }
712 }
713
714 $row[] = CRM_Core_Action::formLink($newLinks,
715 $mask,
87dab4a4
AH
716 $params,
717 ts('more'),
718 FALSE,
719 'profile.selector.row',
720 'Contact',
721 $result->contact_id
6a488035
TO
722 );
723
724 if (!$empty) {
725 $rows[] = $row;
726 }
727 }
728 return $rows;
729 }
730
731 /**
100fef9d 732 * Name of export file.
6a488035 733 *
68c9fb83
TO
734 * @param string $output
735 * Type of output.
6a488035 736 *
a6c01b45
CW
737 * @return string
738 * name of the file
6a488035 739 */
00be9182 740 public function getExportFileName($output = 'csv') {
6a488035
TO
741 return ts('CiviCRM Profile Listings');
742 }
743
744 /**
ea3ddccf 745 * Set the _multiRecordTableName to display the result set.
746 *
747 * (according to multi record custom field values).
748 *
749 * @param array $fields
6a488035 750 */
00be9182 751 public function setMultiRecordTableName($fields) {
dc98079b
TO
752 $customGroupId = $multiRecordTableName = NULL;
753 $selectorSet = FALSE;
6a488035
TO
754
755 foreach ($fields as $field => $properties) {
756 if (!CRM_Core_BAO_CustomField::getKeyID($field)) {
757 continue;
758 }
759 if ($cgId = CRM_Core_BAO_CustomField::isMultiRecordField($field)) {
760 $customGroupId = CRM_Utils_System::isNull($customGroupId) ? $cgId : $customGroupId;
761
762 //if the field is submitted set multiRecordTableName
763 if ($customGroupId) {
764 $isSubmitted = FALSE;
765 foreach ($this->_query->_params as $key => $value) {
766 //check the query params 'where' element
767 if ($value[0] == $field) {
768 $isSubmitted = TRUE;
769 break;
770 }
771 }
772
773 if ($isSubmitted) {
c301f76e 774 $this->_multiRecordTableName
775 = $multiRecordTableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');
6a488035 776 if ($multiRecordTableName) {
ea3ddccf 777 return;
6a488035
TO
778 }
779 }
780
a7488080 781 if (!empty($properties['in_selector'])) {
6a488035
TO
782 $selectorSet = TRUE;
783 }
784 }
785 }
786 }
787
788 if (!isset($customGroupId) || !$customGroupId) {
ea3ddccf 789 return;
6a488035
TO
790 }
791
792 //if the field is in selector and not a searchable field
abdd5e3e 793 //get the proper custom value table name
6a488035 794 if ($selectorSet) {
c301f76e 795 $this->_multiRecordTableName
796 = $multiRecordTableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name');
6a488035
TO
797 }
798 } //func close
96025800 799
6a488035 800}