* @param bool $isMergeSameHousehold
* @param bool $isPostalableOnly
* @param bool $isMergeSameAddress
- */
- public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE, $isPostalableOnly = FALSE, $isMergeSameAddress = FALSE) {
+ * @param array $formValues
+ * Values from the export options form on contact export. We currently support these keys
+ * - postal_greeting
+ * - postal_other
+ * - addresee_greeting
+ * - addressee_other
+ */
+ public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE, $isPostalableOnly = FALSE, $isMergeSameAddress = FALSE, $formValues = []) {
$this->setExportMode($exportMode);
$this->setQueryMode();
$this->setQueryOperator($queryOperator);
$this->setAdditionalFieldsForSameAddressMerge();
$this->setAdditionalFieldsForPostalExport();
$this->setHouseholdMergeReturnProperties();
+ $this->setGreetingStringsForSameAddressMerge($formValues);
}
/**
*/
public function runQuery($params, $order) {
$returnProperties = $this->getReturnProperties();
- $addressWhere = '';
$params = array_merge($params, $this->getWhereParams());
- if ($this->isPostalableOnly) {
- if (array_key_exists('street_address', $returnProperties)) {
- $addressWhere = " civicrm_address.street_address <> ''";
- if (array_key_exists('supplemental_address_1', $returnProperties)) {
- // We need this to be an OR rather than AND on the street_address so, hack it in.
- $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
- 'address_options', TRUE, NULL, TRUE
- );
- if (!empty($addressOptions['supplemental_address_1'])) {
- $addressWhere .= " OR civicrm_address.supplemental_address_1 <> ''";
- }
- }
- $addressWhere = ' AND (' . $addressWhere . ')';
- }
- }
+
$query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
FALSE, FALSE, $this->getQueryMode(),
FALSE, TRUE, TRUE, NULL, $this->getQueryOperator()
}
}
+ if ($this->isPostalableOnly) {
+ if (array_key_exists('street_address', $returnProperties)) {
+ $addressWhere = " civicrm_address.street_address <> ''";
+ if (array_key_exists('supplemental_address_1', $returnProperties)) {
+ // We need this to be an OR rather than AND on the street_address so, hack it in.
+ $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
+ 'address_options', TRUE, NULL, TRUE
+ );
+ if (!empty($addressOptions['supplemental_address_1'])) {
+ $addressWhere .= " OR civicrm_address.supplemental_address_1 <> ''";
+ }
+ }
+ $whereClauses['address'] = $addressWhere;
+ }
+ }
+
if (empty($where)) {
$where = "WHERE " . implode(' AND ', $whereClauses);
}
$fieldValue = CRM_Utils_Array::value($fieldValue, $imProviders);
}
elseif (strstr($field, 'master_id')) {
- $masterAddressId = NULL;
- if (isset($iterationDAO->$field)) {
- $masterAddressId = $iterationDAO->$field;
- }
+ // @todo - why not just $field === 'master_id' - what else would it be?
+ $masterAddressId = $iterationDAO->$field ?? NULL;
// get display name of contact that address is shared.
$fieldValue = CRM_Contact_BAO_Contact::getMasterDisplayName($masterAddressId);
}
}
$queryFields = $this->getQueryFields();
- $lookUp = ['prefix_id', 'suffix_id'];
+ // @todo remove the enotice avoidance here, ensure all columns are declared.
+ // tests will fail on the enotices until they all are & then all the 'else'
+ // below can go.
+ $fieldSpec = $queryFields[$columnName] ?? [];
+
// set the sql columns
- if (isset($queryFields[$columnName]['type'])) {
- switch ($queryFields[$columnName]['type']) {
+ if (isset($fieldSpec['type'])) {
+ switch ($fieldSpec['type']) {
case CRM_Utils_Type::T_INT:
case CRM_Utils_Type::T_BOOLEAN:
- if (in_array($columnName, $lookUp)) {
- return "$fieldName varchar(255)";
- }
- else {
- return "$fieldName varchar(16)";
- }
+ return "$fieldName varchar(16)";
case CRM_Utils_Type::T_STRING:
if (isset($queryFields[$columnName]['maxlength'])) {
/**
* @param int $contactId
- * @param array $exportParams
*
* @return array
*/
- public function replaceMergeTokens($contactId, $exportParams) {
+ public function replaceMergeTokens($contactId) {
$greetings = [];
$contact = NULL;
'addressee' => $this->getAddresseeGreetingTemplate(),
];
foreach ($greetingFields as $greeting => $greetingLabel) {
- if (!empty($exportParams[$greeting])) {
+ $tokens = CRM_Utils_Token::getTokens($greetingLabel);
+ if (!empty($tokens)) {
if (empty($contact)) {
$values = [
'id' => $contactId,
* Build array for merging same addresses.
*
* @param $sql
- * @param array $exportParams
* @param bool $sharedAddress
*
* @return array
*/
- public function buildMasterCopyArray($sql, $exportParams, $sharedAddress = FALSE) {
+ public function buildMasterCopyArray($sql, $sharedAddress = FALSE) {
$addresseeOptions = CRM_Core_OptionGroup::values('addressee');
$postalOptions = CRM_Core_OptionGroup::values('postal_greeting');
if (!$sharedAddress) {
if (!isset($this->contactGreetingFields[$dao->master_contact_id])) {
- $this->contactGreetingFields[$dao->master_contact_id] = $this->replaceMergeTokens($dao->master_contact_id, $exportParams);
+ $this->contactGreetingFields[$dao->master_contact_id] = $this->replaceMergeTokens($dao->master_contact_id);
}
$masterPostalGreeting = CRM_Utils_Array::value('postal_greeting',
$this->contactGreetingFields[$dao->master_contact_id], $dao->master_postal_greeting
);
if (!isset($contactGreetingTokens[$dao->copy_contact_id])) {
- $this->contactGreetingFields[$dao->copy_contact_id] = $this->replaceMergeTokens($dao->copy_contact_id, $exportParams);
+ $this->contactGreetingFields[$dao->copy_contact_id] = $this->replaceMergeTokens($dao->copy_contact_id);
}
$copyPostalGreeting = CRM_Utils_Array::value('postal_greeting',
$this->contactGreetingFields[$dao->copy_contact_id], $dao->copy_postal_greeting
if (!$sharedAddress && !array_key_exists($copyID, $merge[$masterID]['copy'])) {
- if (!empty($exportParams['postal_greeting_other']) &&
- count($merge[$masterID]['copy']) >= 1
- ) {
- // use static greetings specified if no of contacts > 2
- $merge[$masterID]['postalGreeting'] = $exportParams['postal_greeting_other'];
- }
- elseif ($copyPostalGreeting) {
+ if ($copyPostalGreeting) {
$this->trimNonTokensFromAddressString($copyPostalGreeting,
$postalOptions[$dao->copy_postal_greeting_id],
- $exportParams
+ $this->getPostalGreetingTemplate()
);
$merge[$masterID]['postalGreeting'] = "{$merge[$masterID]['postalGreeting']}, {$copyPostalGreeting}";
// if there happens to be a duplicate, remove it
$merge[$masterID]['postalGreeting'] = str_replace(" {$copyPostalGreeting},", "", $merge[$masterID]['postalGreeting']);
}
- if (!empty($exportParams['addressee_other']) &&
- count($merge[$masterID]['copy']) >= 1
- ) {
- // use static greetings specified if no of contacts > 2
- $merge[$masterID]['addressee'] = $exportParams['addressee_other'];
- }
- elseif ($copyAddressee) {
+ if ($copyAddressee) {
$this->trimNonTokensFromAddressString($copyAddressee,
$addresseeOptions[$dao->copy_addressee_id],
- $exportParams, 'addressee'
+ $this->getAddresseeGreetingTemplate()
);
$merge[$masterID]['addressee'] = "{$merge[$masterID]['addressee']}, " . trim($copyAddressee);
}
/**
* Merge contacts with the same address.
- *
- * @param $sqlColumns
- * @param array $exportParams
*/
- public function mergeSameAddress(&$sqlColumns, $exportParams) {
- $greetingOptions = CRM_Export_Form_Select::getGreetingOptions();
-
- if (!empty($greetingOptions)) {
- // Greeting options is keyed by 'postal_greeting' or 'addressee'.
- foreach ($greetingOptions as $key => $value) {
- if ($option = CRM_Utils_Array::value($key, $exportParams)) {
- if ($greetingOptions[$key][$option] == ts('Other')) {
- $exportParams[$key] = $exportParams["{$key}_other"];
- }
- elseif ($greetingOptions[$key][$option] == ts('List of names')) {
- $exportParams[$key] = '';
- }
- else {
- $exportParams[$key] = $greetingOptions[$key][$option];
- }
- }
- }
- }
- if (!empty($exportParams['postal_greeting'])) {
- $this->setPostalGreetingTemplate($exportParams['postal_greeting']);
- }
- if (!empty($exportParams['addressee'])) {
- $this->setAddresseeGreetingTemplate($exportParams['addressee']);
- }
+ public function mergeSameAddress() {
$tableName = $this->getTemporaryTable();
// check if any records are present based on if they have used shared address feature,
INNER JOIN civicrm_address adr ON r1.master_id = adr.id
INNER JOIN $tableName r2 ON adr.contact_id = r2.civicrm_primary_id
ORDER BY r1.id";
- $linkedMerge = $this->buildMasterCopyArray($sql, $exportParams, TRUE);
+ $linkedMerge = $this->buildMasterCopyArray($sql, TRUE);
// find all the records that have the same street address BUT not in a household
// require match on city and state as well
AND r2.id > r1.id
ORDER BY r1.id
";
- $merge = $this->buildMasterCopyArray($sql, $exportParams);
+ $merge = $this->buildMasterCopyArray($sql);
// unset ids from $merge already present in $linkedMerge
foreach ($linkedMerge as $masterID => $values) {
";
CRM_Core_DAO::executeQuery($sql);
}
-
- // unset temporary columns that were added for postal mailing format
- // @todo - this part is pretty close to ready to be removed....
- if (!empty($exportParams['merge_same_address']['temp_columns'])) {
- $unsetKeys = array_keys($sqlColumns);
- foreach ($unsetKeys as $headerKey => $sqlColKey) {
- if (array_key_exists($sqlColKey, $exportParams['merge_same_address']['temp_columns'])) {
- unset($sqlColumns[$sqlColKey]);
- }
- }
- }
}
/**
*
* @param string $parsedString
* @param string $defaultGreeting
- * @param bool $addressMergeGreetings
- * @param string $greetingType
+ * @param string $greetingLabel
*
* @return mixed
*/
public function trimNonTokensFromAddressString(
&$parsedString, $defaultGreeting,
- $addressMergeGreetings, $greetingType = 'postal_greeting'
+ $greetingLabel
) {
- if (!empty($addressMergeGreetings[$greetingType])) {
- $greetingLabel = $addressMergeGreetings[$greetingType];
- }
$greetingLabel = empty($greetingLabel) ? $defaultGreeting : $greetingLabel;
$stringsToBeReplaced = preg_replace('/(\{[a-zA-Z._ ]+\})/', ';;', $greetingLabel);
return $rows;
}
+ /**
+ * Set the template strings to be used when merging two contacts with the same address.
+ *
+ * @param array $formValues
+ * Values from first form. In this case we care about the keys
+ * - postal_greeting
+ * - postal_other
+ * - address_greeting
+ * - addressee_other
+ *
+ * @return mixed
+ */
+ protected function setGreetingStringsForSameAddressMerge($formValues) {
+ $greetingOptions = CRM_Export_Form_Select::getGreetingOptions();
+
+ if (!empty($greetingOptions)) {
+ // Greeting options is keyed by 'postal_greeting' or 'addressee'.
+ foreach ($greetingOptions as $key => $value) {
+ $option = CRM_Utils_Array::value($key, $formValues);
+ if ($option) {
+ if ($greetingOptions[$key][$option] == ts('Other')) {
+ $formValues[$key] = $formValues["{$key}_other"];
+ }
+ elseif ($greetingOptions[$key][$option] == ts('List of names')) {
+ $formValues[$key] = '';
+ }
+ else {
+ $formValues[$key] = $greetingOptions[$key][$option];
+ }
+ }
+ }
+ }
+ if (!empty($formValues['postal_greeting'])) {
+ $this->setPostalGreetingTemplate($formValues['postal_greeting']);
+ }
+ if (!empty($formValues['addressee'])) {
+ $this->setAddresseeGreetingTemplate($formValues['addressee']);
+ }
+ }
+
+ /**
+ * Create the temporary table for output.
+ */
+ public function createTempTable() {
+ //creating a temporary table for the search result that need be exported
+ $exportTempTable = CRM_Utils_SQL_TempTable::build()->setDurable()->setCategory('export');
+ $sqlColumns = $this->getSQLColumns();
+ // also create the sql table
+ $exportTempTable->drop();
+
+ $sql = " id int unsigned NOT NULL AUTO_INCREMENT, ";
+ if (!empty($sqlColumns)) {
+ $sql .= implode(",\n", array_values($sqlColumns)) . ',';
+ }
+
+ $sql .= "\n PRIMARY KEY ( id )";
+
+ // add indexes for street_address and household_name if present
+ $addIndices = [
+ 'street_address',
+ 'household_name',
+ 'civicrm_primary_id',
+ ];
+
+ foreach ($addIndices as $index) {
+ if (isset($sqlColumns[$index])) {
+ $sql .= ",
+ INDEX index_{$index}( $index )
+";
+ }
+ }
+
+ $exportTempTable->createWithColumns($sql);
+ $this->setTemporaryTable($exportTempTable->getName());
+ }
+
+ /**
+ * Get the values of linked household contact.
+ *
+ * @param CRM_Core_DAO $relDAO
+ * @param array $value
+ * @param string $field
+ * @param array $row
+ *
+ * @throws \Exception
+ */
+ public function fetchRelationshipDetails($relDAO, $value, $field, &$row) {
+ $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
+ $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
+ $i18n = CRM_Core_I18n::singleton();
+ $field = $field . '_';
+
+ foreach ($value as $relationField => $relationValue) {
+ if (is_object($relDAO) && property_exists($relDAO, $relationField)) {
+ $fieldValue = $relDAO->$relationField;
+ if ($relationField == 'phone_type_id') {
+ $fieldValue = $phoneTypes[$relationValue];
+ }
+ elseif ($relationField == 'provider_id') {
+ $fieldValue = CRM_Utils_Array::value($relationValue, $imProviders);
+ }
+ // CRM-13995
+ elseif (is_object($relDAO) && in_array($relationField, [
+ 'email_greeting',
+ 'postal_greeting',
+ 'addressee',
+ ])) {
+ //special case for greeting replacement
+ $fldValue = "{$relationField}_display";
+ $fieldValue = $relDAO->$fldValue;
+ }
+ }
+ elseif (is_object($relDAO) && $relationField == 'state_province') {
+ $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id);
+ }
+ elseif (is_object($relDAO) && $relationField == 'country') {
+ $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id);
+ }
+ else {
+ $fieldValue = '';
+ }
+ $relPrefix = $field . $relationField;
+
+ if (is_object($relDAO) && $relationField == 'id') {
+ $row[$relPrefix] = $relDAO->contact_id;
+ }
+ elseif (is_array($relationValue) && $relationField == 'location') {
+ foreach ($relationValue as $ltype => $val) {
+ // If the location name has a space in it the we need to handle that. This
+ // is kinda hacky but specifically covered in the ExportTest so later efforts to
+ // improve it should be secure in the knowled it will be caught.
+ $ltype = str_replace(' ', '_', $ltype);
+ foreach (array_keys($val) as $fld) {
+ $type = explode('-', $fld);
+ $fldValue = "{$ltype}-" . $type[0];
+ if (!empty($type[1])) {
+ $fldValue .= "-" . $type[1];
+ }
+ // CRM-3157: localise country, region (both have ‘country’ context)
+ // and state_province (‘province’ context)
+ switch (TRUE) {
+ case (!is_object($relDAO)):
+ $row[$field . '_' . $fldValue] = '';
+ break;
+
+ case in_array('country', $type):
+ case in_array('world_region', $type):
+ $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
+ ['context' => 'country']
+ );
+ break;
+
+ case in_array('state_province', $type):
+ $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
+ ['context' => 'province']
+ );
+ break;
+
+ default:
+ $row[$field . '_' . $fldValue] = $relDAO->$fldValue;
+ break;
+ }
+ }
+ }
+ }
+ elseif (isset($fieldValue) && $fieldValue != '') {
+ //check for custom data
+ if ($cfID = CRM_Core_BAO_CustomField::getKeyID($relationField)) {
+ $row[$relPrefix] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID);
+ }
+ else {
+ //normal relationship fields
+ // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context)
+ switch ($relationField) {
+ case 'country':
+ case 'world_region':
+ $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'country']);
+ break;
+
+ case 'state_province':
+ $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'province']);
+ break;
+
+ default:
+ $row[$relPrefix] = $fieldValue;
+ break;
+ }
+ }
+ }
+ else {
+ // if relation field is empty or null
+ $row[$relPrefix] = '';
+ }
+ }
+ }
+
+ /**
+ * Write to the csv from the temp table.
+ */
+ public function writeCSVFromTable() {
+ // call export hook
+ $headerRows = $this->getHeaderRows();
+ $exportTempTable = $this->getTemporaryTable();
+ CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode, $componentTable, $ids);
+ if ($exportTempTable !== $this->getTemporaryTable()) {
+ CRM_Core_Error::deprecatedFunctionWarning('altering the export table in the hook is deprecated (in some flows the table itself will be)');
+ $this->setTemporaryTable($exportTempTable);
+ }
+ $exportTempTable = $this->getTemporaryTable();
+ $writeHeader = TRUE;
+ $offset = 0;
+ // increase this number a lot to avoid making too many queries
+ // LIMIT is not much faster than a no LIMIT query
+ // CRM-7675
+ $limit = 100000;
+
+ $query = "SELECT * FROM $exportTempTable";
+
+ while (1) {
+ $limitQuery = $query . "
+LIMIT $offset, $limit
+";
+ $dao = CRM_Core_DAO::executeQuery($limitQuery);
+
+ if ($dao->N <= 0) {
+ break;
+ }
+
+ $componentDetails = [];
+ while ($dao->fetch()) {
+ $row = [];
+
+ foreach (array_keys($this->getSQLColumns()) as $column) {
+ $row[$column] = $dao->$column;
+ }
+ $componentDetails[] = $row;
+ }
+ CRM_Core_Report_Excel::writeCSVFile($this->getExportFileName(),
+ $headerRows,
+ $componentDetails,
+ NULL,
+ $writeHeader
+ );
+
+ $writeHeader = FALSE;
+ $offset += $limit;
+ }
+ }
+
}