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