From f5c0f096725a7358a85088a95ae48feb6fc68e43 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 28 Dec 2019 21:17:12 -0500 Subject: [PATCH] Add contact type to schema to allow GetFields filtering --- CRM/Contact/DAO/Contact.php | 19 +++++++++++++- CRM/Core/CodeGen/Specification.php | 1 + .../Provider/ContactCreationSpecProvider.php | 6 +++-- Civi/Api4/Service/Spec/SpecGatherer.php | 8 ++++-- .../api/v4/Action/GetExtraFieldsTest.php | 25 +++++++++++++------ xml/schema/Contact/Contact.xml | 17 +++++++++++++ xml/templates/dao.tpl | 4 ++- 7 files changed, 66 insertions(+), 14 deletions(-) diff --git a/CRM/Contact/DAO/Contact.php b/CRM/Contact/DAO/Contact.php index 3c14834a79..99405af76c 100644 --- a/CRM/Contact/DAO/Contact.php +++ b/CRM/Contact/DAO/Contact.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Contact/Contact.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:b72765d873639076299753a8c4e676d5) + * (GenCodeChecksum:773bf2fc16c1af3c283f6ecc7098e927) */ /** @@ -434,6 +434,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'size' => CRM_Utils_Type::BIG, 'where' => 'civicrm_contact.contact_type', 'export' => TRUE, + 'contactType' => NULL, 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -847,6 +848,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^first|(f(irst\s)?name)$/i', 'dataPattern' => '/^\w+$/', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -867,6 +869,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^middle|(m(iddle\s)?name)$/i', 'dataPattern' => '/^\w+$/', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -887,6 +890,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^last|(l(ast\s)?name)$/i', 'dataPattern' => '/^\w+(\s\w+)?+$/', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -905,6 +909,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^(prefix|title)/i', 'dataPattern' => '/^(mr|ms|mrs|sir|dr)\.?$/i', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -927,6 +932,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^suffix$/i', 'dataPattern' => '/^(sr|jr)\.?|i{2,}$/', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -950,6 +956,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'where' => 'civicrm_contact.formal_title', 'headerPattern' => '/^title/i', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1141,6 +1148,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^job|(j(ob\s)?title)$/i', 'dataPattern' => '//', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1158,6 +1166,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'where' => 'civicrm_contact.gender_id', 'headerPattern' => '/^gender$/i', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1180,6 +1189,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^birth|(b(irth\s)?date)|D(\W*)O(\W*)B(\W*)$/i', 'dataPattern' => '/\d{4}-?\d{2}-?\d{2}/', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1197,6 +1207,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'where' => 'civicrm_contact.is_deceased', 'headerPattern' => '/i(s\s)?d(eceased)$/i', 'export' => TRUE, + 'contactType' => 'Individual', 'default' => '0', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', @@ -1215,6 +1226,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'where' => 'civicrm_contact.deceased_date', 'headerPattern' => '/^deceased|(d(eceased\s)?date)$/i', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1236,6 +1248,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^household|(h(ousehold\s)?name)$/i', 'dataPattern' => '/^\w+$/', 'export' => TRUE, + 'contactType' => 'Household', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1250,6 +1263,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'title' => ts('Household Primary Contact ID'), 'description' => ts('Optional FK to Primary Contact for this household.'), 'where' => 'civicrm_contact.primary_contact_id', + 'contactType' => 'Household', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1268,6 +1282,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'headerPattern' => '/^organization|(o(rganization\s)?name)$/i', 'dataPattern' => '/^\w+$/', 'export' => TRUE, + 'contactType' => 'Organization', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1287,6 +1302,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'where' => 'civicrm_contact.sic_code', 'headerPattern' => '/^sic|(s(ic\s)?code)$/i', 'export' => TRUE, + 'contactType' => 'Organization', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', @@ -1323,6 +1339,7 @@ class CRM_Contact_DAO_Contact extends CRM_Core_DAO { 'description' => ts('OPTIONAL FK to civicrm_contact record.'), 'where' => 'civicrm_contact.employer_id', 'export' => TRUE, + 'contactType' => 'Individual', 'table_name' => 'civicrm_contact', 'entity' => 'Contact', 'bao' => 'CRM_Contact_BAO_Contact', diff --git a/CRM/Core/CodeGen/Specification.php b/CRM/Core/CodeGen/Specification.php index 17bfc45a55..5ee9bb2025 100644 --- a/CRM/Core/CodeGen/Specification.php +++ b/CRM/Core/CodeGen/Specification.php @@ -371,6 +371,7 @@ class CRM_Core_CodeGen_Specification { $field['uniqueTitle'] = $this->value('uniqueTitle', $fieldXML); $field['serialize'] = $this->value('serialize', $fieldXML); $field['html'] = $this->value('html', $fieldXML); + $field['contactType'] = $this->value('contactType', $fieldXML); if (isset($fieldXML->permission)) { $field['permission'] = trim($this->value('permission', $fieldXML)); $field['permission'] = $field['permission'] ? array_filter(array_map('trim', explode(',', $field['permission']))) : []; diff --git a/Civi/Api4/Service/Spec/Provider/ContactCreationSpecProvider.php b/Civi/Api4/Service/Spec/Provider/ContactCreationSpecProvider.php index 4810d03d05..1f77f16d2d 100644 --- a/Civi/Api4/Service/Spec/Provider/ContactCreationSpecProvider.php +++ b/Civi/Api4/Service/Spec/Provider/ContactCreationSpecProvider.php @@ -29,8 +29,10 @@ class ContactCreationSpecProvider implements Generic\SpecProviderInterface { * @param \Civi\Api4\Service\Spec\RequestSpec $spec */ public function modifySpec(RequestSpec $spec) { - $spec->getFieldByName('contact_type') - ->setDefaultValue('Individual'); + $contactTypeField = $spec->getFieldByName('contact_type'); + if ($contactTypeField) { + $contactTypeField->setDefaultValue('Individual'); + } $spec->getFieldByName('is_opt_out')->setRequired(FALSE); $spec->getFieldByName('is_deleted')->setRequired(FALSE); diff --git a/Civi/Api4/Service/Spec/SpecGatherer.php b/Civi/Api4/Service/Spec/SpecGatherer.php index d10c95ecf1..0edb4e8c6a 100644 --- a/Civi/Api4/Service/Spec/SpecGatherer.php +++ b/Civi/Api4/Service/Spec/SpecGatherer.php @@ -56,7 +56,7 @@ class SpecGatherer { // Real entities if (strpos($entity, 'Custom_') !== 0) { - $this->addDAOFields($entity, $action, $specification); + $this->addDAOFields($entity, $action, $specification, $values); if ($includeCustom && array_key_exists($entity, \CRM_Core_SelectValues::customGroupExtends())) { $this->addCustomFields($entity, $specification, $values); } @@ -93,14 +93,18 @@ class SpecGatherer { * @param string $entity * @param string $action * @param \Civi\Api4\Service\Spec\RequestSpec $specification + * @param array $values */ - private function addDAOFields($entity, $action, RequestSpec $specification) { + private function addDAOFields($entity, $action, RequestSpec $specification, $values = []) { $DAOFields = $this->getDAOFields($entity); foreach ($DAOFields as $DAOField) { if ($DAOField['name'] == 'id' && $action == 'create') { continue; } + if (array_key_exists('contactType', $DAOField) && !empty($values['contact_type']) && $DAOField['contactType'] != $values['contact_type']) { + continue; + } if ($action !== 'create' || isset($DAOField['default'])) { $DAOField['required'] = FALSE; } diff --git a/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php b/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php index adab653b86..60126b16de 100644 --- a/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php +++ b/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php @@ -29,17 +29,26 @@ use Civi\Api4\Contact; */ class GetExtraFieldsTest extends UnitTestCase { - public function testBAOFieldsWillBeReturned() { - $returnedFields = Contact::getFields() - ->execute() - ->getArrayCopy(); + public function testGetFieldsByContactType() { + $getFields = Contact::getFields()->setCheckPermissions(FALSE)->addSelect('name')->setIncludeCustom(FALSE); - $baseFields = \CRM_Contact_BAO_Contact::fields(); - $baseFieldNames = array_column($baseFields, 'name'); - $returnedFieldNames = array_column($returnedFields, 'name'); - $notReturned = array_diff($baseFieldNames, $returnedFieldNames); + $baseFields = array_column(\CRM_Contact_BAO_Contact::fields(), 'name'); + $returnedFields = $getFields->execute()->column('name'); + $notReturned = array_diff($baseFields, $returnedFields); + // With no contact_type specified, all fields should be returned $this->assertEmpty($notReturned); + + $individualFields = $getFields->setValues(['contact_type' => 'Individual'])->execute()->column('name'); + $this->assertNotContains('sic_code', $individualFields); + $this->assertNotContains('contact_type', $individualFields); + $this->assertContains('first_name', $individualFields); + + $organizationFields = $getFields->setValues(['contact_type' => 'Organization'])->execute()->column('name'); + $this->assertContains('sic_code', $organizationFields); + $this->assertNotContains('contact_type', $organizationFields); + $this->assertNotContains('first_name', $organizationFields); + $this->assertNotContains('household_name', $organizationFields); } } diff --git a/xml/schema/Contact/Contact.xml b/xml/schema/Contact/Contact.xml index 7facb329ca..262358eaf1 100644 --- a/xml/schema/Contact/Contact.xml +++ b/xml/schema/Contact/Contact.xml @@ -39,6 +39,7 @@ 1.1 3.1 + null index_contact_type @@ -385,6 +386,7 @@ First Name. 1.1 + Individual index_first_name @@ -405,6 +407,7 @@ Middle Name. 1.1 + Individual last_name @@ -420,6 +423,7 @@ Last Name. 1.1 + Individual index_last_name @@ -441,6 +445,7 @@ /^(mr|ms|mrs|sir|dr)\.?$/i true 1.2 + Individual UI_prefix @@ -462,6 +467,7 @@ /^suffix$/i /^(sr|jr)\.?|i{2,}$/ 1.2 + Individual UI_suffix @@ -479,6 +485,7 @@ /^title/i Formal (academic or similar) title in front of name. (Prof., Dr. etc.) 4.5 + Individual communication_style_id @@ -642,6 +649,7 @@ // Job Title 1.1 + Individual gender_id @@ -657,6 +665,7 @@ FK to gender ID true 1.2 + Individual UI_gender @@ -675,6 +684,7 @@ Select Date birth + Individual is_deceased @@ -688,6 +698,7 @@ CheckBox + Individual index_is_deceased @@ -706,6 +717,7 @@ Select Date birth + Individual mail_to_household_id @@ -738,6 +750,7 @@ Household Name. 1.1 + Household index_household_name @@ -750,6 +763,7 @@ Household Primary Contact ID Optional FK to Primary Contact for this household. 1.1 + Household primary_contact_id @@ -772,6 +786,7 @@ Organization Name. 1.1 + Organization index_organization_name @@ -789,6 +804,7 @@ Text + Organization user_unique_id @@ -816,6 +832,7 @@ EntityRef + Individual employer_id diff --git a/xml/templates/dao.tpl b/xml/templates/dao.tpl index 6718b8541f..ac6b477ec3 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -124,7 +124,9 @@ class {$table.className} extends CRM_Core_DAO {ldelim} {if $field.export} 'export' => {$field.export|strtoupper}, {/if} {* field.export *} - +{if $field.contactType} + 'contactType' => {if $field.contactType == 'null'}NULL{else}'{$field.contactType}'{/if}, +{/if} {if $field.rule} 'rule' => '{$field.rule}', {/if} {* field.rule *} -- 2.25.1