From a275d4b6f3ad3826272b1018e69f9ce34b262bcf Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Fri, 6 Jul 2018 22:21:20 +0100 Subject: [PATCH] financial_type_id is not required for a premium product so may not be set in civicrm_premium_product. The filter for display should select those with a NULL financial_type as well as specific financial types. --- CRM/Contribute/BAO/ManagePremiums.php | 2 +- CRM/Contribute/BAO/Premium.php | 40 ++++++++++--------- CRM/Contribute/BAO/Product.php | 8 +--- CRM/Contribute/Form/ManagePremiums.php | 14 +++++-- .../CRM/Contribute/BAO/ContributionTest.php | 6 +-- .../CRM/Contribute/BAO/ProductTest.php | 8 ++-- 6 files changed, 41 insertions(+), 37 deletions(-) diff --git a/CRM/Contribute/BAO/ManagePremiums.php b/CRM/Contribute/BAO/ManagePremiums.php index a7ba644ff7..f122fb0b88 100644 --- a/CRM/Contribute/BAO/ManagePremiums.php +++ b/CRM/Contribute/BAO/ManagePremiums.php @@ -50,7 +50,7 @@ class CRM_Contribute_BAO_ManagePremiums extends CRM_Contribute_BAO_Product { * @param array $defaults * (reference ) an assoc array to hold the flattened values. * - * @return CRM_Contribute_BAO_ManagePremiums + * @return CRM_Contribute_BAO_Product */ public static function retrieve(&$params, &$defaults) { CRM_Core_Error::deprecatedFunctionWarning('CRM_Contribute_BAO_Product::retrieve'); diff --git a/CRM/Contribute/BAO/Premium.php b/CRM/Contribute/BAO/Premium.php index 31ace03233..a7db1d5efd 100644 --- a/CRM/Contribute/BAO/Premium.php +++ b/CRM/Contribute/BAO/Premium.php @@ -104,31 +104,33 @@ class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium { public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) { $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct')); - $dao = new CRM_Contribute_DAO_Premium(); - $dao->entity_table = 'civicrm_contribution_page'; - $dao->entity_id = $pageID; - $dao->premiums_active = 1; - CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD); - $addWhere = "financial_type_id IN (0)"; - if (!empty($financialTypes)) { - $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")"; - } + $premiumDao = new CRM_Contribute_DAO_Premium(); + $premiumDao->entity_table = 'civicrm_contribution_page'; + $premiumDao->entity_id = $pageID; + $premiumDao->premiums_active = 1; - if ($dao->find(TRUE)) { - $premiumID = $dao->id; + if ($premiumDao->find(TRUE)) { + $premiumID = $premiumDao->id; $premiumBlock = array(); - CRM_Core_DAO::storeValues($dao, $premiumBlock); + CRM_Core_DAO::storeValues($premiumDao, $premiumBlock); - $dao = new CRM_Contribute_DAO_PremiumsProduct(); - $dao->premiums_id = $premiumID; - $dao->whereAdd($addWhere); - $dao->orderBy('weight'); - $dao->find(); + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD); + $addWhere = "financial_type_id IN (0)"; + if (!empty($financialTypes)) { + $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")"; + } + $addWhere = "{$addWhere} OR financial_type_id IS NULL"; + + $premiumsProductDao = new CRM_Contribute_DAO_PremiumsProduct(); + $premiumsProductDao->premiums_id = $premiumID; + $premiumsProductDao->whereAdd($addWhere); + $premiumsProductDao->orderBy('weight'); + $premiumsProductDao->find(); $products = array(); - while ($dao->fetch()) { + while ($premiumsProductDao->fetch()) { $productDAO = new CRM_Contribute_DAO_Product(); - $productDAO->id = $dao->product_id; + $productDAO->id = $premiumsProductDao->product_id; $productDAO->is_active = 1; if ($productDAO->find(TRUE)) { if ($selectedProductID != NULL) { diff --git a/CRM/Contribute/BAO/Product.php b/CRM/Contribute/BAO/Product.php index 498e2fcb28..22f1f9739d 100644 --- a/CRM/Contribute/BAO/Product.php +++ b/CRM/Contribute/BAO/Product.php @@ -134,13 +134,9 @@ class CRM_Contribute_BAO_Product extends CRM_Contribute_DAO_Product { $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct(); $premiumsProduct->product_id = $productID; if ($premiumsProduct->find(TRUE)) { - $session = CRM_Core_Session::singleton(); - $message .= ts('This Premium is being linked to Online Contribution page. Please remove it in order to delete this Premium.', array(1 => CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1')), ts('Deletion Error'), 'error'); - CRM_Core_Session::setStatus($message); - return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/contribute/managePremiums', 'reset=1&action=browse')); + throw new CRM_Core_Exception('Cannot delete a Premium that is linked to a Contribution page'); } - - //delete from financial Type table + // delete product $premium = new CRM_Contribute_DAO_Product(); $premium->id = $productID; $premium->delete(); diff --git a/CRM/Contribute/Form/ManagePremiums.php b/CRM/Contribute/Form/ManagePremiums.php index c685ee0452..37bad336ab 100644 --- a/CRM/Contribute/Form/ManagePremiums.php +++ b/CRM/Contribute/Form/ManagePremiums.php @@ -51,7 +51,6 @@ class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { if ($this->_id) { $params = array('id' => $this->_id); CRM_Contribute_BAO_Product::retrieve($params, $tempDefaults); - $imageUrl = (isset($tempDefaults['image'])) ? $tempDefaults['image'] : ""; if (isset($tempDefaults['image']) && isset($tempDefaults['thumbnail'])) { $defaults['imageUrl'] = $tempDefaults['image']; $defaults['thumbnailUrl'] = $tempDefaults['thumbnail']; @@ -272,7 +271,15 @@ class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { // If deleting, then only delete and skip the rest of the post-processing if ($this->_action & CRM_Core_Action::DELETE) { - CRM_Contribute_BAO_Product::del($this->_id); + try { + CRM_Contribute_BAO_Product::del($this->_id); + } + catch (CRM_Core_Exception $e) { + $message = ts("This Premium is linked to an Online Contribution page. Please remove it before deleting this Premium.", array(1 => CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'))); + CRM_Core_Session::setStatus($message, ts('Cannot delete Premium'), 'error'); + CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/contribute/managePremiums', 'reset=1&action=browse')); + return; + } CRM_Core_Session::setStatus( ts('Selected Premium Product type has been deleted.'), ts('Deleted'), 'info'); @@ -287,13 +294,14 @@ class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); } + // If we're updating, we need to pass in the premium product Id if ($this->_action & CRM_Core_Action::UPDATE) { $params['id'] = $this->_id; } $this->_processImages($params); - // Save to database + // Save the premium product to database $premium = CRM_Contribute_BAO_Product::create($params); CRM_Core_Session::setStatus( diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index 83d94b0041..783bb40325 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -376,7 +376,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.'); - $param = array( + $contributionParams = array( 'contact_id' => $contactId, 'currency' => 'USD', 'financial_type_id' => 1, @@ -395,9 +395,9 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'thankyou_date' => '20080522', 'sequential' => TRUE, ); - $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0]; + $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0]; - $this->assertEquals($param['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.'); + $this->assertEquals($contributionParams['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.'); $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); //parameter for adding premium to contribution diff --git a/tests/phpunit/CRM/Contribute/BAO/ProductTest.php b/tests/phpunit/CRM/Contribute/BAO/ProductTest.php index 6779d29be9..85d970a32a 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ProductTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ProductTest.php @@ -50,7 +50,6 @@ class CRM_Contribute_BAO_ProductTest extends CiviUnitTestCase { ); $product = CRM_Contribute_BAO_Product::create($params); - $result = $this->assertDBNotNull('CRM_Contribute_BAO_Product', $product->id, 'sku', 'id', 'Database check on updated product record.' @@ -120,14 +119,13 @@ class CRM_Contribute_BAO_ProductTest extends CiviUnitTestCase { ); $product = CRM_Contribute_BAO_Product::create($params); - CRM_Contribute_BAO_Product::del($product->id); $params = array('id' => $product->id); - $default = array(); - $result = CRM_Contribute_BAO_Product::retrieve($params, $defaults); + $defaults = array(); + $retrievedProduct = CRM_Contribute_BAO_Product::retrieve($params, $defaults); - $this->assertEquals(empty($result), TRUE, 'Verify product record deletion.'); + $this->assertEquals(empty($retrievedProduct), TRUE, 'Verify product record deletion.'); } } -- 2.25.1