-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
Copy pathExportProcessor.php
2431 lines (2208 loc) · 77.7 KB
/
ExportProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
/**
* Class CRM_Export_BAO_ExportProcessor
*
* Class to handle logic of export.
*/
class CRM_Export_BAO_ExportProcessor {
/**
* @var int
*/
protected $queryMode;
/**
* @var int
*/
protected $exportMode;
/**
* Array of fields in the main query.
*
* @var array
*/
protected $queryFields = [];
/**
* Either AND or OR.
*
* @var string
*/
protected $queryOperator;
/**
* Requested output fields.
*
* If set to NULL then it is 'primary fields only'
* which actually means pretty close to all fields!
*
* @var array|null
*/
protected $requestedFields;
/**
* Is the contact being merged into a single household.
*
* @var bool
*/
protected $isMergeSameHousehold;
/**
* Should contacts with the same address be merged.
*
* @var bool
*/
protected $isMergeSameAddress = FALSE;
/**
* Fields that need to be retrieved for address merge purposes but should not be in output.
*
* @var array
*/
protected $additionalFieldsForSameAddressMerge = [];
/**
* Fields used for merging same contacts.
*
* @var array
*/
protected $contactGreetingFields = [];
/**
* An array of primary IDs of the entity being exported.
*
* @var array
*/
protected $ids = [];
/**
* Greeting options mapping to various greeting ids.
*
* This stores the option values for the addressee, postal_greeting & email_greeting
* option groups.
*
* @var array
*/
protected $greetingOptions = [];
/**
* Get additional non-visible fields for address merge purposes.
*
* @return array
*/
public function getAdditionalFieldsForSameAddressMerge(): array {
return $this->additionalFieldsForSameAddressMerge;
}
/**
* Set additional non-visible fields for address merge purposes.
*/
public function setAdditionalFieldsForSameAddressMerge() {
if ($this->isMergeSameAddress) {
$fields = ['id', 'master_id', 'state_province_id', 'postal_greeting_id', 'addressee_id'];
foreach ($fields as $index => $field) {
if (!empty($this->getReturnProperties()[$field])) {
unset($fields[$index]);
}
}
$this->additionalFieldsForSameAddressMerge = array_fill_keys($fields, 1);
}
}
/**
* Should contacts with the same address be merged.
*
* @return bool
*/
public function isMergeSameAddress(): bool {
return $this->isMergeSameAddress;
}
/**
* Set same address is to be merged.
*
* @param bool $isMergeSameAddress
*/
public function setIsMergeSameAddress(bool $isMergeSameAddress) {
$this->isMergeSameAddress = $isMergeSameAddress;
}
/**
* Additional fields required to export postal fields.
*
* @var array
*/
protected $additionalFieldsForPostalExport = [];
/**
* Get additional fields required to do a postal export.
*
* @return array
*/
public function getAdditionalFieldsForPostalExport() {
return $this->additionalFieldsForPostalExport;
}
/**
* Set additional fields required for a postal export.
*/
public function setAdditionalFieldsForPostalExport() {
if ($this->getRequestedFields() && $this->isPostalableOnly()) {
$fields = ['is_deceased', 'do_not_mail', 'street_address', 'supplemental_address_1'];
foreach ($fields as $index => $field) {
if (!empty($this->getReturnProperties()[$field])) {
unset($fields[$index]);
}
}
$this->additionalFieldsForPostalExport = array_fill_keys($fields, 1);
}
}
/**
* Only export contacts that can receive postal mail.
*
* Includes being alive, having an address & not having do_not_mail.
*
* @var bool
*/
protected $isPostalableOnly;
/**
* Key representing the head of household in the relationship array.
*
* e.g. ['8_b_a' => 'Household Member Is', '8_a_b = 'Household Member Of'.....]
*
* @var array
*/
protected $relationshipTypes = [];
/**
* Array of properties to retrieve for relationships.
*
* @var array
*/
protected $relationshipReturnProperties = [];
/**
* IDs of households that have already been exported.
*
* @var array
*/
protected $exportedHouseholds = [];
/**
* Contacts to be merged by virtue of their shared address.
*
* @var array
*/
protected $contactsToMerge = [];
/**
* Households to skip during export as they will be exported via their relationships anyway.
*
* @var array
*/
protected $householdsToSkip = [];
/**
* Additional fields to return.
*
* This doesn't make much sense when we have a fields set but search build add it's own onto
* the 'Primary fields' (all) option.
*
* @var array
*/
protected $additionalRequestedReturnProperties = [];
/**
* Get additional return properties.
*
* @return array
*/
public function getAdditionalRequestedReturnProperties() {
return $this->additionalRequestedReturnProperties;
}
/**
* Set additional return properties.
*
* @param array $value
*/
public function setAdditionalRequestedReturnProperties($value) {
// fix for CRM-7066
if (!empty($value['group'])) {
unset($value['group']);
$value['groups'] = 1;
}
$this->additionalRequestedReturnProperties = $value;
}
/**
* Get return properties by relationship.
* @return array
*/
public function getRelationshipReturnProperties() {
return $this->relationshipReturnProperties;
}
/**
* Export values for related contacts.
*
* @var array
*/
protected $relatedContactValues = [];
/**
* @var array
*/
protected $returnProperties = [];
/**
* @var array
*/
protected $outputSpecification = [];
/**
* @var string
*/
protected $componentTable = '';
/**
* @return string
*/
public function getComponentTable() {
return $this->componentTable;
}
/**
* Set the component table (if any).
*
* @param string $componentTable
*/
public function setComponentTable($componentTable) {
$this->componentTable = $componentTable;
}
/**
* Clause from component search.
*
* @var string
*/
protected $componentClause = '';
/**
* @return string
*/
public function getComponentClause() {
return $this->componentClause;
}
/**
* @param string $componentClause
*/
public function setComponentClause($componentClause) {
$this->componentClause = $componentClause;
}
/**
* Name of a temporary table created to hold the results.
*
* Current decision making on when to create a temp table is kinda bad so this might change
* a bit as it is reviewed but basically we need a temp table or similar to calculate merging
* addresses. Merging households is handled in php. We create a temp table even when we don't need them.
*
* @var string
*/
protected $temporaryTable;
/**
* @return string
*/
public function getTemporaryTable(): string {
return $this->temporaryTable;
}
/**
* @param string $temporaryTable
*/
public function setTemporaryTable(string $temporaryTable) {
$this->temporaryTable = $temporaryTable;
}
protected $postalGreetingTemplate;
/**
* @return mixed
*/
public function getPostalGreetingTemplate() {
return $this->postalGreetingTemplate;
}
/**
* @param mixed $postalGreetingTemplate
*/
public function setPostalGreetingTemplate($postalGreetingTemplate) {
$this->postalGreetingTemplate = $postalGreetingTemplate;
}
/**
* @return mixed
*/
public function getAddresseeGreetingTemplate() {
return $this->addresseeGreetingTemplate;
}
/**
* @param mixed $addresseeGreetingTemplate
*/
public function setAddresseeGreetingTemplate($addresseeGreetingTemplate) {
$this->addresseeGreetingTemplate = $addresseeGreetingTemplate;
}
protected $addresseeGreetingTemplate;
/**
* CRM_Export_BAO_ExportProcessor constructor.
*
* @param int $exportMode
* @param array|null $requestedFields
* @param string $queryOperator
* @param bool $isMergeSameHousehold
* @param bool $isPostalableOnly
* @param bool $isMergeSameAddress
* @param array $formValues
* Values from the export options form on contact export. We currently support these keys
* - postal_greeting
* - postal_other
* - addresee_greeting
* - addressee_other
*/
public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE, $isPostalableOnly = FALSE, $isMergeSameAddress = FALSE, $formValues = []) {
$this->setExportMode((int) $exportMode);
$this->setQueryMode();
$this->setQueryOperator($queryOperator);
$this->setRequestedFields($requestedFields);
$this->setRelationshipTypes();
$this->setIsMergeSameHousehold($isMergeSameHousehold || $isMergeSameAddress);
$this->setIsPostalableOnly($isPostalableOnly);
$this->setIsMergeSameAddress($isMergeSameAddress);
$this->setReturnProperties($this->determineReturnProperties());
$this->setAdditionalFieldsForSameAddressMerge();
$this->setAdditionalFieldsForPostalExport();
$this->setHouseholdMergeReturnProperties();
$this->setGreetingStringsForSameAddressMerge($formValues);
$this->setGreetingOptions();
}
/**
* Set the greeting options, if relevant.
*/
public function setGreetingOptions() {
if ($this->isMergeSameAddress()) {
$this->greetingOptions['addressee'] = CRM_Core_OptionGroup::values('addressee');
$this->greetingOptions['postal_greeting'] = CRM_Core_OptionGroup::values('postal_greeting');
}
}
/**
* @return bool
*/
public function isPostalableOnly() {
return $this->isPostalableOnly;
}
/**
* @param bool $isPostalableOnly
*/
public function setIsPostalableOnly($isPostalableOnly) {
$this->isPostalableOnly = $isPostalableOnly;
}
/**
* @return array|null
*/
public function getRequestedFields() {
return empty($this->requestedFields) ? NULL : $this->requestedFields;
}
/**
* @param array|null $requestedFields
*/
public function setRequestedFields($requestedFields) {
$this->requestedFields = $requestedFields;
}
/**
* @return array
*/
public function getReturnProperties() {
return array_merge($this->returnProperties, $this->getAdditionalRequestedReturnProperties(), $this->getAdditionalFieldsForSameAddressMerge(), $this->getAdditionalFieldsForPostalExport());
}
/**
* @param array $returnProperties
*/
public function setReturnProperties($returnProperties) {
$this->returnProperties = $returnProperties;
}
/**
* @return array
*/
public function getRelationshipTypes() {
return $this->relationshipTypes;
}
/**
*/
public function setRelationshipTypes() {
$this->relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(
NULL,
NULL,
NULL,
NULL,
TRUE,
'name',
FALSE
);
}
/**
* Set the value for a relationship type field.
*
* In this case we are building up an array of properties for a related contact.
*
* These may be used for direct exporting or for merge to household depending on the
* options selected.
*
* @param string $relationshipType
* @param int $contactID
* @param string $field
* @param string $value
*/
public function setRelationshipValue($relationshipType, $contactID, $field, $value) {
$this->relatedContactValues[$relationshipType][$contactID][$field] = $value;
if ($field === 'id' && $this->isHouseholdMergeRelationshipTypeKey($relationshipType)) {
$this->householdsToSkip[] = $value;
}
}
/**
* Get the value for a relationship type field.
*
* In this case we are building up an array of properties for a related contact.
*
* These may be used for direct exporting or for merge to household depending on the
* options selected.
*
* @param string $relationshipType
* @param int $contactID
* @param string $field
*
* @return string
*/
public function getRelationshipValue($relationshipType, $contactID, $field) {
return $this->relatedContactValues[$relationshipType][$contactID][$field] ?? '';
}
/**
* Get the id of the related household.
*
* @param int $contactID
* @param string $relationshipType
*
* @return int
*/
public function getRelatedHouseholdID($contactID, $relationshipType) {
return $this->relatedContactValues[$relationshipType][$contactID]['id'];
}
/**
* Has the household already been exported.
*
* @param int $housholdContactID
*
* @return bool
*/
public function isHouseholdExported($housholdContactID) {
return isset($this->exportedHouseholds[$housholdContactID]);
}
/**
* @return bool
*/
public function isMergeSameHousehold() {
return $this->isMergeSameHousehold;
}
/**
* @param bool $isMergeSameHousehold
*/
public function setIsMergeSameHousehold($isMergeSameHousehold) {
$this->isMergeSameHousehold = $isMergeSameHousehold;
}
/**
* Return relationship types for household merge.
*
* @return mixed
*/
public function getHouseholdRelationshipTypes() {
if (!$this->isMergeSameHousehold()) {
return [];
}
return [
CRM_Utils_Array::key('Household Member of', $this->getRelationshipTypes()),
CRM_Utils_Array::key('Head of Household for', $this->getRelationshipTypes()),
];
}
/**
* @param $fieldName
* @return bool
*/
public function isRelationshipTypeKey($fieldName) {
return array_key_exists($fieldName, $this->relationshipTypes);
}
/**
* @param $fieldName
* @return bool
*/
public function isHouseholdMergeRelationshipTypeKey($fieldName) {
return in_array($fieldName, $this->getHouseholdRelationshipTypes());
}
/**
* @return string
*/
public function getQueryOperator() {
return $this->queryOperator;
}
/**
* @param string $queryOperator
*/
public function setQueryOperator($queryOperator) {
$this->queryOperator = $queryOperator;
}
/**
* @return array
*/
public function getIds() {
return $this->ids;
}
/**
* @param array $ids
*/
public function setIds($ids) {
$this->ids = $ids;
}
/**
* @return array
*/
public function getQueryFields() {
return array_merge(
$this->queryFields,
$this->getComponentPaymentFields()
);
}
/**
* @param array $queryFields
*/
public function setQueryFields($queryFields) {
// legacy hacks - we add these to queryFields because this
// pseudometadata is currently required.
$queryFields['im_provider']['pseudoconstant']['var'] = 'imProviders';
$queryFields['country']['context'] = 'country';
$queryFields['world_region']['context'] = 'country';
$queryFields['state_province']['context'] = 'province';
$queryFields['contact_id'] = ['title' => ts('Contact ID'), 'type' => CRM_Utils_Type::T_INT];
$queryFields['tags']['type'] = CRM_Utils_Type::T_LONGTEXT;
$queryFields['groups']['type'] = CRM_Utils_Type::T_LONGTEXT;
$queryFields['notes']['type'] = CRM_Utils_Type::T_LONGTEXT;
// Set the label to gender for gender_id as we it's ... magic (not in a good way).
// In other places the query object offers e.g contribution_status & contribution_status_id
$queryFields['gender_id']['title'] = ts('Gender');
$this->queryFields = $queryFields;
}
/**
* @return int
*/
public function getQueryMode() {
return $this->queryMode;
}
/**
* Set the query mode based on the export mode.
*/
public function setQueryMode() {
switch ($this->getExportMode()) {
case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
$this->queryMode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE;
break;
case CRM_Export_Form_Select::EVENT_EXPORT:
$this->queryMode = CRM_Contact_BAO_Query::MODE_EVENT;
break;
case CRM_Export_Form_Select::MEMBER_EXPORT:
$this->queryMode = CRM_Contact_BAO_Query::MODE_MEMBER;
break;
case CRM_Export_Form_Select::PLEDGE_EXPORT:
$this->queryMode = CRM_Contact_BAO_Query::MODE_PLEDGE;
break;
case CRM_Export_Form_Select::CASE_EXPORT:
$this->queryMode = CRM_Contact_BAO_Query::MODE_CASE;
break;
case CRM_Export_Form_Select::GRANT_EXPORT:
$this->queryMode = CRM_Contact_BAO_Query::MODE_GRANT;
break;
case CRM_Export_Form_Select::ACTIVITY_EXPORT:
$this->queryMode = CRM_Contact_BAO_Query::MODE_ACTIVITY;
break;
default:
$this->queryMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
}
}
/**
* @return int
*/
public function getExportMode(): int {
return $this->exportMode;
}
/**
* @param int $exportMode
*/
public function setExportMode(int $exportMode) {
$this->exportMode = $exportMode;
}
/**
* Get the name for the export file.
*
* @return string
*/
public function getExportFileName() {
switch ($this->getExportMode()) {
case CRM_Export_Form_Select::CONTACT_EXPORT:
return ts('CiviCRM Contact Search');
case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
return ts('CiviCRM Contribution Search');
case CRM_Export_Form_Select::MEMBER_EXPORT:
return ts('CiviCRM Member Search');
case CRM_Export_Form_Select::EVENT_EXPORT:
return ts('CiviCRM Participant Search');
case CRM_Export_Form_Select::PLEDGE_EXPORT:
return ts('CiviCRM Pledge Search');
case CRM_Export_Form_Select::CASE_EXPORT:
return ts('CiviCRM Case Search');
case CRM_Export_Form_Select::GRANT_EXPORT:
return ts('CiviCRM Grant Search');
case CRM_Export_Form_Select::ACTIVITY_EXPORT:
return ts('CiviCRM Activity Search');
default:
// Legacy code suggests the value could be 'financial' - ie. something
// other than what should be accepted. However, I suspect that this line is
// never hit.
return ts('CiviCRM Search');
}
}
/**
* Get the label for the header row based on the field to output.
*
* @param string $field
*
* @return string
*/
public function getHeaderForRow($field) {
if (substr($field, -11) === 'campaign_id') {
// @todo - set this correctly in the xml rather than here.
// This will require a generalised handling cleanup
return ts('Campaign ID');
}
if ($this->isMergeSameHousehold() && !$this->isMergeSameAddress() && $field === 'id') {
// This is weird - even if we are merging households not every contact in the export is a household so this would not be accurate.
return ts('Household ID');
}
elseif (isset($this->getQueryFields()[$field]['title'])) {
return $this->getQueryFields()[$field]['title'];
}
elseif ($this->isExportPaymentFields() && array_key_exists($field, $this->getcomponentPaymentFields())) {
return CRM_Utils_Array::value($field, $this->getcomponentPaymentFields())['title'];
}
else {
return $field;
}
}
/**
* @param $params
* @param $order
*
* @return array
*/
public function runQuery($params, $order) {
$returnProperties = $this->getReturnProperties();
$params = array_merge($params, $this->getWhereParams());
$query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
FALSE, FALSE, $this->getQueryMode(),
FALSE, TRUE, TRUE, NULL, $this->getQueryOperator(),
NULL, TRUE
);
//sort by state
//CRM-15301
$query->_sort = $order;
[$select, $from, $where, $having] = $query->query();
$this->setQueryFields($query->_fields);
$whereClauses = ['trash_clause' => "contact_a.is_deleted != 1"];
if ($this->getComponentClause()) {
$whereClauses[] = $this->getComponentClause();
}
elseif ($this->getRequestedFields() && $this->getComponentTable() && $this->getComponentTable() !== 'civicrm_contact') {
$from .= " INNER JOIN " . $this->getComponentTable() . " ctTable ON ctTable.contact_id = contact_a.id ";
}
// CRM-13982 - check if is deleted
foreach ($params as $value) {
if ($value[0] === 'contact_is_deleted') {
unset($whereClauses['trash_clause']);
}
}
if ($this->isPostalableOnly) {
if (array_key_exists('street_address', $returnProperties)) {
$addressWhere = " civicrm_address.street_address <> ''";
if (array_key_exists('supplemental_address_1', $returnProperties)) {
// We need this to be an OR rather than AND on the street_address so, hack it in.
$addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'address_options', TRUE, NULL, TRUE
);
if (!empty($addressOptions['supplemental_address_1'])) {
$addressWhere .= " OR civicrm_address.supplemental_address_1 <> ''";
}
}
$whereClauses['address'] = '(' . $addressWhere . ')';
}
}
if (empty($where)) {
$where = 'WHERE ' . implode(' AND ', $whereClauses);
}
else {
$where .= ' AND ' . implode(' AND ', $whereClauses);
}
$groupBy = $this->getGroupBy($query);
$queryString = "$select $from $where $having $groupBy";
if ($order) {
// always add contact_a.id to the ORDER clause
// so the order is deterministic
//CRM-15301
if (strpos('contact_a.id', $order) === FALSE) {
$order .= ", contact_a.id";
}
[$field, $dir] = explode(' ', $order, 2);
$field = trim($field);
if (!empty($this->getReturnProperties()[$field])) {
//CRM-15301
$queryString .= " ORDER BY $order";
}
}
return [$query, $queryString];
}
/**
* Add a row to the specification for how to output data.
*
* @param string $key
* @param string $relationshipType
* @param string $locationType
* @param int $entityTypeID phone_type_id or provider_id for phone or im fields.
*/
public function addOutputSpecification($key, $relationshipType = NULL, $locationType = NULL, $entityTypeID = NULL) {
$entityLabel = '';
if ($entityTypeID) {
if ($key === 'phone') {
$entityLabel = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Phone', 'phone_type_id', $entityTypeID);
}
if ($key === 'im') {
$entityLabel = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_IM', 'provider_id', $entityTypeID);
}
}
// These oddly constructed keys are for legacy reasons. Altering them will affect test success
// but in time it may be good to rationalise them.
$label = $this->getOutputSpecificationLabel($key, $relationshipType, $locationType, $entityLabel);
$index = $this->getOutputSpecificationIndex($key, $relationshipType, $locationType, $entityTypeID);
$fieldKey = $this->getOutputSpecificationFieldKey($key, $relationshipType, $locationType, $entityTypeID);
$this->outputSpecification[$index]['header'] = $label;
$this->outputSpecification[$index]['sql_columns'] = $this->getSqlColumnDefinition($fieldKey, $key);
if ($relationshipType && $this->isHouseholdMergeRelationshipTypeKey($relationshipType)) {
$this->setColumnAsCalculationOnly($index);
}
$this->outputSpecification[$index]['metadata'] = $this->getMetaDataForField($key);
}
/**
* Get the metadata for the given field.
*
* @param $key
*
* @return array
*/
public function getMetaDataForField($key) {
$mappings = ['contact_id' => 'id'];
if (isset($this->getQueryFields()[$key])) {
return $this->getQueryFields()[$key];
}
if (isset($mappings[$key])) {
return $this->getQueryFields()[$mappings[$key]];
}
return [];
}
/**
* @param $key
*/
public function setSqlColumnDefn($key) {
$this->outputSpecification[$this->getMungedFieldName($key)]['sql_columns'] = $this->getSqlColumnDefinition($key, $this->getMungedFieldName($key));
}
/**
* Mark a column as only required for calculations.
*
* Do not include the row with headers.
*
* @param string $column
*/
public function setColumnAsCalculationOnly($column) {
$this->outputSpecification[$column]['do_not_output_to_csv'] = TRUE;
}
/**
* @return array
*/
public function getHeaderRows() {
$headerRows = [];
foreach ($this->outputSpecification as $key => $spec) {
if (empty($spec['do_not_output_to_csv'])) {
$headerRows[] = $spec['header'];
}
}
return $headerRows;
}
/**
* @return array
*/
public function getSQLColumns() {
$sqlColumns = [];
foreach ($this->outputSpecification as $key => $spec) {
if (empty($spec['do_not_output_to_sql'])) {
$sqlColumns[$key] = $spec['sql_columns'];
}
}
return $sqlColumns;
}
/**
* @return array
*/
public function getMetadata() {
$metadata = [];
foreach ($this->outputSpecification as $key => $spec) {
$metadata[$key] = $spec['metadata'];
}
return $metadata;
}
/**
* Build the row for output.
*
* @param \CRM_Contact_BAO_Query $query
* @param CRM_Core_DAO $iterationDAO
* @param array $outputColumns
* @param $paymentDetails
* @param $addPaymentHeader
*
* @return array|bool
*/
public function buildRow($query, $iterationDAO, $outputColumns, $paymentDetails, $addPaymentHeader) {
$paymentTableId = $this->getPaymentTableID();
if ($this->isHouseholdToSkip($iterationDAO->contact_id)) {
return FALSE;
}
$imProviders = CRM_Core_DAO_IM::buildOptions('provider_id');
$row = [];
$householdMergeRelationshipType = $this->getHouseholdMergeTypeForRow($iterationDAO->contact_id);
if ($householdMergeRelationshipType) {
$householdID = $this->getRelatedHouseholdID($iterationDAO->contact_id, $householdMergeRelationshipType);
if ($this->isHouseholdExported($householdID)) {
return FALSE;
}
foreach (array_keys($outputColumns) as $column) {
$row[$column] = $this->getRelationshipValue($householdMergeRelationshipType, $iterationDAO->contact_id, $column);
}
$this->markHouseholdExported($householdID);
return $row;
}
$query->convertToPseudoNames($iterationDAO);
//first loop through output columns so that we return what is required, and in same order.
foreach ($outputColumns as $field => $value) {
// add im_provider to $dao object