* @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');
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) {
$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 <a href=\'%1\'>Online Contribution page</a>. 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();
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'];
// 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 <a href='%1'>Online Contribution page</a>. 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');
$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(
$this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
- $param = array(
+ $contributionParams = array(
'contact_id' => $contactId,
'currency' => 'USD',
'financial_type_id' => 1,
'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
);
$product = CRM_Contribute_BAO_Product::create($params);
-
$result = $this->assertDBNotNull('CRM_Contribute_BAO_Product', $product->id,
'sku', 'id',
'Database check on updated product record.'
);
$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.');
}
}