| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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-2018 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Class CRM_Export_BAO_ExportProcessor |
| 36 | * |
| 37 | * Class to handle logic of export. |
| 38 | */ |
| 39 | class CRM_Export_BAO_ExportProcessor { |
| 40 | |
| 41 | /** |
| 42 | * @var int |
| 43 | */ |
| 44 | protected $queryMode; |
| 45 | |
| 46 | /** |
| 47 | * @var int |
| 48 | */ |
| 49 | protected $exportMode; |
| 50 | |
| 51 | /** |
| 52 | * Array of fields in the main query. |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
| 56 | protected $queryFields = []; |
| 57 | |
| 58 | /** |
| 59 | * Either AND or OR. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $queryOperator; |
| 64 | |
| 65 | /** |
| 66 | * Requested output fields. |
| 67 | * |
| 68 | * If set to NULL then it is 'primary fields only' |
| 69 | * which actually means pretty close to all fields! |
| 70 | * |
| 71 | * @var array|null |
| 72 | */ |
| 73 | protected $requestedFields; |
| 74 | |
| 75 | /** |
| 76 | * Is the contact being merged into a single household. |
| 77 | * |
| 78 | * @var bool |
| 79 | */ |
| 80 | protected $isMergeSameHousehold; |
| 81 | |
| 82 | /** |
| 83 | * Key representing the head of household in the relationship array. |
| 84 | * |
| 85 | * e.g. ['8_b_a' => 'Household Member Is', '8_a_b = 'Household Member Of'.....] |
| 86 | * |
| 87 | * @var |
| 88 | */ |
| 89 | protected $relationshipTypes = []; |
| 90 | |
| 91 | /** |
| 92 | * Array of properties to retrieve for relationships. |
| 93 | * |
| 94 | * @var array |
| 95 | */ |
| 96 | protected $relationshipReturnProperties = []; |
| 97 | |
| 98 | /** |
| 99 | * Get return properties by relationship. |
| 100 | * @return array |
| 101 | */ |
| 102 | public function getRelationshipReturnProperties() { |
| 103 | return $this->relationshipReturnProperties; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Export values for related contacts. |
| 108 | * |
| 109 | * @var array |
| 110 | */ |
| 111 | protected $relatedContactValues = []; |
| 112 | |
| 113 | /** |
| 114 | * @var array |
| 115 | */ |
| 116 | protected $returnProperties = []; |
| 117 | |
| 118 | /** |
| 119 | * CRM_Export_BAO_ExportProcessor constructor. |
| 120 | * |
| 121 | * @param int $exportMode |
| 122 | * @param array|NULL $requestedFields |
| 123 | * @param string $queryOperator |
| 124 | * @param bool $isMergeSameHousehold |
| 125 | */ |
| 126 | public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE) { |
| 127 | $this->setExportMode($exportMode); |
| 128 | $this->setQueryMode(); |
| 129 | $this->setQueryOperator($queryOperator); |
| 130 | $this->setRequestedFields($requestedFields); |
| 131 | $this->setRelationshipTypes(); |
| 132 | $this->setIsMergeSameHousehold($isMergeSameHousehold); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return array|null |
| 137 | */ |
| 138 | public function getRequestedFields() { |
| 139 | return $this->requestedFields; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param array|null $requestedFields |
| 144 | */ |
| 145 | public function setRequestedFields($requestedFields) { |
| 146 | $this->requestedFields = $requestedFields; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return array |
| 151 | */ |
| 152 | public function getReturnProperties() { |
| 153 | return $this->returnProperties; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @param array $returnProperties |
| 158 | */ |
| 159 | public function setReturnProperties($returnProperties) { |
| 160 | $this->returnProperties = $returnProperties; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @return array |
| 165 | */ |
| 166 | public function getRelationshipTypes() { |
| 167 | return $this->relationshipTypes; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | */ |
| 172 | public function setRelationshipTypes() { |
| 173 | $this->relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType( |
| 174 | NULL, |
| 175 | NULL, |
| 176 | NULL, |
| 177 | NULL, |
| 178 | TRUE, |
| 179 | 'name', |
| 180 | FALSE |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Set the value for a relationship type field. |
| 186 | * |
| 187 | * In this case we are building up an array of properties for a related contact. |
| 188 | * |
| 189 | * These may be used for direct exporting or for merge to household depending on the |
| 190 | * options selected. |
| 191 | * |
| 192 | * @param string $relationshipType |
| 193 | * @param int $contactID |
| 194 | * @param string $field |
| 195 | * @param string $value |
| 196 | */ |
| 197 | public function setRelationshipValue($relationshipType, $contactID, $field, $value) { |
| 198 | $this->relatedContactValues[$relationshipType][$contactID][$field] = $value; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get the value for a relationship type field. |
| 203 | * |
| 204 | * In this case we are building up an array of properties for a related contact. |
| 205 | * |
| 206 | * These may be used for direct exporting or for merge to household depending on the |
| 207 | * options selected. |
| 208 | * |
| 209 | * @param string $relationshipType |
| 210 | * @param int $contactID |
| 211 | * @param string $field |
| 212 | * |
| 213 | * @return string |
| 214 | */ |
| 215 | public function getRelationshipValue($relationshipType, $contactID, $field) { |
| 216 | return isset($this->relatedContactValues[$relationshipType][$contactID][$field]) ? $this->relatedContactValues[$relationshipType][$contactID][$field] : ''; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @return bool |
| 221 | */ |
| 222 | public function isMergeSameHousehold() { |
| 223 | return $this->isMergeSameHousehold; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param bool $isMergeSameHousehold |
| 228 | */ |
| 229 | public function setIsMergeSameHousehold($isMergeSameHousehold) { |
| 230 | $this->isMergeSameHousehold = $isMergeSameHousehold; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Return relationship types for household merge. |
| 235 | * |
| 236 | * @return mixed |
| 237 | */ |
| 238 | public function getHouseholdRelationshipTypes() { |
| 239 | if (!$this->isMergeSameHousehold()) { |
| 240 | return []; |
| 241 | } |
| 242 | return [ |
| 243 | CRM_Utils_Array::key('Household Member of', $this->getRelationshipTypes()), |
| 244 | CRM_Utils_Array::key('Head of Household for', $this->getRelationshipTypes()), |
| 245 | ]; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @param $fieldName |
| 250 | * @return bool |
| 251 | */ |
| 252 | public function isRelationshipTypeKey($fieldName) { |
| 253 | return array_key_exists($fieldName, $this->relationshipTypes); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * @param $fieldName |
| 259 | * @return bool |
| 260 | */ |
| 261 | public function isHouseholdMergeRelationshipTypeKey($fieldName) { |
| 262 | return in_array($fieldName, $this->getHouseholdRelationshipTypes()); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @return string |
| 267 | */ |
| 268 | public function getQueryOperator() { |
| 269 | return $this->queryOperator; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @param string $queryOperator |
| 274 | */ |
| 275 | public function setQueryOperator($queryOperator) { |
| 276 | $this->queryOperator = $queryOperator; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @return array |
| 281 | */ |
| 282 | public function getQueryFields() { |
| 283 | return $this->queryFields; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @param array $queryFields |
| 288 | */ |
| 289 | public function setQueryFields($queryFields) { |
| 290 | $this->queryFields = $queryFields; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @return int |
| 295 | */ |
| 296 | public function getQueryMode() { |
| 297 | return $this->queryMode; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Set the query mode based on the export mode. |
| 302 | */ |
| 303 | public function setQueryMode() { |
| 304 | |
| 305 | switch ($this->getExportMode()) { |
| 306 | case CRM_Export_Form_Select::CONTRIBUTE_EXPORT: |
| 307 | $this->queryMode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE; |
| 308 | break; |
| 309 | |
| 310 | case CRM_Export_Form_Select::EVENT_EXPORT: |
| 311 | $this->queryMode = CRM_Contact_BAO_Query::MODE_EVENT; |
| 312 | break; |
| 313 | |
| 314 | case CRM_Export_Form_Select::MEMBER_EXPORT: |
| 315 | $this->queryMode = CRM_Contact_BAO_Query::MODE_MEMBER; |
| 316 | break; |
| 317 | |
| 318 | case CRM_Export_Form_Select::PLEDGE_EXPORT: |
| 319 | $this->queryMode = CRM_Contact_BAO_Query::MODE_PLEDGE; |
| 320 | break; |
| 321 | |
| 322 | case CRM_Export_Form_Select::CASE_EXPORT: |
| 323 | $this->queryMode = CRM_Contact_BAO_Query::MODE_CASE; |
| 324 | break; |
| 325 | |
| 326 | case CRM_Export_Form_Select::GRANT_EXPORT: |
| 327 | $this->queryMode = CRM_Contact_BAO_Query::MODE_GRANT; |
| 328 | break; |
| 329 | |
| 330 | case CRM_Export_Form_Select::ACTIVITY_EXPORT: |
| 331 | $this->queryMode = CRM_Contact_BAO_Query::MODE_ACTIVITY; |
| 332 | break; |
| 333 | |
| 334 | default: |
| 335 | $this->queryMode = CRM_Contact_BAO_Query::MODE_CONTACTS; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * @return int |
| 341 | */ |
| 342 | public function getExportMode() { |
| 343 | return $this->exportMode; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * @param int $exportMode |
| 348 | */ |
| 349 | public function setExportMode($exportMode) { |
| 350 | $this->exportMode = $exportMode; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Get the name for the export file. |
| 355 | * |
| 356 | * @return string |
| 357 | */ |
| 358 | public function getExportFileName() { |
| 359 | switch ($this->getExportMode()) { |
| 360 | case CRM_Export_Form_Select::CONTACT_EXPORT: |
| 361 | return ts('CiviCRM Contact Search'); |
| 362 | |
| 363 | case CRM_Export_Form_Select::CONTRIBUTE_EXPORT: |
| 364 | return ts('CiviCRM Contribution Search'); |
| 365 | |
| 366 | case CRM_Export_Form_Select::MEMBER_EXPORT: |
| 367 | return ts('CiviCRM Member Search'); |
| 368 | |
| 369 | case CRM_Export_Form_Select::EVENT_EXPORT: |
| 370 | return ts('CiviCRM Participant Search'); |
| 371 | |
| 372 | case CRM_Export_Form_Select::PLEDGE_EXPORT: |
| 373 | return ts('CiviCRM Pledge Search'); |
| 374 | |
| 375 | case CRM_Export_Form_Select::CASE_EXPORT: |
| 376 | return ts('CiviCRM Case Search'); |
| 377 | |
| 378 | case CRM_Export_Form_Select::GRANT_EXPORT: |
| 379 | return ts('CiviCRM Grant Search'); |
| 380 | |
| 381 | case CRM_Export_Form_Select::ACTIVITY_EXPORT: |
| 382 | return ts('CiviCRM Activity Search'); |
| 383 | |
| 384 | default: |
| 385 | // Legacy code suggests the value could be 'financial' - ie. something |
| 386 | // other than what should be accepted. However, I suspect that this line is |
| 387 | // never hit. |
| 388 | return ts('CiviCRM Search'); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * @param $params |
| 394 | * @param $order |
| 395 | * @param $returnProperties |
| 396 | * @return array |
| 397 | */ |
| 398 | public function runQuery($params, $order, $returnProperties) { |
| 399 | $query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL, |
| 400 | FALSE, FALSE, $this->getQueryMode(), |
| 401 | FALSE, TRUE, TRUE, NULL, $this->getQueryOperator() |
| 402 | ); |
| 403 | |
| 404 | //sort by state |
| 405 | //CRM-15301 |
| 406 | $query->_sort = $order; |
| 407 | list($select, $from, $where, $having) = $query->query(); |
| 408 | $this->setQueryFields($query->_fields); |
| 409 | return array($query, $select, $from, $where, $having); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Get array of fields to return, over & above those defined in the main contact exportable fields. |
| 414 | * |
| 415 | * These include export mode specific fields & some fields apparently required as 'exportableFields' |
| 416 | * but not returned by the function of the same name. |
| 417 | * |
| 418 | * @return array |
| 419 | * Array of fields to return in the format ['field_name' => 1,...] |
| 420 | */ |
| 421 | public function getAdditionalReturnProperties() { |
| 422 | |
| 423 | $missing = [ |
| 424 | 'location_type', |
| 425 | 'im_provider', |
| 426 | 'phone_type_id', |
| 427 | 'provider_id', |
| 428 | 'current_employer', |
| 429 | ]; |
| 430 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTACTS) { |
| 431 | $componentSpecificFields = []; |
| 432 | } |
| 433 | else { |
| 434 | $componentSpecificFields = CRM_Contact_BAO_Query::defaultReturnProperties($this->getQueryMode()); |
| 435 | } |
| 436 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_PLEDGE) { |
| 437 | $componentSpecificFields = array_merge($componentSpecificFields, CRM_Pledge_BAO_Query::extraReturnProperties($this->getQueryMode())); |
| 438 | unset($componentSpecificFields['contribution_status_id']); |
| 439 | unset($componentSpecificFields['pledge_status_id']); |
| 440 | unset($componentSpecificFields['pledge_payment_status_id']); |
| 441 | } |
| 442 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CASE) { |
| 443 | $componentSpecificFields = array_merge($componentSpecificFields, CRM_Case_BAO_Query::extraReturnProperties($this->getQueryMode())); |
| 444 | } |
| 445 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { |
| 446 | $componentSpecificFields = array_merge($componentSpecificFields, CRM_Contribute_BAO_Query::softCreditReturnProperties(TRUE)); |
| 447 | unset($componentSpecificFields['contribution_status_id']); |
| 448 | } |
| 449 | return array_merge(array_fill_keys($missing, 1), $componentSpecificFields); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Should payment fields be appended to the export. |
| 454 | * |
| 455 | * (This is pretty hacky so hopefully this function won't last long - notice |
| 456 | * how obviously it should be part of the above function!). |
| 457 | */ |
| 458 | public function isExportPaymentFields() { |
| 459 | if ($this->getRequestedFields() === NULL |
| 460 | && in_array($this->getQueryMode(), [ |
| 461 | CRM_Contact_BAO_Query::MODE_EVENT, |
| 462 | CRM_Contact_BAO_Query::MODE_MEMBER, |
| 463 | CRM_Contact_BAO_Query::MODE_PLEDGE, |
| 464 | ])) { |
| 465 | return TRUE; |
| 466 | } |
| 467 | elseif ($this->isExportSpecifiedPaymentFields()) { |
| 468 | return TRUE; |
| 469 | } |
| 470 | return FALSE; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Has specific payment fields been requested (as opposed to via all fields). |
| 475 | * |
| 476 | * If specific fields have been requested then they get added at various points. |
| 477 | * |
| 478 | * @return bool |
| 479 | */ |
| 480 | public function isExportSpecifiedPaymentFields() { |
| 481 | if ($this->getRequestedFields() !== NULL && $this->hasRequestedComponentPaymentFields()) { |
| 482 | return TRUE; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Get the name of the id field in the table that connects contributions to the export entity. |
| 488 | */ |
| 489 | public function getPaymentTableID() { |
| 490 | if ($this->getRequestedFields() === NULL) { |
| 491 | $mapping = [ |
| 492 | CRM_Contact_BAO_Query::MODE_EVENT => 'participant_id', |
| 493 | CRM_Contact_BAO_Query::MODE_MEMBER => 'membership_id', |
| 494 | CRM_Contact_BAO_Query::MODE_PLEDGE => 'pledge_payment_id', |
| 495 | ]; |
| 496 | return isset($mapping[$this->getQueryMode()]) ? $mapping[$this->getQueryMode()] : ''; |
| 497 | } |
| 498 | elseif ($this->hasRequestedComponentPaymentFields()) { |
| 499 | return 'participant_id'; |
| 500 | } |
| 501 | return FALSE; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Have component payment fields been requested. |
| 506 | * |
| 507 | * @return bool |
| 508 | */ |
| 509 | protected function hasRequestedComponentPaymentFields() { |
| 510 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_EVENT) { |
| 511 | $participantPaymentFields = array_intersect_key($this->getComponentPaymentFields(), $this->getReturnProperties()); |
| 512 | if (!empty($participantPaymentFields)) { |
| 513 | return TRUE; |
| 514 | } |
| 515 | } |
| 516 | return FALSE; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Get fields that indicate payment fields have been requested for a component. |
| 521 | * |
| 522 | * @return array |
| 523 | */ |
| 524 | protected function getComponentPaymentFields() { |
| 525 | return [ |
| 526 | 'componentPaymentField_total_amount' => ts('Total Amount'), |
| 527 | 'componentPaymentField_contribution_status' => ts('Contribution Status'), |
| 528 | 'componentPaymentField_received_date' => ts('Date Received'), |
| 529 | 'componentPaymentField_payment_instrument' => ts('Payment Method'), |
| 530 | 'componentPaymentField_transaction_id' => ts('Transaction ID'), |
| 531 | ]; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Get the default properties when not specified. |
| 536 | * |
| 537 | * In the UI this appears as 'Primary fields only' but in practice it's |
| 538 | * most of the kitchen sink and the hallway closet thrown in. |
| 539 | * |
| 540 | * Since CRM-952 custom fields are excluded, but no other form of mercy is shown. |
| 541 | * |
| 542 | * @return array |
| 543 | */ |
| 544 | public function getDefaultReturnProperties() { |
| 545 | $returnProperties = []; |
| 546 | $fields = CRM_Contact_BAO_Contact::exportableFields('All', TRUE, TRUE); |
| 547 | $skippedFields = ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTACTS) ? [] : [ |
| 548 | 'groups', |
| 549 | 'tags', |
| 550 | 'notes' |
| 551 | ]; |
| 552 | |
| 553 | foreach ($fields as $key => $var) { |
| 554 | if ($key && (substr($key, 0, 6) != 'custom') && !in_array($key, $skippedFields)) { |
| 555 | $returnProperties[$key] = 1; |
| 556 | } |
| 557 | } |
| 558 | $returnProperties = array_merge($returnProperties, $this->getAdditionalReturnProperties()); |
| 559 | return $returnProperties; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Add the field to relationship return properties & return it. |
| 564 | * |
| 565 | * This function is doing both setting & getting which is yuck but it is an interim |
| 566 | * refactor. |
| 567 | * |
| 568 | * @param array $value |
| 569 | * @param string $relationshipKey |
| 570 | * |
| 571 | * @return array |
| 572 | */ |
| 573 | public function setRelationshipReturnProperties($value, $relationshipKey) { |
| 574 | $relPhoneTypeId = $relIMProviderId = NULL; |
| 575 | if (!empty($value[2])) { |
| 576 | $relationField = CRM_Utils_Array::value(2, $value); |
| 577 | if (trim(CRM_Utils_Array::value(3, $value))) { |
| 578 | $relLocTypeId = CRM_Utils_Array::value(3, $value); |
| 579 | } |
| 580 | else { |
| 581 | $relLocTypeId = 'Primary'; |
| 582 | } |
| 583 | |
| 584 | if ($relationField == 'phone') { |
| 585 | $relPhoneTypeId = CRM_Utils_Array::value(4, $value); |
| 586 | } |
| 587 | elseif ($relationField == 'im') { |
| 588 | $relIMProviderId = CRM_Utils_Array::value(4, $value); |
| 589 | } |
| 590 | } |
| 591 | elseif (!empty($value[4])) { |
| 592 | $relationField = CRM_Utils_Array::value(4, $value); |
| 593 | $relLocTypeId = CRM_Utils_Array::value(5, $value); |
| 594 | if ($relationField == 'phone') { |
| 595 | $relPhoneTypeId = CRM_Utils_Array::value(6, $value); |
| 596 | } |
| 597 | elseif ($relationField == 'im') { |
| 598 | $relIMProviderId = CRM_Utils_Array::value(6, $value); |
| 599 | } |
| 600 | } |
| 601 | if (in_array($relationField, $this->getValidLocationFields()) && is_numeric($relLocTypeId)) { |
| 602 | $locationName = CRM_Core_PseudoConstant::getName('CRM_Core_BAO_Address', 'location_type_id', $relLocTypeId); |
| 603 | if ($relPhoneTypeId) { |
| 604 | $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName]['phone-' . $relPhoneTypeId] = 1; |
| 605 | } |
| 606 | elseif ($relIMProviderId) { |
| 607 | $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName]['im-' . $relIMProviderId] = 1; |
| 608 | } |
| 609 | else { |
| 610 | $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName][$relationField] = 1; |
| 611 | } |
| 612 | } |
| 613 | else { |
| 614 | $this->relationshipReturnProperties[$relationshipKey][$relationField] = 1; |
| 615 | } |
| 616 | return $this->relationshipReturnProperties[$relationshipKey]; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * Add the main return properties to the household merge properties if needed for merging. |
| 621 | * |
| 622 | * If we are using household merge we need to add these to the relationship properties to |
| 623 | * be retrieved. |
| 624 | * |
| 625 | * @param $returnProperties |
| 626 | */ |
| 627 | public function setHouseholdMergeReturnProperties($returnProperties) { |
| 628 | foreach ($this->getHouseholdRelationshipTypes() as $householdRelationshipType) { |
| 629 | $this->relationshipReturnProperties[$householdRelationshipType] = $returnProperties; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Get the default location fields to request. |
| 635 | * |
| 636 | * @return array |
| 637 | */ |
| 638 | public function getValidLocationFields() { |
| 639 | return [ |
| 640 | 'street_address', |
| 641 | 'supplemental_address_1', |
| 642 | 'supplemental_address_2', |
| 643 | 'supplemental_address_3', |
| 644 | 'city', |
| 645 | 'postal_code', |
| 646 | 'postal_code_suffix', |
| 647 | 'geo_code_1', |
| 648 | 'geo_code_2', |
| 649 | 'state_province', |
| 650 | 'country', |
| 651 | 'phone', |
| 652 | 'email', |
| 653 | 'im', |
| 654 | ]; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Get the sql column definition for the given field. |
| 659 | * |
| 660 | * @param $field |
| 661 | * |
| 662 | * @return mixed |
| 663 | */ |
| 664 | public function getSqlColumnDefinition($field) { |
| 665 | $fieldName = $this->getMungedFieldName($field); |
| 666 | |
| 667 | // early exit for master_id, CRM-12100 |
| 668 | // in the DB it is an ID, but in the export, we retrive the display_name of the master record |
| 669 | // also for current_employer, CRM-16939 |
| 670 | if ($fieldName == 'master_id' || $fieldName == 'current_employer') { |
| 671 | return "$fieldName varchar(128)"; |
| 672 | } |
| 673 | |
| 674 | if (substr($fieldName, -11) == 'campaign_id') { |
| 675 | // CRM-14398 |
| 676 | return "$fieldName varchar(128)"; |
| 677 | } |
| 678 | |
| 679 | $queryFields = $this->getQueryFields(); |
| 680 | $lookUp = ['prefix_id', 'suffix_id']; |
| 681 | // set the sql columns |
| 682 | if (isset($queryFields[$field]['type'])) { |
| 683 | switch ($queryFields[$field]['type']) { |
| 684 | case CRM_Utils_Type::T_INT: |
| 685 | case CRM_Utils_Type::T_BOOLEAN: |
| 686 | if (in_array($field, $lookUp)) { |
| 687 | return "$fieldName varchar(255)"; |
| 688 | } |
| 689 | else { |
| 690 | return "$fieldName varchar(16)"; |
| 691 | } |
| 692 | |
| 693 | case CRM_Utils_Type::T_STRING: |
| 694 | if (isset($queryFields[$field]['maxlength'])) { |
| 695 | return "$fieldName varchar({$queryFields[$field]['maxlength']})"; |
| 696 | } |
| 697 | else { |
| 698 | return "$fieldName varchar(255)"; |
| 699 | } |
| 700 | |
| 701 | case CRM_Utils_Type::T_TEXT: |
| 702 | case CRM_Utils_Type::T_LONGTEXT: |
| 703 | case CRM_Utils_Type::T_BLOB: |
| 704 | case CRM_Utils_Type::T_MEDIUMBLOB: |
| 705 | return "$fieldName longtext"; |
| 706 | |
| 707 | case CRM_Utils_Type::T_FLOAT: |
| 708 | case CRM_Utils_Type::T_ENUM: |
| 709 | case CRM_Utils_Type::T_DATE: |
| 710 | case CRM_Utils_Type::T_TIME: |
| 711 | case CRM_Utils_Type::T_TIMESTAMP: |
| 712 | case CRM_Utils_Type::T_MONEY: |
| 713 | case CRM_Utils_Type::T_EMAIL: |
| 714 | case CRM_Utils_Type::T_URL: |
| 715 | case CRM_Utils_Type::T_CCNUM: |
| 716 | default: |
| 717 | return "$fieldName varchar(32)"; |
| 718 | } |
| 719 | } |
| 720 | else { |
| 721 | if (substr($fieldName, -3, 3) == '_id') { |
| 722 | return "$fieldName varchar(255)"; |
| 723 | } |
| 724 | elseif (substr($fieldName, -5, 5) == '_note') { |
| 725 | return "$fieldName text"; |
| 726 | } |
| 727 | else { |
| 728 | $changeFields = [ |
| 729 | 'groups', |
| 730 | 'tags', |
| 731 | 'notes', |
| 732 | ]; |
| 733 | |
| 734 | if (in_array($fieldName, $changeFields)) { |
| 735 | return "$fieldName text"; |
| 736 | } |
| 737 | else { |
| 738 | // set the sql columns for custom data |
| 739 | if (isset($queryFields[$field]['data_type'])) { |
| 740 | |
| 741 | switch ($queryFields[$field]['data_type']) { |
| 742 | case 'String': |
| 743 | // May be option labels, which could be up to 512 characters |
| 744 | $length = max(512, CRM_Utils_Array::value('text_length', $queryFields[$field])); |
| 745 | return "$fieldName varchar($length)"; |
| 746 | |
| 747 | case 'Country': |
| 748 | case 'StateProvince': |
| 749 | case 'Link': |
| 750 | return "$fieldName varchar(255)"; |
| 751 | |
| 752 | case 'Memo': |
| 753 | return "$fieldName text"; |
| 754 | |
| 755 | default: |
| 756 | return "$fieldName varchar(255)"; |
| 757 | } |
| 758 | } |
| 759 | else { |
| 760 | return "$fieldName text"; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Get the munged field name. |
| 769 | * |
| 770 | * @param string $field |
| 771 | * @return string |
| 772 | */ |
| 773 | public function getMungedFieldName($field) { |
| 774 | $fieldName = CRM_Utils_String::munge(strtolower($field), '_', 64); |
| 775 | if ($fieldName == 'id') { |
| 776 | $fieldName = 'civicrm_primary_id'; |
| 777 | } |
| 778 | return $fieldName; |
| 779 | } |
| 780 | |
| 781 | } |