Merge pull request #22584 from civicrm/5.46
[civicrm-core.git] / CRM / Contribute / BAO / Premium.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium {
18
19 /**
20 * Product information.
21 *
22 * @var array
23 */
24 private static $productInfo;
25
26 /**
27 * Fetch object based on array of properties.
28 *
29 * @param array $params
30 * (reference ) an assoc array of name/value pairs.
31 * @param array $defaults
32 * (reference ) an assoc array to hold the flattened values.
33 *
34 * @return CRM_Contribute_DAO_Product
35 */
36 public static function retrieve(&$params, &$defaults) {
37 $premium = new CRM_Contribute_DAO_Product();
38 $premium->copyValues($params);
39 if ($premium->find(TRUE)) {
40 CRM_Core_DAO::storeValues($premium, $defaults);
41 return $premium;
42 }
43 return NULL;
44 }
45
46 /**
47 * Update the is_active flag in the db.
48 *
49 * @param int $id
50 * Id of the database record.
51 * @param bool $is_active
52 * Value we want to set the is_active field.
53 *
54 * @return bool
55 * true if we found and updated the object, else false
56 */
57 public static function setIsActive($id, $is_active) {
58 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
59 }
60
61 /**
62 * Delete financial Types.
63 *
64 * @param int $premiumID
65 */
66 public static function del($premiumID) {
67 $premium = new CRM_Contribute_DAO_Premium();
68 $premium->id = $premiumID;
69 $premium->delete();
70 }
71
72 /**
73 * Build Premium Block im Contribution Pages.
74 *
75 * @param CRM_Core_Form $form
76 * @param int $pageID
77 * @param bool $formItems
78 * @param int $selectedProductID
79 * @param string $selectedOption
80 */
81 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
82 $form->add('hidden', "selectProduct", $selectedProductID, ['id' => 'selectProduct']);
83
84 $premiumDao = new CRM_Contribute_DAO_Premium();
85 $premiumDao->entity_table = 'civicrm_contribution_page';
86 $premiumDao->entity_id = $pageID;
87 $premiumDao->premiums_active = 1;
88
89 if ($premiumDao->find(TRUE)) {
90 $premiumID = $premiumDao->id;
91 $premiumBlock = [];
92 CRM_Core_DAO::storeValues($premiumDao, $premiumBlock);
93
94 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD);
95 $addWhere = "financial_type_id IN (0)";
96 if (!empty($financialTypes)) {
97 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
98 }
99 $addWhere = "{$addWhere} OR financial_type_id IS NULL";
100
101 $premiumsProductDao = new CRM_Contribute_DAO_PremiumsProduct();
102 $premiumsProductDao->premiums_id = $premiumID;
103 $premiumsProductDao->whereAdd($addWhere);
104 $premiumsProductDao->orderBy('weight');
105 $premiumsProductDao->find();
106
107 $products = [];
108 while ($premiumsProductDao->fetch()) {
109 $productDAO = new CRM_Contribute_DAO_Product();
110 $productDAO->id = $premiumsProductDao->product_id;
111 $productDAO->is_active = 1;
112 if ($productDAO->find(TRUE)) {
113 if ($selectedProductID != NULL) {
114 if ($selectedProductID == $productDAO->id) {
115 if ($selectedOption) {
116 $productDAO->options = ts('Selected Option') . ': ' . $selectedOption;
117 }
118 else {
119 $productDAO->options = NULL;
120 }
121 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
122 }
123 }
124 else {
125 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
126 }
127 }
128 $options = $temp = [];
129 $temp = explode(',', $productDAO->options);
130 foreach ($temp as $value) {
131 $options[trim($value)] = trim($value);
132 }
133 if ($temp[0] != '') {
134 $form->addElement('select', 'options_' . $productDAO->id, NULL, $options);
135 }
136 }
137 if (count($products)) {
138 $form->assign('showPremium', $formItems);
139 $form->assign('showSelectOptions', $formItems);
140 $form->assign('products', $products);
141 $form->assign('premiumBlock', $premiumBlock);
142 }
143 }
144 }
145
146 /**
147 * Build Premium Preview block for Contribution Pages.
148 *
149 * @param CRM_Core_Form $form
150 * @param int|null $productID
151 *
152 * @return void
153 */
154 public static function buildPremiumPreviewBlock($form, $productID) {
155 $productDAO = new CRM_Contribute_DAO_Product();
156 $productDAO->id = $productID;
157 $productDAO->is_active = 1;
158 $products = [];
159
160 if ($productDAO->find(TRUE)) {
161 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
162 }
163
164 $radio[$productDAO->id] = NULL;
165 $options = $temp = [];
166 $temp = explode(',', $productDAO->options);
167 foreach ($temp as $value) {
168 $options[$value] = $value;
169 }
170 if ($temp[0] != '') {
171 $form->add('select', 'options_' . $productDAO->id, NULL, $options);
172 }
173
174 $form->addRadio('selectProduct', NULL, $radio);
175
176 $form->assign('showRadio', TRUE);
177 $form->assign('showSelectOptions', TRUE);
178 $form->assign('products', $products);
179 $form->assign('preview', TRUE);
180 }
181
182 /**
183 * Delete premium associated w/ contribution page.
184 *
185 * @param int $contributionPageID
186 */
187 public static function deletePremium($contributionPageID) {
188 if (!$contributionPageID) {
189 return;
190 }
191
192 //need to delete entries from civicrm_premiums
193 //as well as from civicrm_premiums_product, CRM-4586
194
195 $params = [
196 'entity_id' => $contributionPageID,
197 'entity_table' => 'civicrm_contribution_page',
198 ];
199
200 $premium = new CRM_Contribute_DAO_Premium();
201 $premium->copyValues($params);
202 $premium->find();
203 while ($premium->fetch()) {
204 //lets delete from civicrm_premiums_product
205 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
206 $premiumsProduct->premiums_id = $premium->id;
207 $premiumsProduct->delete();
208
209 //now delete premium
210 $premium->delete();
211 }
212 }
213
214 /**
215 * Retrieve premium product and their options.
216 *
217 * @return array
218 * product and option arrays
219 */
220 public static function getPremiumProductInfo() {
221 if (!self::$productInfo) {
222 $products = $options = [];
223
224 $dao = new CRM_Contribute_DAO_Product();
225 $dao->is_active = 1;
226 $dao->find();
227
228 while ($dao->fetch()) {
229 $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )";
230 $opts = explode(',', $dao->options);
231 foreach ($opts as $k => $v) {
232 $ops[$k] = trim($v);
233 }
234 if ($ops[0] != '') {
235 $options[$dao->id] = $opts;
236 }
237 }
238
239 self::$productInfo = [$products, $options];
240 }
241 return self::$productInfo;
242 }
243
244 }