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