class CRM_Export_BAO_ExportProcessor

Class CRM_Export_BAO_ExportProcessor

Class to handle logic of export.

Properties

protected int $queryMode
protected int $exportMode
protected array $queryFields Array of fields in the main query.
protected string $queryOperator Either AND or OR.
protected array|null $requestedFields Requested output fields.
protected bool $isMergeSameHousehold Is the contact being merged into a single household.
protected bool $isMergeSameAddress Should contacts with the same address be merged.
protected array $additionalFieldsForSameAddressMerge Fields that need to be retrieved for address merge purposes but should not be in output.
protected array $contactGreetingFields Fields used for merging same contacts.
protected array $ids An array of primary IDs of the entity being exported.
protected array $additionalFieldsForPostalExport Additional fields required to export postal fields.
protected bool $isPostalableOnly Only export contacts that can receive postal mail.
protected array $relationshipTypes Key representing the head of household in the relationship array.
protected array $relationshipReturnProperties Array of properties to retrieve for relationships.
protected array $exportedHouseholds IDs of households that have already been exported.
protected array $householdsToSkip Households to skip during export as they will be exported via their relationships anyway.
protected array $additionalRequestedReturnProperties Additional fields to return.
protected array $relatedContactValues Export values for related contacts.
protected array $returnProperties
protected array $outputSpecification
protected string $componentTable
protected string $componentClause Clause from component search.
protected string $temporaryTable Name of a temporary table created to hold the results.
protected $postalGreetingTemplate
protected $addresseeGreetingTemplate

Methods

array
getAdditionalFieldsForSameAddressMerge()

Get additional non-visible fields for address merge purposes.

setAdditionalFieldsForSameAddressMerge()

Set additional non-visible fields for address merge purposes.

bool
isMergeSameAddress()

Should contacts with the same address be merged.

setIsMergeSameAddress(bool $isMergeSameAddress)

Set same address is to be merged.

array
getAdditionalFieldsForPostalExport()

Get additional fields required to do a postal export.

setAdditionalFieldsForPostalExport()

Set additional fields required for a postal export.

array
getAdditionalRequestedReturnProperties()

Get additional return properties.

setAdditionalRequestedReturnProperties(array $value)

Set additional return properties.

array
getRelationshipReturnProperties()

Get return properties by relationship.

string
getComponentTable()

No description

setComponentTable(string $componentTable)

Set the component table (if any).

string
getComponentClause()

No description

setComponentClause(string $componentClause)

No description

string
getTemporaryTable()

No description

setTemporaryTable(string $temporaryTable)

No description

mixed
getPostalGreetingTemplate()

No description

setPostalGreetingTemplate(mixed $postalGreetingTemplate)

No description

mixed
getAddresseeGreetingTemplate()

No description

setAddresseeGreetingTemplate(mixed $addresseeGreetingTemplate)

No description

__construct(int $exportMode, array|null $requestedFields, string $queryOperator, bool $isMergeSameHousehold = FALSE, bool $isPostalableOnly = FALSE, bool $isMergeSameAddress = FALSE, array $formValues = [])

CRM_Export_BAO_ExportProcessor constructor.

bool
isPostalableOnly()

No description

setIsPostalableOnly(bool $isPostalableOnly)

No description

array|null
getRequestedFields()

No description

setRequestedFields(array|null $requestedFields)

No description

array
getReturnProperties()

No description

setReturnProperties(array $returnProperties)

No description

array
getRelationshipTypes()

No description

setRelationshipTypes()

No description

setRelationshipValue(string $relationshipType, int $contactID, string $field, string $value)

Set the value for a relationship type field.

string
getRelationshipValue(string $relationshipType, int $contactID, string $field)

Get the value for a relationship type field.

int
getRelatedHouseholdID(int $contactID, string $relationshipType)

Get the id of the related household.

bool
isHouseholdExported(int $housholdContactID)

Has the household already been exported.

bool
isMergeSameHousehold()

No description

setIsMergeSameHousehold(bool $isMergeSameHousehold)

No description

mixed
getHouseholdRelationshipTypes()

Return relationship types for household merge.

bool
isRelationshipTypeKey($fieldName)

No description

bool
isHouseholdMergeRelationshipTypeKey($fieldName)

No description

string
getQueryOperator()

No description

setQueryOperator(string $queryOperator)

No description

array
getIds()

No description

setIds(array $ids)

No description

array
getQueryFields()

No description

setQueryFields(array $queryFields)

No description

int
getQueryMode()

No description

setQueryMode()

Set the query mode based on the export mode.

int
getExportMode()

No description

setExportMode(int $exportMode)

No description

string
getExportFileName()

Get the name for the export file.

string
getHeaderForRow(string $field)

Get the label for the header row based on the field to output.

array
runQuery($params, $order)

No description

addOutputSpecification(string $key, string $relationshipType = NULL, string $locationType = NULL, int $entityTypeID = NULL)

Add a row to the specification for how to output data.

array
getMetaDataForField($key)

Get the metadata for the given field.

setSqlColumnDefn($key)

No description

setColumnAsCalculationOnly(string $column)

Mark a column as only required for calculations.

array
getHeaderRows()

No description

array
getSQLColumns()

No description

array
getMetadata()

No description

array|bool
buildRow(CRM_Contact_BAO_Query $query, CRM_Core_DAO $iterationDAO, array $outputColumns, $metadata, $paymentDetails, $addPaymentHeader)

Build the row for output.

bool
getHouseholdMergeTypeForRow($contactID)

If this row has a household whose details we should use get the relationship type key.

markHouseholdExported($householdID)

Mark the given household as already exported.

string
getTransformedFieldValue($field, $iterationDAO, $fieldValue, $metadata, $paymentDetails)

No description

array
getAdditionalReturnProperties()

Get array of fields to return, over & above those defined in the main contact exportable fields.

isExportPaymentFields()

Should payment fields be appended to the export.

bool
isExportSpecifiedPaymentFields()

Has specific payment fields been requested (as opposed to via all fields).

getPaymentTableID()

Get the name of the id field in the table that connects contributions to the export entity.

bool
hasRequestedComponentPaymentFields()

Have component payment fields been requested.

array
getComponentPaymentFields()

Get fields that indicate payment fields have been requested for a component.

getPaymentHeaders()

Get headers for payment fields.

array
getDefaultReturnProperties()

Get the default properties when not specified.

array
setRelationshipReturnProperties(array $value, string $relationshipKey)

Add the field to relationship return properties & return it.

setHouseholdMergeReturnProperties()

Add the main return properties to the household merge properties if needed for merging.

array
getValidLocationFields()

Get the default location fields to request.

mixed
getSqlColumnDefinition(string $fieldName, string $columnName)

Get the sql column definition for the given field.

string
getMungedFieldName(string $field)

Get the munged field name.

string
getOutputSpecificationIndex(string $key, string $relationshipType, string $locationType, $entityLabel)

In order to respect the history of this class we need to index kinda illogically.

string
getOutputSpecificationLabel(string $key, string $relationshipType, string $locationType, string $entityLabel)

Get the compiled label for the column.

string
getOutputSpecificationFieldKey(string $key, $relationshipType, $locationType, $entityLabel)

Get the mysql field name key.

mixed
getWhereParams()

Get params for the where criteria.

buildRelationshipFieldsForRow($row, $contactID, $value, $field)

No description

bool
isHouseholdToSkip(int $contactID)

Is this contact a household that is already set to be exported by virtue of it's household members.

array
getExportStructureArrays()

Get the various arrays that we use to structure our output.

string
defaultReturnProperty()

Get default return property for export based on mode

array
determineReturnProperties()

Determine the required return properties from the input parameters.

string
getGroupBy(object $query)

No description

array
replaceMergeTokens(int $contactId)

No description

array
buildMasterCopyArray($sql, bool $sharedAddress = FALSE)

Build array for merging same addresses.

mergeSameAddress()

Merge contacts with the same address.

mixed
trimNonTokensFromAddressString(string $parsedString, string $defaultGreeting, string $greetingLabel)

The function unsets static part of the string, if token is the dynamic part.

array
getPreview(int $limit)

Preview export output.

mixed
setGreetingStringsForSameAddressMerge(array $formValues)

Set the template strings to be used when merging two contacts with the same address.

createTempTable()

Create the temporary table for output.

fetchRelationshipDetails(CRM_Core_DAO $relDAO, array $value, string $field, array $row)

Get the values of linked household contact.

writeCSVFromTable()

Write to the csv from the temp table.

instantiateTempTable(array $headerRows)

Set up the temp table.

writeRows(array $headerRows, array $componentDetails)

No description

Details

at line 115
array getAdditionalFieldsForSameAddressMerge()

Get additional non-visible fields for address merge purposes.

Return Value

array

at line 122
setAdditionalFieldsForSameAddressMerge()

Set additional non-visible fields for address merge purposes.

at line 139
bool isMergeSameAddress()

Should contacts with the same address be merged.

Return Value

bool

at line 148
setIsMergeSameAddress(bool $isMergeSameAddress)

Set same address is to be merged.

Parameters

bool $isMergeSameAddress

at line 164
array getAdditionalFieldsForPostalExport()

Get additional fields required to do a postal export.

Return Value

array

at line 171
setAdditionalFieldsForPostalExport()

Set additional fields required for a postal export.

at line 237
array getAdditionalRequestedReturnProperties()

Get additional return properties.

Return Value

array

at line 246
setAdditionalRequestedReturnProperties(array $value)

Set additional return properties.

Parameters

array $value

at line 259
array getRelationshipReturnProperties()

Get return properties by relationship.

Return Value

array

at line 288
string getComponentTable()

Return Value

string

at line 297
setComponentTable(string $componentTable)

Set the component table (if any).

Parameters

string $componentTable

at line 311
string getComponentClause()

Return Value

string

at line 318
setComponentClause(string $componentClause)

Parameters

string $componentClause

at line 336
string getTemporaryTable()

Return Value

string

at line 343
setTemporaryTable(string $temporaryTable)

Parameters

string $temporaryTable

at line 352
mixed getPostalGreetingTemplate()

Return Value

mixed

at line 359
setPostalGreetingTemplate(mixed $postalGreetingTemplate)

Parameters

mixed $postalGreetingTemplate

at line 366
mixed getAddresseeGreetingTemplate()

Return Value

mixed

at line 373
setAddresseeGreetingTemplate(mixed $addresseeGreetingTemplate)

Parameters

mixed $addresseeGreetingTemplate

at line 395
__construct(int $exportMode, array|null $requestedFields, string $queryOperator, bool $isMergeSameHousehold = FALSE, bool $isPostalableOnly = FALSE, bool $isMergeSameAddress = FALSE, array $formValues = [])

CRM_Export_BAO_ExportProcessor constructor.

Parameters

int $exportMode
array|null $requestedFields
string $queryOperator
bool $isMergeSameHousehold
bool $isPostalableOnly
bool $isMergeSameAddress
array $formValues Values from the export options form on contact export. We currently support these keys - postal_greeting - postal_other - addresee_greeting - addressee_other

at line 414
bool isPostalableOnly()

Return Value

bool

at line 421
setIsPostalableOnly(bool $isPostalableOnly)

Parameters

bool $isPostalableOnly

at line 428
array|null getRequestedFields()

Return Value

array|null

at line 435
setRequestedFields(array|null $requestedFields)

Parameters

array|null $requestedFields

at line 442
array getReturnProperties()

Return Value

array

at line 449
setReturnProperties(array $returnProperties)

Parameters

array $returnProperties

at line 456
array getRelationshipTypes()

Return Value

array

at line 462
setRelationshipTypes()

at line 487
setRelationshipValue(string $relationshipType, int $contactID, string $field, string $value)

Set the value for a relationship type field.

In this case we are building up an array of properties for a related contact.

These may be used for direct exporting or for merge to household depending on the options selected.

Parameters

string $relationshipType
int $contactID
string $field
string $value

at line 508
string getRelationshipValue(string $relationshipType, int $contactID, string $field)

Get the value for a relationship type field.

In this case we are building up an array of properties for a related contact.

These may be used for direct exporting or for merge to household depending on the options selected.

Parameters

string $relationshipType
int $contactID
string $field

Return Value

string

at line 520
int getRelatedHouseholdID(int $contactID, string $relationshipType)

Get the id of the related household.

Parameters

int $contactID
string $relationshipType

Return Value

int

at line 531
bool isHouseholdExported(int $housholdContactID)

Has the household already been exported.

Parameters

int $housholdContactID

Return Value

bool

at line 539
bool isMergeSameHousehold()

Return Value

bool

at line 546
setIsMergeSameHousehold(bool $isMergeSameHousehold)

Parameters

bool $isMergeSameHousehold

at line 555
mixed getHouseholdRelationshipTypes()

Return relationship types for household merge.

Return Value

mixed

at line 569
bool isRelationshipTypeKey($fieldName)

Parameters

$fieldName

Return Value

bool

at line 577
bool isHouseholdMergeRelationshipTypeKey($fieldName)

Parameters

$fieldName

Return Value

bool

at line 584
string getQueryOperator()

Return Value

string

at line 591
setQueryOperator(string $queryOperator)

Parameters

string $queryOperator

at line 598
array getIds()

Return Value

array

at line 605
setIds(array $ids)

Parameters

array $ids

at line 612
array getQueryFields()

Return Value

array

at line 622
setQueryFields(array $queryFields)

Parameters

array $queryFields

at line 636
int getQueryMode()

Return Value

int

at line 643
setQueryMode()

Set the query mode based on the export mode.

at line 682
int getExportMode()

Return Value

int

at line 689
setExportMode(int $exportMode)

Parameters

int $exportMode

at line 698
string getExportFileName()

Get the name for the export file.

Return Value

string

at line 739
string getHeaderForRow(string $field)

Get the label for the header row based on the field to output.

Parameters

string $field

Return Value

string

at line 765
array runQuery($params, $order)

Parameters

$params
$order

Return Value

array

at line 845
addOutputSpecification(string $key, string $relationshipType = NULL, string $locationType = NULL, int $entityTypeID = NULL)

Add a row to the specification for how to output data.

Parameters

string $key
string $relationshipType
string $locationType
int $entityTypeID phone_type_id or provider_id for phone or im fields.

at line 878
array getMetaDataForField($key)

Get the metadata for the given field.

Parameters

$key

Return Value

array

at line 892
setSqlColumnDefn($key)

Parameters

$key

at line 903
setColumnAsCalculationOnly(string $column)

Mark a column as only required for calculations.

Do not include the row with headers.

Parameters

string $column

at line 910
array getHeaderRows()

Return Value

array

at line 923
array getSQLColumns()

Return Value

array

at line 936
array getMetadata()

Return Value

array

at line 956
array|bool buildRow(CRM_Contact_BAO_Query $query, CRM_Core_DAO $iterationDAO, array $outputColumns, $metadata, $paymentDetails, $addPaymentHeader)

Build the row for output.

Parameters

CRM_Contact_BAO_Query $query
CRM_Core_DAO $iterationDAO
array $outputColumns
$metadata
$paymentDetails
$addPaymentHeader

Return Value

array|bool

at line 1048
bool getHouseholdMergeTypeForRow($contactID)

If this row has a household whose details we should use get the relationship type key.

Parameters

$contactID

Return Value

bool

at line 1064
markHouseholdExported($householdID)

Mark the given household as already exported.

Parameters

$householdID

at line 1077
string getTransformedFieldValue($field, $iterationDAO, $fieldValue, $metadata, $paymentDetails)

Parameters

$field
$iterationDAO
$fieldValue
$metadata
$paymentDetails

Return Value

string

at line 1185
array getAdditionalReturnProperties()

Get array of fields to return, over & above those defined in the main contact exportable fields.

These include export mode specific fields & some fields apparently required as 'exportableFields' but not returned by the function of the same name.

Return Value

array Array of fields to return in the format ['field_name' => 1,...]

at line 1214
isExportPaymentFields()

Should payment fields be appended to the export.

(This is pretty hacky so hopefully this function won't last long - notice how obviously it should be part of the above function!).

at line 1236
bool isExportSpecifiedPaymentFields()

Has specific payment fields been requested (as opposed to via all fields).

If specific fields have been requested then they get added at various points.

Return Value

bool

at line 1245
getPaymentTableID()

Get the name of the id field in the table that connects contributions to the export entity.

at line 1265
protected bool hasRequestedComponentPaymentFields()

Have component payment fields been requested.

Return Value

bool

at line 1282
array getComponentPaymentFields()

Get fields that indicate payment fields have been requested for a component.

Ideally this should be protected but making it temporarily public helps refactoring..

Return Value

array

at line 1298
getPaymentHeaders()

Get headers for payment fields.

Returns an array of contribution fields when the entity supports payment fields and specific fields are not specified. This is a transitional function for refactoring legacy code.

at line 1315
array getDefaultReturnProperties()

Get the default properties when not specified.

In the UI this appears as 'Primary fields only' but in practice it's most of the kitchen sink and the hallway closet thrown in.

Since CRM-952 custom fields are excluded, but no other form of mercy is shown.

Return Value

array

at line 1344
array setRelationshipReturnProperties(array $value, string $relationshipKey)

Add the field to relationship return properties & return it.

This function is doing both setting & getting which is yuck but it is an interim refactor.

Parameters

array $value
string $relationshipKey

Return Value

array

at line 1374
setHouseholdMergeReturnProperties()

Add the main return properties to the household merge properties if needed for merging.

If we are using household merge we need to add these to the relationship properties to be retrieved.

at line 1389
array getValidLocationFields()

Get the default location fields to request.

Return Value

array

at line 1416
mixed getSqlColumnDefinition(string $fieldName, string $columnName)

Get the sql column definition for the given field.

Parameters

string $fieldName
string $columnName

Return Value

mixed

at line 1519
string getMungedFieldName(string $field)

Get the munged field name.

Parameters

string $field

Return Value

string

at line 1541
protected string getOutputSpecificationIndex(string $key, string $relationshipType, string $locationType, $entityLabel)

In order to respect the history of this class we need to index kinda illogically.

On the bright side - this stuff is tested within a nano-byte of it's life.

e.g '2-a-b_Home-City'

Parameters

string $key
string $relationshipType
string $locationType
$entityLabel

Return Value

string

at line 1571
protected string getOutputSpecificationLabel(string $key, string $relationshipType, string $locationType, string $entityLabel)

Get the compiled label for the column.

e.g 'Gender', 'Employee Of-Home-city'

Parameters

string $key
string $relationshipType
string $locationType
string $entityLabel

Return Value

string

at line 1594
protected string getOutputSpecificationFieldKey(string $key, $relationshipType, $locationType, $entityLabel)

Get the mysql field name key.

This key is locked in by tests but the reasons for the specific conventions - ie. headings are used for keying fields in some cases, are likely accidental rather than deliberate.

This key is used for the output sql array.

Parameters

string $key
$relationshipType
$locationType
$entityLabel

Return Value

string

at line 1618
mixed getWhereParams()

Get params for the where criteria.

Return Value

mixed

at line 1633
protected buildRelationshipFieldsForRow($row, $contactID, $value, $field)

Parameters

$row
$contactID
$value
$field

at line 1657
protected bool isHouseholdToSkip(int $contactID)

Is this contact a household that is already set to be exported by virtue of it's household members.

Parameters

int $contactID

Return Value

bool

at line 1686
array getExportStructureArrays()

Get the various arrays that we use to structure our output.

The extraction of these has been moved to a separate function for clarity and so that tests can be added - in particular on the $outputHeaders array.

However it still feels a bit like something that I'm too polite to write down and this should be seen as a step on the refactoring path rather than how it should be.

Return Value

array
  • outputColumns Array of columns to be exported. The values don't matter but the key must match the alias for the field generated by BAO_Query object.
    • headerRows Array of the column header strings to put in the csv header - non-associative.
    • sqlColumns Array of column names for the temp table. Not too sure why outputColumns can't be used here.
    • metadata Array of fields with specific parameters to pass to the translate function or another hacky nasty solution I'm too embarassed to discuss here. The keys need
    • to match the outputColumns keys (yes, the fact we ignore the output columns values & then pass another array with values we could use does suggest further refactors. However, you future improver, do remember that every check you do in the main DAO loop is done once per row & that coule be 100,000 times.) Finally a pop quiz: We need the translate context because we use a function other than ts() - is this because
    • a) the function used is more efficient or
    • b) this code is old & outdated. Submit your answers to circular bin or better yet find a way to comment them for posterity.

at line 1739
string defaultReturnProperty()

Get default return property for export based on mode

Return Value

string Default Return property

at line 1772
array determineReturnProperties()

Determine the required return properties from the input parameters.

Return Value

array

at line 1840
string getGroupBy(object $query)

Parameters

object $query CRM_Contact_BAO_Query

Return Value

string Group By Clause

at line 1883
array replaceMergeTokens(int $contactId)

Parameters

int $contactId

Return Value

array

at line 1922
array buildMasterCopyArray($sql, bool $sharedAddress = FALSE)

Build array for merging same addresses.

Parameters

$sql
bool $sharedAddress

Return Value

array

at line 2008
mergeSameAddress()

Merge contacts with the same address.

at line 2115
mixed trimNonTokensFromAddressString(string $parsedString, string $defaultGreeting, string $greetingLabel)

The function unsets static part of the string, if token is the dynamic part.

Example: 'Hello {contact.first_name}' => converted to => '{contact.first_name}' i.e 'Hello Alan' => converted to => 'Alan'

Parameters

string $parsedString
string $defaultGreeting
string $greetingLabel

Return Value

mixed

at line 2138
array getPreview(int $limit)

Preview export output.

Parameters

int $limit

Return Value

array

at line 2163
protected mixed setGreetingStringsForSameAddressMerge(array $formValues)

Set the template strings to be used when merging two contacts with the same address.

Parameters

array $formValues Values from first form. In this case we care about the keys - postal_greeting - postal_other - address_greeting - addressee_other

Return Value

mixed

at line 2194
createTempTable()

Create the temporary table for output.

at line 2237
fetchRelationshipDetails(CRM_Core_DAO $relDAO, array $value, string $field, array $row)

Get the values of linked household contact.

Parameters

CRM_Core_DAO $relDAO
array $value
string $field
array $row

Exceptions

Exception

at line 2350
writeCSVFromTable()

Write to the csv from the temp table.

at line 2410
protected instantiateTempTable(array $headerRows)

Set up the temp table.

Parameters

array $headerRows

at line 2424
protected writeRows(array $headerRows, array $componentDetails)

Parameters

array $headerRows
array $componentDetails