| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 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 | /** |
| 45 | * Array of supported links, currently view and edit |
| 46 | * |
| 47 | * @var array |
| 48 | */ |
| 49 | static $_links = NULL; |
| 50 | |
| 51 | /** |
| 52 | * We use desc to remind us what that column is, name is used in the tpl |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
| 56 | static $_columnHeaders; |
| 57 | |
| 58 | /** |
| 59 | * The sql params we use to get the list of contacts. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $_params; |
| 64 | |
| 65 | /** |
| 66 | * The public visible fields to be shown to the user. |
| 67 | * |
| 68 | * @var array |
| 69 | */ |
| 70 | protected $_fields; |
| 71 | |
| 72 | /** |
| 73 | * The custom fields for this domain. |
| 74 | * |
| 75 | * @var array |
| 76 | */ |
| 77 | protected $_customFields; |
| 78 | |
| 79 | /** |
| 80 | * Cache the query object. |
| 81 | * |
| 82 | * @var object |
| 83 | */ |
| 84 | protected $_query; |
| 85 | |
| 86 | /** |
| 87 | * Cache the expanded options list if any. |
| 88 | * |
| 89 | * @var object |
| 90 | */ |
| 91 | protected $_options; |
| 92 | |
| 93 | /** |
| 94 | * The group id that we are editing. |
| 95 | * |
| 96 | * @var int |
| 97 | */ |
| 98 | protected $_gid; |
| 99 | |
| 100 | /** |
| 101 | * Do we enable mapping of users. |
| 102 | * |
| 103 | * @var boolean |
| 104 | */ |
| 105 | protected $_map; |
| 106 | |
| 107 | /** |
| 108 | * Do we enable edit link. |
| 109 | * |
| 110 | * @var boolean |
| 111 | */ |
| 112 | protected $_editLink; |
| 113 | |
| 114 | /** |
| 115 | * Should we link to the UF Profile. |
| 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; |
| 128 | |
| 129 | /** |
| 130 | * Class constructor. |
| 131 | * |
| 132 | * @param array $params the params for the where clause |
| 133 | * @param array $customFields |
| 134 | * @param array $ufGroupIds |
| 135 | * @param bool $map |
| 136 | * @param bool $editLink |
| 137 | * @param bool $linkToUF |
| 138 | * |
| 139 | * @return \CRM_Profile_Selector_Listings |
| 140 | */ |
| 141 | public function __construct( |
| 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 | |
| 160 | $this->_map = $map; |
| 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) { |
| 173 | if (!empty($this->_params['group'])) { |
| 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 | |
| 194 | $queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_params, 1); |
| 195 | $this->_query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, $this->_fields); |
| 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 | } |
| 203 | |
| 204 | /** |
| 205 | * This method returns the links that are given for each search row. |
| 206 | * |
| 207 | * @param bool $map |
| 208 | * @param bool $editLink |
| 209 | * @param bool $ufLink |
| 210 | * @param null $gids |
| 211 | * |
| 212 | * @return array |
| 213 | */ |
| 214 | public static function &links($map = FALSE, $editLink = FALSE, $ufLink = FALSE, $gids = NULL) { |
| 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 | } |
| 268 | |
| 269 | /** |
| 270 | * Getter for array of the parameters required for creating pager. |
| 271 | * |
| 272 | * @param $action |
| 273 | * @param array $params |
| 274 | */ |
| 275 | public function getPagerParams($action, &$params) { |
| 276 | $status = CRM_Utils_System::isNull($this->_multiRecordTableName) ? ts('Contact %%StatusMessage%%') : ts('Contact Multi Records %%StatusMessage%%'); |
| 277 | $params['status'] = $status; |
| 278 | $params['csvString'] = NULL; |
| 279 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; |
| 280 | |
| 281 | $params['buttonTop'] = 'PagerTopButton'; |
| 282 | $params['buttonBottom'] = 'PagerBottomButton'; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Returns the column headers as an array of tuples: |
| 287 | * (name, sortName (key to the sort array)) |
| 288 | * |
| 289 | * @param string $action |
| 290 | * The action being performed. |
| 291 | * @param string $output |
| 292 | * What should the result set include (web/email/csv). |
| 293 | * |
| 294 | * @return array |
| 295 | * the column headers that need to be displayed |
| 296 | */ |
| 297 | public function &getColumnHeaders($action = NULL, $output = NULL) { |
| 298 | static $skipFields = array('group', 'tag'); |
| 299 | $multipleFields = array('url'); |
| 300 | $direction = CRM_Utils_Sort::ASCENDING; |
| 301 | $empty = TRUE; |
| 302 | if (!isset(self::$_columnHeaders)) { |
| 303 | self::$_columnHeaders = array( |
| 304 | array('name' => ''), |
| 305 | array( |
| 306 | 'name' => ts('Name'), |
| 307 | 'sort' => 'sort_name', |
| 308 | 'direction' => CRM_Utils_Sort::ASCENDING, |
| 309 | 'field_name' => 'sort_name', |
| 310 | ), |
| 311 | ); |
| 312 | |
| 313 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
| 314 | |
| 315 | foreach ($this->_fields as $name => $field) { |
| 316 | // skip pseudo fields |
| 317 | if (substr($name, 0, 9) == 'phone_ext') { |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | if (!empty($field['in_selector']) && |
| 322 | !in_array($name, $skipFields) |
| 323 | ) { |
| 324 | |
| 325 | if (strpos($name, '-') !== FALSE) { |
| 326 | $value = explode('-', $name); |
| 327 | $fieldName = CRM_Utils_Array::value(0, $value); |
| 328 | $lType = CRM_Utils_Array::value(1, $value); |
| 329 | $type = CRM_Utils_Array::value(2, $value); |
| 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( |
| 340 | 'phone', |
| 341 | 'im', |
| 342 | 'email', |
| 343 | ))) { |
| 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, |
| 364 | 'field_name' => CRM_Core_BAO_UFField::isValidFieldName($name) ? $name : $fieldName, |
| 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 | * |
| 387 | * @param |
| 388 | * |
| 389 | * @return int |
| 390 | * Total number of rows |
| 391 | */ |
| 392 | public function getTotalCount($action) { |
| 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 | ) { |
| 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 | /** |
| 419 | * Return the qill for this selector. |
| 420 | * |
| 421 | * @return string |
| 422 | */ |
| 423 | public function getQill() { |
| 424 | return $this->_query->qill(); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Returns all the rows in the given offset and rowCount. |
| 429 | * |
| 430 | * @param string $action |
| 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. |
| 438 | * @param string $output |
| 439 | * What should the result set include (web/email/csv). |
| 440 | * |
| 441 | * @return int |
| 442 | * the total number of rows for this action |
| 443 | */ |
| 444 | public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $extraWhereClause = NULL) { |
| 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) { |
| 452 | $field = $vars[$key]; |
| 453 | $fieldArray = explode('-', $field['name']); |
| 454 | $fieldType = CRM_Utils_Array::value('2', $fieldArray); |
| 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 | } |
| 473 | $sort->_vars = $varArray; |
| 474 | } |
| 475 | |
| 476 | $additionalWhereClause = 'contact_a.is_deleted = 0'; |
| 477 | |
| 478 | if ($extraWhereClause) { |
| 479 | $additionalWhereClause .= " AND {$extraWhereClause}"; |
| 480 | } |
| 481 | |
| 482 | $returnQuery = NULL; |
| 483 | if ($this->_multiRecordTableName) { |
| 484 | $returnQuery = TRUE; |
| 485 | } |
| 486 | $this->_query->_useGroupBy = TRUE; |
| 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); |
| 493 | $result = CRM_Core_DAO::executeQuery($resQuery); |
| 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 | |
| 515 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
| 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 | |
| 526 | if (!empty($field['in_selector']) && |
| 527 | !in_array($key, $skipFields) |
| 528 | ) { |
| 529 | if (strpos($key, '-') !== FALSE) { |
| 530 | $value = explode('-', $key); |
| 531 | $fieldName = CRM_Utils_Array::value(0, $value); |
| 532 | $id = CRM_Utils_Array::value(1, $value); |
| 533 | $type = CRM_Utils_Array::value(2, $value); |
| 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( |
| 551 | 'phone', |
| 552 | 'im', |
| 553 | 'email', |
| 554 | ))) { |
| 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 | |
| 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 | |
| 588 | while ($result->fetch()) { |
| 589 | $this->_query->convertToPseudoNames($result); |
| 590 | |
| 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 | } |
| 596 | $row = array(); |
| 597 | $empty = TRUE; |
| 598 | $row[] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, |
| 599 | FALSE, |
| 600 | $result->contact_id, |
| 601 | $showProfileOverlay |
| 602 | ); |
| 603 | if ($result->sort_name) { |
| 604 | $row[] = $result->sort_name; |
| 605 | $empty = FALSE; |
| 606 | } |
| 607 | else { |
| 608 | continue; |
| 609 | } |
| 610 | |
| 611 | foreach ($names as $name) { |
| 612 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) { |
| 613 | $row[] = CRM_Core_BAO_CustomField::getDisplayValue($result->$name, |
| 614 | $cfID, |
| 615 | $this->_options, |
| 616 | $result->contact_id |
| 617 | ); |
| 618 | } |
| 619 | elseif (substr($name, -4) == '-url' && |
| 620 | !empty($result->$name) |
| 621 | ) { |
| 622 | $url = CRM_Utils_System::fixURL($result->$name); |
| 623 | $typeId = substr($name, 0, -4) . "-website_type_id"; |
| 624 | $typeName = CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId); |
| 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') { |
| 633 | $row[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name); |
| 634 | } |
| 635 | elseif ($multipleSelectFields && |
| 636 | array_key_exists($name, $multipleSelectFields) |
| 637 | ) { |
| 638 | $paramsNew = array($name => $result->$name); |
| 639 | $name = array($name => array('newName' => $name, 'groupName' => $name)); |
| 640 | |
| 641 | CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE); |
| 642 | $row[] = $paramsNew[$key]; |
| 643 | } |
| 644 | elseif (strpos($name, '-im')) { |
| 645 | if (!empty($result->$name)) { |
| 646 | $providerId = $name . "-provider_id"; |
| 647 | $providerName = CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->$providerId); |
| 648 | $row[] = $result->$name . " ({$providerName})"; |
| 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 | } |
| 663 | elseif (in_array($name, array( |
| 664 | 'addressee', |
| 665 | 'email_greeting', |
| 666 | 'postal_greeting', |
| 667 | ))) { |
| 668 | $dname = $name . '_display'; |
| 669 | $row[] = $result->$dname; |
| 670 | } |
| 671 | elseif (in_array($name, array( |
| 672 | 'birth_date', |
| 673 | 'deceased_date', |
| 674 | ))) { |
| 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; |
| 690 | $params = array( |
| 691 | 'id' => $result->contact_id, |
| 692 | 'gid' => implode(',', $this->_profileIds), |
| 693 | ); |
| 694 | |
| 695 | // pass record id param to view url for multi record view |
| 696 | if ($multiRecordTableId && $newLinks) { |
| 697 | if ($result->$multiRecordTableId) { |
| 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, |
| 717 | $params, |
| 718 | ts('more'), |
| 719 | FALSE, |
| 720 | 'profile.selector.row', |
| 721 | 'Contact', |
| 722 | $result->contact_id |
| 723 | ); |
| 724 | |
| 725 | if (!$empty) { |
| 726 | $rows[] = $row; |
| 727 | } |
| 728 | } |
| 729 | return $rows; |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Name of export file. |
| 734 | * |
| 735 | * @param string $output |
| 736 | * Type of output. |
| 737 | * |
| 738 | * @return string |
| 739 | * name of the file |
| 740 | */ |
| 741 | public function getExportFileName($output = 'csv') { |
| 742 | return ts('CiviCRM Profile Listings'); |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * set the _multiRecordTableName to display the result set. |
| 747 | * according to multi record custom field values |
| 748 | */ |
| 749 | public function setMultiRecordTableName($fields) { |
| 750 | $customGroupId = $multiRecordTableName = NULL; |
| 751 | $selectorSet = FALSE; |
| 752 | |
| 753 | foreach ($fields as $field => $properties) { |
| 754 | if (!CRM_Core_BAO_CustomField::getKeyID($field)) { |
| 755 | continue; |
| 756 | } |
| 757 | if ($cgId = CRM_Core_BAO_CustomField::isMultiRecordField($field)) { |
| 758 | $customGroupId = CRM_Utils_System::isNull($customGroupId) ? $cgId : $customGroupId; |
| 759 | |
| 760 | //if the field is submitted set multiRecordTableName |
| 761 | if ($customGroupId) { |
| 762 | $isSubmitted = FALSE; |
| 763 | foreach ($this->_query->_params as $key => $value) { |
| 764 | //check the query params 'where' element |
| 765 | if ($value[0] == $field) { |
| 766 | $isSubmitted = TRUE; |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if ($isSubmitted) { |
| 772 | $this->_multiRecordTableName |
| 773 | = $multiRecordTableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name'); |
| 774 | if ($multiRecordTableName) { |
| 775 | return NULL; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (!empty($properties['in_selector'])) { |
| 780 | $selectorSet = TRUE; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | if (!isset($customGroupId) || !$customGroupId) { |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | //if the field is in selector and not a searchable field |
| 791 | //get the proper customvalue table name |
| 792 | if ($selectorSet) { |
| 793 | $this->_multiRecordTableName |
| 794 | = $multiRecordTableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name'); |
| 795 | } |
| 796 | } //func close |
| 797 | |
| 798 | } |