dev/core#735 Do not include product in search results if site has none
authoreileen <emcnaughton@wikimedia.org>
Tue, 19 Feb 2019 04:23:21 +0000 (17:23 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 25 Feb 2019 02:30:04 +0000 (15:30 +1300)
CRM/Contribute/BAO/Contribution.php
CRM/Contribute/BAO/Query.php
CRM/Contribute/Selector/Search.php
tests/phpunit/CRM/Export/BAO/ExportTest.php

index 010d68df8f1689e60179d0a1b2cfee4d474cd552..41d7fa54425599a60eee2f89e1ac5e2fd2296198 100644 (file)
@@ -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)
       );
 
index 6b2ff0e93c044ccb992241212c52b32e5d2ba1b7..0ada67fe6f5ec3d90013fc341e78989a44dac95a 100644 (file)
@@ -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'));
 
index 6bfc54f5ba375bfec7a791ffbea6168d18870f32..143e840bac863e9334788f0cfda2ec0bbad181d3 100644 (file)
@@ -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(
index e698fc550b668db38d2d91f1b89fa55aad7c8c28..ce74aa6eeeea29a1d47a8790f4675ee6c2a5216c 100644 (file)
@@ -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)',