From: eileen Date: Tue, 19 Feb 2019 04:23:21 +0000 (+1300) Subject: dev/core#735 Do not include product in search results if site has none X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c8dd63018b51af6ea8bcdb0f5c2077383a2d899e;p=civicrm-core.git dev/core#735 Do not include product in search results if site has none --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 010d68df8f..41d7fa5442 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -787,9 +787,23 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { self::$_exportableFields = array(); } - $impFields = CRM_Contribute_DAO_Contribution::export(); - $expFieldProduct = CRM_Contribute_DAO_Product::export(); - $expFieldsContrib = CRM_Contribute_DAO_ContributionProduct::export(); + $fields = CRM_Contribute_DAO_Contribution::export(); + if (CRM_Contribute_BAO_Query::isSiteHasProducts()) { + $fields = array_merge( + $fields, + CRM_Contribute_DAO_Product::export(), + CRM_Contribute_DAO_ContributionProduct::export(), + // CRM-16713 - contribution search by Premiums on 'Find Contribution' form. + [ + 'contribution_product_id' => [ + 'title' => ts('Premium'), + 'name' => 'contribution_product_id', + 'where' => 'civicrm_product.id', + 'data_type' => CRM_Utils_Type::T_INT, + ], + ] + ); + } $typeField = CRM_Financial_DAO_FinancialType::export(); $financialAccount = CRM_Financial_DAO_FinancialAccount::export(); @@ -858,18 +872,8 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { ), ); - // CRM-16713 - contribution search by Premiums on 'Find Contribution' form. - $premiums = array( - 'contribution_product_id' => array( - 'title' => ts('Premium'), - 'name' => 'contribution_product_id', - 'where' => 'civicrm_product.id', - 'data_type' => CRM_Utils_Type::T_INT, - ), - ); - - $fields = array_merge($impFields, $typeField, $contributionPage, $expFieldProduct, - $expFieldsContrib, $contributionNote, $extraFields, $softCreditFields, $financialAccount, $premiums, $campaignTitle, + $fields = array_merge($fields, $typeField, $contributionPage, + $contributionNote, $extraFields, $softCreditFields, $financialAccount, $campaignTitle, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution', FALSE, FALSE, FALSE, $checkPermission) ); diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index 6b2ff0e93c..0ada67fe6f 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -773,16 +773,14 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { 'contribution_status_id' => 1, // @todo return this & fix query to do pseudoconstant thing. 'contribution_status' => 1, - // @todo the product field got added because it suited someone's use case. - // ideally we would have some configurability here because I think 90% of sites would - // disagree this is the right field to show - but they wouldn't agree with each other - // on what field to show instead. - 'contribution_product_id' => 1, - 'product_name' => 1, 'currency' => 1, 'cancel_date' => 1, 'contribution_recur_id' => 1, ); + if (self::isSiteHasProducts()) { + $properties['product_name'] = 1; + $properties['contribution_product_id'] = 1; + } if (self::isSoftCreditOptionEnabled($queryParams)) { $properties = array_merge($properties, self::softCreditReturnProperties()); } @@ -790,6 +788,18 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { return $properties; } + /** + * Do any products exist in this site's database. + * + * @return bool + */ + public static function isSiteHasProducts() { + if (!isset(\Civi::$statics[__CLASS__]['has_products'])) { + \Civi::$statics[__CLASS__]['has_products'] = (bool) CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_contribution_product LIMIT 1'); + } + return \Civi::$statics[__CLASS__]['has_products']; + } + /** * Function you should avoid. * @@ -854,17 +864,11 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { //every 'receipt_date' => 1, // query - 'product_name' => 1, //whether - 'sku' => 1, // or - 'product_option' => 1, // not - 'fulfilled_date' => 1, // the - 'contribution_start_date' => 1, // field - 'contribution_end_date' => 1, // is 'is_test' => 1, // actually @@ -886,9 +890,17 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { // on 'contribution_campaign_id' => 1, // calling - 'contribution_product_id' => 1, //function ); + if (self::isSiteHasProducts()) { + $properties['fulfilled_date'] = 1; + $properties['product_name'] = 1; + $properties['contribution_product_id'] = 1; + $properties['product_option'] = 1; + $properties['sku'] = 1; + $properties['contribution_start_date'] = 1; + $properties['contribution_end_date'] = 1; + } if (self::isSoftCreditOptionEnabled()) { $properties = array_merge($properties, self::softCreditReturnProperties()); } @@ -1014,12 +1026,18 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { 'autocomplete' => 'off', )); - // CRM-16713 - contribution search by premiums on 'Find Contribution' form. - $form->add('select', 'contribution_product_id', - ts('Premium'), - CRM_Contribute_PseudoConstant::products(), - FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple', 'placeholder' => ts('- any -')) - ); + if (CRM_Contribute_BAO_Query::isSiteHasProducts()) { + // CRM-16713 - contribution search by premiums on 'Find Contribution' form. + $form->add('select', 'contribution_product_id', + ts('Premium'), + CRM_Contribute_PseudoConstant::products(), + FALSE, [ + 'class' => 'crm-select2', + 'multiple' => 'multiple', + 'placeholder' => ts('- any -') + ] + ); + } self::addCustomFormFields($form, array('Contribution')); diff --git a/CRM/Contribute/Selector/Search.php b/CRM/Contribute/Selector/Search.php index 6bfc54f5ba..143e840bac 100644 --- a/CRM/Contribute/Selector/Search.php +++ b/CRM/Contribute/Selector/Search.php @@ -563,14 +563,16 @@ class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements C 'field_name' => 'contribution_status', 'direction' => CRM_Utils_Sort::DONTCARE, ), - array( - 'name' => ts('Premium'), - 'sort' => 'product_name', - 'field_name' => 'product_name', - 'direction' => CRM_Utils_Sort::DONTCARE, - ), ) ); + if (CRM_Contribute_BAO_Query::isSiteHasProducts()) { + self::$_columnHeaders[] = [ + 'name' => ts('Premium'), + 'sort' => 'product_name', + 'field_name' => 'product_name', + 'direction' => CRM_Utils_Sort::DONTCARE, + ]; + } if (!$this->_single) { $pre = array( array( diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index e698fc550b..ce74aa6eee 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -1634,12 +1634,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'currency' => 1, 'cancel_reason' => 1, 'receipt_date' => 1, - 'product_name' => 1, - 'sku' => 1, - 'product_option' => 1, - 'fulfilled_date' => 1, - 'contribution_start_date' => 1, - 'contribution_end_date' => 1, 'is_test' => 1, 'is_pay_later' => 1, 'contribution_status' => 1, @@ -1649,7 +1643,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'contribution_batch' => 1, 'contribution_campaign_title' => 1, 'contribution_campaign_id' => 1, - 'contribution_product_id' => 1, 'contribution_soft_credit_name' => 1, 'contribution_soft_credit_amount' => 1, 'contribution_soft_credit_type' => 1, @@ -2204,12 +2197,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 97 => 'Currency', 98 => 'Cancellation / Refund Reason', 99 => 'Receipt Date', - 100 => 'Product Name', - 101 => 'SKU', - 102 => 'Product Option', - 103 => 'Fulfilled Date', - 104 => 'Start date for premium', - 105 => 'End date for premium', 106 => 'Test', 107 => 'Is Pay Later', 108 => 'contribution_status', @@ -2219,7 +2206,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 112 => 'Batch Name', 113 => 'Campaign Title', 114 => 'Campaign ID', - 115 => 'Premium', 116 => 'Soft Credit For', 117 => 'Soft Credit Amount', 118 => 'Soft Credit Type', @@ -2612,12 +2598,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'currency' => 'currency varchar(3)', 'cancel_reason' => 'cancel_reason longtext', 'receipt_date' => 'receipt_date varchar(32)', - 'product_name' => 'product_name varchar(255)', - 'sku' => 'sku varchar(50)', - 'product_option' => 'product_option varchar(255)', - 'fulfilled_date' => 'fulfilled_date varchar(32)', - 'contribution_start_date' => 'contribution_start_date varchar(32)', - 'contribution_end_date' => 'contribution_end_date varchar(32)', 'is_test' => 'is_test varchar(16)', 'is_pay_later' => 'is_pay_later varchar(16)', 'contribution_status' => 'contribution_status text', @@ -2627,7 +2607,6 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'contribution_batch' => 'contribution_batch text', 'contribution_campaign_title' => 'contribution_campaign_title varchar(255)', 'contribution_campaign_id' => 'contribution_campaign_id varchar(128)', - 'contribution_product_id' => 'contribution_product_id varchar(255)', 'contribution_soft_credit_name' => 'contribution_soft_credit_name varchar(255)', 'contribution_soft_credit_amount' => 'contribution_soft_credit_amount varchar(255)', 'contribution_soft_credit_type' => 'contribution_soft_credit_type varchar(255)',