Fix issues found by Dave in testing
[civicrm-core.git] / CRM / Export / BAO / Export.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
748450ad 37 * This class contains the functions for Component export
6a488035
TO
38 *
39 */
40class CRM_Export_BAO_Export {
41 // increase this number a lot to avoid making too many queries
42 // LIMIT is not much faster than a no LIMIT query
43 // CRM-7675
7da04cde 44 const EXPORT_ROW_COUNT = 10000;
6a488035 45
f34f5143
SL
46 /**
47 * Get Querymode based on ExportMode
48 *
49 * @param int $exportMode
50 * Export mode.
51 *
748450ad
SL
52 * @return string $Querymode
53 * Query Mode
f34f5143 54 */
919eddec 55 public static function getQueryMode($exportMode) {
b72702ac
SL
56 $queryMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
57
f34f5143
SL
58 switch ($exportMode) {
59 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
60 $queryMode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE;
61 break;
62
63 case CRM_Export_Form_Select::EVENT_EXPORT:
64 $queryMode = CRM_Contact_BAO_Query::MODE_EVENT;
65 break;
66
67 case CRM_Export_Form_Select::MEMBER_EXPORT:
68 $queryMode = CRM_Contact_BAO_Query::MODE_MEMBER;
69 break;
70
71 case CRM_Export_Form_Select::PLEDGE_EXPORT:
72 $queryMode = CRM_Contact_BAO_Query::MODE_PLEDGE;
73 break;
74
75 case CRM_Export_Form_Select::CASE_EXPORT:
76 $queryMode = CRM_Contact_BAO_Query::MODE_CASE;
77 break;
78
79 case CRM_Export_Form_Select::GRANT_EXPORT:
80 $queryMode = CRM_Contact_BAO_Query::MODE_GRANT;
81 break;
82
83 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
84 $queryMode = CRM_Contact_BAO_Query::MODE_ACTIVITY;
85 break;
86 }
87 return $queryMode;
88 }
89
90 /**
91 * Get default return property for export based on mode
92 *
93 * @param int $exportMode
94 * Export mode.
919eddec 95 *
748450ad
SL
96 * @return string $property
97 * Default Return property
f34f5143 98 */
2e28a8b9 99 public static function defaultReturnProperty($exportMode) {
748450ad 100 // hack to add default return property based on export mode
f34f5143 101 if ($exportMode == CRM_Export_Form_Select::CONTRIBUTE_EXPORT) {
919eddec 102 $property = 'contribution_id';
f34f5143
SL
103 }
104 elseif ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) {
919eddec 105 $property = 'participant_id';
f34f5143
SL
106 }
107 elseif ($exportMode == CRM_Export_Form_Select::MEMBER_EXPORT) {
919eddec 108 $property = 'membership_id';
f34f5143
SL
109 }
110 elseif ($exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT) {
919eddec 111 $property = 'pledge_id';
f34f5143
SL
112 }
113 elseif ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
919eddec 114 $property = 'case_id';
f34f5143
SL
115 }
116 elseif ($exportMode == CRM_Export_Form_Select::GRANT_EXPORT) {
919eddec 117 $property = 'grant_id';
f34f5143
SL
118 }
119 elseif ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
919eddec 120 $property = 'activity_id';
f34f5143 121 }
c339dbc0 122 elseif ($exportMode == CRM_Export_Form_Select::CONTACT_EXPORT) {
123 $property = 'contact_id';
124 }
919eddec 125 return $property;
f34f5143
SL
126 }
127
128 /**
129 * Get Export component
130 *
131 * @param int $exportMode
132 * Export mode.
133 *
748450ad
SL
134 * @return string $component
135 * CiviCRM Export Component
f34f5143 136 */
7bf9f91e 137 public static function exportComponent($exportMode) {
f34f5143
SL
138 switch ($exportMode) {
139 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
140 $component = 'civicrm_contribution';
141 break;
142
143 case CRM_Export_Form_Select::EVENT_EXPORT:
144 $component = 'civicrm_participant';
145 break;
146
147 case CRM_Export_Form_Select::MEMBER_EXPORT:
148 $component = 'civicrm_membership';
149 break;
150
151 case CRM_Export_Form_Select::PLEDGE_EXPORT:
152 $component = 'civicrm_pledge';
153 break;
154
155 case CRM_Export_Form_Select::GRANT_EXPORT:
156 $component = 'civicrm_grant';
157 break;
158 }
159 return $component;
160 }
161
748450ad 162 /**
f1ff3965 163 * Get Query Group By Clause
748450ad
SL
164 * @param int $exportMode
165 * Export Mode
166 * @param string $queryMode
167 * Query Mode
168 * @param array $returnProperties
169 * Return Properties
0cc9927d
SL
170 * @param object $query
171 * CRM_Contact_BAO_Query
172 *
748450ad
SL
173 * @return string $groupBy
174 * Group By Clause
175 */
0cc9927d 176 public static function getGroupBy($exportMode, $queryMode, $returnProperties, $query) {
748450ad
SL
177 if (!empty($returnProperties['tags']) || !empty($returnProperties['groups']) ||
178 CRM_Utils_Array::value('notes', $returnProperties) ||
179 // CRM-9552
180 ($queryMode & CRM_Contact_BAO_Query::MODE_CONTACTS && $query->_useGroupBy)
181 ) {
182 $groupBy = " GROUP BY contact_a.id";
183 }
184
185 switch ($exportMode) {
186 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
187 $groupBy = 'GROUP BY civicrm_contribution.id';
188 if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) {
189 // especial group by when soft credit columns are included
190 $groupBy = 'GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.scredit_id';
191 }
192 break;
193
194 case CRM_Export_Form_Select::EVENT_EXPORT:
195 $groupBy = 'GROUP BY civicrm_participant.id';
196 break;
197
198 case CRM_Export_Form_Select::MEMBER_EXPORT:
199 $groupBy = " GROUP BY civicrm_membership.id";
200 break;
201 }
202
203 if ($queryMode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
204 $groupBy = " GROUP BY civicrm_activity.id ";
205 }
0cc9927d
SL
206
207 $groupBy = !empty($groupBy) ? $groupBy : '';
208
748450ad
SL
209 return $groupBy;
210 }
211
212 /**
213 * Define extra properties for the export based on query mode
214 *
215 * @param string $queryMode
216 * Query Mode
217 * @return array $extraProperties
218 * Extra Properties
219 */
220 public static function defineExtraProperties($queryMode) {
221 switch ($queryMode) {
222 case CRM_Contact_BAO_Query::MODE_EVENT:
223 $paymentFields = TRUE;
224 $paymentTableId = 'participant_id';
225 $extraReturnProperties = array();
226 break;
227
228 case CRM_Contact_BAO_Query::MODE_MEMBER:
229 $paymentFields = TRUE;
230 $paymentTableId = 'membership_id';
231 $extraReturnProperties = array();
232 break;
233
234 case CRM_Contact_BAO_Query::MODE_PLEDGE:
235 $extraReturnProperties = CRM_Pledge_BAO_Query::extraReturnProperties($queryMode);
236 $paymentFields = TRUE;
237 $paymentTableId = 'pledge_payment_id';
238 break;
239
240 case CRM_Contact_BAO_Query::MODE_CASE:
241 $extraReturnProperties = CRM_Case_BAO_Query::extraReturnProperties($queryMode);
242 $paymentFields = FALSE;
243 $paymentTableID = '';
244 break;
245
246 default:
247 $paymentFields = FALSE;
248 $paymentTableID = '';
249 $extraReturnProperties = array();
250 }
251 $extraProperties = array(
252 'paymentFields' => $paymentFields,
253 'paymentTableID' => $paymentTableId,
254 'extraReturnProperties' => $extraReturnProperties,
255 );
256 return $extraProperties;
257 }
258
6a488035 259 /**
fe482240 260 * Get the list the export fields.
6a488035 261 *
b9add4b3
TO
262 * @param int $selectAll
263 * User preference while export.
264 * @param array $ids
265 * Contact ids.
266 * @param array $params
267 * Associated array of fields.
268 * @param string $order
269 * Order by clause.
270 * @param array $fields
271 * Associated array of fields.
272 * @param array $moreReturnProperties
273 * Additional return fields.
274 * @param int $exportMode
275 * Export mode.
276 * @param string $componentClause
277 * Component clause.
278 * @param string $componentTable
279 * Component table.
280 * @param bool $mergeSameAddress
281 * Merge records if they have same address.
282 * @param bool $mergeSameHousehold
283 * Merge records if they belong to the same household.
6c8f6e67
EM
284 *
285 * @param array $exportParams
286 * @param string $queryOperator
6a488035 287 *
6a488035 288 */
317fceb4 289 public static function exportComponents(
97f6897c 290 $selectAll,
6a488035
TO
291 $ids,
292 $params,
293 $order = NULL,
294 $fields = NULL,
295 $moreReturnProperties = NULL,
296 $exportMode = CRM_Export_Form_Select::CONTACT_EXPORT,
297 $componentClause = NULL,
298 $componentTable = NULL,
299 $mergeSameAddress = FALSE,
300 $mergeSameHousehold = FALSE,
301 $exportParams = array(),
302 $queryOperator = 'AND'
303 ) {
304 $headerRows = $returnProperties = array();
97f6897c 305 $primary = $paymentFields = $selectedPaymentFields = FALSE;
6a488035 306 $origFields = $fields;
55f37718 307 $relationField = NULL;
6a488035 308
b4f964d9 309 $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
e7e657f0 310 $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
6a488035
TO
311 $contactRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(
312 NULL,
313 NULL,
314 NULL,
315 NULL,
316 TRUE,
545285b8 317 'name',
6a488035
TO
318 FALSE
319 );
6a488035 320
f34f5143 321 $queryMode = self::getQueryMode($exportMode);
919eddec 322
6a488035
TO
323 if ($fields) {
324 //construct return properties
b2b0530a 325 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
326 $locationTypeFields = array(
327 'street_address',
328 'supplemental_address_1',
329 'supplemental_address_2',
330 'city',
331 'postal_code',
332 'postal_code_suffix',
333 'geo_code_1',
334 'geo_code_2',
335 'state_province',
336 'country',
337 'phone',
338 'email',
339 'im',
340 );
341
342 foreach ($fields as $key => $value) {
343 $phoneTypeId = $imProviderId = NULL;
344 $relationshipTypes = $fieldName = CRM_Utils_Array::value(1, $value);
345 if (!$fieldName) {
346 continue;
347 }
b44e3f84 348 // get phoneType id and IM service provider id separately
6a488035
TO
349 if ($fieldName == 'phone') {
350 $phoneTypeId = CRM_Utils_Array::value(3, $value);
351 }
352 elseif ($fieldName == 'im') {
353 $imProviderId = CRM_Utils_Array::value(3, $value);
354 }
6a488035
TO
355
356 if (array_key_exists($relationshipTypes, $contactRelationshipTypes)) {
a7488080 357 if (!empty($value[2])) {
6a488035
TO
358 $relationField = CRM_Utils_Array::value(2, $value);
359 if (trim(CRM_Utils_Array::value(3, $value))) {
360 $relLocTypeId = CRM_Utils_Array::value(3, $value);
361 }
362 else {
363 $relLocTypeId = 'Primary';
364 }
365
366 if ($relationField == 'phone') {
367 $relPhoneTypeId = CRM_Utils_Array::value(4, $value);
368 }
369 elseif ($relationField == 'im') {
370 $relIMProviderId = CRM_Utils_Array::value(4, $value);
371 }
372 }
a7488080 373 elseif (!empty($value[4])) {
6a488035
TO
374 $relationField = CRM_Utils_Array::value(4, $value);
375 $relLocTypeId = CRM_Utils_Array::value(5, $value);
376 if ($relationField == 'phone') {
377 $relPhoneTypeId = CRM_Utils_Array::value(6, $value);
378 }
379 elseif ($relationField == 'im') {
380 $relIMProviderId = CRM_Utils_Array::value(6, $value);
381 }
382 }
383 }
384
385 $contactType = CRM_Utils_Array::value(0, $value);
386 $locTypeId = CRM_Utils_Array::value(2, $value);
387
388 if ($relationField) {
389 if (in_array($relationField, $locationTypeFields) && is_numeric($relLocTypeId)) {
390 if ($relPhoneTypeId) {
391 $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['phone-' . $relPhoneTypeId] = 1;
392 }
393 elseif ($relIMProviderId) {
394 $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['im-' . $relIMProviderId] = 1;
395 }
396 else {
397 $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]][$relationField] = 1;
398 }
399 $relPhoneTypeId = $relIMProviderId = NULL;
400 }
401 else {
402 $returnProperties[$relationshipTypes][$relationField] = 1;
403 }
404 }
405 elseif (is_numeric($locTypeId)) {
406 if ($phoneTypeId) {
407 $returnProperties['location'][$locationTypes[$locTypeId]]['phone-' . $phoneTypeId] = 1;
408 }
409 elseif ($imProviderId) {
410 $returnProperties['location'][$locationTypes[$locTypeId]]['im-' . $imProviderId] = 1;
411 }
412 else {
413 $returnProperties['location'][$locationTypes[$locTypeId]][$fieldName] = 1;
414 }
415 }
416 else {
417 //hack to fix component fields
8b8fa582 418 //revert mix of event_id and title
6a488035 419 if ($fieldName == 'event_id') {
8b8fa582 420 $returnProperties['event_id'] = 1;
6a488035 421 }
4c9b6178 422 elseif (
545285b8 423 $exportMode == CRM_Export_Form_Select::EVENT_EXPORT &&
97f6897c 424 array_key_exists($fieldName, self::componentPaymentFields())
545285b8 425 ) {
97f6897c
TO
426 $selectedPaymentFields = TRUE;
427 $paymentTableId = 'participant_id';
428 $returnProperties[$fieldName] = 1;
d77aba4b 429 }
6a488035
TO
430 else {
431 $returnProperties[$fieldName] = 1;
6a488035
TO
432 }
433 }
434 }
2e28a8b9 435 $returnProperties[self::defaultReturnProperty($exportMode)] = 1;
919eddec 436 }
6a488035
TO
437 else {
438 $primary = TRUE;
439 $fields = CRM_Contact_BAO_Contact::exportableFields('All', TRUE, TRUE);
440 foreach ($fields as $key => $var) {
441 if ($key && (substr($key, 0, 6) != 'custom')) {
442 //for CRM=952
443 $returnProperties[$key] = 1;
444 }
445 }
446
447 if ($primary) {
448 $returnProperties['location_type'] = 1;
449 $returnProperties['im_provider'] = 1;
450 $returnProperties['phone_type_id'] = 1;
451 $returnProperties['provider_id'] = 1;
452 $returnProperties['current_employer'] = 1;
453 }
454
455 $extraReturnProperties = array();
456 $paymentFields = FALSE;
457
f1ff3965 458 $extraProperties = self::defineExtraProperties($queryMode);
748450ad
SL
459 $paymentFields = $extraProperties['paymentFields'];
460 $extraReturnProperties = $extraProperties['extraReturnProperties'];
461 $paymentTableID = $extraProperties['paymentTableID'];
6a488035
TO
462
463 if ($queryMode != CRM_Contact_BAO_Query::MODE_CONTACTS) {
464 $componentReturnProperties = CRM_Contact_BAO_Query::defaultReturnProperties($queryMode);
81ec6180 465 if ($queryMode == CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
4f3846df 466 // soft credit columns are not automatically populated, because contribution search doesn't require them by default
317fceb4 467 $componentReturnProperties = array_merge(
6c8f6e67 468 $componentReturnProperties,
526bdd5e 469 CRM_Contribute_BAO_Query::softCreditReturnProperties(TRUE));
81ec6180 470 }
6a488035
TO
471 $returnProperties = array_merge($returnProperties, $componentReturnProperties);
472
473 if (!empty($extraReturnProperties)) {
474 $returnProperties = array_merge($returnProperties, $extraReturnProperties);
475 }
476
477 // unset non exportable fields for components
478 $nonExpoFields = array(
97f6897c
TO
479 'groups',
480 'tags',
481 'notes',
482 'contribution_status_id',
483 'pledge_status_id',
484 'pledge_payment_status_id',
6a488035
TO
485 );
486 foreach ($nonExpoFields as $value) {
487 unset($returnProperties[$value]);
488 }
489 }
490 }
491
492 if ($mergeSameAddress) {
493 //make sure the addressee fields are selected
494 //while using merge same address feature
495 $returnProperties['addressee'] = 1;
496 $returnProperties['postal_greeting'] = 1;
497 $returnProperties['email_greeting'] = 1;
498 $returnProperties['street_name'] = 1;
499 $returnProperties['household_name'] = 1;
500 $returnProperties['street_address'] = 1;
501 $returnProperties['city'] = 1;
502 $returnProperties['state_province'] = 1;
503
504 // some columns are required for assistance incase they are not already present
505 $exportParams['merge_same_address']['temp_columns'] = array();
506 $tempColumns = array('id', 'master_id', 'state_province_id', 'postal_greeting_id', 'addressee_id');
507 foreach ($tempColumns as $column) {
508 if (!array_key_exists($column, $returnProperties)) {
509 $returnProperties[$column] = 1;
510 $column = $column == 'id' ? 'civicrm_primary_id' : $column;
511 $exportParams['merge_same_address']['temp_columns'][$column] = 1;
512 }
513 }
514 }
515
8cc574cf 516 if (!$selectAll && $componentTable && !empty($exportParams['additional_group'])) {
6a488035
TO
517 // If an Additional Group is selected, then all contacts in that group are
518 // added to the export set (filtering out duplicates).
519 $query = "
520INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_contact gc WHERE gc.group_id = {$exportParams['additional_group']} ON DUPLICATE KEY UPDATE {$componentTable}.contact_id = gc.contact_id";
521 CRM_Core_DAO::executeQuery($query);
522 }
523
524 if ($moreReturnProperties) {
525 // fix for CRM-7066
a7488080 526 if (!empty($moreReturnProperties['group'])) {
6a488035
TO
527 unset($moreReturnProperties['group']);
528 $moreReturnProperties['groups'] = 1;
529 }
530 $returnProperties = array_merge($returnProperties, $moreReturnProperties);
531 }
532
533 $exportParams['postal_mailing_export']['temp_columns'] = array();
534 if ($exportParams['exportOption'] == 2 &&
535 isset($exportParams['postal_mailing_export']) &&
536 CRM_Utils_Array::value('postal_mailing_export', $exportParams['postal_mailing_export']) == 1
537 ) {
538 $postalColumns = array('is_deceased', 'do_not_mail', 'street_address', 'supplemental_address_1');
539 foreach ($postalColumns as $column) {
540 if (!array_key_exists($column, $returnProperties)) {
541 $returnProperties[$column] = 1;
542 $exportParams['postal_mailing_export']['temp_columns'][$column] = 1;
543 }
544 }
545 }
546
9e49a7b8
PJ
547 // rectify params to what proximity search expects if there is a value for prox_distance
548 // CRM-7021
549 if (!empty($params)) {
550 CRM_Contact_BAO_ProximityQuery::fixInputParams($params);
551 }
552
fdb9cf51 553 $query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
6a488035
TO
554 FALSE, FALSE, $queryMode,
555 FALSE, TRUE, TRUE, NULL, $queryOperator
556 );
557
33a5a53d 558 //sort by state
559 //CRM-15301
560 $query->_sort = $order;
6a488035
TO
561 list($select, $from, $where, $having) = $query->query();
562
563 if ($mergeSameHousehold == 1) {
564 if (!$returnProperties['id']) {
565 $returnProperties['id'] = 1;
566 }
567
568 //also merge Head of Household
569 $relationKeyMOH = CRM_Utils_Array::key('Household Member of', $contactRelationshipTypes);
570 $relationKeyHOH = CRM_Utils_Array::key('Head of Household for', $contactRelationshipTypes);
571
572 foreach ($returnProperties as $key => $value) {
573 if (!array_key_exists($key, $contactRelationshipTypes)) {
574 $returnProperties[$relationKeyMOH][$key] = $value;
575 $returnProperties[$relationKeyHOH][$key] = $value;
576 }
577 }
578
579 unset($returnProperties[$relationKeyMOH]['location_type']);
580 unset($returnProperties[$relationKeyMOH]['im_provider']);
581 unset($returnProperties[$relationKeyHOH]['location_type']);
582 unset($returnProperties[$relationKeyHOH]['im_provider']);
583 }
584
585 $allRelContactArray = $relationQuery = array();
586
587 foreach ($contactRelationshipTypes as $rel => $dnt) {
588 if ($relationReturnProperties = CRM_Utils_Array::value($rel, $returnProperties)) {
589 $allRelContactArray[$rel] = array();
590 // build Query for each relationship
591 $relationQuery[$rel] = new CRM_Contact_BAO_Query(NULL, $relationReturnProperties,
592 NULL, FALSE, FALSE, $queryMode
593 );
594 list($relationSelect, $relationFrom, $relationWhere, $relationHaving) = $relationQuery[$rel]->query();
595
596 list($id, $direction) = explode('_', $rel, 2);
597 // identify the relationship direction
598 $contactA = 'contact_id_a';
599 $contactB = 'contact_id_b';
600 if ($direction == 'b_a') {
601 $contactA = 'contact_id_b';
602 $contactB = 'contact_id_a';
603 }
604 if ($exportMode == CRM_Export_Form_Select::CONTACT_EXPORT) {
605 $relIDs = $ids;
606 }
607 elseif ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
ad7695c2 608 $sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
b4eac1e9 609 $query = "SELECT contact_id FROM civicrm_activity_contact
610 WHERE activity_id IN ( " . implode(',', $ids) . ") AND
611 record_type_id = {$sourceID}";
6a488035
TO
612 $dao = CRM_Core_DAO::executeQuery($query);
613 while ($dao->fetch()) {
b4eac1e9 614 $relIDs[] = $dao->contact_id;
6a488035
TO
615 }
616 }
617 else {
919eddec 618 $component = self::exportComponent($exportMode);
040898fe 619
620 if ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
621 $relIDs = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($ids);
622 }
623 else {
624 $relIDs = CRM_Core_DAO::getContactIDsFromComponent($ids, $component);
625 }
6a488035
TO
626 }
627
628 $relationshipJoin = $relationshipClause = '';
34b773b7 629 if (!$selectAll && $componentTable) {
630 $relationshipJoin = " INNER JOIN {$componentTable} ctTable ON ctTable.contact_id = {$contactA}";
6a488035 631 }
34b773b7 632 elseif (!empty($relIDs)) {
6a488035
TO
633 $relID = implode(',', $relIDs);
634 $relationshipClause = " AND crel.{$contactA} IN ( {$relID} )";
635 }
636
637 $relationFrom = " {$relationFrom}
638 INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_a.id AND crel.relationship_type_id = {$id}
639 {$relationshipJoin} ";
640
641 //check for active relationship status only
97f6897c
TO
642 $today = date('Ymd');
643 $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
644 $relationWhere = " WHERE contact_a.is_deleted = 0 {$relationshipClause} {$relationActive}";
645 $relationGroupBy = " GROUP BY crel.{$contactA}";
646 $relationSelect = "{$relationSelect}, {$contactA} as refContact ";
6a488035
TO
647 $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationHaving $relationGroupBy";
648
649 $allRelContactDAO = CRM_Core_DAO::executeQuery($relationQueryString);
650 while ($allRelContactDAO->fetch()) {
651 //FIX Me: Migrate this to table rather than array
652 // build the array of all related contacts
653 $allRelContactArray[$rel][$allRelContactDAO->refContact] = clone($allRelContactDAO);
654 }
655 $allRelContactDAO->free();
656 }
657 }
658
659 // make sure the groups stuff is included only if specifically specified
660 // by the fields param (CRM-1969), else we limit the contacts outputted to only
661 // ones that are part of a group
a7488080 662 if (!empty($returnProperties['groups'])) {
6a488035
TO
663 $oldClause = "( contact_a.id = civicrm_group_contact.contact_id )";
664 $newClause = " ( $oldClause AND ( civicrm_group_contact.status = 'Added' OR civicrm_group_contact.status IS NULL ) )";
665 // total hack for export, CRM-3618
666 $from = str_replace($oldClause,
667 $newClause,
668 $from
669 );
670 }
671
34b773b7 672 if (!$selectAll && $componentTable) {
6a488035
TO
673 $from .= " INNER JOIN $componentTable ctTable ON ctTable.contact_id = contact_a.id ";
674 }
675 elseif ($componentClause) {
676 if (empty($where)) {
677 $where = "WHERE $componentClause";
678 }
679 else {
680 $where .= " AND $componentClause";
681 }
682 }
683
14a08308 684 // CRM-13982 - check if is deleted
685 $excludeTrashed = TRUE;
686 foreach ($params as $value) {
687 if ($value[0] == 'contact_is_deleted') {
688 $excludeTrashed = FALSE;
689 }
690 }
17bc4f1b
SL
691 $trashClause = $excludeTrashed ? "contact_a.is_deleted != 1" : "( 1 )";
692
0b02cdf2 693 if (empty($where)) {
17bc4f1b 694 $where = "WHERE $trashClause";
14a08308 695 }
17bc4f1b
SL
696 else {
697 $where .= " AND $trashClause";
14a08308 698 }
699
6a488035
TO
700 $queryString = "$select $from $where $having";
701
702 $groupBy = "";
8a13a745 703
0cc9927d 704 $groupBy = self::getGroupBy($exportMode, $queryMode, $returnProperties, $query);
8a13a745 705
6a488035 706 $queryString .= $groupBy;
33a5a53d 707
708 // always add contact_a.id to the ORDER clause
709 // so the order is deterministic
710 //CRM-15301
711 if (strpos('contact_a.id', $order) === FALSE) {
712 $order .= ", contact_a.id";
713 }
714
6a488035
TO
715 if ($order) {
716 list($field, $dir) = explode(' ', $order, 2);
717 $field = trim($field);
a7488080 718 if (!empty($returnProperties[$field])) {
33a5a53d 719 //CRM-15301
720 $queryString .= " ORDER BY $order";
6a488035
TO
721 }
722 }
723
724 $multipleSelectFields = array('preferred_communication_method' => 1);
725
726 $addPaymentHeader = FALSE;
727
728 $paymentDetails = array();
d77aba4b 729 if ($paymentFields || $selectedPaymentFields) {
6a488035
TO
730
731 // get payment related in for event and members
732 $paymentDetails = CRM_Contribute_BAO_Contribution::getContributionDetails($exportMode, $ids);
d77aba4b 733 //get all payment headers.
7460c131
AS
734 // If we haven't selected specific payment fields, load in all the
735 // payment headers.
d77aba4b
AS
736 if (!$selectedPaymentFields) {
737 $paymentHeaders = self::componentPaymentFields();
738 if (!empty($paymentDetails)) {
739 $addPaymentHeader = TRUE;
740 }
6a488035 741 }
55f37718 742 // If we have selected specific payment fields, leave the payment headers
7460c131
AS
743 // as an empty array; the headers for each selected field will be added
744 // elsewhere.
745 else {
746 $paymentHeaders = array();
747 }
6a488035
TO
748 $nullContributionDetails = array_fill_keys(array_keys($paymentHeaders), NULL);
749 }
750
6a488035
TO
751 $componentDetails = $headerRows = $sqlColumns = array();
752 $setHeader = TRUE;
753
754 $rowCount = self::EXPORT_ROW_COUNT;
755 $offset = 0;
756 // we write to temp table often to avoid using too much memory
757 $tempRowCount = 100;
758
759 $count = -1;
760
761 // for CRM-3157 purposes
762 $i18n = CRM_Core_I18n::singleton();
55f37718
EM
763 $outputColumns = array();
764 //@todo - it would be clearer to start defining output columns earlier in this function rather than stick with return properties until this point
765 // as the array is not actually 'returnProperties' after the sql query is formed - making the alterations to it confusing
766 foreach ($returnProperties as $key => $value) {
d1456dd6 767 $outputColumns[$key] = $value;
55f37718 768 }
6a488035
TO
769 while (1) {
770 $limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}";
771 $dao = CRM_Core_DAO::executeQuery($limitQuery);
772 if ($dao->N <= 0) {
773 break;
774 }
775
776 while ($dao->fetch()) {
777 $count++;
778 $row = array();
779
b23943b8 780 //convert the pseudo constants
62835cac
EM
781 // CRM-14398 there is problem in this architecture that is not easily solved. For now we are using the cloned
782 // temporary iterationDAO object to get around it.
783 // the issue is that the convertToPseudoNames function is adding additional properties (e.g for campaign) to the DAO object
784 // these additional properties are NOT reset when the $dao cycles through the while loop
785 // nor are they overwritten as they are not in the loop
786 // the convertToPseudoNames will not adequately over-write them either as it doesn't 'kick-in' unless the
787 // relevant property is set.
788 // It may be that a long-term fix could be introduced there - however, it's probably necessary to figure out how to test the
789 // export class before tackling a better architectural fix
790 $iterationDAO = clone $dao;
791 $query->convertToPseudoNames($iterationDAO);
d9ab802d 792
55f37718 793 //first loop through output columns so that we return what is required, and in same order.
6a488035 794 $relationshipField = 0;
55f37718 795 foreach ($outputColumns as $field => $value) {
6a488035
TO
796 //we should set header only once
797 if ($setHeader) {
798 $sqlDone = FALSE;
93c29fb5 799 // Split campaign into 2 fields for id and title
9ebd3386 800 if (substr($field, -14) == 'campaign_title') {
93c29fb5
CW
801 $headerRows[] = ts('Campaign Title');
802 }
9ebd3386 803 elseif (substr($field, -11) == 'campaign_id') {
93c29fb5
CW
804 $headerRows[] = ts('Campaign ID');
805 }
806 elseif (isset($query->_fields[$field]['title'])) {
6a488035
TO
807 $headerRows[] = $query->_fields[$field]['title'];
808 }
809 elseif ($field == 'phone_type_id') {
810 $headerRows[] = ts('Phone Type');
811 }
812 elseif ($field == 'provider_id') {
813 $headerRows[] = ts('IM Service Provider');
814 }
815 elseif (is_array($value) && $field == 'location') {
816 // fix header for location type case
817 foreach ($value as $ltype => $val) {
818 foreach (array_keys($val) as $fld) {
819 $type = explode('-', $fld);
820 $hdr = "{$ltype}-" . $query->_fields[$type[0]]['title'];
821
a7488080 822 if (!empty($type[1])) {
6a488035
TO
823 if (CRM_Utils_Array::value(0, $type) == 'phone') {
824 $hdr .= "-" . CRM_Utils_Array::value($type[1], $phoneTypes);
825 }
826 elseif (CRM_Utils_Array::value(0, $type) == 'im') {
827 $hdr .= "-" . CRM_Utils_Array::value($type[1], $imProviders);
828 }
829 }
830 $headerRows[] = $hdr;
831 self::sqlColumnDefn($query, $sqlColumns, $hdr);
832 }
833 $sqlDone = TRUE;
834 }
835 }
836 elseif (substr($field, 0, 5) == 'case_') {
837 if ($query->_fields['case'][$field]['title']) {
838 $headerRows[] = $query->_fields['case'][$field]['title'];
839 }
840 elseif ($query->_fields['activity'][$field]['title']) {
841 $headerRows[] = $query->_fields['activity'][$field]['title'];
842 }
843 }
844 elseif (array_key_exists($field, $contactRelationshipTypes)) {
845 $relName = $field;
846 foreach ($value as $relationField => $relationValue) {
847 // below block is same as primary block (duplicate)
848 if (isset($relationQuery[$field]->_fields[$relationField]['title'])) {
849 if ($relationQuery[$field]->_fields[$relationField]['name'] == 'name') {
850 $headerName = $field . '-' . $relationField;
851 }
852 else {
853 if ($relationField == 'current_employer') {
854 $headerName = $field . '-' . 'current_employer';
855 }
856 else {
857 $headerName = $field . '-' . $relationQuery[$field]->_fields[$relationField]['name'];
858 }
859 }
860
861 $headerRows[] = $headerName;
862
863 self::sqlColumnDefn($query, $sqlColumns, $headerName);
864 }
865 elseif ($relationField == 'phone_type_id') {
866 $headerName = $field . '-' . 'Phone Type';
867 $headerRows[] = $headerName;
868 self::sqlColumnDefn($query, $sqlColumns, $headerName);
869 }
870 elseif ($relationField == 'provider_id') {
871 $headerName = $field . '-' . 'Im Service Provider';
872 $headerRows[] = $headerName;
873 self::sqlColumnDefn($query, $sqlColumns, $headerName);
874 }
875 elseif ($relationField == 'state_province_id') {
876 $headerName = $field . '-' . 'state_province_id';
877 $headerRows[] = $headerName;
878 self::sqlColumnDefn($query, $sqlColumns, $headerName);
879 }
880 elseif (is_array($relationValue) && $relationField == 'location') {
881 // fix header for location type case
882 foreach ($relationValue as $ltype => $val) {
883 foreach (array_keys($val) as $fld) {
884 $type = explode('-', $fld);
885
886 $hdr = "{$ltype}-" . $relationQuery[$field]->_fields[$type[0]]['title'];
887
a7488080 888 if (!empty($type[1])) {
6a488035
TO
889 if (CRM_Utils_Array::value(0, $type) == 'phone') {
890 $hdr .= "-" . CRM_Utils_Array::value($type[1], $phoneTypes);
891 }
892 elseif (CRM_Utils_Array::value(0, $type) == 'im') {
893 $hdr .= "-" . CRM_Utils_Array::value($type[1], $imProviders);
894 }
895 }
896 $headerName = $field . '-' . $hdr;
897 $headerRows[] = $headerName;
898 self::sqlColumnDefn($query, $sqlColumns, $headerName);
899 }
900 }
901 }
902 }
903 }
d77aba4b
AS
904 elseif ($selectedPaymentFields && array_key_exists($field, self::componentPaymentFields())) {
905 $headerRows[] = CRM_Utils_Array::value($field, self::componentPaymentFields());
906 }
6a488035
TO
907 else {
908 $headerRows[] = $field;
909 }
910
911 if (!$sqlDone) {
912 self::sqlColumnDefn($query, $sqlColumns, $field);
913 }
914 }
915
916 // add im_provider to $dao object
62835cac
EM
917 if ($field == 'im_provider' && property_exists($iterationDAO, 'provider_id')) {
918 $iterationDAO->im_provider = $iterationDAO->provider_id;
6a488035
TO
919 }
920
921 //build row values (data)
922 $fieldValue = NULL;
62835cac
EM
923 if (property_exists($iterationDAO, $field)) {
924 $fieldValue = $iterationDAO->$field;
6a488035
TO
925 // to get phone type from phone type id
926 if ($field == 'phone_type_id' && isset($phoneTypes[$fieldValue])) {
927 $fieldValue = $phoneTypes[$fieldValue];
928 }
929 elseif ($field == 'provider_id' || $field == 'im_provider') {
930 $fieldValue = CRM_Utils_Array::value($fieldValue, $imProviders);
931 }
6a488035
TO
932 elseif ($field == 'master_id') {
933 $masterAddressId = NULL;
62835cac
EM
934 if (isset($iterationDAO->master_id)) {
935 $masterAddressId = $iterationDAO->master_id;
6a488035
TO
936 }
937 // get display name of contact that address is shared.
62835cac 938 $fieldValue = CRM_Contact_BAO_Contact::getMasterDisplayName($masterAddressId, $iterationDAO->contact_id);
6a488035
TO
939 }
940 }
941
942 if ($field == 'id') {
62835cac 943 $row[$field] = $iterationDAO->contact_id;
6a488035
TO
944 // special case for calculated field
945 }
d4a280d8 946 elseif ($field == 'source_contact_id') {
62835cac 947 $row[$field] = $iterationDAO->contact_id;
d4a280d8 948 }
6a488035 949 elseif ($field == 'pledge_balance_amount') {
62835cac 950 $row[$field] = $iterationDAO->pledge_amount - $iterationDAO->pledge_total_paid;
6a488035
TO
951 // special case for calculated field
952 }
953 elseif ($field == 'pledge_next_pay_amount') {
62835cac 954 $row[$field] = $iterationDAO->pledge_next_pay_amount + $iterationDAO->pledge_outstanding_amount;
6a488035
TO
955 }
956 elseif (is_array($value) && $field == 'location') {
957 // fix header for location type case
958 foreach ($value as $ltype => $val) {
959 foreach (array_keys($val) as $fld) {
960 $type = explode('-', $fld);
961 $fldValue = "{$ltype}-" . $type[0];
6f7e23df
CW
962 // CRM-14076 - fix label to work as the query object expects
963 // FIXME: We should not be using labels as keys!
964 $daoField = CRM_Utils_String::munge($ltype) . '-' . $type[0];
6a488035 965
a7488080 966 if (!empty($type[1])) {
6a488035 967 $fldValue .= "-" . $type[1];
3d4498eb 968 $daoField .= "-" . $type[1];
6a488035
TO
969 }
970
971 // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context)
972 switch ($fld) {
973 case 'country':
974 case 'world_region':
62835cac 975 $row[$fldValue] = $i18n->crm_translate($iterationDAO->$daoField, array('context' => 'country'));
6a488035
TO
976 break;
977
978 case 'state_province':
62835cac 979 $row[$fldValue] = $i18n->crm_translate($iterationDAO->$daoField, array('context' => 'province'));
6a488035
TO
980 break;
981
982 case 'im_provider':
6f7e23df 983 $imFieldvalue = $daoField . "-provider_id";
62835cac 984 $row[$fldValue] = CRM_Utils_Array::value($iterationDAO->$imFieldvalue, $imProviders);
6a488035
TO
985 break;
986
987 default:
62835cac 988 $row[$fldValue] = $iterationDAO->$daoField;
6a488035
TO
989 break;
990 }
991 }
992 }
993 }
994 elseif (array_key_exists($field, $contactRelationshipTypes)) {
62835cac 995 $relDAO = CRM_Utils_Array::value($iterationDAO->contact_id, $allRelContactArray[$field]);
02709b11 996 $relationQuery[$field]->convertToPseudoNames($relDAO);
6a488035
TO
997 foreach ($value as $relationField => $relationValue) {
998 if (is_object($relDAO) && property_exists($relDAO, $relationField)) {
999 $fieldValue = $relDAO->$relationField;
1000 if ($relationField == 'phone_type_id') {
1001 $fieldValue = $phoneTypes[$relationValue];
1002 }
1003 elseif ($relationField == 'provider_id') {
1004 $fieldValue = CRM_Utils_Array::value($relationValue, $imProviders);
1005 }
36ae0094
JL
1006 // CRM-13995
1007 elseif (is_object($relDAO) && in_array($relationField, array(
1008 'email_greeting',
1009 'postal_greeting',
1010 'addressee',
1011 ))
1012 ) {
1013 //special case for greeting replacement
1014 $fldValue = "{$relationField}_display";
1015 $fieldValue = $relDAO->$fldValue;
1016 }
2bf55c30 1017 }
97f6897c 1018 elseif (is_object($relDAO) && $relationField == 'state_province') {
2bf55c30
BS
1019 $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id);
1020 }
97f6897c 1021 elseif (is_object($relDAO) && $relationField == 'country') {
b07c1873
DG
1022 $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id);
1023 }
6a488035
TO
1024 else {
1025 $fieldValue = '';
1026 }
1027 $field = $field . '_';
2bf55c30 1028
02709b11 1029 if (array_key_exists($relationField, $multipleSelectFields)) {
1030 $param = array($relationField => $fieldValue);
1031 $names = array($relationField => array('newName' => $relationField, 'groupName' => $relationField));
1032 CRM_Core_OptionGroup::lookupValues($param, $names, FALSE);
1033 $fieldValue = $param[$relationField];
1034 }
6a488035
TO
1035 if (is_object($relDAO) && $relationField == 'id') {
1036 $row[$field . $relationField] = $relDAO->contact_id;
2bf55c30 1037 }
97f6897c 1038 elseif (is_array($relationValue) && $relationField == 'location') {
6a488035
TO
1039 foreach ($relationValue as $ltype => $val) {
1040 foreach (array_keys($val) as $fld) {
1041 $type = explode('-', $fld);
1042 $fldValue = "{$ltype}-" . $type[0];
a7488080 1043 if (!empty($type[1])) {
6a488035
TO
1044 $fldValue .= "-" . $type[1];
1045 }
1046 // CRM-3157: localise country, region (both have ‘country’ context)
1047 // and state_province (‘province’ context)
1048 switch (TRUE) {
97f6897c 1049 case (!is_object($relDAO)):
6a488035
TO
1050 $row[$field . '_' . $fldValue] = '';
1051 break;
1052
1053 case in_array('country', $type):
1054 case in_array('world_region', $type):
1055 $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
1056 array('context' => 'country')
1057 );
1058 break;
1059
1060 case in_array('state_province', $type):
1061 $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
1062 array('context' => 'province')
1063 );
1064 break;
1065
1066 default:
1067 $row[$field . '_' . $fldValue] = $relDAO->$fldValue;
1068 break;
1069 }
1070 }
1071 }
1072 }
1073 elseif (isset($fieldValue) && $fieldValue != '') {
1074 //check for custom data
1075 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($relationField)) {
1076 $row[$field . $relationField] = CRM_Core_BAO_CustomField::getDisplayValue($fieldValue, $cfID,
1077 $relationQuery[$field]->_options
1078 );
1079 }
6a488035
TO
1080 else {
1081 //normal relationship fields
1082 // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context)
1083 switch ($relationField) {
1084 case 'country':
1085 case 'world_region':
1086 $row[$field . $relationField] = $i18n->crm_translate($fieldValue, array('context' => 'country'));
1087 break;
1088
1089 case 'state_province':
1090 $row[$field . $relationField] = $i18n->crm_translate($fieldValue, array('context' => 'province'));
1091 break;
1092
1093 default:
1094 $row[$field . $relationField] = $fieldValue;
1095 break;
1096 }
1097 }
1098 }
1099 else {
1100 // if relation field is empty or null
1101 $row[$field . $relationField] = '';
1102 }
1103 }
1104 }
1105 elseif (isset($fieldValue) &&
1106 $fieldValue != ''
1107 ) {
1108 //check for custom data
1109 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($field)) {
1110 $row[$field] = CRM_Core_BAO_CustomField::getDisplayValue($fieldValue, $cfID, $query->_options);
1111 }
1112 elseif (array_key_exists($field, $multipleSelectFields)) {
1113 //option group fixes
1114 $paramsNew = array($field => $fieldValue);
1115 if ($field == 'test_tutoring') {
1116 $name = array($field => array('newName' => $field, 'groupName' => 'test'));
1117 // for readers group
1118 }
1119 elseif (substr($field, 0, 4) == 'cmr_') {
1120 $name = array($field => array('newName' => $field, 'groupName' => substr($field, 0, -3)));
1121 }
1122 else {
1123 $name = array($field => array('newName' => $field, 'groupName' => $field));
1124 }
1125 CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE);
1126 $row[$field] = $paramsNew[$field];
1127 }
55f37718 1128
6a488035 1129 elseif (in_array($field, array(
97f6897c
TO
1130 'email_greeting',
1131 'postal_greeting',
21dfd5f5 1132 'addressee',
97f6897c 1133 ))) {
6a488035
TO
1134 //special case for greeting replacement
1135 $fldValue = "{$field}_display";
62835cac 1136 $row[$field] = $iterationDAO->$fldValue;
6a488035
TO
1137 }
1138 else {
1139 //normal fields with a touch of CRM-3157
1140 switch ($field) {
1141 case 'country':
1142 case 'world_region':
1143 $row[$field] = $i18n->crm_translate($fieldValue, array('context' => 'country'));
1144 break;
1145
1146 case 'state_province':
1147 $row[$field] = $i18n->crm_translate($fieldValue, array('context' => 'province'));
1148 break;
1149
1150 case 'gender':
1151 case 'preferred_communication_method':
1152 case 'preferred_mail_format':
aa62b355 1153 case 'communication_style':
6a488035
TO
1154 $row[$field] = $i18n->crm_translate($fieldValue);
1155 break;
1156
1157 default:
1158 $row[$field] = $fieldValue;
1159 break;
1160 }
1161 }
1162 }
d77aba4b 1163 elseif ($selectedPaymentFields && array_key_exists($field, self::componentPaymentFields())) {
62835cac 1164 $paymentData = CRM_Utils_Array::value($iterationDAO->$paymentTableId, $paymentDetails);
d77aba4b 1165 $payFieldMapper = array(
97f6897c 1166 'componentPaymentField_total_amount' => 'total_amount',
d77aba4b 1167 'componentPaymentField_contribution_status' => 'contribution_status',
97f6897c
TO
1168 'componentPaymentField_payment_instrument' => 'pay_instru',
1169 'componentPaymentField_transaction_id' => 'trxn_id',
1170 'componentPaymentField_received_date' => 'receive_date',
d77aba4b
AS
1171 );
1172 $row[$field] = CRM_Utils_Array::value($payFieldMapper[$field], $paymentData, '');
1173 }
6a488035
TO
1174 else {
1175 // if field is empty or null
1176 $row[$field] = '';
1177 }
1178 }
1179
1180 // add payment headers if required
1181 if ($addPaymentHeader && $paymentFields) {
1182 $headerRows = array_merge($headerRows, $paymentHeaders);
1183 foreach (array_keys($paymentHeaders) as $paymentHdr) {
1184 self::sqlColumnDefn($query, $sqlColumns, $paymentHdr);
1185 }
6a488035
TO
1186 }
1187
1188 if ($setHeader) {
1189 $exportTempTable = self::createTempTable($sqlColumns);
1190 }
1191
1192 //build header only once
1193 $setHeader = FALSE;
1194
e026db3c 1195 // If specific payment fields have been selected for export, payment
545285b8 1196 // data will already be in $row. Otherwise, add payment related
e026db3c 1197 // information, if appropriate.
fc33177b 1198 if ($addPaymentHeader) {
1199 if (!$selectedPaymentFields) {
1200 if ($paymentFields) {
1201 $paymentData = CRM_Utils_Array::value($row[$paymentTableId], $paymentDetails);
1202 if (!is_array($paymentData) || empty($paymentData)) {
1203 $paymentData = $nullContributionDetails;
1204 }
1205 $row = array_merge($row, $paymentData);
1206 }
1207 elseif (!empty($paymentDetails)) {
1208 $row = array_merge($row, $nullContributionDetails);
e026db3c 1209 }
e026db3c 1210 }
6a488035 1211 }
6a488035 1212 //remove organization name for individuals if it is set for current employer
a7488080 1213 if (!empty($row['contact_type']) &&
6a488035
TO
1214 $row['contact_type'] == 'Individual' && array_key_exists('organization_name', $row)
1215 ) {
1216 $row['organization_name'] = '';
1217 }
1218
1219 // add component info
1220 // write the row to a file
1221 $componentDetails[] = $row;
1222
1223 // output every $tempRowCount rows
1224 if ($count % $tempRowCount == 0) {
1225 self::writeDetailsToTable($exportTempTable, $componentDetails, $sqlColumns);
1226 $componentDetails = array();
1227 }
1228 }
1229 $dao->free();
1230 $offset += $rowCount;
1231 }
1232
1233 if ($exportTempTable) {
1234 self::writeDetailsToTable($exportTempTable, $componentDetails, $sqlColumns);
1235
1236 // do merge same address and merge same household processing
1237 if ($mergeSameAddress) {
1238 self::mergeSameAddress($exportTempTable, $headerRows, $sqlColumns, $exportParams);
1239 }
1240
1241 // merge the records if they have corresponding households
1242 if ($mergeSameHousehold) {
1243 self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, $relationKeyMOH);
1244 self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, $relationKeyHOH);
1245 }
1246
1247 // fix the headers for rows with relationship type
1248 if (!empty($relName)) {
1249 self::manipulateHeaderRows($headerRows, $contactRelationshipTypes);
1250 }
1251
1252 // if postalMailing option is checked, exclude contacts who are deceased, have
1253 // "Do not mail" privacy setting, or have no street address
1254 if (isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
1255 $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
1256 ) {
1257 self::postalMailingFormat($exportTempTable, $headerRows, $sqlColumns, $exportMode);
1258 }
1259
1260 // call export hook
1261 CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode);
1262
1263 // now write the CSV file
1264 self::writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode);
1265
1266 // delete the export temp table and component table
1267 $sql = "DROP TABLE IF EXISTS {$exportTempTable}";
1268 CRM_Core_DAO::executeQuery($sql);
1269
1270 CRM_Utils_System::civiExit();
1271 }
1272 else {
1273 CRM_Core_Error::fatal(ts('No records to export'));
1274 }
1275 }
1276
1277 /**
fe482240 1278 * Name of the export file based on mode.
6a488035 1279 *
b9add4b3
TO
1280 * @param string $output
1281 * Type of output.
1282 * @param int $mode
1283 * Export mode.
6a488035 1284 *
a6c01b45
CW
1285 * @return string
1286 * name of the file
6a488035 1287 */
00be9182 1288 public static function getExportFileName($output = 'csv', $mode = CRM_Export_Form_Select::CONTACT_EXPORT) {
6a488035
TO
1289 switch ($mode) {
1290 case CRM_Export_Form_Select::CONTACT_EXPORT:
1291 return ts('CiviCRM Contact Search');
1292
1293 case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
1294 return ts('CiviCRM Contribution Search');
1295
1296 case CRM_Export_Form_Select::MEMBER_EXPORT:
1297 return ts('CiviCRM Member Search');
1298
1299 case CRM_Export_Form_Select::EVENT_EXPORT:
1300 return ts('CiviCRM Participant Search');
1301
1302 case CRM_Export_Form_Select::PLEDGE_EXPORT:
1303 return ts('CiviCRM Pledge Search');
1304
1305 case CRM_Export_Form_Select::CASE_EXPORT:
1306 return ts('CiviCRM Case Search');
1307
1308 case CRM_Export_Form_Select::GRANT_EXPORT:
1309 return ts('CiviCRM Grant Search');
1310
1311 case CRM_Export_Form_Select::ACTIVITY_EXPORT:
1312 return ts('CiviCRM Activity Search');
1313 }
1314 }
1315
1316 /**
100fef9d 1317 * Handle import error file creation.
6a488035 1318 */
00be9182 1319 public static function invoke() {
6a488035
TO
1320 $type = CRM_Utils_Request::retrieve('type', 'Positive', CRM_Core_DAO::$_nullObject);
1321 $parserName = CRM_Utils_Request::retrieve('parser', 'String', CRM_Core_DAO::$_nullObject);
1322 if (empty($parserName) || empty($type)) {
1323 return;
1324 }
1325
1326 // clean and ensure parserName is a valid string
1327 $parserName = CRM_Utils_String::munge($parserName);
1328 $parserClass = explode('_', $parserName);
1329
1330 // make sure parserClass is in the CRM namespace and
1331 // at least 3 levels deep
1332 if ($parserClass[0] == 'CRM' &&
1333 count($parserClass) >= 3
1334 ) {
d3e86119 1335 require_once str_replace('_', DIRECTORY_SEPARATOR, $parserName) . ".php";
6a488035
TO
1336 // ensure the functions exists
1337 if (method_exists($parserName, 'errorFileName') &&
1338 method_exists($parserName, 'saveFileName')
1339 ) {
1340 $errorFileName = $parserName::errorFileName($type);
1341 $saveFileName = $parserName::saveFileName($type);
1342 if (!empty($errorFileName) && !empty($saveFileName)) {
d42a224c
CW
1343 CRM_Utils_System::setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
1344 CRM_Utils_System::setHttpHeader('Content-Description', 'File Transfer');
1345 CRM_Utils_System::setHttpHeader('Content-Type', 'text/csv');
1346 CRM_Utils_System::setHttpHeader('Content-Length', filesize($errorFileName));
1347 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename=' . $saveFileName);
6a488035
TO
1348
1349 readfile($errorFileName);
1350 }
1351 }
1352 }
1353 CRM_Utils_System::civiExit();
1354 }
1355
e0ef6999
EM
1356 /**
1357 * @param $customSearchClass
1358 * @param $formValues
1359 * @param $order
1360 */
00be9182 1361 public static function exportCustom($customSearchClass, $formValues, $order) {
6a488035
TO
1362 $ext = CRM_Extension_System::singleton()->getMapper();
1363 if (!$ext->isExtensionClass($customSearchClass)) {
d3e86119 1364 require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php';
6a488035
TO
1365 }
1366 else {
d3e86119 1367 require_once $ext->classToPath($customSearchClass);
6a488035
TO
1368 }
1369 $search = new $customSearchClass($formValues);
1370
1371 $includeContactIDs = FALSE;
1372 if ($formValues['radio_ts'] == 'ts_sel') {
1373 $includeContactIDs = TRUE;
1374 }
1375
1376 $sql = $search->all(0, 0, $order, $includeContactIDs);
1377
1378 $columns = $search->columns();
1379
1380 $header = array_keys($columns);
1381 $fields = array_values($columns);
1382
97f6897c
TO
1383 $rows = array();
1384 $dao = CRM_Core_DAO::executeQuery($sql);
6a488035
TO
1385 $alterRow = FALSE;
1386 if (method_exists($search, 'alterRow')) {
1387 $alterRow = TRUE;
1388 }
1389 while ($dao->fetch()) {
1390 $row = array();
1391
1392 foreach ($fields as $field) {
1393 $row[$field] = $dao->$field;
1394 }
1395 if ($alterRow) {
1396 $search->alterRow($row);
1397 }
1398 $rows[] = $row;
1399 }
1400
1401 CRM_Core_Report_Excel::writeCSVFile(self::getExportFileName(), $header, $rows);
1402 CRM_Utils_System::civiExit();
1403 }
1404
e0ef6999
EM
1405 /**
1406 * @param $query
1407 * @param $sqlColumns
1408 * @param $field
1409 */
00be9182 1410 public static function sqlColumnDefn(&$query, &$sqlColumns, $field) {
0944820d 1411 if (substr($field, -4) == '_a_b' || substr($field, -4) == '_b_a') {
6a488035
TO
1412 return;
1413 }
1414
1415 $fieldName = CRM_Utils_String::munge(strtolower($field), '_', 64);
1416 if ($fieldName == 'id') {
1417 $fieldName = 'civicrm_primary_id';
1418 }
1419
0944820d
DL
1420 // early exit for master_id, CRM-12100
1421 // in the DB it is an ID, but in the export, we retrive the display_name of the master record
86eef7b6
J
1422 // also for current_employer, CRM-16939
1423 if ($fieldName == 'master_id' || $fieldName == 'current_employer') {
0944820d
DL
1424 $sqlColumns[$fieldName] = "$fieldName varchar(128)";
1425 return;
1426 }
1427
55f37718
EM
1428 if (substr($fieldName, -11) == 'campaign_id') {
1429 // CRM-14398
1430 $sqlColumns[$fieldName] = "$fieldName varchar(128)";
55f37718
EM
1431 return;
1432 }
1433
4f2997e2 1434 $lookUp = array('prefix_id', 'suffix_id');
6a488035
TO
1435 // set the sql columns
1436 if (isset($query->_fields[$field]['type'])) {
1437 switch ($query->_fields[$field]['type']) {
1438 case CRM_Utils_Type::T_INT:
6a488035 1439 case CRM_Utils_Type::T_BOOLEAN:
a6332b16 1440 if (in_array($field, $lookUp)) {
4f2997e2 1441 $sqlColumns[$fieldName] = "$fieldName varchar(255)";
1442 }
1443 else {
1444 $sqlColumns[$fieldName] = "$fieldName varchar(16)";
1445 }
6a488035
TO
1446 break;
1447
1448 case CRM_Utils_Type::T_STRING:
1449 if (isset($query->_fields[$field]['maxlength'])) {
1450 $sqlColumns[$fieldName] = "$fieldName varchar({$query->_fields[$field]['maxlength']})";
1451 }
1452 else {
fe66d6ea 1453 $sqlColumns[$fieldName] = "$fieldName varchar(255)";
6a488035
TO
1454 }
1455 break;
1456
1457 case CRM_Utils_Type::T_TEXT:
1458 case CRM_Utils_Type::T_LONGTEXT:
1459 case CRM_Utils_Type::T_BLOB:
1460 case CRM_Utils_Type::T_MEDIUMBLOB:
1461 $sqlColumns[$fieldName] = "$fieldName longtext";
1462 break;
1463
1464 case CRM_Utils_Type::T_FLOAT:
1465 case CRM_Utils_Type::T_ENUM:
1466 case CRM_Utils_Type::T_DATE:
1467 case CRM_Utils_Type::T_TIME:
1468 case CRM_Utils_Type::T_TIMESTAMP:
1469 case CRM_Utils_Type::T_MONEY:
1470 case CRM_Utils_Type::T_EMAIL:
1471 case CRM_Utils_Type::T_URL:
1472 case CRM_Utils_Type::T_CCNUM:
1473 default:
1474 $sqlColumns[$fieldName] = "$fieldName varchar(32)";
1475 break;
1476 }
1477 }
1478 else {
1479 if (substr($fieldName, -3, 3) == '_id') {
1338f0b1 1480 // for trxn_id and its variants use a longer buffer
b44e3f84 1481 // to accommodate different systems - CRM-13739
1338f0b1
DL
1482 static $notRealIDFields = NULL;
1483 if ($notRealIDFields == NULL) {
97f6897c 1484 $notRealIDFields = array('trxn_id', 'componentpaymentfield_transaction_id');
1338f0b1
DL
1485 }
1486
1487 if (in_array($fieldName, $notRealIDFields)) {
1488 $sqlColumns[$fieldName] = "$fieldName varchar(255)";
1489 }
1490 else {
1491 $sqlColumns[$fieldName] = "$fieldName varchar(16)";
1492 }
6a488035 1493 }
c05a91c9 1494 elseif (substr($fieldName, -5, 5) == '_note') {
1495 $sqlColumns[$fieldName] = "$fieldName text";
1496 }
6a488035
TO
1497 else {
1498 $changeFields = array(
1499 'groups',
1500 'tags',
21dfd5f5 1501 'notes',
6a488035
TO
1502 );
1503
1504 if (in_array($fieldName, $changeFields)) {
1505 $sqlColumns[$fieldName] = "$fieldName text";
1506 }
1507 else {
1508 // set the sql columns for custom data
1509 if (isset($query->_fields[$field]['data_type'])) {
1510
1511 switch ($query->_fields[$field]['data_type']) {
26509325
CW
1512 case 'String':
1513 $length = empty($query->_fields[$field]['text_length']) ? 255 : $query->_fields[$field]['text_length'];
1514 $sqlColumns[$fieldName] = "$fieldName varchar($length)";
1515 break;
1516
6a488035
TO
1517 case 'Country':
1518 case 'StateProvince':
1519 case 'Link':
6a488035
TO
1520 $sqlColumns[$fieldName] = "$fieldName varchar(255)";
1521 break;
1522
1523 case 'Memo':
1524 $sqlColumns[$fieldName] = "$fieldName text";
1525 break;
1526
1527 default:
fe4afc7b 1528 $sqlColumns[$fieldName] = "$fieldName varchar(255)";
6a488035
TO
1529 break;
1530 }
1531 }
1532 else {
fe4afc7b 1533 $sqlColumns[$fieldName] = "$fieldName varchar(255)";
6a488035
TO
1534 }
1535 }
1536 }
1537 }
1538 }
1539
e0ef6999 1540 /**
100fef9d 1541 * @param string $tableName
e0ef6999
EM
1542 * @param $details
1543 * @param $sqlColumns
1544 */
00be9182 1545 public static function writeDetailsToTable($tableName, &$details, &$sqlColumns) {
6a488035
TO
1546 if (empty($details)) {
1547 return;
1548 }
1549
1550 $sql = "
1551SELECT max(id)
1552FROM $tableName
1553";
1554
1555 $id = CRM_Core_DAO::singleValueQuery($sql);
1556 if (!$id) {
1557 $id = 0;
1558 }
1559
1560 $sqlClause = array();
1561
1562 foreach ($details as $dontCare => $row) {
1563 $id++;
1564 $valueString = array($id);
1565 foreach ($row as $dontCare => $value) {
1566 if (empty($value)) {
1567 $valueString[] = "''";
1568 }
1569 else {
1570 $valueString[] = "'" . CRM_Core_DAO::escapeString($value) . "'";
1571 }
1572 }
1573 $sqlClause[] = '(' . implode(',', $valueString) . ')';
1574 }
1575
1576 $sqlColumnString = '(id, ' . implode(',', array_keys($sqlColumns)) . ')';
1577
1578 $sqlValueString = implode(",\n", $sqlClause);
1579
1580 $sql = "
1581INSERT INTO $tableName $sqlColumnString
1582VALUES $sqlValueString
1583";
1584
1585 CRM_Core_DAO::executeQuery($sql);
1586 }
1587
e0ef6999
EM
1588 /**
1589 * @param $sqlColumns
1590 *
1591 * @return string
1592 */
00be9182 1593 public static function createTempTable(&$sqlColumns) {
6a488035
TO
1594 //creating a temporary table for the search result that need be exported
1595 $exportTempTable = CRM_Core_DAO::createTempTableName('civicrm_export', TRUE);
1596
1597 // also create the sql table
1598 $sql = "DROP TABLE IF EXISTS {$exportTempTable}";
1599 CRM_Core_DAO::executeQuery($sql);
1600
1601 $sql = "
1602CREATE TABLE {$exportTempTable} (
1603 id int unsigned NOT NULL AUTO_INCREMENT,
1604";
1605 $sql .= implode(",\n", array_values($sqlColumns));
1606
1607 $sql .= ",
1608 PRIMARY KEY ( id )
1609";
1610 // add indexes for street_address and household_name if present
1611 $addIndices = array(
1612 'street_address',
1613 'household_name',
1614 'civicrm_primary_id',
1615 );
1616
1617 foreach ($addIndices as $index) {
1618 if (isset($sqlColumns[$index])) {
1619 $sql .= ",
1620 INDEX index_{$index}( $index )
1621";
1622 }
1623 }
1624
1625 $sql .= "
1626) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
1627";
1628
1629 CRM_Core_DAO::executeQuery($sql);
1630 return $exportTempTable;
1631 }
1632
e0ef6999 1633 /**
100fef9d 1634 * @param string $tableName
e0ef6999
EM
1635 * @param $headerRows
1636 * @param $sqlColumns
100fef9d 1637 * @param array $exportParams
e0ef6999 1638 */
00be9182 1639 public static function mergeSameAddress($tableName, &$headerRows, &$sqlColumns, $exportParams) {
6a488035
TO
1640 // check if any records are present based on if they have used shared address feature,
1641 // and not based on if city / state .. matches.
1642 $sql = "
1643SELECT r1.id as copy_id,
1644 r1.civicrm_primary_id as copy_contact_id,
1645 r1.addressee as copy_addressee,
1646 r1.addressee_id as copy_addressee_id,
1647 r1.postal_greeting as copy_postal_greeting,
1648 r1.postal_greeting_id as copy_postal_greeting_id,
1649 r2.id as master_id,
1650 r2.civicrm_primary_id as master_contact_id,
1651 r2.postal_greeting as master_postal_greeting,
1652 r2.postal_greeting_id as master_postal_greeting_id,
1653 r2.addressee as master_addressee,
1654 r2.addressee_id as master_addressee_id
1655FROM $tableName r1
1656INNER JOIN civicrm_address adr ON r1.master_id = adr.id
1657INNER JOIN $tableName r2 ON adr.contact_id = r2.civicrm_primary_id
1658ORDER BY r1.id";
1659 $linkedMerge = self::_buildMasterCopyArray($sql, $exportParams, TRUE);
1660
1661 // find all the records that have the same street address BUT not in a household
1662 // require match on city and state as well
1663 $sql = "
1664SELECT r1.id as master_id,
1665 r1.civicrm_primary_id as master_contact_id,
1666 r1.postal_greeting as master_postal_greeting,
1667 r1.postal_greeting_id as master_postal_greeting_id,
1668 r1.addressee as master_addressee,
1669 r1.addressee_id as master_addressee_id,
1670 r2.id as copy_id,
1671 r2.civicrm_primary_id as copy_contact_id,
1672 r2.postal_greeting as copy_postal_greeting,
1673 r2.postal_greeting_id as copy_postal_greeting_id,
1674 r2.addressee as copy_addressee,
1675 r2.addressee_id as copy_addressee_id
1676FROM $tableName r1
1677LEFT JOIN $tableName r2 ON ( r1.street_address = r2.street_address AND
1678 r1.city = r2.city AND
1679 r1.state_province_id = r2.state_province_id )
1680WHERE ( r1.household_name IS NULL OR r1.household_name = '' )
1681AND ( r2.household_name IS NULL OR r2.household_name = '' )
1682AND ( r1.street_address != '' )
1683AND r2.id > r1.id
1684ORDER BY r1.id
1685";
1686 $merge = self::_buildMasterCopyArray($sql, $exportParams);
1687
1688 // unset ids from $merge already present in $linkedMerge
1689 foreach ($linkedMerge as $masterID => $values) {
1690 $keys = array($masterID);
1691 $keys = array_merge($keys, array_keys($values['copy']));
1692 foreach ($merge as $mid => $vals) {
1693 if (in_array($mid, $keys)) {
1694 unset($merge[$mid]);
1695 }
1696 else {
1697 foreach ($values['copy'] as $copyId) {
1698 if (in_array($copyId, $keys)) {
1699 unset($merge[$mid]['copy'][$copyId]);
1700 }
1701 }
1702 }
1703 }
1704 }
1705 $merge = $merge + $linkedMerge;
1706
1707 foreach ($merge as $masterID => $values) {
1708 $sql = "
1709UPDATE $tableName
1710SET addressee = %1, postal_greeting = %2, email_greeting = %3
1711WHERE id = %4
1712";
97f6897c
TO
1713 $params = array(
1714 1 => array($values['addressee'], 'String'),
6a488035
TO
1715 2 => array($values['postalGreeting'], 'String'),
1716 3 => array($values['emailGreeting'], 'String'),
1717 4 => array($masterID, 'Integer'),
1718 );
1719 CRM_Core_DAO::executeQuery($sql, $params);
1720
1721 // delete all copies
97f6897c 1722 $deleteIDs = array_keys($values['copy']);
6a488035 1723 $deleteIDString = implode(',', $deleteIDs);
97f6897c 1724 $sql = "
6a488035
TO
1725DELETE FROM $tableName
1726WHERE id IN ( $deleteIDString )
1727";
1728 CRM_Core_DAO::executeQuery($sql);
1729 }
1730
1731 // unset temporary columns that were added for postal mailing format
1732 if (!empty($exportParams['merge_same_address']['temp_columns'])) {
1733 $unsetKeys = array_keys($sqlColumns);
1734 foreach ($unsetKeys as $headerKey => $sqlColKey) {
1735 if (array_key_exists($sqlColKey, $exportParams['merge_same_address']['temp_columns'])) {
1736 unset($sqlColumns[$sqlColKey], $headerRows[$headerKey]);
1737 }
1738 }
1739 }
1740 }
1741
e0ef6999 1742 /**
100fef9d
CW
1743 * @param int $contactId
1744 * @param array $exportParams
e0ef6999
EM
1745 *
1746 * @return array
1747 */
00be9182 1748 public static function _replaceMergeTokens($contactId, $exportParams) {
6a488035
TO
1749 $greetings = array();
1750 $contact = NULL;
1751
1752 $greetingFields = array(
1753 'postal_greeting',
1754 'addressee',
1755 );
1756 foreach ($greetingFields as $greeting) {
a7488080 1757 if (!empty($exportParams[$greeting])) {
6a488035
TO
1758 $greetingLabel = $exportParams[$greeting];
1759 if (empty($contact)) {
1760 $values = array(
1761 'id' => $contactId,
1762 'version' => 3,
1763 );
1764 $contact = civicrm_api('contact', 'get', $values);
1765
a7488080 1766 if (!empty($contact['is_error'])) {
6a488035
TO
1767 return $greetings;
1768 }
1769 $contact = $contact['values'][$contact['id']];
1770 }
1771
1772 $tokens = array('contact' => $greetingLabel);
1773 $greetings[$greeting] = CRM_Utils_Token::replaceContactTokens($greetingLabel, $contact, NULL, $tokens);
1774 }
1775 }
1776 return $greetings;
1777 }
1778
1779 /**
1780 * The function unsets static part of the string, if token is the dynamic part.
54957108 1781 *
6a488035
TO
1782 * Example: 'Hello {contact.first_name}' => converted to => '{contact.first_name}'
1783 * i.e 'Hello Alan' => converted to => 'Alan'
54957108 1784 *
1785 * @param string $parsedString
1786 * @param string $defaultGreeting
1787 * @param bool $addressMergeGreetings
1788 * @param string $greetingType
1789 *
1790 * @return mixed
6a488035 1791 */
317fceb4 1792 public static function _trimNonTokens(
97f6897c 1793 &$parsedString, $defaultGreeting,
353ffa53 1794 $addressMergeGreetings, $greetingType = 'postal_greeting'
6a488035 1795 ) {
a7488080 1796 if (!empty($addressMergeGreetings[$greetingType])) {
6a488035
TO
1797 $greetingLabel = $addressMergeGreetings[$greetingType];
1798 }
1799 $greetingLabel = empty($greetingLabel) ? $defaultGreeting : $greetingLabel;
1800
1801 $stringsToBeReplaced = preg_replace('/(\{[a-zA-Z._ ]+\})/', ';;', $greetingLabel);
1802 $stringsToBeReplaced = explode(';;', $stringsToBeReplaced);
1803 foreach ($stringsToBeReplaced as $key => $string) {
1804 // to keep one space
1805 $stringsToBeReplaced[$key] = ltrim($string);
1806 }
1807 $parsedString = str_replace($stringsToBeReplaced, "", $parsedString);
1808
1809 return $parsedString;
1810 }
1811
e0ef6999
EM
1812 /**
1813 * @param $sql
100fef9d 1814 * @param array $exportParams
e0ef6999
EM
1815 * @param bool $sharedAddress
1816 *
1817 * @return array
1818 */
00be9182 1819 public static function _buildMasterCopyArray($sql, $exportParams, $sharedAddress = FALSE) {
6a488035
TO
1820 static $contactGreetingTokens = array();
1821
1822 $addresseeOptions = CRM_Core_OptionGroup::values('addressee');
1823 $postalOptions = CRM_Core_OptionGroup::values('postal_greeting');
1824
1825 $merge = $parents = array();
1826 $dao = CRM_Core_DAO::executeQuery($sql);
1827
1828 while ($dao->fetch()) {
1829 $masterID = $dao->master_id;
1830 $copyID = $dao->copy_id;
1831 $masterPostalGreeting = $dao->master_postal_greeting;
1832 $masterAddressee = $dao->master_addressee;
1833 $copyAddressee = $dao->copy_addressee;
1834
1835 if (!$sharedAddress) {
1836 if (!isset($contactGreetingTokens[$dao->master_contact_id])) {
1837 $contactGreetingTokens[$dao->master_contact_id] = self::_replaceMergeTokens($dao->master_contact_id, $exportParams);
1838 }
1839 $masterPostalGreeting = CRM_Utils_Array::value('postal_greeting',
1840 $contactGreetingTokens[$dao->master_contact_id], $dao->master_postal_greeting
1841 );
1842 $masterAddressee = CRM_Utils_Array::value('addressee',
1843 $contactGreetingTokens[$dao->master_contact_id], $dao->master_addressee
1844 );
1845
1846 if (!isset($contactGreetingTokens[$dao->copy_contact_id])) {
1847 $contactGreetingTokens[$dao->copy_contact_id] = self::_replaceMergeTokens($dao->copy_contact_id, $exportParams);
1848 }
1849 $copyPostalGreeting = CRM_Utils_Array::value('postal_greeting',
1850 $contactGreetingTokens[$dao->copy_contact_id], $dao->copy_postal_greeting
1851 );
1852 $copyAddressee = CRM_Utils_Array::value('addressee',
1853 $contactGreetingTokens[$dao->copy_contact_id], $dao->copy_addressee
1854 );
1855 }
1856
1857 if (!isset($merge[$masterID])) {
1858 // check if this is an intermediate child
1859 // this happens if there are 3 or more matches a,b, c
1860 // the above query will return a, b / a, c / b, c
1861 // we might be doing a bit more work, but for now its ok, unless someone
1862 // knows how to fix the query above
1863 if (isset($parents[$masterID])) {
1864 $masterID = $parents[$masterID];
1865 }
1866 else {
1867 $merge[$masterID] = array(
1868 'addressee' => $masterAddressee,
1869 'copy' => array(),
1870 'postalGreeting' => $masterPostalGreeting,
1871 );
1872 $merge[$masterID]['emailGreeting'] = &$merge[$masterID]['postalGreeting'];
1873 }
1874 }
1875 $parents[$copyID] = $masterID;
1876
1877 if (!$sharedAddress && !array_key_exists($copyID, $merge[$masterID]['copy'])) {
1878
a7488080 1879 if (!empty($exportParams['postal_greeting_other']) &&
6a488035
TO
1880 count($merge[$masterID]['copy']) >= 1
1881 ) {
1882 // use static greetings specified if no of contacts > 2
1883 $merge[$masterID]['postalGreeting'] = $exportParams['postal_greeting_other'];
1884 }
1885 elseif ($copyPostalGreeting) {
1886 self::_trimNonTokens($copyPostalGreeting,
1887 $postalOptions[$dao->copy_postal_greeting_id],
1888 $exportParams
1889 );
1890 $merge[$masterID]['postalGreeting'] = "{$merge[$masterID]['postalGreeting']}, {$copyPostalGreeting}";
1891 // if there happens to be a duplicate, remove it
1892 $merge[$masterID]['postalGreeting'] = str_replace(" {$copyPostalGreeting},", "", $merge[$masterID]['postalGreeting']);
1893 }
1894
a7488080 1895 if (!empty($exportParams['addressee_other']) &&
6a488035
TO
1896 count($merge[$masterID]['copy']) >= 1
1897 ) {
1898 // use static greetings specified if no of contacts > 2
1899 $merge[$masterID]['addressee'] = $exportParams['addressee_other'];
1900 }
1901 elseif ($copyAddressee) {
1902 self::_trimNonTokens($copyAddressee,
1903 $addresseeOptions[$dao->copy_addressee_id],
1904 $exportParams, 'addressee'
1905 );
1906 $merge[$masterID]['addressee'] = "{$merge[$masterID]['addressee']}, " . trim($copyAddressee);
1907 }
1908 }
1909 $merge[$masterID]['copy'][$copyID] = $copyAddressee;
1910 }
1911
1912 return $merge;
1913 }
1914
1915 /**
100fef9d 1916 * Merge household record into the individual record
6a488035
TO
1917 * if exists
1918 *
b9add4b3
TO
1919 * @param string $exportTempTable
1920 * Temporary temp table that stores the records.
1921 * @param array $headerRows
1922 * Array of headers for the export file.
1923 * @param array $sqlColumns
1924 * Array of names of the table columns of the temp table.
1925 * @param string $prefix
1926 * Name of the relationship type that is prefixed to the table columns.
6a488035 1927 */
00be9182 1928 public static function mergeSameHousehold($exportTempTable, &$headerRows, &$sqlColumns, $prefix) {
6a488035 1929 $prefixColumn = $prefix . '_';
97f6897c
TO
1930 $allKeys = array_keys($sqlColumns);
1931 $replaced = array();
1932 $headerRows = array_values($headerRows);
6a488035
TO
1933
1934 // name map of the non standard fields in header rows & sql columns
1935 $mappingFields = array(
1936 'civicrm_primary_id' => 'id',
1937 'contact_source' => 'source',
1938 'current_employer_id' => 'employer_id',
1939 'contact_is_deleted' => 'is_deleted',
1940 'name' => 'address_name',
1941 'provider_id' => 'im_service_provider',
21dfd5f5 1942 'phone_type_id' => 'phone_type',
6a488035
TO
1943 );
1944
1945 //figure out which columns are to be replaced by which ones
1946 foreach ($sqlColumns as $columnNames => $dontCare) {
1947 if ($rep = CRM_Utils_Array::value($columnNames, $mappingFields)) {
1948 $replaced[$columnNames] = CRM_Utils_String::munge($prefixColumn . $rep, '_', 64);
1949 }
1950 else {
1951 $householdColName = CRM_Utils_String::munge($prefixColumn . $columnNames, '_', 64);
1952
a7488080 1953 if (!empty($sqlColumns[$householdColName])) {
6a488035
TO
1954 $replaced[$columnNames] = $householdColName;
1955 }
1956 }
1957 }
1958 $query = "UPDATE $exportTempTable SET ";
1959
1960 $clause = array();
1961 foreach ($replaced as $from => $to) {
1962 $clause[] = "$from = $to ";
1963 unset($sqlColumns[$to]);
1964 if ($key = CRM_Utils_Array::key($to, $allKeys)) {
1965 unset($headerRows[$key]);
1966 }
1967 }
1968 $query .= implode(",\n", $clause);
1969 $query .= " WHERE {$replaced['civicrm_primary_id']} != ''";
1970
1971 CRM_Core_DAO::executeQuery($query);
1972
1973 //drop the table columns that store redundant household info
1974 $dropQuery = "ALTER TABLE $exportTempTable ";
1975 foreach ($replaced as $householdColumns) {
1976 $dropClause[] = " DROP $householdColumns ";
1977 }
1978 $dropQuery .= implode(",\n", $dropClause);
1979
1980 CRM_Core_DAO::executeQuery($dropQuery);
1981
1982 // also drop the temp table if exists
1983 $sql = "DROP TABLE IF EXISTS {$exportTempTable}_temp";
1984 CRM_Core_DAO::executeQuery($sql);
1985
1986 // clean up duplicate records
1987 $query = "
1988CREATE TABLE {$exportTempTable}_temp SELECT *
1989FROM {$exportTempTable}
1990GROUP BY civicrm_primary_id ";
1991
1992 CRM_Core_DAO::executeQuery($query);
1993
1994 $query = "DROP TABLE $exportTempTable";
1995 CRM_Core_DAO::executeQuery($query);
1996
1997 $query = "ALTER TABLE {$exportTempTable}_temp RENAME TO {$exportTempTable}";
1998 CRM_Core_DAO::executeQuery($query);
1999 }
2000
e0ef6999
EM
2001 /**
2002 * @param $exportTempTable
2003 * @param $headerRows
2004 * @param $sqlColumns
2005 * @param $exportMode
2006 * @param null $saveFile
2007 * @param string $batchItems
2008 */
97f6897c 2009 public static function writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode, $saveFile = NULL, $batchItems = '') {
6a488035 2010 $writeHeader = TRUE;
97f6897c
TO
2011 $offset = 0;
2012 $limit = self::EXPORT_ROW_COUNT;
6a488035
TO
2013
2014 $query = "SELECT * FROM $exportTempTable";
2015
2016 while (1) {
2017 $limitQuery = $query . "
2018LIMIT $offset, $limit
2019";
2020 $dao = CRM_Core_DAO::executeQuery($limitQuery);
2021
2022 if ($dao->N <= 0) {
2023 break;
2024 }
2025
2026 $componentDetails = array();
2027 while ($dao->fetch()) {
2028 $row = array();
2029
2030 foreach ($sqlColumns as $column => $dontCare) {
2031 $row[$column] = $dao->$column;
2032 }
2033 $componentDetails[] = $row;
2034 }
97f6897c 2035 if ($exportMode == 'financial') {
6a488035
TO
2036 $getExportFileName = 'CiviCRM Contribution Search';
2037 }
2038 else {
97f6897c 2039 $getExportFileName = self::getExportFileName('csv', $exportMode);
6a488035 2040 }
97f6897c 2041 $csvRows = CRM_Core_Report_Excel::writeCSVFile($getExportFileName,
6a488035
TO
2042 $headerRows,
2043 $componentDetails,
97f6897c 2044 NULL,
6a488035 2045 $writeHeader,
97f6897c 2046 $saveFile);
6a488035
TO
2047
2048 if ($saveFile && !empty($csvRows)) {
2049 $batchItems .= $csvRows;
2050 }
2051
97f6897c 2052 $writeHeader = FALSE;
6a488035
TO
2053 $offset += $limit;
2054 }
2055 }
2056
2057 /**
fe482240 2058 * Manipulate header rows for relationship fields.
ca87146b
EM
2059 *
2060 * @param $headerRows
2061 * @param $contactRelationshipTypes
6a488035 2062 */
1571c7e7 2063 public static function manipulateHeaderRows(&$headerRows, $contactRelationshipTypes) {
6a488035
TO
2064 foreach ($headerRows as & $header) {
2065 $split = explode('-', $header);
2066 if ($relationTypeName = CRM_Utils_Array::value($split[0], $contactRelationshipTypes)) {
2067 $split[0] = $relationTypeName;
2068 $header = implode('-', $split);
2069 }
2070 }
2071 }
2072
2073 /**
100fef9d 2074 * Exclude contacts who are deceased, have "Do not mail" privacy setting,
6a488035 2075 * or have no street address
ca87146b
EM
2076 * @param $exportTempTable
2077 * @param $headerRows
2078 * @param $sqlColumns
2079 * @param $exportParams
6a488035 2080 */
00be9182 2081 public static function postalMailingFormat($exportTempTable, &$headerRows, &$sqlColumns, $exportParams) {
6a488035
TO
2082 $whereClause = array();
2083
2084 if (array_key_exists('is_deceased', $sqlColumns)) {
2085 $whereClause[] = 'is_deceased = 1';
2086 }
2087
2088 if (array_key_exists('do_not_mail', $sqlColumns)) {
2089 $whereClause[] = 'do_not_mail = 1';
2090 }
2091
2092 if (array_key_exists('street_address', $sqlColumns)) {
2093 $addressWhereClause = " ( (street_address IS NULL) OR (street_address = '') ) ";
2094
2095 // check for supplemental_address_1
2096 if (array_key_exists('supplemental_address_1', $sqlColumns)) {
2097 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
2098 'address_options', TRUE, NULL, TRUE
2099 );
a7488080 2100 if (!empty($addressOptions['supplemental_address_1'])) {
6a488035
TO
2101 $addressWhereClause .= " AND ( (supplemental_address_1 IS NULL) OR (supplemental_address_1 = '') ) ";
2102 // enclose it again, since we are doing an AND in between a set of ORs
2103 $addressWhereClause = "( $addressWhereClause )";
2104 }
2105 }
2106
2107 $whereClause[] = $addressWhereClause;
2108 }
2109
2110 if (!empty($whereClause)) {
2111 $whereClause = implode(' OR ', $whereClause);
2112 $query = "
2113DELETE
2114FROM $exportTempTable
2115WHERE {$whereClause}";
2116 CRM_Core_DAO::singleValueQuery($query);
2117 }
2118
2119 // unset temporary columns that were added for postal mailing format
2120 if (!empty($exportParams['postal_mailing_export']['temp_columns'])) {
2121 $unsetKeys = array_keys($sqlColumns);
2122 foreach ($unsetKeys as $headerKey => $sqlColKey) {
2123 if (array_key_exists($sqlColKey, $exportParams['postal_mailing_export']['temp_columns'])) {
2124 unset($sqlColumns[$sqlColKey], $headerRows[$headerKey]);
2125 }
2126 }
2127 }
2128 }
d77aba4b
AS
2129
2130 /**
2131 * Build componentPayment fields.
2132 */
00be9182 2133 public static function componentPaymentFields() {
d77aba4b 2134 static $componentPaymentFields;
97f6897c 2135 if (!isset($componentPaymentFields)) {
d77aba4b 2136 $componentPaymentFields = array(
97f6897c 2137 'componentPaymentField_total_amount' => ts('Total Amount'),
d77aba4b 2138 'componentPaymentField_contribution_status' => ts('Contribution Status'),
7bc6b5bb 2139 'componentPaymentField_received_date' => ts('Date Received'),
536f0e02 2140 'componentPaymentField_payment_instrument' => ts('Payment Method'),
97f6897c 2141 'componentPaymentField_transaction_id' => ts('Transaction ID'),
d77aba4b
AS
2142 );
2143 }
2144 return $componentPaymentFields;
2145 }
96025800 2146
6a488035 2147}