[Ref] Simplify field reference
[civicrm-core.git] / CRM / Export / BAO / ExportProcessor.php
CommitLineData
6003a964 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6003a964 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6003a964 9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6003a964 16 */
17
18/**
19 * Class CRM_Export_BAO_ExportProcessor
20 *
21 * Class to handle logic of export.
22 */
23class CRM_Export_BAO_ExportProcessor {
24
25 /**
26 * @var int
27 */
28 protected $queryMode;
29
30 /**
31 * @var int
32 */
33 protected $exportMode;
34
adabfa40 35 /**
36 * Array of fields in the main query.
37 *
38 * @var array
39 */
40 protected $queryFields = [];
41
71464b73 42 /**
43 * Either AND or OR.
44 *
45 * @var string
46 */
47 protected $queryOperator;
48
d41ab886 49 /**
50 * Requested output fields.
51 *
52 * If set to NULL then it is 'primary fields only'
53 * which actually means pretty close to all fields!
54 *
55 * @var array|null
56 */
57 protected $requestedFields;
58
b7db6051 59 /**
60 * Is the contact being merged into a single household.
61 *
62 * @var bool
63 */
64 protected $isMergeSameHousehold;
65
2761a916 66 /**
67 * Should contacts with the same address be merged.
68 *
69 * @var bool
70 */
71 protected $isMergeSameAddress = FALSE;
72
73 /**
74 * Fields that need to be retrieved for address merge purposes but should not be in output.
75 *
76 * @var array
77 */
78 protected $additionalFieldsForSameAddressMerge = [];
79
d97cefdc 80 /**
81 * Fields used for merging same contacts.
82 *
83 * @var array
84 */
85 protected $contactGreetingFields = [];
86
246e571e
JG
87 /**
88 * An array of primary IDs of the entity being exported.
89 *
90 * @var array
91 */
92 protected $ids = [];
93
d183e23a 94 /**
95 * Greeting options mapping to various greeting ids.
96 *
97 * This stores the option values for the addressee, postal_greeting & email_greeting
98 * option groups.
99 *
100 * @var array
101 */
102 protected $greetingOptions = [];
103
2761a916 104 /**
105 * Get additional non-visible fields for address merge purposes.
106 *
107 * @return array
108 */
109 public function getAdditionalFieldsForSameAddressMerge(): array {
110 return $this->additionalFieldsForSameAddressMerge;
111 }
112
113 /**
114 * Set additional non-visible fields for address merge purposes.
115 */
116 public function setAdditionalFieldsForSameAddressMerge() {
117 if ($this->isMergeSameAddress) {
118 $fields = ['id', 'master_id', 'state_province_id', 'postal_greeting_id', 'addressee_id'];
119 foreach ($fields as $index => $field) {
120 if (!empty($this->getReturnProperties()[$field])) {
121 unset($fields[$index]);
122 }
123 }
124 $this->additionalFieldsForSameAddressMerge = array_fill_keys($fields, 1);
125 }
126 }
127
128 /**
129 * Should contacts with the same address be merged.
130 *
131 * @return bool
132 */
133 public function isMergeSameAddress(): bool {
134 return $this->isMergeSameAddress;
135 }
136
137 /**
138 * Set same address is to be merged.
139 *
140 * @param bool $isMergeSameAddress
141 */
142 public function setIsMergeSameAddress(bool $isMergeSameAddress) {
143 $this->isMergeSameAddress = $isMergeSameAddress;
144 }
145
51d84eca 146 /**
147 * Additional fields required to export postal fields.
148 *
149 * @var array
150 */
151 protected $additionalFieldsForPostalExport = [];
152
153 /**
154 * Get additional fields required to do a postal export.
155 *
156 * @return array
157 */
158 public function getAdditionalFieldsForPostalExport() {
159 return $this->additionalFieldsForPostalExport;
160 }
161
162 /**
163 * Set additional fields required for a postal export.
164 */
165 public function setAdditionalFieldsForPostalExport() {
166 if ($this->getRequestedFields() && $this->isPostalableOnly()) {
167 $fields = ['is_deceased', 'do_not_mail', 'street_address', 'supplemental_address_1'];
168 foreach ($fields as $index => $field) {
169 if (!empty($this->getReturnProperties()[$field])) {
e8f0579a 170 unset($fields[$index]);
51d84eca 171 }
172 }
173 $this->additionalFieldsForPostalExport = array_fill_keys($fields, 1);
174 }
175 }
176
6d52bfe5 177 /**
178 * Only export contacts that can receive postal mail.
179 *
180 * Includes being alive, having an address & not having do_not_mail.
181 *
182 * @var bool
183 */
184 protected $isPostalableOnly;
185
944ed388 186 /**
187 * Key representing the head of household in the relationship array.
188 *
189 * e.g. ['8_b_a' => 'Household Member Is', '8_a_b = 'Household Member Of'.....]
190 *
7b966967 191 * @var array
944ed388 192 */
193 protected $relationshipTypes = [];
194
704e3e9a 195 /**
196 * Array of properties to retrieve for relationships.
197 *
198 * @var array
199 */
200 protected $relationshipReturnProperties = [];
201
136f69a8 202 /**
203 * IDs of households that have already been exported.
204 *
205 * @var array
206 */
207 protected $exportedHouseholds = [];
208
0b743fb1 209 /**
210 * Contacts to be merged by virtue of their shared address.
211 *
212 * @var array
213 */
214 protected $contactsToMerge = [];
215
ebebd629 216 /**
217 * Households to skip during export as they will be exported via their relationships anyway.
218 *
219 * @var array
220 */
221 protected $householdsToSkip = [];
222
e530cef4 223 /**
224 * Additional fields to return.
225 *
226 * This doesn't make much sense when we have a fields set but search build add it's own onto
227 * the 'Primary fields' (all) option.
228 *
229 * @var array
230 */
231 protected $additionalRequestedReturnProperties = [];
232
233 /**
234 * Get additional return properties.
235 *
236 * @return array
237 */
238 public function getAdditionalRequestedReturnProperties() {
239 return $this->additionalRequestedReturnProperties;
240 }
241
242 /**
243 * Set additional return properties.
244 *
245 * @param array $value
246 */
247 public function setAdditionalRequestedReturnProperties($value) {
248 // fix for CRM-7066
249 if (!empty($value['group'])) {
250 unset($value['group']);
251 $value['groups'] = 1;
252 }
253 $this->additionalRequestedReturnProperties = $value;
254 }
255
ce12a9e0 256 /**
257 * Get return properties by relationship.
258 * @return array
259 */
260 public function getRelationshipReturnProperties() {
261 return $this->relationshipReturnProperties;
262 }
263
264 /**
265 * Export values for related contacts.
266 *
267 * @var array
268 */
269 protected $relatedContactValues = [];
270
c66a5741 271 /**
272 * @var array
273 */
274 protected $returnProperties = [];
275
28254dcb 276 /**
277 * @var array
278 */
279 protected $outputSpecification = [];
280
e034ee91 281 /**
282 * @var string
283 */
284 protected $componentTable = '';
285
286 /**
287 * @return string
288 */
289 public function getComponentTable() {
290 return $this->componentTable;
291 }
292
293 /**
294 * Set the component table (if any).
295 *
296 * @param string $componentTable
297 */
298 public function setComponentTable($componentTable) {
299 $this->componentTable = $componentTable;
300 }
301
302 /**
303 * Clause from component search.
304 *
305 * @var string
306 */
307 protected $componentClause = '';
308
309 /**
310 * @return string
311 */
312 public function getComponentClause() {
313 return $this->componentClause;
314 }
315
316 /**
317 * @param string $componentClause
318 */
319 public function setComponentClause($componentClause) {
320 $this->componentClause = $componentClause;
321 }
322
68989e71 323 /**
324 * Name of a temporary table created to hold the results.
325 *
326 * Current decision making on when to create a temp table is kinda bad so this might change
327 * a bit as it is reviewed but basically we need a temp table or similar to calculate merging
328 * addresses. Merging households is handled in php. We create a temp table even when we don't need them.
329 *
330 * @var string
331 */
332 protected $temporaryTable;
333
334 /**
335 * @return string
336 */
337 public function getTemporaryTable(): string {
338 return $this->temporaryTable;
339 }
340
341 /**
342 * @param string $temporaryTable
343 */
344 public function setTemporaryTable(string $temporaryTable) {
345 $this->temporaryTable = $temporaryTable;
346 }
347
a2b21ae3 348 protected $postalGreetingTemplate;
349
350 /**
351 * @return mixed
352 */
353 public function getPostalGreetingTemplate() {
354 return $this->postalGreetingTemplate;
355 }
356
357 /**
358 * @param mixed $postalGreetingTemplate
359 */
360 public function setPostalGreetingTemplate($postalGreetingTemplate) {
361 $this->postalGreetingTemplate = $postalGreetingTemplate;
362 }
363
364 /**
365 * @return mixed
366 */
367 public function getAddresseeGreetingTemplate() {
368 return $this->addresseeGreetingTemplate;
369 }
370
371 /**
372 * @param mixed $addresseeGreetingTemplate
373 */
374 public function setAddresseeGreetingTemplate($addresseeGreetingTemplate) {
375 $this->addresseeGreetingTemplate = $addresseeGreetingTemplate;
376 }
377
378 protected $addresseeGreetingTemplate;
379
71464b73 380 /**
381 * CRM_Export_BAO_ExportProcessor constructor.
382 *
383 * @param int $exportMode
041ecc95 384 * @param array|null $requestedFields
71464b73 385 * @param string $queryOperator
b7db6051 386 * @param bool $isMergeSameHousehold
6d52bfe5 387 * @param bool $isPostalableOnly
2761a916 388 * @param bool $isMergeSameAddress
1e9e062d 389 * @param array $formValues
390 * Values from the export options form on contact export. We currently support these keys
391 * - postal_greeting
392 * - postal_other
393 * - addresee_greeting
394 * - addressee_other
395 */
396 public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE, $isPostalableOnly = FALSE, $isMergeSameAddress = FALSE, $formValues = []) {
71464b73 397 $this->setExportMode($exportMode);
398 $this->setQueryMode();
399 $this->setQueryOperator($queryOperator);
d41ab886 400 $this->setRequestedFields($requestedFields);
944ed388 401 $this->setRelationshipTypes();
8669d9a7 402 $this->setIsMergeSameHousehold($isMergeSameHousehold || $isMergeSameAddress);
806c5e1e 403 $this->setIsPostalableOnly($isPostalableOnly);
2761a916 404 $this->setIsMergeSameAddress($isMergeSameAddress);
806c5e1e 405 $this->setReturnProperties($this->determineReturnProperties());
2761a916 406 $this->setAdditionalFieldsForSameAddressMerge();
51d84eca 407 $this->setAdditionalFieldsForPostalExport();
3611a532 408 $this->setHouseholdMergeReturnProperties();
1e9e062d 409 $this->setGreetingStringsForSameAddressMerge($formValues);
d183e23a 410 $this->setGreetingOptions();
411 }
412
413 /**
414 * Set the greeting options, if relevant.
415 */
416 public function setGreetingOptions() {
417 if ($this->isMergeSameAddress()) {
418 $this->greetingOptions['addressee'] = CRM_Core_OptionGroup::values('addressee');
419 $this->greetingOptions['postal_greeting'] = CRM_Core_OptionGroup::values('postal_greeting');
420 }
d41ab886 421 }
422
6d52bfe5 423 /**
424 * @return bool
425 */
426 public function isPostalableOnly() {
427 return $this->isPostalableOnly;
428 }
429
430 /**
431 * @param bool $isPostalableOnly
432 */
433 public function setIsPostalableOnly($isPostalableOnly) {
434 $this->isPostalableOnly = $isPostalableOnly;
435 }
7b966967 436
d41ab886 437 /**
438 * @return array|null
439 */
440 public function getRequestedFields() {
04f8f758 441 return empty($this->requestedFields) ? NULL : $this->requestedFields;
d41ab886 442 }
443
444 /**
445 * @param array|null $requestedFields
446 */
447 public function setRequestedFields($requestedFields) {
448 $this->requestedFields = $requestedFields;
71464b73 449 }
450
c66a5741 451 /**
452 * @return array
453 */
454 public function getReturnProperties() {
51d84eca 455 return array_merge($this->returnProperties, $this->getAdditionalRequestedReturnProperties(), $this->getAdditionalFieldsForSameAddressMerge(), $this->getAdditionalFieldsForPostalExport());
c66a5741 456 }
457
458 /**
459 * @param array $returnProperties
460 */
461 public function setReturnProperties($returnProperties) {
462 $this->returnProperties = $returnProperties;
463 }
464
944ed388 465 /**
466 * @return array
467 */
468 public function getRelationshipTypes() {
469 return $this->relationshipTypes;
470 }
471
472 /**
473 */
474 public function setRelationshipTypes() {
475 $this->relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(
476 NULL,
477 NULL,
478 NULL,
479 NULL,
480 TRUE,
481 'name',
482 FALSE
483 );
484 }
485
ce12a9e0 486 /**
487 * Set the value for a relationship type field.
488 *
489 * In this case we are building up an array of properties for a related contact.
490 *
491 * These may be used for direct exporting or for merge to household depending on the
492 * options selected.
493 *
494 * @param string $relationshipType
495 * @param int $contactID
496 * @param string $field
497 * @param string $value
498 */
499 public function setRelationshipValue($relationshipType, $contactID, $field, $value) {
500 $this->relatedContactValues[$relationshipType][$contactID][$field] = $value;
69edd19f 501 if ($field === 'id' && $this->isHouseholdMergeRelationshipTypeKey($relationshipType)) {
ebebd629 502 $this->householdsToSkip[] = $value;
503 }
ce12a9e0 504 }
505
506 /**
507 * Get the value for a relationship type field.
508 *
509 * In this case we are building up an array of properties for a related contact.
510 *
511 * These may be used for direct exporting or for merge to household depending on the
512 * options selected.
513 *
514 * @param string $relationshipType
515 * @param int $contactID
516 * @param string $field
517 *
518 * @return string
519 */
520 public function getRelationshipValue($relationshipType, $contactID, $field) {
2e1f50d6 521 return $this->relatedContactValues[$relationshipType][$contactID][$field] ?? '';
ce12a9e0 522 }
523
136f69a8 524 /**
525 * Get the id of the related household.
526 *
527 * @param int $contactID
528 * @param string $relationshipType
529 *
530 * @return int
531 */
532 public function getRelatedHouseholdID($contactID, $relationshipType) {
533 return $this->relatedContactValues[$relationshipType][$contactID]['id'];
534 }
535
536 /**
537 * Has the household already been exported.
538 *
539 * @param int $housholdContactID
540 *
541 * @return bool
542 */
543 public function isHouseholdExported($housholdContactID) {
544 return isset($this->exportedHouseholds[$housholdContactID]);
545
546 }
547
b7db6051 548 /**
549 * @return bool
550 */
551 public function isMergeSameHousehold() {
552 return $this->isMergeSameHousehold;
553 }
554
555 /**
556 * @param bool $isMergeSameHousehold
557 */
558 public function setIsMergeSameHousehold($isMergeSameHousehold) {
559 $this->isMergeSameHousehold = $isMergeSameHousehold;
560 }
561
562 /**
563 * Return relationship types for household merge.
564 *
565 * @return mixed
566 */
567 public function getHouseholdRelationshipTypes() {
568 if (!$this->isMergeSameHousehold()) {
569 return [];
570 }
571 return [
572 CRM_Utils_Array::key('Household Member of', $this->getRelationshipTypes()),
573 CRM_Utils_Array::key('Head of Household for', $this->getRelationshipTypes()),
574 ];
575 }
944ed388 576
577 /**
578 * @param $fieldName
579 * @return bool
580 */
581 public function isRelationshipTypeKey($fieldName) {
582 return array_key_exists($fieldName, $this->relationshipTypes);
583 }
584
b7db6051 585 /**
586 * @param $fieldName
587 * @return bool
588 */
589 public function isHouseholdMergeRelationshipTypeKey($fieldName) {
590 return in_array($fieldName, $this->getHouseholdRelationshipTypes());
591 }
592
71464b73 593 /**
594 * @return string
595 */
596 public function getQueryOperator() {
597 return $this->queryOperator;
598 }
599
600 /**
601 * @param string $queryOperator
602 */
603 public function setQueryOperator($queryOperator) {
604 $this->queryOperator = $queryOperator;
605 }
606
246e571e
JG
607 /**
608 * @return array
609 */
610 public function getIds() {
611 return $this->ids;
612 }
613
614 /**
615 * @param array $ids
616 */
617 public function setIds($ids) {
618 $this->ids = $ids;
619 }
620
adabfa40 621 /**
622 * @return array
623 */
624 public function getQueryFields() {
749e60f4 625 return array_merge(
626 $this->queryFields,
627 $this->getComponentPaymentFields()
628 );
adabfa40 629 }
630
631 /**
632 * @param array $queryFields
633 */
634 public function setQueryFields($queryFields) {
5ebd7d09 635 // legacy hacks - we add these to queryFields because this
636 // pseudometadata is currently required.
637 $queryFields['im_provider']['pseudoconstant']['var'] = 'imProviders';
638 $queryFields['country']['context'] = 'country';
639 $queryFields['world_region']['context'] = 'country';
640 $queryFields['state_province']['context'] = 'province';
808a80ba 641 $queryFields['contact_id'] = ['title' => ts('Contact ID'), 'type' => CRM_Utils_Type::T_INT];
adabfa40 642 $this->queryFields = $queryFields;
643 }
644
6003a964 645 /**
646 * @return int
647 */
648 public function getQueryMode() {
649 return $this->queryMode;
650 }
651
652 /**
653 * Set the query mode based on the export mode.
654 */
655 public function setQueryMode() {
656
657 switch ($this->getExportMode()) {
658 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
659 $this->queryMode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE;
660 break;
661
662 case CRM_Export_Form_Select::EVENT_EXPORT:
663 $this->queryMode = CRM_Contact_BAO_Query::MODE_EVENT;
664 break;
665
666 case CRM_Export_Form_Select::MEMBER_EXPORT:
667 $this->queryMode = CRM_Contact_BAO_Query::MODE_MEMBER;
668 break;
669
670 case CRM_Export_Form_Select::PLEDGE_EXPORT:
671 $this->queryMode = CRM_Contact_BAO_Query::MODE_PLEDGE;
672 break;
673
674 case CRM_Export_Form_Select::CASE_EXPORT:
675 $this->queryMode = CRM_Contact_BAO_Query::MODE_CASE;
676 break;
677
678 case CRM_Export_Form_Select::GRANT_EXPORT:
679 $this->queryMode = CRM_Contact_BAO_Query::MODE_GRANT;
680 break;
681
682 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
683 $this->queryMode = CRM_Contact_BAO_Query::MODE_ACTIVITY;
684 break;
685
686 default:
687 $this->queryMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
688 }
689 }
690
691 /**
692 * @return int
693 */
694 public function getExportMode() {
695 return $this->exportMode;
696 }
697
698 /**
699 * @param int $exportMode
700 */
701 public function setExportMode($exportMode) {
702 $this->exportMode = $exportMode;
703 }
704
29034a98 705 /**
706 * Get the name for the export file.
707 *
708 * @return string
709 */
710 public function getExportFileName() {
711 switch ($this->getExportMode()) {
712 case CRM_Export_Form_Select::CONTACT_EXPORT:
713 return ts('CiviCRM Contact Search');
714
715 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
716 return ts('CiviCRM Contribution Search');
717
718 case CRM_Export_Form_Select::MEMBER_EXPORT:
719 return ts('CiviCRM Member Search');
720
721 case CRM_Export_Form_Select::EVENT_EXPORT:
722 return ts('CiviCRM Participant Search');
723
724 case CRM_Export_Form_Select::PLEDGE_EXPORT:
725 return ts('CiviCRM Pledge Search');
726
727 case CRM_Export_Form_Select::CASE_EXPORT:
728 return ts('CiviCRM Case Search');
729
730 case CRM_Export_Form_Select::GRANT_EXPORT:
731 return ts('CiviCRM Grant Search');
732
733 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
734 return ts('CiviCRM Activity Search');
735
736 default:
737 // Legacy code suggests the value could be 'financial' - ie. something
738 // other than what should be accepted. However, I suspect that this line is
739 // never hit.
740 return ts('CiviCRM Search');
741 }
742 }
743
af17bedf 744 /**
745 * Get the label for the header row based on the field to output.
746 *
747 * @param string $field
748 *
749 * @return string
750 */
751 public function getHeaderForRow($field) {
d1d108ee 752 if (substr($field, -11) === 'campaign_id') {
af17bedf 753 // @todo - set this correctly in the xml rather than here.
754 // This will require a generalised handling cleanup
755 return ts('Campaign ID');
756 }
8669d9a7 757 if ($this->isMergeSameHousehold() && !$this->isMergeSameAddress() && $field === 'id') {
758 // This is weird - even if we are merging households not every contact in the export is a household so this would not be accurate.
af17bedf 759 return ts('Household ID');
760 }
761 elseif (isset($this->getQueryFields()[$field]['title'])) {
762 return $this->getQueryFields()[$field]['title'];
763 }
764 elseif ($this->isExportPaymentFields() && array_key_exists($field, $this->getcomponentPaymentFields())) {
749e60f4 765 return CRM_Utils_Array::value($field, $this->getcomponentPaymentFields())['title'];
af17bedf 766 }
767 else {
768 return $field;
769 }
770 }
771
adabfa40 772 /**
773 * @param $params
774 * @param $order
6df77635 775 *
adabfa40 776 * @return array
777 */
6df77635 778 public function runQuery($params, $order) {
779 $returnProperties = $this->getReturnProperties();
6d52bfe5 780 $params = array_merge($params, $this->getWhereParams());
efaa3367 781
adabfa40 782 $query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
783 FALSE, FALSE, $this->getQueryMode(),
caa34349 784 FALSE, TRUE, TRUE, NULL, $this->getQueryOperator(),
785 NULL, TRUE
adabfa40 786 );
787
788 //sort by state
789 //CRM-15301
790 $query->_sort = $order;
791 list($select, $from, $where, $having) = $query->query();
792 $this->setQueryFields($query->_fields);
e034ee91 793 $whereClauses = ['trash_clause' => "contact_a.is_deleted != 1"];
9858dc0b 794 if ($this->getRequestedFields() && ($this->getComponentTable())) {
e034ee91 795 $from .= " INNER JOIN " . $this->getComponentTable() . " ctTable ON ctTable.contact_id = contact_a.id ";
796 }
797 elseif ($this->getComponentClause()) {
798 $whereClauses[] = $this->getComponentClause();
799 }
800
801 // CRM-13982 - check if is deleted
802 foreach ($params as $value) {
803 if ($value[0] == 'contact_is_deleted') {
804 unset($whereClauses['trash_clause']);
805 }
806 }
807
efaa3367 808 if ($this->isPostalableOnly) {
809 if (array_key_exists('street_address', $returnProperties)) {
810 $addressWhere = " civicrm_address.street_address <> ''";
811 if (array_key_exists('supplemental_address_1', $returnProperties)) {
812 // We need this to be an OR rather than AND on the street_address so, hack it in.
813 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
814 'address_options', TRUE, NULL, TRUE
815 );
816 if (!empty($addressOptions['supplemental_address_1'])) {
817 $addressWhere .= " OR civicrm_address.supplemental_address_1 <> ''";
818 }
819 }
62a39786 820 $whereClauses['address'] = '(' . $addressWhere . ')';
efaa3367 821 }
822 }
823
e034ee91 824 if (empty($where)) {
825 $where = "WHERE " . implode(' AND ', $whereClauses);
826 }
827 else {
828 $where .= " AND " . implode(' AND ', $whereClauses);
829 }
7628764a 830
831 $groupBy = $this->getGroupBy($query);
43767da2 832 $queryString = "$select $from $where $having $groupBy";
833 if ($order) {
834 // always add contact_a.id to the ORDER clause
835 // so the order is deterministic
836 //CRM-15301
837 if (strpos('contact_a.id', $order) === FALSE) {
838 $order .= ", contact_a.id";
839 }
840
841 list($field, $dir) = explode(' ', $order, 2);
842 $field = trim($field);
843 if (!empty($this->getReturnProperties()[$field])) {
844 //CRM-15301
845 $queryString .= " ORDER BY $order";
846 }
847 }
848 return [$query, $queryString];
adabfa40 849 }
850
28254dcb 851 /**
852 * Add a row to the specification for how to output data.
ae5d4caf 853 *
28254dcb 854 * @param string $key
28254dcb 855 * @param string $relationshipType
ae5d4caf 856 * @param string $locationType
857 * @param int $entityTypeID phone_type_id or provider_id for phone or im fields.
28254dcb 858 */
ae5d4caf 859 public function addOutputSpecification($key, $relationshipType = NULL, $locationType = NULL, $entityTypeID = NULL) {
5ebd7d09 860 $entityLabel = '';
ae5d4caf 861 if ($entityTypeID) {
862 if ($key === 'phone') {
5ebd7d09 863 $entityLabel = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Phone', 'phone_type_id', $entityTypeID);
ae5d4caf 864 }
865 if ($key === 'im') {
5ebd7d09 866 $entityLabel = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_IM', 'provider_id', $entityTypeID);
ae5d4caf 867 }
868 }
ae5d4caf 869
5ebd7d09 870 // These oddly constructed keys are for legacy reasons. Altering them will affect test success
871 // but in time it may be good to rationalise them.
872 $label = $this->getOutputSpecificationLabel($key, $relationshipType, $locationType, $entityLabel);
873 $index = $this->getOutputSpecificationIndex($key, $relationshipType, $locationType, $entityLabel);
874 $fieldKey = $this->getOutputSpecificationFieldKey($key, $relationshipType, $locationType, $entityLabel);
875
876 $this->outputSpecification[$index]['header'] = $label;
877 $this->outputSpecification[$index]['sql_columns'] = $this->getSqlColumnDefinition($fieldKey, $key);
878
879 if ($relationshipType && $this->isHouseholdMergeRelationshipTypeKey($relationshipType)) {
880 $this->setColumnAsCalculationOnly($index);
881 }
882 $this->outputSpecification[$index]['metadata'] = $this->getMetaDataForField($key);
883 }
884
885 /**
886 * Get the metadata for the given field.
887 *
888 * @param $key
889 *
890 * @return array
891 */
892 public function getMetaDataForField($key) {
893 $mappings = ['contact_id' => 'id'];
894 if (isset($this->getQueryFields()[$key])) {
895 return $this->getQueryFields()[$key];
896 }
897 if (isset($mappings[$key])) {
898 return $this->getQueryFields()[$mappings[$key]];
899 }
900 return [];
2cd3d767 901 }
ae5d4caf 902
2cd3d767 903 /**
904 * @param $key
905 */
906 public function setSqlColumnDefn($key) {
5ebd7d09 907 $this->outputSpecification[$this->getMungedFieldName($key)]['sql_columns'] = $this->getSqlColumnDefinition($key, $this->getMungedFieldName($key));
de6ff509 908 }
909
910 /**
911 * Mark a column as only required for calculations.
912 *
913 * Do not include the row with headers.
914 *
915 * @param string $column
916 */
917 public function setColumnAsCalculationOnly($column) {
918 $this->outputSpecification[$column]['do_not_output_to_csv'] = TRUE;
28254dcb 919 }
920
921 /**
922 * @return array
923 */
924 public function getHeaderRows() {
925 $headerRows = [];
926 foreach ($this->outputSpecification as $key => $spec) {
de6ff509 927 if (empty($spec['do_not_output_to_csv'])) {
928 $headerRows[] = $spec['header'];
929 }
28254dcb 930 }
931 return $headerRows;
932 }
933
2cd3d767 934 /**
935 * @return array
936 */
937 public function getSQLColumns() {
938 $sqlColumns = [];
939 foreach ($this->outputSpecification as $key => $spec) {
940 if (empty($spec['do_not_output_to_sql'])) {
941 $sqlColumns[$key] = $spec['sql_columns'];
942 }
943 }
944 return $sqlColumns;
945 }
946
5ebd7d09 947 /**
948 * @return array
949 */
950 public function getMetadata() {
951 $metadata = [];
952 foreach ($this->outputSpecification as $key => $spec) {
953 $metadata[$key] = $spec['metadata'];
954 }
955 return $metadata;
956 }
2cd3d767 957
6e2de55d 958 /**
959 * Build the row for output.
960 *
961 * @param \CRM_Contact_BAO_Query $query
962 * @param CRM_Core_DAO $iterationDAO
963 * @param array $outputColumns
964 * @param $metadata
965 * @param $paymentDetails
966 * @param $addPaymentHeader
6e2de55d 967 *
136f69a8 968 * @return array|bool
6e2de55d 969 */
c58085f8 970 public function buildRow($query, $iterationDAO, $outputColumns, $metadata, $paymentDetails, $addPaymentHeader) {
971 $paymentTableId = $this->getPaymentTableID();
ebebd629 972 if ($this->isHouseholdToSkip($iterationDAO->contact_id)) {
973 return FALSE;
974 }
6e2de55d 975 $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
976 $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
977
978 $row = [];
136f69a8 979 $householdMergeRelationshipType = $this->getHouseholdMergeTypeForRow($iterationDAO->contact_id);
980 if ($householdMergeRelationshipType) {
981 $householdID = $this->getRelatedHouseholdID($iterationDAO->contact_id, $householdMergeRelationshipType);
982 if ($this->isHouseholdExported($householdID)) {
983 return FALSE;
984 }
985 foreach (array_keys($outputColumns) as $column) {
986 $row[$column] = $this->getRelationshipValue($householdMergeRelationshipType, $iterationDAO->contact_id, $column);
987 }
988 $this->markHouseholdExported($householdID);
989 return $row;
990 }
991
6e2de55d 992 $query->convertToPseudoNames($iterationDAO);
993
994 //first loop through output columns so that we return what is required, and in same order.
995 foreach ($outputColumns as $field => $value) {
6e2de55d 996 // add im_provider to $dao object
997 if ($field == 'im_provider' && property_exists($iterationDAO, 'provider_id')) {
998 $iterationDAO->im_provider = $iterationDAO->provider_id;
999 }
1000
1001 //build row values (data)
1002 $fieldValue = NULL;
1003 if (property_exists($iterationDAO, $field)) {
1004 $fieldValue = $iterationDAO->$field;
1005 // to get phone type from phone type id
1006 if ($field == 'phone_type_id' && isset($phoneTypes[$fieldValue])) {
1007 $fieldValue = $phoneTypes[$fieldValue];
1008 }
1009 elseif ($field == 'provider_id' || $field == 'im_provider') {
9c1bc317 1010 $fieldValue = $imProviders[$fieldValue] ?? NULL;
6e2de55d 1011 }
1012 elseif (strstr($field, 'master_id')) {
ffd511ee 1013 // @todo - why not just $field === 'master_id' - what else would it be?
1014 $masterAddressId = $iterationDAO->$field ?? NULL;
6e2de55d 1015 // get display name of contact that address is shared.
1016 $fieldValue = CRM_Contact_BAO_Contact::getMasterDisplayName($masterAddressId);
1017 }
1018 }
1019
1020 if ($this->isRelationshipTypeKey($field)) {
136f69a8 1021 $this->buildRelationshipFieldsForRow($row, $iterationDAO->contact_id, $value, $field);
6e2de55d 1022 }
1023 else {
1024 $row[$field] = $this->getTransformedFieldValue($field, $iterationDAO, $fieldValue, $metadata, $paymentDetails);
1025 }
1026 }
1027
1028 // If specific payment fields have been selected for export, payment
1029 // data will already be in $row. Otherwise, add payment related
1030 // information, if appropriate.
1031 if ($addPaymentHeader) {
1032 if (!$this->isExportSpecifiedPaymentFields()) {
1033 $nullContributionDetails = array_fill_keys(array_keys($this->getPaymentHeaders()), NULL);
1034 if ($this->isExportPaymentFields()) {
9c1bc317 1035 $paymentData = $paymentDetails[$row[$paymentTableId]] ?? NULL;
6e2de55d 1036 if (!is_array($paymentData) || empty($paymentData)) {
1037 $paymentData = $nullContributionDetails;
1038 }
1039 $row = array_merge($row, $paymentData);
1040 }
1041 elseif (!empty($paymentDetails)) {
1042 $row = array_merge($row, $nullContributionDetails);
1043 }
1044 }
1045 }
1046 //remove organization name for individuals if it is set for current employer
1047 if (!empty($row['contact_type']) &&
1048 $row['contact_type'] == 'Individual' && array_key_exists('organization_name', $row)
1049 ) {
1050 $row['organization_name'] = '';
1051 }
1052 return $row;
1053 }
1054
136f69a8 1055 /**
1056 * If this row has a household whose details we should use get the relationship type key.
1057 *
1058 * @param $contactID
1059 *
1060 * @return bool
1061 */
1062 public function getHouseholdMergeTypeForRow($contactID) {
1063 if (!$this->isMergeSameHousehold()) {
1064 return FALSE;
1065 }
1066 foreach ($this->getHouseholdRelationshipTypes() as $relationshipType) {
1067 if (isset($this->relatedContactValues[$relationshipType][$contactID])) {
1068 return $relationshipType;
1069 }
1070 }
1071 }
1072
1073 /**
1074 * Mark the given household as already exported.
1075 *
1076 * @param $householdID
1077 */
1078 public function markHouseholdExported($householdID) {
1079 $this->exportedHouseholds[$householdID] = $householdID;
1080 }
1081
8bd67f47 1082 /**
1083 * @param $field
1084 * @param $iterationDAO
1085 * @param $fieldValue
1086 * @param $metadata
1087 * @param $paymentDetails
1088 *
1089 * @return string
1090 */
1091 public function getTransformedFieldValue($field, $iterationDAO, $fieldValue, $metadata, $paymentDetails) {
1092
1093 $i18n = CRM_Core_I18n::singleton();
1094 if ($field == 'id') {
1095 return $iterationDAO->contact_id;
1096 // special case for calculated field
1097 }
1098 elseif ($field == 'source_contact_id') {
1099 return $iterationDAO->contact_id;
1100 }
1101 elseif ($field == 'pledge_balance_amount') {
1102 return $iterationDAO->pledge_amount - $iterationDAO->pledge_total_paid;
1103 // special case for calculated field
1104 }
1105 elseif ($field == 'pledge_next_pay_amount') {
1106 return $iterationDAO->pledge_next_pay_amount + $iterationDAO->pledge_outstanding_amount;
1107 }
1108 elseif (isset($fieldValue) &&
1109 $fieldValue != ''
1110 ) {
1111 //check for custom data
1112 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($field)) {
cd447195 1113 $html_type = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $cfID, 'html_type');
1114
1115 //need to calculate the link to the file for file custom data
1116 if ($html_type === 'File' && $fieldValue) {
1117 $result = civicrm_api3('attachment', 'get', ['return' => ['url'], 'id' => $fieldValue]);
1118 return $result['values'][$result['id']]['url'];
1119 }
1120
8bd67f47 1121 return CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID);
1122 }
be2fb01f 1123 elseif (in_array($field, [
8bd67f47 1124 'email_greeting',
1125 'postal_greeting',
1126 'addressee',
be2fb01f 1127 ])) {
8bd67f47 1128 //special case for greeting replacement
1129 $fldValue = "{$field}_display";
1130 return $iterationDAO->$fldValue;
1131 }
1132 else {
1133 //normal fields with a touch of CRM-3157
1134 switch ($field) {
1135 case 'country':
1136 case 'world_region':
be2fb01f 1137 return $i18n->crm_translate($fieldValue, ['context' => 'country']);
8bd67f47 1138
1139 case 'state_province':
be2fb01f 1140 return $i18n->crm_translate($fieldValue, ['context' => 'province']);
8bd67f47 1141
1142 case 'gender':
1143 case 'preferred_communication_method':
1144 case 'preferred_mail_format':
1145 case 'communication_style':
1146 return $i18n->crm_translate($fieldValue);
1147
1148 default:
62b6031b 1149 $fieldSpec = $metadata[$field] ?? [];
1150 // No I don't know why we do it this way & whether we could
1151 // make better use of pseudoConstants.
1152 if (!empty($fieldSpec['context'])) {
1153 return $i18n->crm_translate($fieldValue, $fieldSpec);
1154 }
1155 if (!empty($fieldSpec['pseudoconstant'])) {
1156 if (!empty($fieldSpec['bao'])) {
1157 return CRM_Core_PseudoConstant::getLabel($fieldSpec['bao'], $fieldSpec['name'], $fieldValue);
8bd67f47 1158 }
62b6031b 1159 // This is not our normal syntax for pseudoconstants but I am a bit loath to
1160 // call an external function until sure it is not increasing php processing given this
1161 // may be iterated 100,000 times & we already have the $imProvider var loaded.
1162 // That can be next refactor...
1163 // Yes - definitely feeling hatred for this bit of code - I know you will beat me up over it's awfulness
1164 // but I have to reach a stable point....
1165 $varName = $fieldSpec['pseudoconstant']['var'];
1166 if ($varName === 'imProviders') {
1167 return CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_IM', 'provider_id', $fieldValue);
1168 }
1169 if ($varName === 'phoneTypes') {
1170 return CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_Phone', 'phone_type_id', $fieldValue);
8bd67f47 1171 }
8bd67f47 1172 }
62b6031b 1173
8bd67f47 1174 return $fieldValue;
1175 }
1176 }
1177 }
1178 elseif ($this->isExportSpecifiedPaymentFields() && array_key_exists($field, $this->getcomponentPaymentFields())) {
1179 $paymentTableId = $this->getPaymentTableID();
9c1bc317 1180 $paymentData = $paymentDetails[$iterationDAO->$paymentTableId] ?? NULL;
be2fb01f 1181 $payFieldMapper = [
8bd67f47 1182 'componentPaymentField_total_amount' => 'total_amount',
1183 'componentPaymentField_contribution_status' => 'contribution_status',
1184 'componentPaymentField_payment_instrument' => 'pay_instru',
1185 'componentPaymentField_transaction_id' => 'trxn_id',
1186 'componentPaymentField_received_date' => 'receive_date',
be2fb01f 1187 ];
8bd67f47 1188 return CRM_Utils_Array::value($payFieldMapper[$field], $paymentData, '');
1189 }
1190 else {
1191 // if field is empty or null
1192 return '';
1193 }
1194 }
1195
ce14544c 1196 /**
1197 * Get array of fields to return, over & above those defined in the main contact exportable fields.
1198 *
1199 * These include export mode specific fields & some fields apparently required as 'exportableFields'
1200 * but not returned by the function of the same name.
1201 *
1202 * @return array
1203 * Array of fields to return in the format ['field_name' => 1,...]
1204 */
1205 public function getAdditionalReturnProperties() {
ce14544c 1206 if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTACTS) {
1207 $componentSpecificFields = [];
1208 }
1209 else {
1210 $componentSpecificFields = CRM_Contact_BAO_Query::defaultReturnProperties($this->getQueryMode());
1211 }
1212 if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_PLEDGE) {
1213 $componentSpecificFields = array_merge($componentSpecificFields, CRM_Pledge_BAO_Query::extraReturnProperties($this->getQueryMode()));
d28b6cf2 1214 unset($componentSpecificFields['contribution_status_id']);
1215 unset($componentSpecificFields['pledge_status_id']);
1216 unset($componentSpecificFields['pledge_payment_status_id']);
ce14544c 1217 }
1218 if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CASE) {
1219 $componentSpecificFields = array_merge($componentSpecificFields, CRM_Case_BAO_Query::extraReturnProperties($this->getQueryMode()));
1220 }
1221 if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
1222 $componentSpecificFields = array_merge($componentSpecificFields, CRM_Contribute_BAO_Query::softCreditReturnProperties(TRUE));
d28b6cf2 1223 unset($componentSpecificFields['contribution_status_id']);
ce14544c 1224 }
7626754f 1225 return $componentSpecificFields;
ce14544c 1226 }
1227
d41ab886 1228 /**
1229 * Should payment fields be appended to the export.
1230 *
1231 * (This is pretty hacky so hopefully this function won't last long - notice
1232 * how obviously it should be part of the above function!).
1233 */
1234 public function isExportPaymentFields() {
1235 if ($this->getRequestedFields() === NULL
f065c170 1236 && in_array($this->getQueryMode(), [
d41ab886 1237 CRM_Contact_BAO_Query::MODE_EVENT,
1238 CRM_Contact_BAO_Query::MODE_MEMBER,
1239 CRM_Contact_BAO_Query::MODE_PLEDGE,
1240 ])) {
1241 return TRUE;
1242 }
c66a5741 1243 elseif ($this->isExportSpecifiedPaymentFields()) {
1244 return TRUE;
1245 }
d41ab886 1246 return FALSE;
1247 }
1248
c66a5741 1249 /**
1250 * Has specific payment fields been requested (as opposed to via all fields).
1251 *
1252 * If specific fields have been requested then they get added at various points.
1253 *
1254 * @return bool
1255 */
1256 public function isExportSpecifiedPaymentFields() {
1257 if ($this->getRequestedFields() !== NULL && $this->hasRequestedComponentPaymentFields()) {
1258 return TRUE;
1259 }
1260 }
1261
d41ab886 1262 /**
1263 * Get the name of the id field in the table that connects contributions to the export entity.
1264 */
1265 public function getPaymentTableID() {
1266 if ($this->getRequestedFields() === NULL) {
1267 $mapping = [
1268 CRM_Contact_BAO_Query::MODE_EVENT => 'participant_id',
1269 CRM_Contact_BAO_Query::MODE_MEMBER => 'membership_id',
1270 CRM_Contact_BAO_Query::MODE_PLEDGE => 'pledge_payment_id',
1271 ];
1272 return isset($mapping[$this->getQueryMode()]) ? $mapping[$this->getQueryMode()] : '';
1273 }
c66a5741 1274 elseif ($this->hasRequestedComponentPaymentFields()) {
1275 return 'participant_id';
1276 }
d41ab886 1277 return FALSE;
1278 }
1279
c66a5741 1280 /**
1281 * Have component payment fields been requested.
1282 *
1283 * @return bool
1284 */
1285 protected function hasRequestedComponentPaymentFields() {
1286 if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_EVENT) {
1287 $participantPaymentFields = array_intersect_key($this->getComponentPaymentFields(), $this->getReturnProperties());
1288 if (!empty($participantPaymentFields)) {
1289 return TRUE;
1290 }
1291 }
d41ab886 1292 return FALSE;
1293 }
1294
c66a5741 1295 /**
1296 * Get fields that indicate payment fields have been requested for a component.
1297 *
0e32ed68 1298 * Ideally this should be protected but making it temporarily public helps refactoring..
1299 *
c66a5741 1300 * @return array
1301 */
0e32ed68 1302 public function getComponentPaymentFields() {
c66a5741 1303 return [
749e60f4 1304 'componentPaymentField_total_amount' => ['title' => ts('Total Amount'), 'type' => CRM_Utils_Type::T_MONEY],
1305 'componentPaymentField_contribution_status' => ['title' => ts('Contribution Status'), 'type' => CRM_Utils_Type::T_STRING],
1306 'componentPaymentField_received_date' => ['title' => ts('Date Received'), 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME],
1307 'componentPaymentField_payment_instrument' => ['title' => ts('Payment Method'), 'type' => CRM_Utils_Type::T_STRING],
1308 'componentPaymentField_transaction_id' => ['title' => ts('Transaction ID'), 'type' => CRM_Utils_Type::T_STRING],
c66a5741 1309 ];
1310 }
1311
05ad310f 1312 /**
1313 * Get headers for payment fields.
1314 *
1315 * Returns an array of contribution fields when the entity supports payment fields and specific fields
1316 * are not specified. This is a transitional function for refactoring legacy code.
1317 */
1318 public function getPaymentHeaders() {
1319 if ($this->isExportPaymentFields() && !$this->isExportSpecifiedPaymentFields()) {
749e60f4 1320 return CRM_Utils_Array::collect('title', $this->getcomponentPaymentFields());
05ad310f 1321 }
1322 return [];
1323 }
1324
d41ab886 1325 /**
1326 * Get the default properties when not specified.
1327 *
1328 * In the UI this appears as 'Primary fields only' but in practice it's
1329 * most of the kitchen sink and the hallway closet thrown in.
1330 *
1331 * Since CRM-952 custom fields are excluded, but no other form of mercy is shown.
1332 *
1333 * @return array
1334 */
1335 public function getDefaultReturnProperties() {
1336 $returnProperties = [];
1337 $fields = CRM_Contact_BAO_Contact::exportableFields('All', TRUE, TRUE);
704e3e9a 1338 $skippedFields = ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTACTS) ? [] : [
1339 'groups',
1340 'tags',
7b966967 1341 'notes',
704e3e9a 1342 ];
d41ab886 1343
1344 foreach ($fields as $key => $var) {
1345 if ($key && (substr($key, 0, 6) != 'custom') && !in_array($key, $skippedFields)) {
1346 $returnProperties[$key] = 1;
1347 }
1348 }
1349 $returnProperties = array_merge($returnProperties, $this->getAdditionalReturnProperties());
1350 return $returnProperties;
1351 }
1352
704e3e9a 1353 /**
1354 * Add the field to relationship return properties & return it.
1355 *
1356 * This function is doing both setting & getting which is yuck but it is an interim
1357 * refactor.
1358 *
1359 * @param array $value
1360 * @param string $relationshipKey
1361 *
1362 * @return array
1363 */
1364 public function setRelationshipReturnProperties($value, $relationshipKey) {
04f8f758 1365 $relationField = $value['name'];
1366 $relIMProviderId = NULL;
9c1bc317 1367 $relLocTypeId = $value['location_type_id'] ?? NULL;
04f8f758 1368 $locationName = CRM_Core_PseudoConstant::getName('CRM_Core_BAO_Address', 'location_type_id', $relLocTypeId);
1369 $relPhoneTypeId = CRM_Utils_Array::value('phone_type_id', $value, ($locationName ? 'Primary' : NULL));
1370 $relIMProviderId = CRM_Utils_Array::value('im_provider_id', $value, ($locationName ? 'Primary' : NULL));
1371 if (in_array($relationField, $this->getValidLocationFields()) && $locationName) {
1372 if ($relationField === 'phone') {
a16a432a 1373 $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName]['phone-' . $relPhoneTypeId] = 1;
704e3e9a 1374 }
04f8f758 1375 elseif ($relationField === 'im') {
a16a432a 1376 $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName]['im-' . $relIMProviderId] = 1;
704e3e9a 1377 }
1378 else {
a16a432a 1379 $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName][$relationField] = 1;
704e3e9a 1380 }
1381 }
1382 else {
1383 $this->relationshipReturnProperties[$relationshipKey][$relationField] = 1;
1384 }
1385 return $this->relationshipReturnProperties[$relationshipKey];
1386 }
1387
ce12a9e0 1388 /**
1389 * Add the main return properties to the household merge properties if needed for merging.
1390 *
1391 * If we are using household merge we need to add these to the relationship properties to
1392 * be retrieved.
ce12a9e0 1393 */
6df77635 1394 public function setHouseholdMergeReturnProperties() {
3611a532 1395 if ($this->isMergeSameHousehold()) {
1396 $returnProperties = $this->getReturnProperties();
1397 $returnProperties = array_diff_key($returnProperties, array_fill_keys(['location_type', 'im_provider'], 1));
1398 foreach ($this->getHouseholdRelationshipTypes() as $householdRelationshipType) {
1399 $this->relationshipReturnProperties[$householdRelationshipType] = $returnProperties;
1400 }
ce12a9e0 1401 }
1402 }
1403
704e3e9a 1404 /**
1405 * Get the default location fields to request.
1406 *
1407 * @return array
1408 */
1409 public function getValidLocationFields() {
1410 return [
1411 'street_address',
1412 'supplemental_address_1',
1413 'supplemental_address_2',
1414 'supplemental_address_3',
1415 'city',
1416 'postal_code',
1417 'postal_code_suffix',
1418 'geo_code_1',
1419 'geo_code_2',
1420 'state_province',
1421 'country',
1422 'phone',
1423 'email',
1424 'im',
1425 ];
1426 }
1427
aa3a113b 1428 /**
c8adad81 1429 * Get the sql column definition for the given field.
1430 *
5ebd7d09 1431 * @param string $fieldName
1432 * @param string $columnName
aa3a113b 1433 *
1434 * @return mixed
1435 */
5ebd7d09 1436 public function getSqlColumnDefinition($fieldName, $columnName) {
aa3a113b 1437
1438 // early exit for master_id, CRM-12100
1439 // in the DB it is an ID, but in the export, we retrive the display_name of the master record
1440 // also for current_employer, CRM-16939
5ebd7d09 1441 if ($columnName == 'master_id' || $columnName == 'current_employer') {
f61dd144 1442 return "`$fieldName` varchar(128)";
aa3a113b 1443 }
1444
aa3a113b 1445 $queryFields = $this->getQueryFields();
714ab070 1446 // @todo remove the enotice avoidance here, ensure all columns are declared.
1447 // tests will fail on the enotices until they all are & then all the 'else'
1448 // below can go.
1449 $fieldSpec = $queryFields[$columnName] ?? [];
1450
aa3a113b 1451 // set the sql columns
714ab070 1452 if (isset($fieldSpec['type'])) {
1453 switch ($fieldSpec['type']) {
aa3a113b 1454 case CRM_Utils_Type::T_INT:
1455 case CRM_Utils_Type::T_BOOLEAN:
d989f2a4 1456 if (in_array(CRM_Utils_Array::value('data_type', $fieldSpec), ['Country', 'StateProvince', 'ContactReference'])) {
f61dd144 1457 return "`$fieldName` varchar(255)";
d989f2a4 1458 }
f61dd144 1459 return "`$fieldName` varchar(16)";
aa3a113b 1460
1461 case CRM_Utils_Type::T_STRING:
5ebd7d09 1462 if (isset($queryFields[$columnName]['maxlength'])) {
f61dd144 1463 return "`$fieldName` varchar({$queryFields[$columnName]['maxlength']})";
aa3a113b 1464 }
1465 else {
f61dd144 1466 return "`$fieldName` varchar(255)";
aa3a113b 1467 }
1468
1469 case CRM_Utils_Type::T_TEXT:
1470 case CRM_Utils_Type::T_LONGTEXT:
1471 case CRM_Utils_Type::T_BLOB:
1472 case CRM_Utils_Type::T_MEDIUMBLOB:
f61dd144 1473 return "`$fieldName` longtext";
aa3a113b 1474
1475 case CRM_Utils_Type::T_FLOAT:
1476 case CRM_Utils_Type::T_ENUM:
1477 case CRM_Utils_Type::T_DATE:
1478 case CRM_Utils_Type::T_TIME:
1479 case CRM_Utils_Type::T_TIMESTAMP:
1480 case CRM_Utils_Type::T_MONEY:
1481 case CRM_Utils_Type::T_EMAIL:
1482 case CRM_Utils_Type::T_URL:
1483 case CRM_Utils_Type::T_CCNUM:
1484 default:
f61dd144 1485 return "`$fieldName` varchar(32)";
aa3a113b 1486 }
1487 }
1488 else {
1489 if (substr($fieldName, -3, 3) == '_id') {
f61dd144 1490 return "`$fieldName` varchar(255)";
aa3a113b 1491 }
1492 elseif (substr($fieldName, -5, 5) == '_note') {
f61dd144 1493 return "`$fieldName` text";
aa3a113b 1494 }
1495 else {
1496 $changeFields = [
1497 'groups',
1498 'tags',
1499 'notes',
1500 ];
1501
1502 if (in_array($fieldName, $changeFields)) {
f61dd144 1503 return "`$fieldName` text";
aa3a113b 1504 }
1505 else {
1506 // set the sql columns for custom data
5ebd7d09 1507 if (isset($queryFields[$columnName]['data_type'])) {
aa3a113b 1508
5ebd7d09 1509 switch ($queryFields[$columnName]['data_type']) {
aa3a113b 1510 case 'String':
1511 // May be option labels, which could be up to 512 characters
5ebd7d09 1512 $length = max(512, CRM_Utils_Array::value('text_length', $queryFields[$columnName]));
f61dd144 1513 return "`$fieldName` varchar($length)";
aa3a113b 1514
aa3a113b 1515 case 'Link':
f61dd144 1516 return "`$fieldName` varchar(255)";
aa3a113b 1517
1518 case 'Memo':
f61dd144 1519 return "`$fieldName` text";
aa3a113b 1520
1521 default:
f61dd144 1522 return "`$fieldName` varchar(255)";
aa3a113b 1523 }
1524 }
1525 else {
f61dd144 1526 return "`$fieldName` text";
aa3a113b 1527 }
1528 }
1529 }
1530 }
1531 }
1532
c8adad81 1533 /**
1534 * Get the munged field name.
1535 *
1536 * @param string $field
1537 * @return string
1538 */
1539 public function getMungedFieldName($field) {
1540 $fieldName = CRM_Utils_String::munge(strtolower($field), '_', 64);
1541 if ($fieldName == 'id') {
1542 $fieldName = 'civicrm_primary_id';
1543 }
1544 return $fieldName;
1545 }
1546
5ebd7d09 1547 /**
1548 * In order to respect the history of this class we need to index kinda illogically.
1549 *
1550 * On the bright side - this stuff is tested within a nano-byte of it's life.
1551 *
1552 * e.g '2-a-b_Home-City'
1553 *
1554 * @param string $key
1555 * @param string $relationshipType
1556 * @param string $locationType
1557 * @param $entityLabel
1558 *
1559 * @return string
1560 */
1561 protected function getOutputSpecificationIndex($key, $relationshipType, $locationType, $entityLabel) {
001a515b 1562 if ($entityLabel || $key === 'im') {
5ebd7d09 1563 // Just cos that's the history...
1564 if ($key !== 'master_id') {
1565 $key = $this->getHeaderForRow($key);
1566 }
1567 }
1568 if (!$relationshipType || $key !== 'id') {
1569 $key = $this->getMungedFieldName($key);
1570 }
1571 return $this->getMungedFieldName(
1572 ($relationshipType ? ($relationshipType . '_') : '')
1573 . ($locationType ? ($locationType . '_') : '')
1574 . $key
1575 . ($entityLabel ? ('_' . $entityLabel) : '')
1576 );
1577 }
1578
1579 /**
1580 * Get the compiled label for the column.
1581 *
1582 * e.g 'Gender', 'Employee Of-Home-city'
1583 *
1584 * @param string $key
1585 * @param string $relationshipType
1586 * @param string $locationType
1587 * @param string $entityLabel
1588 *
1589 * @return string
1590 */
1591 protected function getOutputSpecificationLabel($key, $relationshipType, $locationType, $entityLabel) {
1592 return ($relationshipType ? $this->getRelationshipTypes()[$relationshipType] . '-' : '')
1593 . ($locationType ? $locationType . '-' : '')
1594 . $this->getHeaderForRow($key)
1595 . ($entityLabel ? '-' . $entityLabel : '');
1596 }
1597
1598 /**
1599 * Get the mysql field name key.
1600 *
1601 * This key is locked in by tests but the reasons for the specific conventions -
1602 * ie. headings are used for keying fields in some cases, are likely
1603 * accidental rather than deliberate.
1604 *
1605 * This key is used for the output sql array.
1606 *
1607 * @param string $key
1608 * @param $relationshipType
1609 * @param $locationType
1610 * @param $entityLabel
1611 *
1612 * @return string
1613 */
1614 protected function getOutputSpecificationFieldKey($key, $relationshipType, $locationType, $entityLabel) {
001a515b 1615 if ($entityLabel || $key === 'im') {
5ebd7d09 1616 if ($key !== 'state_province' && $key !== 'id') {
001a515b 1617 // @todo - test removing this - indexing by $key should be fine...
5ebd7d09 1618 $key = $this->getHeaderForRow($key);
1619 }
1620 }
1621 if (!$relationshipType || $key !== 'id') {
1622 $key = $this->getMungedFieldName($key);
1623 }
1624 $fieldKey = $this->getMungedFieldName(
1625 ($relationshipType ? ($relationshipType . '_') : '')
1626 . ($locationType ? ($locationType . '_') : '')
1627 . $key
1628 . ($entityLabel ? ('_' . $entityLabel) : '')
1629 );
1630 return $fieldKey;
1631 }
1632
6d52bfe5 1633 /**
1634 * Get params for the where criteria.
1635 *
1636 * @return mixed
1637 */
1638 public function getWhereParams() {
1639 if (!$this->isPostalableOnly()) {
1640 return [];
1641 }
1642 $params['is_deceased'] = ['is_deceased', '=', 0, CRM_Contact_BAO_Query::MODE_CONTACTS];
1643 $params['do_not_mail'] = ['do_not_mail', '=', 0, CRM_Contact_BAO_Query::MODE_CONTACTS];
1644 return $params;
1645 }
1646
136f69a8 1647 /**
1648 * @param $row
1649 * @param $contactID
1650 * @param $value
1651 * @param $field
1652 */
1653 protected function buildRelationshipFieldsForRow(&$row, $contactID, $value, $field) {
1654 foreach (array_keys($value) as $property) {
1655 if ($property === 'location') {
1656 // @todo just undo all this nasty location wrangling!
1657 foreach ($value['location'] as $locationKey => $locationFields) {
1658 foreach (array_keys($locationFields) as $locationField) {
1659 $fieldKey = str_replace(' ', '_', $locationKey . '-' . $locationField);
1660 $row[$field . '_' . $fieldKey] = $this->getRelationshipValue($field, $contactID, $fieldKey);
1661 }
1662 }
1663 }
1664 else {
1665 $row[$field . '_' . $property] = $this->getRelationshipValue($field, $contactID, $property);
1666 }
1667 }
1668 }
1669
ebebd629 1670 /**
1671 * Is this contact a household that is already set to be exported by virtue of it's household members.
1672 *
1673 * @param int $contactID
1674 *
1675 * @return bool
1676 */
1677 protected function isHouseholdToSkip($contactID) {
1678 return in_array($contactID, $this->householdsToSkip);
1679 }
1680
44fc53ab 1681 /**
1682 * Get the various arrays that we use to structure our output.
1683 *
1684 * The extraction of these has been moved to a separate function for clarity and so that
1685 * tests can be added - in particular on the $outputHeaders array.
1686 *
1687 * However it still feels a bit like something that I'm too polite to write down and this should be seen
1688 * as a step on the refactoring path rather than how it should be.
1689 *
1690 * @return array
1691 * - outputColumns Array of columns to be exported. The values don't matter but the key must match the
1692 * alias for the field generated by BAO_Query object.
1693 * - headerRows Array of the column header strings to put in the csv header - non-associative.
1694 * - sqlColumns Array of column names for the temp table. Not too sure why outputColumns can't be used here.
1695 * - metadata Array of fields with specific parameters to pass to the translate function or another hacky nasty solution
1696 * I'm too embarassed to discuss here.
1697 * The keys need
1698 * - to match the outputColumns keys (yes, the fact we ignore the output columns values & then pass another array with values
1699 * we could use does suggest further refactors. However, you future improver, do remember that every check you do
1700 * in the main DAO loop is done once per row & that coule be 100,000 times.)
1701 * Finally a pop quiz: We need the translate context because we use a function other than ts() - is this because
1702 * - a) the function used is more efficient or
1703 * - b) this code is old & outdated. Submit your answers to circular bin or better
1704 * yet find a way to comment them for posterity.
1705 */
1706 public function getExportStructureArrays() {
1707 $outputColumns = $metadata = [];
1708 $queryFields = $this->getQueryFields();
1709 foreach ($this->getReturnProperties() as $key => $value) {
1710 if (($key != 'location' || !is_array($value)) && !$this->isRelationshipTypeKey($key)) {
1711 $outputColumns[$key] = $value;
1712 $this->addOutputSpecification($key);
1713 }
1714 elseif ($this->isRelationshipTypeKey($key)) {
1715 $outputColumns[$key] = $value;
1716 foreach ($value as $relationField => $relationValue) {
1717 // below block is same as primary block (duplicate)
1718 if (isset($queryFields[$relationField]['title'])) {
1719 $this->addOutputSpecification($relationField, $key);
1720 }
1721 elseif (is_array($relationValue) && $relationField == 'location') {
1722 // fix header for location type case
1723 foreach ($relationValue as $ltype => $val) {
1724 foreach (array_keys($val) as $fld) {
1725 $type = explode('-', $fld);
1726 $this->addOutputSpecification($type[0], $key, $ltype, CRM_Utils_Array::value(1, $type));
1727 }
1728 }
1729 }
1730 }
1731 }
1732 else {
1733 foreach ($value as $locationType => $locationFields) {
1734 foreach (array_keys($locationFields) as $locationFieldName) {
1735 $type = explode('-', $locationFieldName);
1736
1737 $actualDBFieldName = $type[0];
1738 $daoFieldName = CRM_Utils_String::munge($locationType) . '-' . $actualDBFieldName;
1739
1740 if (!empty($type[1])) {
1741 $daoFieldName .= "-" . $type[1];
1742 }
1743 $this->addOutputSpecification($actualDBFieldName, NULL, $locationType, CRM_Utils_Array::value(1, $type));
1744 $metadata[$daoFieldName] = $this->getMetaDataForField($actualDBFieldName);
1745 $outputColumns[$daoFieldName] = TRUE;
1746 }
1747 }
1748 }
1749 }
1750 return [$outputColumns, $metadata];
1751 }
1752
ccc3ac8a 1753 /**
1754 * Get default return property for export based on mode
1755 *
1756 * @return string
1757 * Default Return property
1758 */
1759 public function defaultReturnProperty() {
1760 // hack to add default return property based on export mode
1761 $property = NULL;
1762 $exportMode = $this->getExportMode();
1763 if ($exportMode == CRM_Export_Form_Select::CONTRIBUTE_EXPORT) {
1764 $property = 'contribution_id';
1765 }
1766 elseif ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) {
1767 $property = 'participant_id';
1768 }
1769 elseif ($exportMode == CRM_Export_Form_Select::MEMBER_EXPORT) {
1770 $property = 'membership_id';
1771 }
1772 elseif ($exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT) {
1773 $property = 'pledge_id';
1774 }
1775 elseif ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
1776 $property = 'case_id';
1777 }
1778 elseif ($exportMode == CRM_Export_Form_Select::GRANT_EXPORT) {
1779 $property = 'grant_id';
1780 }
1781 elseif ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
1782 $property = 'activity_id';
1783 }
1784 return $property;
1785 }
1786
806c5e1e 1787 /**
1788 * Determine the required return properties from the input parameters.
1789 *
1790 * @return array
1791 */
1792 public function determineReturnProperties() {
1793 if ($this->getRequestedFields()) {
1794 $returnProperties = [];
1795 foreach ($this->getRequestedFields() as $key => $value) {
04f8f758 1796 $fieldName = $value['name'];
7d0e773c 1797 $locationName = !empty($value['location_type_id']) ? CRM_Core_PseudoConstant::getName('CRM_Core_BAO_Address', 'location_type_id', $value['location_type_id']) : NULL;
04f8f758 1798 $relationshipTypeKey = !empty($value['relationship_type_id']) ? $value['relationship_type_id'] . '_' . $value['relationship_direction'] : NULL;
1799 if (!$fieldName || $this->isHouseholdMergeRelationshipTypeKey($relationshipTypeKey)) {
806c5e1e 1800 continue;
1801 }
1802
04f8f758 1803 if ($this->isRelationshipTypeKey($relationshipTypeKey)) {
1804 $returnProperties[$relationshipTypeKey] = $this->setRelationshipReturnProperties($value, $relationshipTypeKey);
806c5e1e 1805 }
04f8f758 1806 elseif ($locationName) {
1807 if ($fieldName === 'phone') {
1808 $returnProperties['location'][$locationName]['phone-' . $value['phone_type_id'] ?? NULL] = 1;
806c5e1e 1809 }
04f8f758 1810 elseif ($fieldName === 'im') {
1811 $returnProperties['location'][$locationName]['im-' . $value['im_provider_id'] ?? NULL] = 1;
806c5e1e 1812 }
1813 else {
1814 $returnProperties['location'][$locationName][$fieldName] = 1;
1815 }
1816 }
1817 else {
1818 //hack to fix component fields
1819 //revert mix of event_id and title
1820 if ($fieldName == 'event_id') {
1821 $returnProperties['event_id'] = 1;
1822 }
1823 else {
1824 $returnProperties[$fieldName] = 1;
1825 }
1826 }
1827 }
1828 $defaultExportMode = $this->defaultReturnProperty();
1829 if ($defaultExportMode) {
1830 $returnProperties[$defaultExportMode] = 1;
1831 }
1832 }
1833 else {
1834 $returnProperties = $this->getDefaultReturnProperties();
1835 }
25e51a1c 1836 if ($this->isMergeSameHousehold()) {
1837 $returnProperties['id'] = 1;
1838 }
2761a916 1839 if ($this->isMergeSameAddress()) {
1840 $returnProperties['addressee'] = 1;
1841 $returnProperties['postal_greeting'] = 1;
1842 $returnProperties['email_greeting'] = 1;
1843 $returnProperties['street_name'] = 1;
1844 $returnProperties['household_name'] = 1;
1845 $returnProperties['street_address'] = 1;
1846 $returnProperties['city'] = 1;
1847 $returnProperties['state_province'] = 1;
1848
1849 }
806c5e1e 1850 return $returnProperties;
1851 }
1852
571f3b5b 1853 /**
7628764a 1854 * @param object $query
1855 * CRM_Contact_BAO_Query
1856 *
1857 * @return string
1858 * Group By Clause
1859 */
1860 public function getGroupBy($query) {
1861 $groupBy = NULL;
1862 $returnProperties = $this->getReturnProperties();
1863 $exportMode = $this->getExportMode();
1864 $queryMode = $this->getQueryMode();
1865 if (!empty($returnProperties['tags']) || !empty($returnProperties['groups']) ||
1866 CRM_Utils_Array::value('notes', $returnProperties) ||
1867 // CRM-9552
1868 ($queryMode & CRM_Contact_BAO_Query::MODE_CONTACTS && $query->_useGroupBy)
1869 ) {
1870 $groupBy = "contact_a.id";
1871 }
1872
1873 switch ($exportMode) {
1874 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
1875 $groupBy = 'civicrm_contribution.id';
1876 if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) {
1877 // especial group by when soft credit columns are included
1878 $groupBy = ['contribution_search_scredit_combined.id', 'contribution_search_scredit_combined.scredit_id'];
1879 }
1880 break;
1881
1882 case CRM_Export_Form_Select::EVENT_EXPORT:
1883 $groupBy = 'civicrm_participant.id';
1884 break;
1885
1886 case CRM_Export_Form_Select::MEMBER_EXPORT:
1887 $groupBy = "civicrm_membership.id";
1888 break;
1889 }
1890
1891 if ($queryMode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
1892 $groupBy = "civicrm_activity.id ";
1893 }
1894
1895 return $groupBy ? ' GROUP BY ' . implode(', ', (array) $groupBy) : '';
1896 }
806c5e1e 1897
571f3b5b 1898 /**
1899 * @param int $contactId
571f3b5b 1900 *
1901 * @return array
1902 */
423e3da6 1903 public function replaceMergeTokens($contactId) {
571f3b5b 1904 $greetings = [];
1905 $contact = NULL;
1906
1907 $greetingFields = [
a2b21ae3 1908 'postal_greeting' => $this->getPostalGreetingTemplate(),
1909 'addressee' => $this->getAddresseeGreetingTemplate(),
571f3b5b 1910 ];
a2b21ae3 1911 foreach ($greetingFields as $greeting => $greetingLabel) {
423e3da6 1912 $tokens = CRM_Utils_Token::getTokens($greetingLabel);
1913 if (!empty($tokens)) {
571f3b5b 1914 if (empty($contact)) {
1915 $values = [
1916 'id' => $contactId,
1917 'version' => 3,
1918 ];
1919 $contact = civicrm_api('contact', 'get', $values);
1920
1921 if (!empty($contact['is_error'])) {
1922 return $greetings;
1923 }
1924 $contact = $contact['values'][$contact['id']];
1925 }
1926
1927 $tokens = ['contact' => $greetingLabel];
1928 $greetings[$greeting] = CRM_Utils_Token::replaceContactTokens($greetingLabel, $contact, NULL, $tokens);
1929 }
1930 }
1931 return $greetings;
1932 }
1933
c8713fd4 1934 /**
1935 * Build array for merging same addresses.
1936 *
0b743fb1 1937 * @param string $sql
c8713fd4 1938 */
8669d9a7 1939 public function buildMasterCopyArray($sql) {
c8713fd4 1940
0b743fb1 1941 $parents = [];
c8713fd4 1942 $dao = CRM_Core_DAO::executeQuery($sql);
1943
1944 while ($dao->fetch()) {
1945 $masterID = $dao->master_id;
1946 $copyID = $dao->copy_id;
c8713fd4 1947
cf7c5cb8 1948 $this->cacheContactGreetings((int) $dao->master_contact_id);
1949 $this->cacheContactGreetings((int) $dao->copy_contact_id);
c8713fd4 1950
0b743fb1 1951 if (!isset($this->contactsToMerge[$masterID])) {
c8713fd4 1952 // check if this is an intermediate child
1953 // this happens if there are 3 or more matches a,b, c
1954 // the above query will return a, b / a, c / b, c
1955 // we might be doing a bit more work, but for now its ok, unless someone
1956 // knows how to fix the query above
1957 if (isset($parents[$masterID])) {
1958 $masterID = $parents[$masterID];
1959 }
1960 else {
0b743fb1 1961 $this->contactsToMerge[$masterID] = [
d183e23a 1962 'addressee' => $this->getContactGreeting((int) $dao->master_contact_id, 'addressee', $dao->master_addressee),
c8713fd4 1963 'copy' => [],
d183e23a 1964 'postalGreeting' => $this->getContactGreeting((int) $dao->master_contact_id, 'postal_greeting', $dao->master_postal_greeting),
c8713fd4 1965 ];
0b743fb1 1966 $this->contactsToMerge[$masterID]['emailGreeting'] = &$this->contactsToMerge[$masterID]['postalGreeting'];
c8713fd4 1967 }
1968 }
1969 $parents[$copyID] = $masterID;
1970
8669d9a7 1971 if (!array_key_exists($copyID, $this->contactsToMerge[$masterID]['copy'])) {
d183e23a 1972 $copyPostalGreeting = $this->getContactPortionOfGreeting((int) $dao->copy_contact_id, (int) $dao->copy_postal_greeting_id, 'postal_greeting', $dao->copy_postal_greeting);
ee769c00 1973 if ($copyPostalGreeting) {
0b743fb1 1974 $this->contactsToMerge[$masterID]['postalGreeting'] = "{$this->contactsToMerge[$masterID]['postalGreeting']}, {$copyPostalGreeting}";
c8713fd4 1975 // if there happens to be a duplicate, remove it
0b743fb1 1976 $this->contactsToMerge[$masterID]['postalGreeting'] = str_replace(" {$copyPostalGreeting},", "", $this->contactsToMerge[$masterID]['postalGreeting']);
c8713fd4 1977 }
1978
d183e23a 1979 $copyAddressee = $this->getContactPortionOfGreeting((int) $dao->copy_contact_id, (int) $dao->copy_addressee_id, 'addressee', $dao->copy_addressee);
ee769c00 1980 if ($copyAddressee) {
0b743fb1 1981 $this->contactsToMerge[$masterID]['addressee'] = "{$this->contactsToMerge[$masterID]['addressee']}, " . trim($copyAddressee);
c8713fd4 1982 }
1983 }
0b743fb1 1984 if (!isset($this->contactsToMerge[$masterID]['copy'][$copyID])) {
1985 // If it was set in the first run through - share routine, don't subsequently clobber.
1986 $this->contactsToMerge[$masterID]['copy'][$copyID] = $copyAddressee ?? $dao->copy_addressee;
1987 }
c8713fd4 1988 }
c8713fd4 1989 }
1990
30237bf3 1991 /**
1992 * Merge contacts with the same address.
30237bf3 1993 */
f8fb0608 1994 public function mergeSameAddress() {
a2b21ae3 1995
30237bf3 1996 $tableName = $this->getTemporaryTable();
30237bf3 1997
1998 // find all the records that have the same street address BUT not in a household
1999 // require match on city and state as well
2000 $sql = "
2001SELECT r1.id as master_id,
2002 r1.civicrm_primary_id as master_contact_id,
2003 r1.postal_greeting as master_postal_greeting,
2004 r1.postal_greeting_id as master_postal_greeting_id,
2005 r1.addressee as master_addressee,
2006 r1.addressee_id as master_addressee_id,
2007 r2.id as copy_id,
2008 r2.civicrm_primary_id as copy_contact_id,
2009 r2.postal_greeting as copy_postal_greeting,
2010 r2.postal_greeting_id as copy_postal_greeting_id,
2011 r2.addressee as copy_addressee,
2012 r2.addressee_id as copy_addressee_id
2013FROM $tableName r1
2014LEFT JOIN $tableName r2 ON ( r1.street_address = r2.street_address AND
8669d9a7 2015 r1.city = r2.city AND
2016 r1.state_province_id = r2.state_province_id )
2017WHERE ( r1.street_address != '' )
30237bf3 2018AND r2.id > r1.id
2019ORDER BY r1.id
2020";
0b743fb1 2021 $this->buildMasterCopyArray($sql);
30237bf3 2022
0b743fb1 2023 foreach ($this->contactsToMerge as $masterID => $values) {
30237bf3 2024 $sql = "
2025UPDATE $tableName
2026SET addressee = %1, postal_greeting = %2, email_greeting = %3
2027WHERE id = %4
2028";
2029 $params = [
2030 1 => [$values['addressee'], 'String'],
2031 2 => [$values['postalGreeting'], 'String'],
2032 3 => [$values['emailGreeting'], 'String'],
2033 4 => [$masterID, 'Integer'],
2034 ];
2035 CRM_Core_DAO::executeQuery($sql, $params);
2036
2037 // delete all copies
2038 $deleteIDs = array_keys($values['copy']);
2039 $deleteIDString = implode(',', $deleteIDs);
2040 $sql = "
2041DELETE FROM $tableName
2042WHERE id IN ( $deleteIDString )
2043";
2044 CRM_Core_DAO::executeQuery($sql);
2045 }
30237bf3 2046 }
2047
e358ebd1 2048 /**
2049 * The function unsets static part of the string, if token is the dynamic part.
2050 *
2051 * Example: 'Hello {contact.first_name}' => converted to => '{contact.first_name}'
2052 * i.e 'Hello Alan' => converted to => 'Alan'
2053 *
2054 * @param string $parsedString
2055 * @param string $defaultGreeting
ca76de35 2056 * @param string $greetingLabel
e358ebd1 2057 *
2058 * @return mixed
2059 */
2060 public function trimNonTokensFromAddressString(
2061 &$parsedString, $defaultGreeting,
ca76de35 2062 $greetingLabel
e358ebd1 2063 ) {
e358ebd1 2064 $greetingLabel = empty($greetingLabel) ? $defaultGreeting : $greetingLabel;
2065
2066 $stringsToBeReplaced = preg_replace('/(\{[a-zA-Z._ ]+\})/', ';;', $greetingLabel);
2067 $stringsToBeReplaced = explode(';;', $stringsToBeReplaced);
2068 foreach ($stringsToBeReplaced as $key => $string) {
2069 // to keep one space
2070 $stringsToBeReplaced[$key] = ltrim($string);
2071 }
2072 $parsedString = str_replace($stringsToBeReplaced, "", $parsedString);
2073
2074 return $parsedString;
2075 }
2076
978f5005 2077 /**
2078 * Preview export output.
2079 *
2080 * @param int $limit
2081 * @return array
2082 */
2083 public function getPreview($limit) {
2084 $rows = [];
2085 list($outputColumns, $metadata) = $this->getExportStructureArrays();
2086 $query = $this->runQuery([], '');
2087 CRM_Core_DAO::disableFullGroupByMode();
2088 $result = CRM_Core_DAO::executeQuery($query[1] . ' LIMIT ' . (int) $limit);
2089 CRM_Core_DAO::reenableFullGroupByMode();
2090 while ($result->fetch()) {
2091 $rows[] = $this->buildRow($query[0], $result, $outputColumns, $metadata, [], []);
2092 }
2093 return $rows;
2094 }
2095
1e9e062d 2096 /**
2097 * Set the template strings to be used when merging two contacts with the same address.
2098 *
2099 * @param array $formValues
2100 * Values from first form. In this case we care about the keys
2101 * - postal_greeting
2102 * - postal_other
2103 * - address_greeting
2104 * - addressee_other
2105 *
2106 * @return mixed
2107 */
2108 protected function setGreetingStringsForSameAddressMerge($formValues) {
2109 $greetingOptions = CRM_Export_Form_Select::getGreetingOptions();
2110
2111 if (!empty($greetingOptions)) {
2112 // Greeting options is keyed by 'postal_greeting' or 'addressee'.
2113 foreach ($greetingOptions as $key => $value) {
9c1bc317 2114 $option = $formValues[$key] ?? NULL;
1e9e062d 2115 if ($option) {
2116 if ($greetingOptions[$key][$option] == ts('Other')) {
2117 $formValues[$key] = $formValues["{$key}_other"];
2118 }
2119 elseif ($greetingOptions[$key][$option] == ts('List of names')) {
2120 $formValues[$key] = '';
2121 }
2122 else {
2123 $formValues[$key] = $greetingOptions[$key][$option];
2124 }
2125 }
2126 }
2127 }
2128 if (!empty($formValues['postal_greeting'])) {
2129 $this->setPostalGreetingTemplate($formValues['postal_greeting']);
2130 }
2131 if (!empty($formValues['addressee'])) {
2132 $this->setAddresseeGreetingTemplate($formValues['addressee']);
2133 }
2134 }
2135
3e644436 2136 /**
2137 * Create the temporary table for output.
2138 */
2139 public function createTempTable() {
2140 //creating a temporary table for the search result that need be exported
2141 $exportTempTable = CRM_Utils_SQL_TempTable::build()->setDurable()->setCategory('export');
2142 $sqlColumns = $this->getSQLColumns();
2143 // also create the sql table
2144 $exportTempTable->drop();
2145
2146 $sql = " id int unsigned NOT NULL AUTO_INCREMENT, ";
2147 if (!empty($sqlColumns)) {
2148 $sql .= implode(",\n", array_values($sqlColumns)) . ',';
2149 }
2150
2151 $sql .= "\n PRIMARY KEY ( id )";
2152
2153 // add indexes for street_address and household_name if present
2154 $addIndices = [
2155 'street_address',
2156 'household_name',
2157 'civicrm_primary_id',
2158 ];
2159
2160 foreach ($addIndices as $index) {
2161 if (isset($sqlColumns[$index])) {
2162 $sql .= ",
2163 INDEX index_{$index}( $index )
2164";
2165 }
2166 }
2167
2168 $exportTempTable->createWithColumns($sql);
2169 $this->setTemporaryTable($exportTempTable->getName());
2170 }
2171
b0b40cd7 2172 /**
2173 * Get the values of linked household contact.
2174 *
2175 * @param CRM_Core_DAO $relDAO
2176 * @param array $value
2177 * @param string $field
2178 * @param array $row
2179 *
2180 * @throws \Exception
2181 */
2182 public function fetchRelationshipDetails($relDAO, $value, $field, &$row) {
2183 $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
2184 $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
2185 $i18n = CRM_Core_I18n::singleton();
2186 $field = $field . '_';
2187
2188 foreach ($value as $relationField => $relationValue) {
2189 if (is_object($relDAO) && property_exists($relDAO, $relationField)) {
2190 $fieldValue = $relDAO->$relationField;
2191 if ($relationField == 'phone_type_id') {
2192 $fieldValue = $phoneTypes[$relationValue];
2193 }
2194 elseif ($relationField == 'provider_id') {
9c1bc317 2195 $fieldValue = $imProviders[$relationValue] ?? NULL;
b0b40cd7 2196 }
2197 // CRM-13995
2198 elseif (is_object($relDAO) && in_array($relationField, [
2199 'email_greeting',
2200 'postal_greeting',
2201 'addressee',
2202 ])) {
2203 //special case for greeting replacement
2204 $fldValue = "{$relationField}_display";
2205 $fieldValue = $relDAO->$fldValue;
2206 }
2207 }
2208 elseif (is_object($relDAO) && $relationField == 'state_province') {
2209 $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id);
2210 }
2211 elseif (is_object($relDAO) && $relationField == 'country') {
2212 $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id);
2213 }
2214 else {
2215 $fieldValue = '';
2216 }
2217 $relPrefix = $field . $relationField;
2218
2219 if (is_object($relDAO) && $relationField == 'id') {
2220 $row[$relPrefix] = $relDAO->contact_id;
2221 }
2222 elseif (is_array($relationValue) && $relationField == 'location') {
2223 foreach ($relationValue as $ltype => $val) {
2224 // If the location name has a space in it the we need to handle that. This
2225 // is kinda hacky but specifically covered in the ExportTest so later efforts to
2226 // improve it should be secure in the knowled it will be caught.
2227 $ltype = str_replace(' ', '_', $ltype);
2228 foreach (array_keys($val) as $fld) {
2229 $type = explode('-', $fld);
2230 $fldValue = "{$ltype}-" . $type[0];
2231 if (!empty($type[1])) {
2232 $fldValue .= "-" . $type[1];
2233 }
2234 // CRM-3157: localise country, region (both have ‘country’ context)
2235 // and state_province (‘province’ context)
2236 switch (TRUE) {
2237 case (!is_object($relDAO)):
2238 $row[$field . '_' . $fldValue] = '';
2239 break;
2240
2241 case in_array('country', $type):
2242 case in_array('world_region', $type):
2243 $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
2244 ['context' => 'country']
2245 );
2246 break;
2247
2248 case in_array('state_province', $type):
2249 $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
2250 ['context' => 'province']
2251 );
2252 break;
2253
2254 default:
2255 $row[$field . '_' . $fldValue] = $relDAO->$fldValue;
2256 break;
2257 }
2258 }
2259 }
2260 }
2261 elseif (isset($fieldValue) && $fieldValue != '') {
2262 //check for custom data
2263 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($relationField)) {
2264 $row[$relPrefix] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID);
2265 }
2266 else {
2267 //normal relationship fields
2268 // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context)
2269 switch ($relationField) {
2270 case 'country':
2271 case 'world_region':
2272 $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'country']);
2273 break;
2274
2275 case 'state_province':
2276 $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'province']);
2277 break;
2278
2279 default:
2280 $row[$relPrefix] = $fieldValue;
2281 break;
2282 }
2283 }
2284 }
2285 else {
2286 // if relation field is empty or null
2287 $row[$relPrefix] = '';
2288 }
2289 }
2290 }
2291
b6149515 2292 /**
2293 * Write to the csv from the temp table.
2294 */
2295 public function writeCSVFromTable() {
2296 // call export hook
2297 $headerRows = $this->getHeaderRows();
2298 $exportTempTable = $this->getTemporaryTable();
246e571e
JG
2299 $exportMode = $this->getExportMode();
2300 $sqlColumns = $this->getSQLColumns();
2301 $componentTable = $this->getComponentTable();
2302 $ids = $this->getIds();
b6149515 2303 CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode, $componentTable, $ids);
246e571e
JG
2304 if ($exportMode !== $this->getExportMode() || $componentTable !== $this->getComponentTable()) {
2305 CRM_Core_Error::deprecatedFunctionWarning('altering the export mode and/or component table in the hook is no longer supported.');
2306 }
ca934663
SL
2307 if ($ids !== $this->getIds()) {
2308 CRM_Core_Error::deprecatedFunctionWarning('altering the ids in the hook is no longer supported.');
2309 }
b6149515 2310 if ($exportTempTable !== $this->getTemporaryTable()) {
2311 CRM_Core_Error::deprecatedFunctionWarning('altering the export table in the hook is deprecated (in some flows the table itself will be)');
2312 $this->setTemporaryTable($exportTempTable);
2313 }
2314 $exportTempTable = $this->getTemporaryTable();
2315 $writeHeader = TRUE;
2316 $offset = 0;
2317 // increase this number a lot to avoid making too many queries
2318 // LIMIT is not much faster than a no LIMIT query
2319 // CRM-7675
2320 $limit = 100000;
2321
2322 $query = "SELECT * FROM $exportTempTable";
2323
e2ea63f5 2324 $this->instantiateTempTable($headerRows);
b6149515 2325 while (1) {
2326 $limitQuery = $query . "
2327LIMIT $offset, $limit
2328";
2329 $dao = CRM_Core_DAO::executeQuery($limitQuery);
2330
2331 if ($dao->N <= 0) {
2332 break;
2333 }
2334
2335 $componentDetails = [];
2336 while ($dao->fetch()) {
2337 $row = [];
2338
246e571e 2339 foreach (array_keys($sqlColumns) as $column) {
b6149515 2340 $row[$column] = $dao->$column;
2341 }
2342 $componentDetails[] = $row;
2343 }
e2ea63f5 2344 $this->writeRows($headerRows, $componentDetails);
2345
b6149515 2346 $offset += $limit;
2347 }
2348 }
2349
e2ea63f5 2350 /**
2351 * Set up the temp table.
2352 *
2353 * @param array $headerRows
2354 */
2355 protected function instantiateTempTable(array $headerRows) {
2356 CRM_Utils_System::download(CRM_Utils_String::munge($this->getExportFileName()),
2357 'text/x-csv',
2358 CRM_Core_DAO::$_nullObject,
2359 'csv',
2360 FALSE
2361 );
1fc9f4a7 2362 // Output UTF BOM so that MS Excel copes with diacritics. This is recommended as
2363 // the Windows variant but is tested with MS Excel for Mac (Office 365 v 16.31)
2364 // and it continues to work on Libre Office, Numbers, Notes etc.
2365 echo "\xEF\xBB\xBF";
a83ea1d0 2366 CRM_Core_Report_Excel::makeCSVTable($headerRows, [], TRUE);
e2ea63f5 2367 }
2368
2369 /**
1493ea37 2370 * Write rows to the csv.
2371 *
e2ea63f5 2372 * @param array $headerRows
1493ea37 2373 * @param array $rows
e2ea63f5 2374 */
1493ea37 2375 protected function writeRows(array $headerRows, array $rows) {
2376 if (!empty($rows)) {
2377 CRM_Core_Report_Excel::makeCSVTable($headerRows, $rows, FALSE);
2378 }
e2ea63f5 2379 }
2380
cf7c5cb8 2381 /**
2382 * Cache the greeting fields for the given contact.
2383 *
2384 * @param int $contactID
2385 */
2386 protected function cacheContactGreetings(int $contactID) {
2387 if (!isset($this->contactGreetingFields[$contactID])) {
2388 $this->contactGreetingFields[$contactID] = $this->replaceMergeTokens($contactID);
2389 }
2390 }
2391
2392 /**
2393 * Get the greeting value for the given contact.
2394 *
2395 * The values have already been cached so we are grabbing the value at this point.
2396 *
2397 * @param int $contactID
2398 * @param string $type
2399 * postal_greeting|addressee|email_greeting
2400 * @param string $default
2401 *
2402 * @return string
2403 */
2404 protected function getContactGreeting(int $contactID, string $type, string $default) {
2405 return CRM_Utils_Array::value($type,
2406 $this->contactGreetingFields[$contactID], $default
2407 );
2408 }
2409
d183e23a 2410 /**
2411 * Get the portion of the greeting string that relates to the contact.
2412 *
2413 * For example if the greeting id 'Dear Sarah' we are going to combine it with 'Dear Mike'
2414 * so we want to strip the 'Dear ' and just get 'Sarah
2415 * @param int $contactID
2416 * @param int $greetingID
2417 * @param string $type
2418 * postal_greeting, addressee (email_greeting not currently implemented for unknown reasons.
2419 * @param string $defaultGreeting
2420 *
2421 * @return mixed|string
2422 */
2423 protected function getContactPortionOfGreeting(int $contactID, int $greetingID, string $type, string $defaultGreeting) {
2424 $copyPostalGreeting = $this->getContactGreeting($contactID, $type, $defaultGreeting);
2425 $template = $type === 'postal_greeting' ? $this->getPostalGreetingTemplate() : $this->getAddresseeGreetingTemplate();
2426 if ($copyPostalGreeting) {
2427 $copyPostalGreeting = $this->trimNonTokensFromAddressString($copyPostalGreeting,
2428 $this->greetingOptions[$type][$greetingID],
2429 $template
2430 );
2431 }
2432 return $copyPostalGreeting;
2433 }
2434
6003a964 2435}