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