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 // ensure the functions exists
251 if (method_exists($parserName, 'errorFileName') &&
252 method_exists($parserName, 'saveFileName')
254 $errorFileName = $parserName::errorFileName($type);
255 $saveFileName = $parserName::saveFileName($type);
256 if (!empty($errorFileName) && !empty($saveFileName)) {
257 CRM_Utils_System
::setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
258 CRM_Utils_System
::setHttpHeader('Content-Description', 'File Transfer');
259 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/csv');
260 CRM_Utils_System
::setHttpHeader('Content-Length', filesize($errorFileName));
261 CRM_Utils_System
::setHttpHeader('Content-Disposition', 'attachment; filename=' . $saveFileName);
263 readfile($errorFileName);
267 CRM_Utils_System
::civiExit();
271 * @param $customSearchClass
275 public static function exportCustom($customSearchClass, $formValues, $order) {
276 $ext = CRM_Extension_System
::singleton()->getMapper();
277 if (!$ext->isExtensionClass($customSearchClass)) {
278 require_once str_replace('_', DIRECTORY_SEPARATOR
, $customSearchClass) . '.php';
281 require_once $ext->classToPath($customSearchClass);
283 $search = new $customSearchClass($formValues);
285 $includeContactIDs = FALSE;
286 if ($formValues['radio_ts'] == 'ts_sel') {
287 $includeContactIDs = TRUE;
290 $sql = $search->all(0, 0, $order, $includeContactIDs);
292 $columns = $search->columns();
294 $header = array_keys($columns);
295 $fields = array_values($columns);
298 $dao = CRM_Core_DAO
::executeQuery($sql);
300 if (method_exists($search, 'alterRow')) {
303 while ($dao->fetch()) {
306 foreach ($fields as $field) {
307 $unqualified_field = CRM_Utils_Array
::First(array_slice(explode('.', $field), -1));
308 $row[$field] = $dao->$unqualified_field;
311 $search->alterRow($row);
316 CRM_Core_Report_Excel
::writeCSVFile(ts('CiviCRM Contact Search'), $header, $rows);
317 CRM_Utils_System
::civiExit();
321 * @param \CRM_Export_BAO_ExportProcessor $processor
324 public static function writeDetailsToTable($processor, $details) {
325 $tableName = $processor->getTemporaryTable();
326 if (empty($details)) {
335 $id = CRM_Core_DAO
::singleValueQuery($sql);
342 foreach ($details as $row) {
344 $valueString = [$id];
345 foreach ($row as $value) {
347 $valueString[] = "''";
350 $valueString[] = "'" . CRM_Core_DAO
::escapeString($value) . "'";
353 $sqlClause[] = '(' . implode(',', $valueString) . ')';
355 $sqlColumns = array_merge(['id' => 1], $processor->getSQLColumns());
356 $sqlColumnString = '(`' . implode('`, `', array_keys($sqlColumns)) . '`)';
358 $sqlValueString = implode(",\n", $sqlClause);
361 INSERT INTO $tableName $sqlColumnString
362 VALUES $sqlValueString
364 CRM_Core_DAO
::executeQuery($sql);
368 * Get the ids that we want to get related contact details for.
371 * @param int $exportMode
375 protected static function getIDsForRelatedContact($ids, $exportMode) {
376 if ($exportMode == CRM_Export_Form_Select
::CONTACT_EXPORT
) {
379 if ($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
) {
381 $sourceID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
382 $dao = CRM_Core_DAO
::executeQuery("
383 SELECT contact_id FROM civicrm_activity_contact
384 WHERE activity_id IN ( " . implode(',', $ids) . ") AND
385 record_type_id = {$sourceID}
388 while ($dao->fetch()) {
389 $relIDs[] = $dao->contact_id
;
393 $componentMapping = [
394 CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
=> 'civicrm_contribution',
395 CRM_Export_Form_Select
::EVENT_EXPORT
=> 'civicrm_participant',
396 CRM_Export_Form_Select
::MEMBER_EXPORT
=> 'civicrm_membership',
397 CRM_Export_Form_Select
::PLEDGE_EXPORT
=> 'civicrm_pledge',
398 CRM_Export_Form_Select
::GRANT_EXPORT
=> 'civicrm_grant',
401 if ($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
) {
402 return CRM_Case_BAO_Case
::retrieveContactIdsByCaseId($ids);
405 return CRM_Core_DAO
::getContactIDsFromComponent($ids, $componentMapping[$exportMode]);
412 * @param \CRM_Export_BAO_ExportProcessor $processor
413 * @param $componentTable
415 protected static function buildRelatedContactArray($selectAll, $ids, $processor, $componentTable) {
416 $allRelContactArray = $relationQuery = [];
417 $queryMode = $processor->getQueryMode();
418 $exportMode = $processor->getExportMode();
420 foreach ($processor->getRelationshipReturnProperties() as $relationshipKey => $relationReturnProperties) {
421 $allRelContactArray[$relationshipKey] = [];
422 // build Query for each relationship
423 $relationQuery = new CRM_Contact_BAO_Query(NULL, $relationReturnProperties,
424 NULL, FALSE, FALSE, $queryMode
426 list($relationSelect, $relationFrom, $relationWhere, $relationHaving) = $relationQuery->query();
428 list($id, $direction) = explode('_', $relationshipKey, 2);
429 // identify the relationship direction
430 $contactA = 'contact_id_a';
431 $contactB = 'contact_id_b';
432 if ($direction == 'b_a') {
433 $contactA = 'contact_id_b';
434 $contactB = 'contact_id_a';
436 $relIDs = self
::getIDsForRelatedContact($ids, $exportMode);
438 $relationshipJoin = $relationshipClause = '';
439 if (!$selectAll && $componentTable) {
440 $field = 'contact_id';
441 if ($componentTable === 'civicrm_contact') {
444 $relationshipJoin = " INNER JOIN {$componentTable} ctTable ON ctTable.$field = {$contactA}";
446 elseif (!empty($relIDs)) {
447 $relID = implode(',', $relIDs);
448 $relationshipClause = " AND crel.{$contactA} IN ( {$relID} )";
451 $relationFrom = " {$relationFrom}
452 INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_a.id AND crel.relationship_type_id = {$id}
453 {$relationshipJoin} ";
455 //check for active relationship status only
456 $today = date('Ymd');
457 $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
458 $relationWhere = " WHERE contact_a.is_deleted = 0 {$relationshipClause} {$relationActive}";
459 CRM_Core_DAO
::disableFullGroupByMode();
460 $relationSelect = "{$relationSelect}, {$contactA} as refContact ";
461 $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationHaving GROUP BY crel.{$contactA}";
463 $allRelContactDAO = CRM_Core_DAO
::executeQuery($relationQueryString);
464 CRM_Core_DAO
::reenableFullGroupByMode();
466 while ($allRelContactDAO->fetch()) {
467 $relationQuery->convertToPseudoNames($allRelContactDAO);
469 // @todo pass processor to fetchRelationshipDetails and set fields directly within it.
470 $processor->fetchRelationshipDetails($allRelContactDAO, $relationReturnProperties, $relationshipKey, $row);
471 foreach (array_keys($relationReturnProperties) as $property) {
472 if ($property === 'location') {
473 // @todo - simplify location in fetchRelationshipDetails - remove handling here. Or just call
474 // $processor->setRelationshipValue from fetchRelationshipDetails
475 foreach ($relationReturnProperties['location'] as $locationName => $locationValues) {
476 foreach (array_keys($locationValues) as $locationValue) {
477 $key = str_replace(' ', '_', $locationName) . '-' . $locationValue;
478 $processor->setRelationshipValue($relationshipKey, $allRelContactDAO->refContact
, $key, $row[$relationshipKey . '__' . $key]);
483 $processor->setRelationshipValue($relationshipKey, $allRelContactDAO->refContact
, $property, $row[$relationshipKey . '_' . $property]);