Skip to content

Commit

Permalink
Add phone type pseudoconstant to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jul 26, 2020
1 parent 072b977 commit f438be5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
6 changes: 5 additions & 1 deletion CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1562,9 +1562,13 @@ public static function &exportableFields($contactType = 'Individual', $status =
],
];

$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),
Expand Down
3 changes: 3 additions & 0 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,9 @@ public function addHierarchicalElements() {
}

$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) {
Expand Down
4 changes: 2 additions & 2 deletions CRM/Core/DAO/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/Phone.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:3c1f1743128af72b009c6cf4effc1da2)
* (GenCodeChecksum:8a2fbbdd180e8991adb84c230c9993e6)
*/

/**
Expand Down Expand Up @@ -289,7 +289,7 @@ public static function &fields() {
'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,
Expand Down
13 changes: 3 additions & 10 deletions CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,6 @@ public function buildRow($query, $iterationDAO, $outputColumns, $paymentDetails,
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 = [];
Expand Down Expand Up @@ -1002,10 +1001,7 @@ public function buildRow($query, $iterationDAO, $outputColumns, $paymentDetails,
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')) {
Expand Down Expand Up @@ -1152,7 +1148,7 @@ public function getTransformedFieldValue($field, $iterationDAO, $fieldValue, $pa
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) {
Expand Down Expand Up @@ -2171,10 +2167,7 @@ public function fetchRelationshipDetails($relDAO, $value, $field, &$row) {
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
Expand Down
53 changes: 34 additions & 19 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Civi\Api4\OptionValue;

/**
* Class CRM_Core_DAOTest
*
Expand Down Expand Up @@ -80,6 +82,7 @@ public function tearDown() {
'civicrm_case_activity',
'civicrm_campaign',
]);
OptionValue::update()->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']]);
Expand Down Expand Up @@ -277,6 +280,7 @@ public function testExportComponentsMembership() {
'Country' => 'Netherlands',
'Phone' => '',
'Phone Extension' => '',
'Phone Type ID' => '',
'Phone Type' => '',
'Email' => 'home@example.com',
'On Hold' => 'No',
Expand Down Expand Up @@ -795,12 +799,16 @@ public function testExportIMData() {
*
* 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) {
Expand Down Expand Up @@ -839,26 +847,29 @@ public function testExportPhoneData() {
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']);
}
}

Expand Down Expand Up @@ -1087,6 +1098,7 @@ public function testMergeSameAddress() {
'Country' => 'Netherlands',
'Phone' => '',
'Phone Extension' => '',
'Phone Type ID' => '',
'Phone Type' => '',
'Email' => 'home@example.com',
'On Hold' => 'No',
Expand Down Expand Up @@ -1556,6 +1568,7 @@ protected function getBasicReturnProperties($isContactMode) {
'tags' => 1,
'notes' => 1,
'phone_type_id' => 1,
'phone_type' => 1,
];
if (!$isContactMode) {
unset($returnProperties['groups']);
Expand Down Expand Up @@ -2229,25 +2242,26 @@ protected function getBasicHeaderDefinition($isContactExport) {
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;
}
Expand Down Expand Up @@ -2534,6 +2548,7 @@ protected function getBasicSqlColumnDefinition($isContactExport) {
'groups' => '`groups` text',
'tags' => '`tags` text',
'notes' => '`notes` text',
'phone_type' => '`phone_type` varchar(255)',
];
if (!$isContactExport) {
unset($columns['groups']);
Expand Down
2 changes: 1 addition & 1 deletion xml/schema/Core/Phone.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
</field>
<field>
<name>phone_type_id</name>
<title>Phone Type</title>
<title>Phone Type ID</title>
<type>int unsigned</type>
<export>true</export>
<comment>Which type of phone does this number belongs.</comment>
Expand Down

0 comments on commit f438be5

Please sign in to comment.