Merge pull request #22313 from mlutfy/eventDefaultUrlValues
[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 B im Contribution Pages.
148 *
149 * @param CRM_Core_Form $form
150 * @param int $productID
151 * @param int $premiumProductID
152 */
153 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
154 if ($premiumProductID) {
155 $dao = new CRM_Contribute_DAO_PremiumsProduct();
156 $dao->id = $premiumProductID;
157 $dao->find(TRUE);
158 $productID = $dao->product_id;
159 }
160 $productDAO = new CRM_Contribute_DAO_Product();
161 $productDAO->id = $productID;
162 $productDAO->is_active = 1;
163 if ($productDAO->find(TRUE)) {
164 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
165 }
166
167 $radio[$productDAO->id] = NULL;
168 $options = $temp = [];
169 $temp = explode(',', $productDAO->options);
170 foreach ($temp as $value) {
171 $options[$value] = $value;
172 }
173 if ($temp[0] != '') {
174 $form->add('select', 'options_' . $productDAO->id, NULL, $options);
175 }
176
177 $form->addRadio('selectProduct', NULL, $radio);
178
179 $form->assign('showRadio', TRUE);
180 $form->assign('showSelectOptions', TRUE);
181 $form->assign('products', $products);
182 $form->assign('preview', TRUE);
183 }
184
185 /**
186 * Delete premium associated w/ contribution page.
187 *
188 * @param int $contributionPageID
189 */
190 public static function deletePremium($contributionPageID) {
191 if (!$contributionPageID) {
192 return;
193 }
194
195 //need to delete entries from civicrm_premiums
196 //as well as from civicrm_premiums_product, CRM-4586
197
198 $params = [
199 'entity_id' => $contributionPageID,
200 'entity_table' => 'civicrm_contribution_page',
201 ];
202
203 $premium = new CRM_Contribute_DAO_Premium();
204 $premium->copyValues($params);
205 $premium->find();
206 while ($premium->fetch()) {
207 //lets delete from civicrm_premiums_product
208 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
209 $premiumsProduct->premiums_id = $premium->id;
210 $premiumsProduct->delete();
211
212 //now delete premium
213 $premium->delete();
214 }
215 }
216
217 /**
218 * Retrieve premium product and their options.
219 *
220 * @return array
221 * product and option arrays
222 */
223 public static function getPremiumProductInfo() {
224 if (!self::$productInfo) {
225 $products = $options = [];
226
227 $dao = new CRM_Contribute_DAO_Product();
228 $dao->is_active = 1;
229 $dao->find();
230
231 while ($dao->fetch()) {
232 $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )";
233 $opts = explode(',', $dao->options);
234 foreach ($opts as $k => $v) {
235 $ops[$k] = trim($v);
236 }
237 if ($ops[0] != '') {
238 $options[$dao->id] = $opts;
239 }
240 }
241
242 self::$productInfo = [$products, $options];
243 }
244 return self::$productInfo;
245 }
246
247 }