Merge pull request #14875 from eileenmcnaughton/export_2
[civicrm-core.git] / CRM / Export / BAO / Export.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * This class contains the functions for Component export
36 *
37 */
38 class CRM_Export_BAO_Export {
39 // increase this number a lot to avoid making too many queries
40 // LIMIT is not much faster than a no LIMIT query
41 // CRM-7675
42 const EXPORT_ROW_COUNT = 100000;
43
44 /**
45 * Get the list the export fields.
46 *
47 * @param int $selectAll
48 * User preference while export.
49 * @param array $ids
50 * Contact ids.
51 * @param array $params
52 * Associated array of fields.
53 * @param string $order
54 * Order by clause.
55 * @param array $fields
56 * Associated array of fields.
57 * @param array $moreReturnProperties
58 * Additional return fields.
59 * @param int $exportMode
60 * Export mode.
61 * @param string $componentClause
62 * Component clause.
63 * @param string $componentTable
64 * Component table.
65 * @param bool $mergeSameAddress
66 * Merge records if they have same address.
67 * @param bool $mergeSameHousehold
68 * Merge records if they belong to the same household.
69 *
70 * @param array $exportParams
71 * @param string $queryOperator
72 *
73 * @return array|null
74 * An array can be requested from within a unit test.
75 *
76 * @throws \CRM_Core_Exception
77 */
78 public static function exportComponents(
79 $selectAll,
80 $ids,
81 $params,
82 $order = NULL,
83 $fields = NULL,
84 $moreReturnProperties = NULL,
85 $exportMode = CRM_Export_Form_Select::CONTACT_EXPORT,
86 $componentClause = NULL,
87 $componentTable = NULL,
88 $mergeSameAddress = FALSE,
89 $mergeSameHousehold = FALSE,
90 $exportParams = [],
91 $queryOperator = 'AND'
92 ) {
93
94 $isPostalOnly = (
95 isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
96 $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
97 );
98
99 if (!$selectAll && $componentTable && !empty($exportParams['additional_group'])) {
100 // If an Additional Group is selected, then all contacts in that group are
101 // added to the export set (filtering out duplicates).
102 // Really - the calling function could do this ... just saying
103 // @todo take a whip to the calling function.
104 CRM_Core_DAO::executeQuery("
105 INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_contact gc WHERE gc.group_id = {$exportParams['additional_group']} ON DUPLICATE KEY UPDATE {$componentTable}.contact_id = gc.contact_id"
106 );
107 }
108 // rectify params to what proximity search expects if there is a value for prox_distance
109 // CRM-7021
110 // @todo - move this back to the calling functions
111 if (!empty($params)) {
112 CRM_Contact_BAO_ProximityQuery::fixInputParams($params);
113 }
114 // @todo everything from this line up should go back to the calling functions.
115 $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold, $isPostalOnly, $mergeSameAddress, $exportParams);
116 if ($moreReturnProperties) {
117 $processor->setAdditionalRequestedReturnProperties($moreReturnProperties);
118 }
119 $processor->setComponentTable($componentTable);
120 $processor->setComponentClause($componentClause);
121
122 list($query, $queryString) = $processor->runQuery($params, $order);
123
124 // This perhaps only needs calling when $mergeSameHousehold == 1
125 self::buildRelatedContactArray($selectAll, $ids, $processor, $componentTable);
126
127 $addPaymentHeader = FALSE;
128
129 list($outputColumns, $metadata) = $processor->getExportStructureArrays();
130
131 if ($processor->isMergeSameAddress()) {
132 foreach (array_keys($processor->getAdditionalFieldsForSameAddressMerge()) as $field) {
133 $processor->setColumnAsCalculationOnly($field);
134 }
135 }
136
137 $paymentDetails = [];
138 if ($processor->isExportPaymentFields()) {
139 // get payment related in for event and members
140 $paymentDetails = CRM_Contribute_BAO_Contribution::getContributionDetails($exportMode, $ids);
141 //get all payment headers.
142 // If we haven't selected specific payment fields, load in all the
143 // payment headers.
144 if (!$processor->isExportSpecifiedPaymentFields()) {
145 if (!empty($paymentDetails)) {
146 $addPaymentHeader = TRUE;
147 foreach (array_keys($processor->getPaymentHeaders()) as $paymentField) {
148 $processor->addOutputSpecification($paymentField);
149 }
150 }
151 }
152 }
153
154 $componentDetails = [];
155
156 $rowCount = self::EXPORT_ROW_COUNT;
157 $offset = 0;
158 // we write to temp table often to avoid using too much memory
159 $tempRowCount = 100;
160
161 $count = -1;
162
163 $headerRows = $processor->getHeaderRows();
164 $sqlColumns = $processor->getSQLColumns();
165 $processor->createTempTable();
166 $limitReached = FALSE;
167
168 while (!$limitReached) {
169 $limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}";
170 CRM_Core_DAO::disableFullGroupByMode();
171 $iterationDAO = CRM_Core_DAO::executeQuery($limitQuery);
172 CRM_Core_DAO::reenableFullGroupByMode();
173 // If this is less than our limit by the end of the iteration we do not need to run the query again to
174 // check if some remain.
175 $rowsThisIteration = 0;
176
177 while ($iterationDAO->fetch()) {
178 $count++;
179 $rowsThisIteration++;
180 $row = $processor->buildRow($query, $iterationDAO, $outputColumns, $metadata, $paymentDetails, $addPaymentHeader);
181 if ($row === FALSE) {
182 continue;
183 }
184
185 // add component info
186 // write the row to a file
187 $componentDetails[] = $row;
188
189 // output every $tempRowCount rows
190 if ($count % $tempRowCount == 0) {
191 self::writeDetailsToTable($processor, $componentDetails, $sqlColumns);
192 $componentDetails = [];
193 }
194 }
195 if ($rowsThisIteration < self::EXPORT_ROW_COUNT) {
196 $limitReached = TRUE;
197 }
198 $offset += $rowCount;
199 }
200
201 if ($processor->getTemporaryTable()) {
202 self::writeDetailsToTable($processor, $componentDetails);
203
204 // do merge same address and merge same household processing
205 if ($mergeSameAddress) {
206 $processor->mergeSameAddress();
207 }
208
209 // call export hook
210 $table = $processor->getTemporaryTable();
211 CRM_Utils_Hook::export($table, $headerRows, $sqlColumns, $exportMode, $componentTable, $ids);
212 if ($table !== $processor->getTemporaryTable()) {
213 CRM_Core_Error::deprecatedFunctionWarning('altering the export table in the hook is deprecated (in some flows the table itself will be)');
214 $processor->setTemporaryTable($table);
215 }
216
217 // In order to be able to write a unit test against this function we need to suppress
218 // the csv writing. In future hopefully the csv writing & the main processing will be in separate functions.
219 if (empty($exportParams['suppress_csv_for_testing'])) {
220 self::writeCSVFromTable($headerRows, $sqlColumns, $processor);
221 }
222 else {
223 // return tableName sqlColumns headerRows in test context
224 return [$processor->getTemporaryTable(), $sqlColumns, $headerRows, $processor];
225 }
226
227 // delete the export temp table and component table
228 $sql = "DROP TABLE IF EXISTS " . $processor->getTemporaryTable();
229 CRM_Core_DAO::executeQuery($sql);
230 CRM_Core_DAO::reenableFullGroupByMode();
231 CRM_Utils_System::civiExit(0, ['processor' => $processor]);
232 }
233 else {
234 CRM_Core_DAO::reenableFullGroupByMode();
235 throw new CRM_Core_Exception(ts('No records to export'));
236 }
237 }
238
239 /**
240 * Handle import error file creation.
241 */
242 public static function invoke() {
243 $type = CRM_Utils_Request::retrieve('type', 'Positive');
244 $parserName = CRM_Utils_Request::retrieve('parser', 'String');
245 if (empty($parserName) || empty($type)) {
246 return;
247 }
248
249 // clean and ensure parserName is a valid string
250 $parserName = CRM_Utils_String::munge($parserName);
251 $parserClass = explode('_', $parserName);
252
253 // make sure parserClass is in the CRM namespace and
254 // at least 3 levels deep
255 if ($parserClass[0] == 'CRM' &&
256 count($parserClass) >= 3
257 ) {
258 require_once str_replace('_', DIRECTORY_SEPARATOR, $parserName) . ".php";
259 // ensure the functions exists
260 if (method_exists($parserName, 'errorFileName') &&
261 method_exists($parserName, 'saveFileName')
262 ) {
263 $errorFileName = $parserName::errorFileName($type);
264 $saveFileName = $parserName::saveFileName($type);
265 if (!empty($errorFileName) && !empty($saveFileName)) {
266 CRM_Utils_System::setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
267 CRM_Utils_System::setHttpHeader('Content-Description', 'File Transfer');
268 CRM_Utils_System::setHttpHeader('Content-Type', 'text/csv');
269 CRM_Utils_System::setHttpHeader('Content-Length', filesize($errorFileName));
270 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename=' . $saveFileName);
271
272 readfile($errorFileName);
273 }
274 }
275 }
276 CRM_Utils_System::civiExit();
277 }
278
279 /**
280 * @param $customSearchClass
281 * @param $formValues
282 * @param $order
283 */
284 public static function exportCustom($customSearchClass, $formValues, $order) {
285 $ext = CRM_Extension_System::singleton()->getMapper();
286 if (!$ext->isExtensionClass($customSearchClass)) {
287 require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php';
288 }
289 else {
290 require_once $ext->classToPath($customSearchClass);
291 }
292 $search = new $customSearchClass($formValues);
293
294 $includeContactIDs = FALSE;
295 if ($formValues['radio_ts'] == 'ts_sel') {
296 $includeContactIDs = TRUE;
297 }
298
299 $sql = $search->all(0, 0, $order, $includeContactIDs);
300
301 $columns = $search->columns();
302
303 $header = array_keys($columns);
304 $fields = array_values($columns);
305
306 $rows = [];
307 $dao = CRM_Core_DAO::executeQuery($sql);
308 $alterRow = FALSE;
309 if (method_exists($search, 'alterRow')) {
310 $alterRow = TRUE;
311 }
312 while ($dao->fetch()) {
313 $row = [];
314
315 foreach ($fields as $field) {
316 $unqualified_field = CRM_Utils_Array::First(array_slice(explode('.', $field), -1));
317 $row[$field] = $dao->$unqualified_field;
318 }
319 if ($alterRow) {
320 $search->alterRow($row);
321 }
322 $rows[] = $row;
323 }
324
325 CRM_Core_Report_Excel::writeCSVFile(ts('CiviCRM Contact Search'), $header, $rows);
326 CRM_Utils_System::civiExit();
327 }
328
329 /**
330 * @param \CRM_Export_BAO_ExportProcessor $processor
331 * @param $details
332 */
333 public static function writeDetailsToTable($processor, $details) {
334 $tableName = $processor->getTemporaryTable();
335 if (empty($details)) {
336 return;
337 }
338
339 $sql = "
340 SELECT max(id)
341 FROM $tableName
342 ";
343
344 $id = CRM_Core_DAO::singleValueQuery($sql);
345 if (!$id) {
346 $id = 0;
347 }
348
349 $sqlClause = [];
350
351 foreach ($details as $row) {
352 $id++;
353 $valueString = [$id];
354 foreach ($row as $value) {
355 if (empty($value)) {
356 $valueString[] = "''";
357 }
358 else {
359 $valueString[] = "'" . CRM_Core_DAO::escapeString($value) . "'";
360 }
361 }
362 $sqlClause[] = '(' . implode(',', $valueString) . ')';
363 }
364 $sqlColumns = array_merge(['id' => 1], $processor->getSQLColumns());
365 $sqlColumnString = '(' . implode(',', array_keys($sqlColumns)) . ')';
366
367 $sqlValueString = implode(",\n", $sqlClause);
368
369 $sql = "
370 INSERT INTO $tableName $sqlColumnString
371 VALUES $sqlValueString
372 ";
373 CRM_Core_DAO::executeQuery($sql);
374 }
375
376 /**
377 * @param $headerRows
378 * @param $sqlColumns
379 * @param \CRM_Export_BAO_ExportProcessor $processor
380 */
381 public static function writeCSVFromTable($headerRows, $sqlColumns, $processor) {
382 $exportTempTable = $processor->getTemporaryTable();
383 $writeHeader = TRUE;
384 $offset = 0;
385 $limit = self::EXPORT_ROW_COUNT;
386
387 $query = "SELECT * FROM $exportTempTable";
388
389 while (1) {
390 $limitQuery = $query . "
391 LIMIT $offset, $limit
392 ";
393 $dao = CRM_Core_DAO::executeQuery($limitQuery);
394
395 if ($dao->N <= 0) {
396 break;
397 }
398
399 $componentDetails = [];
400 while ($dao->fetch()) {
401 $row = [];
402
403 foreach (array_keys($processor->getSQLColumns()) as $column) {
404 $row[$column] = $dao->$column;
405 }
406 $componentDetails[] = $row;
407 }
408 CRM_Core_Report_Excel::writeCSVFile($processor->getExportFileName(),
409 $headerRows,
410 $componentDetails,
411 NULL,
412 $writeHeader
413 );
414
415 $writeHeader = FALSE;
416 $offset += $limit;
417 }
418 }
419
420 /**
421 * Build componentPayment fields.
422 *
423 * This is no longer used by export but BAO_Mapping still calls it & we
424 * should find a generic way to handle this or move this to that class.
425 *
426 * @deprecated
427 */
428 public static function componentPaymentFields() {
429 static $componentPaymentFields;
430 if (!isset($componentPaymentFields)) {
431 $componentPaymentFields = [
432 'componentPaymentField_total_amount' => ts('Total Amount'),
433 'componentPaymentField_contribution_status' => ts('Contribution Status'),
434 'componentPaymentField_received_date' => ts('Date Received'),
435 'componentPaymentField_payment_instrument' => ts('Payment Method'),
436 'componentPaymentField_transaction_id' => ts('Transaction ID'),
437 ];
438 }
439 return $componentPaymentFields;
440 }
441
442 /**
443 * Get the values of linked household contact.
444 *
445 * @param CRM_Core_DAO $relDAO
446 * @param array $value
447 * @param string $field
448 * @param array $row
449 */
450 private static function fetchRelationshipDetails($relDAO, $value, $field, &$row) {
451 $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
452 $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
453 $i18n = CRM_Core_I18n::singleton();
454 $field = $field . '_';
455
456 foreach ($value as $relationField => $relationValue) {
457 if (is_object($relDAO) && property_exists($relDAO, $relationField)) {
458 $fieldValue = $relDAO->$relationField;
459 if ($relationField == 'phone_type_id') {
460 $fieldValue = $phoneTypes[$relationValue];
461 }
462 elseif ($relationField == 'provider_id') {
463 $fieldValue = CRM_Utils_Array::value($relationValue, $imProviders);
464 }
465 // CRM-13995
466 elseif (is_object($relDAO) && in_array($relationField, [
467 'email_greeting',
468 'postal_greeting',
469 'addressee',
470 ])) {
471 //special case for greeting replacement
472 $fldValue = "{$relationField}_display";
473 $fieldValue = $relDAO->$fldValue;
474 }
475 }
476 elseif (is_object($relDAO) && $relationField == 'state_province') {
477 $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id);
478 }
479 elseif (is_object($relDAO) && $relationField == 'country') {
480 $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id);
481 }
482 else {
483 $fieldValue = '';
484 }
485 $relPrefix = $field . $relationField;
486
487 if (is_object($relDAO) && $relationField == 'id') {
488 $row[$relPrefix] = $relDAO->contact_id;
489 }
490 elseif (is_array($relationValue) && $relationField == 'location') {
491 foreach ($relationValue as $ltype => $val) {
492 // If the location name has a space in it the we need to handle that. This
493 // is kinda hacky but specifically covered in the ExportTest so later efforts to
494 // improve it should be secure in the knowled it will be caught.
495 $ltype = str_replace(' ', '_', $ltype);
496 foreach (array_keys($val) as $fld) {
497 $type = explode('-', $fld);
498 $fldValue = "{$ltype}-" . $type[0];
499 if (!empty($type[1])) {
500 $fldValue .= "-" . $type[1];
501 }
502 // CRM-3157: localise country, region (both have ‘country’ context)
503 // and state_province (‘province’ context)
504 switch (TRUE) {
505 case (!is_object($relDAO)):
506 $row[$field . '_' . $fldValue] = '';
507 break;
508
509 case in_array('country', $type):
510 case in_array('world_region', $type):
511 $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
512 ['context' => 'country']
513 );
514 break;
515
516 case in_array('state_province', $type):
517 $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
518 ['context' => 'province']
519 );
520 break;
521
522 default:
523 $row[$field . '_' . $fldValue] = $relDAO->$fldValue;
524 break;
525 }
526 }
527 }
528 }
529 elseif (isset($fieldValue) && $fieldValue != '') {
530 //check for custom data
531 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($relationField)) {
532 $row[$relPrefix] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID);
533 }
534 else {
535 //normal relationship fields
536 // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context)
537 switch ($relationField) {
538 case 'country':
539 case 'world_region':
540 $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'country']);
541 break;
542
543 case 'state_province':
544 $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'province']);
545 break;
546
547 default:
548 $row[$relPrefix] = $fieldValue;
549 break;
550 }
551 }
552 }
553 else {
554 // if relation field is empty or null
555 $row[$relPrefix] = '';
556 }
557 }
558 }
559
560 /**
561 * Get the ids that we want to get related contact details for.
562 *
563 * @param array $ids
564 * @param int $exportMode
565 *
566 * @return array
567 */
568 protected static function getIDsForRelatedContact($ids, $exportMode) {
569 if ($exportMode == CRM_Export_Form_Select::CONTACT_EXPORT) {
570 return $ids;
571 }
572 if ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
573 $relIDs = [];
574 $sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
575 $dao = CRM_Core_DAO::executeQuery("
576 SELECT contact_id FROM civicrm_activity_contact
577 WHERE activity_id IN ( " . implode(',', $ids) . ") AND
578 record_type_id = {$sourceID}
579 ");
580
581 while ($dao->fetch()) {
582 $relIDs[] = $dao->contact_id;
583 }
584 return $relIDs;
585 }
586 $componentMapping = [
587 CRM_Export_Form_Select::CONTRIBUTE_EXPORT => 'civicrm_contribution',
588 CRM_Export_Form_Select::EVENT_EXPORT => 'civicrm_participant',
589 CRM_Export_Form_Select::MEMBER_EXPORT => 'civicrm_membership',
590 CRM_Export_Form_Select::PLEDGE_EXPORT => 'civicrm_pledge',
591 CRM_Export_Form_Select::GRANT_EXPORT => 'civicrm_grant',
592 ];
593
594 if ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
595 return CRM_Case_BAO_Case::retrieveContactIdsByCaseId($ids);
596 }
597 else {
598 return CRM_Core_DAO::getContactIDsFromComponent($ids, $componentMapping[$exportMode]);
599 }
600 }
601
602 /**
603 * @param $selectAll
604 * @param $ids
605 * @param \CRM_Export_BAO_ExportProcessor $processor
606 * @param $componentTable
607 */
608 protected static function buildRelatedContactArray($selectAll, $ids, $processor, $componentTable) {
609 $allRelContactArray = $relationQuery = [];
610 $queryMode = $processor->getQueryMode();
611 $exportMode = $processor->getExportMode();
612
613 foreach ($processor->getRelationshipReturnProperties() as $relationshipKey => $relationReturnProperties) {
614 $allRelContactArray[$relationshipKey] = [];
615 // build Query for each relationship
616 $relationQuery = new CRM_Contact_BAO_Query(NULL, $relationReturnProperties,
617 NULL, FALSE, FALSE, $queryMode
618 );
619 list($relationSelect, $relationFrom, $relationWhere, $relationHaving) = $relationQuery->query();
620
621 list($id, $direction) = explode('_', $relationshipKey, 2);
622 // identify the relationship direction
623 $contactA = 'contact_id_a';
624 $contactB = 'contact_id_b';
625 if ($direction == 'b_a') {
626 $contactA = 'contact_id_b';
627 $contactB = 'contact_id_a';
628 }
629 $relIDs = self::getIDsForRelatedContact($ids, $exportMode);
630
631 $relationshipJoin = $relationshipClause = '';
632 if (!$selectAll && $componentTable) {
633 $relationshipJoin = " INNER JOIN {$componentTable} ctTable ON ctTable.contact_id = {$contactA}";
634 }
635 elseif (!empty($relIDs)) {
636 $relID = implode(',', $relIDs);
637 $relationshipClause = " AND crel.{$contactA} IN ( {$relID} )";
638 }
639
640 $relationFrom = " {$relationFrom}
641 INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_a.id AND crel.relationship_type_id = {$id}
642 {$relationshipJoin} ";
643
644 //check for active relationship status only
645 $today = date('Ymd');
646 $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
647 $relationWhere = " WHERE contact_a.is_deleted = 0 {$relationshipClause} {$relationActive}";
648 CRM_Core_DAO::disableFullGroupByMode();
649 $relationSelect = "{$relationSelect}, {$contactA} as refContact ";
650 $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationHaving GROUP BY crel.{$contactA}";
651
652 $allRelContactDAO = CRM_Core_DAO::executeQuery($relationQueryString);
653 CRM_Core_DAO::reenableFullGroupByMode();
654
655 while ($allRelContactDAO->fetch()) {
656 $relationQuery->convertToPseudoNames($allRelContactDAO);
657 $row = [];
658 // @todo pass processor to fetchRelationshipDetails and set fields directly within it.
659 self::fetchRelationshipDetails($allRelContactDAO, $relationReturnProperties, $relationshipKey, $row);
660 foreach (array_keys($relationReturnProperties) as $property) {
661 if ($property === 'location') {
662 // @todo - simplify location in self::fetchRelationshipDetails - remove handling here. Or just call
663 // $processor->setRelationshipValue from fetchRelationshipDetails
664 foreach ($relationReturnProperties['location'] as $locationName => $locationValues) {
665 foreach (array_keys($locationValues) as $locationValue) {
666 $key = str_replace(' ', '_', $locationName) . '-' . $locationValue;
667 $processor->setRelationshipValue($relationshipKey, $allRelContactDAO->refContact, $key, $row[$relationshipKey . '__' . $key]);
668 }
669 }
670 }
671 else {
672 $processor->setRelationshipValue($relationshipKey, $allRelContactDAO->refContact, $property, $row[$relationshipKey . '_' . $property]);
673 }
674 }
675 }
676 }
677 }
678
679 }