3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class contains the functions for Component export
22 class CRM_Export_BAO_Export
{
23 // increase this number a lot to avoid making too many queries
24 // LIMIT is not much faster than a no LIMIT query
26 const EXPORT_ROW_COUNT
= 100000;
29 * Returns a list of exportable entities and their associated component.
31 * Note: Some entities like Contact & Activity are not in components, so
32 * the export form seems to fudge things and accept the entity name instead of
33 * component name in those cases.
35 * TODO: Hardcoded list bad. Needs to support extension export pages.
39 public static function getComponents() {
41 'Contact' => 'Contact',
42 'Contribution' => 'Contribute',
43 'Membership' => 'Member',
44 'Participant' => 'Event',
48 'Activity' => 'Activity',
53 * Get the list the export fields.
55 * @param int $selectAll
56 * User preference while export.
59 * @param array $params
60 * Associated array of fields.
61 * @param string $order
63 * @param array $fields
64 * Associated array of fields.
65 * @param array $moreReturnProperties
66 * Additional return fields.
67 * @param int $exportMode
69 * @param string $componentClause
71 * @param string $componentTable
73 * @param bool $mergeSameAddress
74 * Merge records if they have same address.
75 * @param bool $mergeSameHousehold
76 * Merge records if they belong to the same household.
78 * @param array $exportParams
79 * @param string $queryOperator
81 * @throws \CRM_Core_Exception
83 public static function exportComponents(
89 $moreReturnProperties = NULL,
90 $exportMode = CRM_Export_Form_Select
::CONTACT_EXPORT
,
91 $componentClause = NULL,
92 $componentTable = NULL,
93 $mergeSameAddress = FALSE,
94 $mergeSameHousehold = FALSE,
96 $queryOperator = 'AND'
100 isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
101 $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
104 if (!$selectAll && $componentTable && !empty($exportParams['additional_group'])) {
105 // If an Additional Group is selected, then all contacts in that group are
106 // added to the export set (filtering out duplicates).
107 // Really - the calling function could do this ... just saying
108 // @todo take a whip to the calling function.
109 CRM_Core_DAO
::executeQuery("
110 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"
113 // rectify params to what proximity search expects if there is a value for prox_distance
115 // @todo - move this back to the calling functions
116 if (!empty($params)) {
117 CRM_Contact_BAO_ProximityQuery
::fixInputParams($params);
119 // @todo everything from this line up should go back to the calling functions.
120 $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold, $isPostalOnly, $mergeSameAddress, $exportParams);
121 if ($moreReturnProperties) {
122 $processor->setAdditionalRequestedReturnProperties($moreReturnProperties);
124 $processor->setComponentTable($componentTable);
125 $processor->setComponentClause($componentClause);
126 $processor->setIds($ids);
128 list($query, $queryString) = $processor->runQuery($params, $order);
130 // This perhaps only needs calling when $mergeSameHousehold == 1
131 self
::buildRelatedContactArray($selectAll, $ids, $processor, $componentTable);
133 $addPaymentHeader = FALSE;
135 list($outputColumns) = $processor->getExportStructureArrays();
137 if ($processor->isMergeSameAddress()) {
138 foreach (array_keys($processor->getAdditionalFieldsForSameAddressMerge()) as $field) {
139 if ($field === 'id') {
140 $field = 'civicrm_primary_id';
142 $processor->setColumnAsCalculationOnly($field);
146 $paymentDetails = [];
147 if ($processor->isExportPaymentFields()) {
148 // get payment related in for event and members
149 $paymentDetails = CRM_Contribute_BAO_Contribution
::getContributionDetails($exportMode, $ids);
150 //get all payment headers.
151 // If we haven't selected specific payment fields, load in all the
153 if (!$processor->isExportSpecifiedPaymentFields()) {
154 if (!empty($paymentDetails)) {
155 $addPaymentHeader = TRUE;
156 foreach (array_keys($processor->getPaymentHeaders()) as $paymentField) {
157 $processor->addOutputSpecification($paymentField);
163 $componentDetails = [];
165 $rowCount = self
::EXPORT_ROW_COUNT
;
167 // we write to temp table often to avoid using too much memory
172 $sqlColumns = $processor->getSQLColumns();
173 $processor->createTempTable();
174 $limitReached = FALSE;
176 while (!$limitReached) {
177 $limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}";
178 CRM_Core_DAO
::disableFullGroupByMode();
179 $iterationDAO = CRM_Core_DAO
::executeQuery($limitQuery);
180 CRM_Core_DAO
::reenableFullGroupByMode();
181 // If this is less than our limit by the end of the iteration we do not need to run the query again to
182 // check if some remain.
183 $rowsThisIteration = 0;
185 while ($iterationDAO->fetch()) {
187 $rowsThisIteration++
;
188 $row = $processor->buildRow($query, $iterationDAO, $outputColumns, $paymentDetails, $addPaymentHeader);
189 if ($row === FALSE) {
193 // add component info
194 // write the row to a file
195 $componentDetails[] = $row;
197 // output every $tempRowCount rows
198 if ($count %
$tempRowCount == 0) {
199 self
::writeDetailsToTable($processor, $componentDetails, $sqlColumns);
200 $componentDetails = [];
203 if ($rowsThisIteration < self
::EXPORT_ROW_COUNT
) {
204 $limitReached = TRUE;
206 $offset +
= $rowCount;
209 if ($processor->getTemporaryTable()) {
210 self
::writeDetailsToTable($processor, $componentDetails);
212 // do merge same address and merge same household processing
213 if ($mergeSameAddress) {
214 $processor->mergeSameAddress();
217 $processor->writeCSVFromTable();
219 // delete the export temp table and component table
220 $sql = "DROP TABLE IF EXISTS " . $processor->getTemporaryTable();
221 CRM_Core_DAO
::executeQuery($sql);
222 CRM_Core_DAO
::reenableFullGroupByMode();
223 CRM_Utils_System
::civiExit(0, ['processor' => $processor]);
226 CRM_Core_DAO
::reenableFullGroupByMode();
227 throw new CRM_Core_Exception(ts('No records to export'));
232 * Handle import error file creation.
234 public static function invoke() {
235 $type = CRM_Utils_Request
::retrieve('type', 'Positive');
236 $parserName = CRM_Utils_Request
::retrieve('parser', 'String');
237 if (empty($parserName) ||
empty($type)) {
241 // clean and ensure parserName is a valid string
242 $parserName = CRM_Utils_String
::munge($parserName);
243 $parserClass = explode('_', $parserName);
245 // make sure parserClass is in the CRM namespace and
246 // at least 3 levels deep
247 if ($parserClass[0] == 'CRM' &&
248 count($parserClass) >= 3
250 require_once str_replace('_', DIRECTORY_SEPARATOR
, $parserName) . ".php";
251 // ensure the functions exists
252 if (method_exists($parserName, 'errorFileName') &&
253 method_exists($parserName, 'saveFileName')
255 $errorFileName = $parserName::errorFileName($type);
256 $saveFileName = $parserName::saveFileName($type);
257 if (!empty($errorFileName) && !empty($saveFileName)) {
258 CRM_Utils_System
::setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
259 CRM_Utils_System
::setHttpHeader('Content-Description', 'File Transfer');
260 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/csv');
261 CRM_Utils_System
::setHttpHeader('Content-Length', filesize($errorFileName));
262 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename=' . $saveFileName);
264 readfile($errorFileName);
268 CRM_Utils_System
::civiExit();
272 * @param $customSearchClass
276 public static function exportCustom($customSearchClass, $formValues, $order) {
277 $ext = CRM_Extension_System
::singleton()->getMapper();
278 if (!$ext->isExtensionClass($customSearchClass)) {
279 require_once str_replace('_', DIRECTORY_SEPARATOR
, $customSearchClass) . '.php';
282 require_once $ext->classToPath($customSearchClass);
284 $search = new $customSearchClass($formValues);
286 $includeContactIDs = FALSE;
287 if ($formValues['radio_ts'] == 'ts_sel') {
288 $includeContactIDs = TRUE;
291 $sql = $search->all(0, 0, $order, $includeContactIDs);
293 $columns = $search->columns();
295 $header = array_keys($columns);
296 $fields = array_values($columns);
299 $dao = CRM_Core_DAO
::executeQuery($sql);
301 if (method_exists($search, 'alterRow')) {
304 while ($dao->fetch()) {
307 foreach ($fields as $field) {
308 $unqualified_field = CRM_Utils_Array
::First(array_slice(explode('.', $field), -1));
309 $row[$field] = $dao->$unqualified_field;
312 $search->alterRow($row);
317 CRM_Core_Report_Excel
::writeCSVFile(ts('CiviCRM Contact Search'), $header, $rows);
318 CRM_Utils_System
::civiExit();
322 * @param \CRM_Export_BAO_ExportProcessor $processor
325 public static function writeDetailsToTable($processor, $details) {
326 $tableName = $processor->getTemporaryTable();
327 if (empty($details)) {
336 $id = CRM_Core_DAO
::singleValueQuery($sql);
343 foreach ($details as $row) {
345 $valueString = [$id];
346 foreach ($row as $value) {
348 $valueString[] = "''";
351 $valueString[] = "'" . CRM_Core_DAO
::escapeString($value) . "'";
354 $sqlClause[] = '(' . implode(',', $valueString) . ')';
356 $sqlColumns = array_merge(['id' => 1], $processor->getSQLColumns());
357 $sqlColumnString = '(`' . implode('`, `', array_keys($sqlColumns)) . '`)';
359 $sqlValueString = implode(",\n", $sqlClause);
362 INSERT INTO $tableName $sqlColumnString
363 VALUES $sqlValueString
365 CRM_Core_DAO
::executeQuery($sql);
369 * Get the ids that we want to get related contact details for.
372 * @param int $exportMode
376 protected static function getIDsForRelatedContact($ids, $exportMode) {
377 if ($exportMode == CRM_Export_Form_Select
::CONTACT_EXPORT
) {
380 if ($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
) {
382 $sourceID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
383 $dao = CRM_Core_DAO
::executeQuery("
384 SELECT contact_id FROM civicrm_activity_contact
385 WHERE activity_id IN ( " . implode(',', $ids) . ") AND
386 record_type_id = {$sourceID}
389 while ($dao->fetch()) {
390 $relIDs[] = $dao->contact_id
;
394 $componentMapping = [
395 CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
=> 'civicrm_contribution',
396 CRM_Export_Form_Select
::EVENT_EXPORT
=> 'civicrm_participant',
397 CRM_Export_Form_Select
::MEMBER_EXPORT
=> 'civicrm_membership',
398 CRM_Export_Form_Select
::PLEDGE_EXPORT
=> 'civicrm_pledge',
399 CRM_Export_Form_Select
::GRANT_EXPORT
=> 'civicrm_grant',
402 if ($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
) {
403 return CRM_Case_BAO_Case
::retrieveContactIdsByCaseId($ids);
406 return CRM_Core_DAO
::getContactIDsFromComponent($ids, $componentMapping[$exportMode]);
413 * @param \CRM_Export_BAO_ExportProcessor $processor
414 * @param $componentTable
416 protected static function buildRelatedContactArray($selectAll, $ids, $processor, $componentTable) {
417 $allRelContactArray = $relationQuery = [];
418 $queryMode = $processor->getQueryMode();
419 $exportMode = $processor->getExportMode();
421 foreach ($processor->getRelationshipReturnProperties() as $relationshipKey => $relationReturnProperties) {
422 $allRelContactArray[$relationshipKey] = [];
423 // build Query for each relationship
424 $relationQuery = new CRM_Contact_BAO_Query(NULL, $relationReturnProperties,
425 NULL, FALSE, FALSE, $queryMode
427 list($relationSelect, $relationFrom, $relationWhere, $relationHaving) = $relationQuery->query();
429 list($id, $direction) = explode('_', $relationshipKey, 2);
430 // identify the relationship direction
431 $contactA = 'contact_id_a';
432 $contactB = 'contact_id_b';
433 if ($direction == 'b_a') {
434 $contactA = 'contact_id_b';
435 $contactB = 'contact_id_a';
437 $relIDs = self
::getIDsForRelatedContact($ids, $exportMode);
439 $relationshipJoin = $relationshipClause = '';
440 if (!$selectAll && $componentTable) {
441 $relationshipJoin = " INNER JOIN {$componentTable} ctTable ON ctTable.contact_id = {$contactA}";
443 elseif (!empty($relIDs)) {
444 $relID = implode(',', $relIDs);
445 $relationshipClause = " AND crel.{$contactA} IN ( {$relID} )";
448 $relationFrom = " {$relationFrom}
449 INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_a.id AND crel.relationship_type_id = {$id}
450 {$relationshipJoin} ";
452 //check for active relationship status only
453 $today = date('Ymd');
454 $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
455 $relationWhere = " WHERE contact_a.is_deleted = 0 {$relationshipClause} {$relationActive}";
456 CRM_Core_DAO
::disableFullGroupByMode();
457 $relationSelect = "{$relationSelect}, {$contactA} as refContact ";
458 $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationHaving GROUP BY crel.{$contactA}";
460 $allRelContactDAO = CRM_Core_DAO
::executeQuery($relationQueryString);
461 CRM_Core_DAO
::reenableFullGroupByMode();
463 while ($allRelContactDAO->fetch()) {
464 $relationQuery->convertToPseudoNames($allRelContactDAO);
466 // @todo pass processor to fetchRelationshipDetails and set fields directly within it.
467 $processor->fetchRelationshipDetails($allRelContactDAO, $relationReturnProperties, $relationshipKey, $row);
468 foreach (array_keys($relationReturnProperties) as $property) {
469 if ($property === 'location') {
470 // @todo - simplify location in fetchRelationshipDetails - remove handling here. Or just call
471 // $processor->setRelationshipValue from fetchRelationshipDetails
472 foreach ($relationReturnProperties['location'] as $locationName => $locationValues) {
473 foreach (array_keys($locationValues) as $locationValue) {
474 $key = str_replace(' ', '_', $locationName) . '-' . $locationValue;
475 $processor->setRelationshipValue($relationshipKey, $allRelContactDAO->refContact
, $key, $row[$relationshipKey . '__' . $key]);
480 $processor->setRelationshipValue($relationshipKey, $allRelContactDAO->refContact
, $property, $row[$relationshipKey . '_' . $property]);