3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contribute_BAO_Premium
extends CRM_Contribute_DAO_Premium
{
20 * Product information.
24 private static $productInfo;
29 public function __construct() {
30 parent
::__construct();
34 * Fetch object based on array of properties.
36 * @param array $params
37 * (reference ) an assoc array of name/value pairs.
38 * @param array $defaults
39 * (reference ) an assoc array to hold the flattened values.
41 * @return CRM_Contribute_DAO_Product
43 public static function retrieve(&$params, &$defaults) {
44 $premium = new CRM_Contribute_DAO_Product();
45 $premium->copyValues($params);
46 if ($premium->find(TRUE)) {
47 CRM_Core_DAO
::storeValues($premium, $defaults);
54 * Update the is_active flag in the db.
57 * Id of the database record.
58 * @param bool $is_active
59 * Value we want to set the is_active field.
62 * true if we found and updated the object, else false
64 public static function setIsActive($id, $is_active) {
65 return CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
69 * Delete financial Types.
71 * @param int $premiumID
73 public static function del($premiumID) {
74 $premium = new CRM_Contribute_DAO_Premium();
75 $premium->id
= $premiumID;
80 * Build Premium Block im Contribution Pages.
82 * @param CRM_Core_Form $form
84 * @param bool $formItems
85 * @param int $selectedProductID
86 * @param string $selectedOption
88 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
89 $form->add('hidden', "selectProduct", $selectedProductID, ['id' => 'selectProduct']);
91 $premiumDao = new CRM_Contribute_DAO_Premium();
92 $premiumDao->entity_table
= 'civicrm_contribution_page';
93 $premiumDao->entity_id
= $pageID;
94 $premiumDao->premiums_active
= 1;
96 if ($premiumDao->find(TRUE)) {
97 $premiumID = $premiumDao->id
;
99 CRM_Core_DAO
::storeValues($premiumDao, $premiumBlock);
101 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
102 $addWhere = "financial_type_id IN (0)";
103 if (!empty($financialTypes)) {
104 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
106 $addWhere = "{$addWhere} OR financial_type_id IS NULL";
108 $premiumsProductDao = new CRM_Contribute_DAO_PremiumsProduct();
109 $premiumsProductDao->premiums_id
= $premiumID;
110 $premiumsProductDao->whereAdd($addWhere);
111 $premiumsProductDao->orderBy('weight');
112 $premiumsProductDao->find();
115 while ($premiumsProductDao->fetch()) {
116 $productDAO = new CRM_Contribute_DAO_Product();
117 $productDAO->id
= $premiumsProductDao->product_id
;
118 $productDAO->is_active
= 1;
119 if ($productDAO->find(TRUE)) {
120 if ($selectedProductID != NULL) {
121 if ($selectedProductID == $productDAO->id
) {
122 if ($selectedOption) {
123 $productDAO->options
= ts('Selected Option') . ': ' . $selectedOption;
126 $productDAO->options
= NULL;
128 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
132 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
135 $options = $temp = [];
136 $temp = explode(',', $productDAO->options
);
137 foreach ($temp as $value) {
138 $options[trim($value)] = trim($value);
140 if ($temp[0] != '') {
141 $form->addElement('select', 'options_' . $productDAO->id
, NULL, $options);
144 if (count($products)) {
145 $form->assign('showPremium', $formItems);
146 $form->assign('showSelectOptions', $formItems);
147 $form->assign('products', $products);
148 $form->assign('premiumBlock', $premiumBlock);
154 * Build Premium B im Contribution Pages.
156 * @param CRM_Core_Form $form
157 * @param int $productID
158 * @param int $premiumProductID
160 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
161 if ($premiumProductID) {
162 $dao = new CRM_Contribute_DAO_PremiumsProduct();
163 $dao->id
= $premiumProductID;
165 $productID = $dao->product_id
;
167 $productDAO = new CRM_Contribute_DAO_Product();
168 $productDAO->id
= $productID;
169 $productDAO->is_active
= 1;
170 if ($productDAO->find(TRUE)) {
171 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
174 $radio[$productDAO->id
] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id
, NULL);
175 $options = $temp = [];
176 $temp = explode(',', $productDAO->options
);
177 foreach ($temp as $value) {
178 $options[$value] = $value;
180 if ($temp[0] != '') {
181 $form->add('select', 'options_' . $productDAO->id
, NULL, $options);
184 $form->addGroup($radio, 'selectProduct', NULL);
186 $form->assign('showRadio', TRUE);
187 $form->assign('showSelectOptions', TRUE);
188 $form->assign('products', $products);
189 $form->assign('preview', TRUE);
193 * Delete premium associated w/ contribution page.
195 * @param int $contributionPageID
197 public static function deletePremium($contributionPageID) {
198 if (!$contributionPageID) {
202 //need to delete entries from civicrm_premiums
203 //as well as from civicrm_premiums_product, CRM-4586
206 'entity_id' => $contributionPageID,
207 'entity_table' => 'civicrm_contribution_page',
210 $premium = new CRM_Contribute_DAO_Premium();
211 $premium->copyValues($params);
213 while ($premium->fetch()) {
214 //lets delete from civicrm_premiums_product
215 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
216 $premiumsProduct->premiums_id
= $premium->id
;
217 $premiumsProduct->delete();
225 * Retrieve premium product and their options.
228 * product and option arrays
230 public static function getPremiumProductInfo() {
231 if (!self
::$productInfo) {
232 $products = $options = [];
234 $dao = new CRM_Contribute_DAO_Product();
238 while ($dao->fetch()) {
239 $products[$dao->id
] = $dao->name
. " ( " . $dao->sku
. " )";
240 $opts = explode(',', $dao->options
);
241 foreach ($opts as $k => $v) {
245 $options[$dao->id
] = $opts;
249 self
::$productInfo = [$products, $options];
251 return self
::$productInfo;