Merge pull request #19277 from yashodha/dev-2273
[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 * Class constructor.
28 */
29 public function __construct() {
30 parent::__construct();
31 }
32
33 /**
34 * Fetch object based on array of properties.
35 *
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.
40 *
41 * @return CRM_Contribute_DAO_Product
42 */
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);
48 return $premium;
49 }
50 return NULL;
51 }
52
53 /**
54 * Update the is_active flag in the db.
55 *
56 * @param int $id
57 * Id of the database record.
58 * @param bool $is_active
59 * Value we want to set the is_active field.
60 *
61 * @return bool
62 * true if we found and updated the object, else false
63 */
64 public static function setIsActive($id, $is_active) {
65 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
66 }
67
68 /**
69 * Delete financial Types.
70 *
71 * @param int $premiumID
72 */
73 public static function del($premiumID) {
74 $premium = new CRM_Contribute_DAO_Premium();
75 $premium->id = $premiumID;
76 $premium->delete();
77 }
78
79 /**
80 * Build Premium Block im Contribution Pages.
81 *
82 * @param CRM_Core_Form $form
83 * @param int $pageID
84 * @param bool $formItems
85 * @param int $selectedProductID
86 * @param string $selectedOption
87 */
88 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
89 $form->add('hidden', "selectProduct", $selectedProductID, ['id' => 'selectProduct']);
90
91 $premiumDao = new CRM_Contribute_DAO_Premium();
92 $premiumDao->entity_table = 'civicrm_contribution_page';
93 $premiumDao->entity_id = $pageID;
94 $premiumDao->premiums_active = 1;
95
96 if ($premiumDao->find(TRUE)) {
97 $premiumID = $premiumDao->id;
98 $premiumBlock = [];
99 CRM_Core_DAO::storeValues($premiumDao, $premiumBlock);
100
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)) . ")";
105 }
106 $addWhere = "{$addWhere} OR financial_type_id IS NULL";
107
108 $premiumsProductDao = new CRM_Contribute_DAO_PremiumsProduct();
109 $premiumsProductDao->premiums_id = $premiumID;
110 $premiumsProductDao->whereAdd($addWhere);
111 $premiumsProductDao->orderBy('weight');
112 $premiumsProductDao->find();
113
114 $products = [];
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;
124 }
125 else {
126 $productDAO->options = NULL;
127 }
128 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
129 }
130 }
131 else {
132 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
133 }
134 }
135 $options = $temp = [];
136 $temp = explode(',', $productDAO->options);
137 foreach ($temp as $value) {
138 $options[trim($value)] = trim($value);
139 }
140 if ($temp[0] != '') {
141 $form->addElement('select', 'options_' . $productDAO->id, NULL, $options);
142 }
143 }
144 if (count($products)) {
145 $form->assign('showPremium', $formItems);
146 $form->assign('showSelectOptions', $formItems);
147 $form->assign('products', $products);
148 $form->assign('premiumBlock', $premiumBlock);
149 }
150 }
151 }
152
153 /**
154 * Build Premium B im Contribution Pages.
155 *
156 * @param CRM_Core_Form $form
157 * @param int $productID
158 * @param int $premiumProductID
159 */
160 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
161 if ($premiumProductID) {
162 $dao = new CRM_Contribute_DAO_PremiumsProduct();
163 $dao->id = $premiumProductID;
164 $dao->find(TRUE);
165 $productID = $dao->product_id;
166 }
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]);
172 }
173
174 $radio[$productDAO->id] = NULL;
175 $options = $temp = [];
176 $temp = explode(',', $productDAO->options);
177 foreach ($temp as $value) {
178 $options[$value] = $value;
179 }
180 if ($temp[0] != '') {
181 $form->add('select', 'options_' . $productDAO->id, NULL, $options);
182 }
183
184 $form->addRadio('selectProduct', NULL, $radio);
185
186 $form->assign('showRadio', TRUE);
187 $form->assign('showSelectOptions', TRUE);
188 $form->assign('products', $products);
189 $form->assign('preview', TRUE);
190 }
191
192 /**
193 * Delete premium associated w/ contribution page.
194 *
195 * @param int $contributionPageID
196 */
197 public static function deletePremium($contributionPageID) {
198 if (!$contributionPageID) {
199 return;
200 }
201
202 //need to delete entries from civicrm_premiums
203 //as well as from civicrm_premiums_product, CRM-4586
204
205 $params = [
206 'entity_id' => $contributionPageID,
207 'entity_table' => 'civicrm_contribution_page',
208 ];
209
210 $premium = new CRM_Contribute_DAO_Premium();
211 $premium->copyValues($params);
212 $premium->find();
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();
218
219 //now delete premium
220 $premium->delete();
221 }
222 }
223
224 /**
225 * Retrieve premium product and their options.
226 *
227 * @return array
228 * product and option arrays
229 */
230 public static function getPremiumProductInfo() {
231 if (!self::$productInfo) {
232 $products = $options = [];
233
234 $dao = new CRM_Contribute_DAO_Product();
235 $dao->is_active = 1;
236 $dao->find();
237
238 while ($dao->fetch()) {
239 $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )";
240 $opts = explode(',', $dao->options);
241 foreach ($opts as $k => $v) {
242 $ops[$k] = trim($v);
243 }
244 if ($ops[0] != '') {
245 $options[$dao->id] = $opts;
246 }
247 }
248
249 self::$productInfo = [$products, $options];
250 }
251 return self::$productInfo;
252 }
253
254 }