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