From 0b23ab2b7e68c641775973d6307339563b723c9e Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 25 Jul 2020 18:42:51 +1200 Subject: [PATCH] Add phone type pseudoconstant to metadata --- CRM/Contact/BAO/Contact.php | 6 ++- CRM/Contact/BAO/Query.php | 3 ++ CRM/Core/DAO/Phone.php | 4 +- CRM/Export/BAO/ExportProcessor.php | 13 ++--- tests/phpunit/CRM/Export/BAO/ExportTest.php | 53 +++++++++++++-------- xml/schema/Core/Phone.xml | 2 +- 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 6a105253d6..29c82bd900 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -1562,9 +1562,13 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); ], ]; + $phoneFields = CRM_Core_DAO_Phone::export(); + // This adds phone_type to the exportable fields and make it available for export. + // with testing the same can be done to the other entities. + CRM_Core_DAO::appendPseudoConstantsToFields($phoneFields); $locationFields = array_merge($locationType, CRM_Core_DAO_Address::export(), - CRM_Core_DAO_Phone::export(), + $phoneFields, CRM_Core_DAO_Email::export(), $IMProvider, CRM_Core_DAO_IM::export(TRUE), diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index a6b2545d8b..1b707301d1 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1121,6 +1121,9 @@ class CRM_Contact_BAO_Query { } $field = $this->_fields[$elementName] ?? NULL; + if (isset($this->_pseudoConstantsSelect[$field['name']])) { + $this->_pseudoConstantsSelect[$name . '-' . $field['name']] = $this->_pseudoConstantsSelect[$field['name']]; + } // hack for profile, add location id if (!$field) { diff --git a/CRM/Core/DAO/Phone.php b/CRM/Core/DAO/Phone.php index 444c0b3a53..ca7ff686ba 100644 --- a/CRM/Core/DAO/Phone.php +++ b/CRM/Core/DAO/Phone.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Core/Phone.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:3c1f1743128af72b009c6cf4effc1da2) + * (GenCodeChecksum:8a2fbbdd180e8991adb84c230c9993e6) */ /** @@ -289,7 +289,7 @@ class CRM_Core_DAO_Phone extends CRM_Core_DAO { 'phone_type_id' => [ 'name' => 'phone_type_id', 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Phone Type'), + 'title' => ts('Phone Type ID'), 'description' => ts('Which type of phone does this number belongs.'), 'where' => 'civicrm_phone.phone_type_id', 'export' => TRUE, diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 1dc733d9fd..681697984b 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -971,7 +971,6 @@ class CRM_Export_BAO_ExportProcessor { if ($this->isHouseholdToSkip($iterationDAO->contact_id)) { return FALSE; } - $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); $row = []; @@ -1002,10 +1001,7 @@ class CRM_Export_BAO_ExportProcessor { if (property_exists($iterationDAO, $field)) { $fieldValue = $iterationDAO->$field; // to get phone type from phone type id - if ($field == 'phone_type_id' && isset($phoneTypes[$fieldValue])) { - $fieldValue = $phoneTypes[$fieldValue]; - } - elseif ($field == 'provider_id' || $field == 'im_provider') { + if ($field == 'provider_id' || $field == 'im_provider') { $fieldValue = $imProviders[$fieldValue] ?? NULL; } elseif (strstr($field, 'master_id')) { @@ -1152,7 +1148,7 @@ class CRM_Export_BAO_ExportProcessor { if (!empty($fieldSpec['context'])) { return $i18n->crm_translate($fieldValue, $fieldSpec); } - if (!empty($fieldSpec['pseudoconstant']) && !empty($fieldSpec['hasLocationType'])) { + if (!empty($fieldSpec['pseudoconstant']) && !empty($fieldSpec['hasLocationType']) && $fieldSpec['name'] !== 'phone_type_id') { if (!empty($fieldSpec['bao'])) { $transformedValue = CRM_Core_PseudoConstant::getLabel($fieldSpec['bao'], $fieldSpec['name'], $fieldValue); if ($transformedValue) { @@ -2171,10 +2167,7 @@ WHERE id IN ( $deleteIDString ) 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') { + if ($relationField == 'provider_id') { $fieldValue = $imProviders[$relationValue] ?? NULL; } // CRM-13995 diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index dd0eb83661..b2359ed4f0 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -1,5 +1,7 @@ addWhere('name', '=', 'Much Much longer than just phone')->setValues(['label' => 'Mobile'])->execute(); if (!empty($this->locationTypes)) { $this->callAPISuccess('LocationType', 'delete', ['id' => $this->locationTypes['Whare Kai']['id']]); @@ -277,6 +280,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'Country' => 'Netherlands', 'Phone' => '', 'Phone Extension' => '', + 'Phone Type ID' => '', 'Phone Type' => '', 'Email' => 'home@example.com', 'On Hold' => 'No', @@ -795,12 +799,16 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * * Less over the top complete than the im test. * + * @throws \API_Exception * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException * @throws \League\Csv\Exception */ public function testExportPhoneData() { $this->contactIDs[] = $this->individualCreate(); $this->contactIDs[] = $this->individualCreate(); + + OptionValue::update()->addWhere('name', '=', 'Mobile')->setValues(['label' => 'Much Much longer than just phone'])->execute(); $locationTypes = ['Billing' => 'Billing', 'Home' => 'Home']; $phoneTypes = ['Mobile', 'Phone']; foreach ($this->contactIDs as $contactID) { @@ -839,26 +847,29 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { foreach (array_keys(array_merge($locationTypes, [' ' => ['Primary']])) as $locationType) { $locationTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType); $fields[] = ['name' => 'phone', 'location_type_id' => $locationTypeID]; + $fields[] = ['name' => 'phone_type', 'location_type_id' => $locationTypeID]; $fields[] = ['name' => 'phone_type_id', 'location_type_id' => $locationTypeID]; foreach ($relationships as $contactID => $relationship) { $fields[] = ['name' => 'phone_type_id', 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; + $fields[] = ['name' => 'phone_type', 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; } foreach ($phoneTypes as $phoneType) { $phoneTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', $phoneType); $fields[] = ['name' => 'phone', 'phone_type_id' => $phoneTypeID, 'location_type_id' => $locationTypeID]; foreach ($relationships as $contactID => $relationship) { $fields[] = ['name' => 'phone_type_id', 'phone_type_id' => $phoneTypeID, 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; + $fields[] = ['name' => 'phone_type', 'phone_type_id' => $phoneTypeID, 'relationship_type_id' => $relationship['relationship_type_id'], 'relationship_direction' => 'a_b', 'location_type_id' => $locationTypeID]; } } } $this->doExportTest(['fields' => $fields, 'ids' => [$this->contactIDs[0]]]); foreach ($this->csv->getRecords() as $row) { - $this->assertEquals('BillingMobile3', $row['Billing-Phone-Mobile']); + $this->assertEquals('BillingMobile3', $row['Billing-Phone-Much Much longer than just phone']); $this->assertEquals('', $row['Billing-Phone-Phone']); - $this->assertEquals('Phone', $row['Spouse of-Phone Type']); - $this->assertEquals('Mobile', $row['Phone Type']); - $this->assertEquals('Mobile', $row['Billing-Phone Type']); + $this->assertEquals('Much Much longer than just phone', $row['Spouse of-Phone Type']); + $this->assertEquals('Much Much longer than just phone', $row['Phone Type']); + $this->assertEquals('Much Much longer than just phone', $row['Billing-Phone Type']); } } @@ -1087,6 +1098,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'Country' => 'Netherlands', 'Phone' => '', 'Phone Extension' => '', + 'Phone Type ID' => '', 'Phone Type' => '', 'Email' => 'home@example.com', 'On Hold' => 'No', @@ -1556,6 +1568,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'tags' => 1, 'notes' => 1, 'phone_type_id' => 1, + 'phone_type' => 1, ]; if (!$isContactMode) { unset($returnProperties['groups']); @@ -2228,25 +2241,26 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 68 => 'Country', 69 => 'Phone', 70 => 'Phone Extension', - 71 => 'Phone Type', - 72 => 'Email', - 73 => 'On Hold', - 74 => 'Use for Bulk Mail', - 75 => 'Signature Text', - 76 => 'Signature Html', - 77 => 'IM Provider', - 78 => 'IM Screen Name', - 79 => 'OpenID', - 80 => 'World Region', - 81 => 'Website', - 82 => 'Group(s)', - 83 => 'Tag(s)', - 84 => 'Note(s)', + 71 => 'Phone Type ID', + 72 => 'Phone Type', + 73 => 'Email', + 74 => 'On Hold', + 75 => 'Use for Bulk Mail', + 76 => 'Signature Text', + 77 => 'Signature Html', + 78 => 'IM Provider', + 79 => 'IM Screen Name', + 80 => 'OpenID', + 81 => 'World Region', + 82 => 'Website', + 83 => 'Group(s)', + 84 => 'Tag(s)', + 85 => 'Note(s)', ]; if (!$isContactExport) { - unset($headers[82]); unset($headers[83]); unset($headers[84]); + unset($headers[85]); } return $headers; } @@ -2532,6 +2546,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'groups' => '`groups` text', 'tags' => '`tags` text', 'notes' => '`notes` text', + 'phone_type' => '`phone_type` varchar(255)', ]; if (!$isContactExport) { unset($columns['groups']); diff --git a/xml/schema/Core/Phone.xml b/xml/schema/Core/Phone.xml index 453c64db5f..f2b95e2478 100644 --- a/xml/schema/Core/Phone.xml +++ b/xml/schema/Core/Phone.xml @@ -148,7 +148,7 @@ phone_type_id - Phone Type + Phone Type ID int unsigned true Which type of phone does this number belongs. -- 2.25.1