Merge pull request #16857 from mattwire/cancelsubscriptionprocessorname
[civicrm-core.git] / CRM / Contribute / BAO / Premium.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium {
18
19 /**
fe482240 20 * Product information.
663b5e57 21 *
6a488035 22 * @var array
6a488035
TO
23 */
24 private static $productInfo;
25
26 /**
fe482240 27 * Class constructor.
665e5ec7 28 */
00be9182 29 public function __construct() {
6a488035
TO
30 parent::__construct();
31 }
32
33 /**
fe482240 34 * Fetch object based on array of properties.
6a488035 35 *
014c4014
TO
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.
6a488035 40 *
663b5e57 41 * @return CRM_Contribute_DAO_Product
6a488035 42 */
00be9182 43 public static function retrieve(&$params, &$defaults) {
6a488035
TO
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 /**
fe482240 54 * Update the is_active flag in the db.
6a488035 55 *
014c4014
TO
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.
6a488035 60 *
8a4fede3 61 * @return bool
62 * true if we found and updated the object, else false
6a488035 63 */
00be9182 64 public static function setIsActive($id, $is_active) {
6a488035
TO
65 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
66 }
67
68 /**
fe482240 69 * Delete financial Types.
6a488035 70 *
100fef9d 71 * @param int $premiumID
6a488035 72 */
00be9182 73 public static function del($premiumID) {
6a488035
TO
74 $premium = new CRM_Contribute_DAO_Premium();
75 $premium->id = $premiumID;
76 $premium->delete();
77 }
78
79 /**
fe482240 80 * Build Premium Block im Contribution Pages.
6a488035 81 *
100fef9d
CW
82 * @param CRM_Core_Form $form
83 * @param int $pageID
f4aaa82a 84 * @param bool $formItems
100fef9d 85 * @param int $selectedProductID
663b5e57 86 * @param string $selectedOption
6a488035 87 */
00be9182 88 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
be2fb01f 89 $form->add('hidden', "selectProduct", $selectedProductID, ['id' => 'selectProduct']);
6a488035 90
a275d4b6
MW
91 $premiumDao = new CRM_Contribute_DAO_Premium();
92 $premiumDao->entity_table = 'civicrm_contribution_page';
93 $premiumDao->entity_id = $pageID;
94 $premiumDao->premiums_active = 1;
6a488035 95
a275d4b6
MW
96 if ($premiumDao->find(TRUE)) {
97 $premiumID = $premiumDao->id;
be2fb01f 98 $premiumBlock = [];
a275d4b6
MW
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";
6a488035 107
a275d4b6
MW
108 $premiumsProductDao = new CRM_Contribute_DAO_PremiumsProduct();
109 $premiumsProductDao->premiums_id = $premiumID;
110 $premiumsProductDao->whereAdd($addWhere);
111 $premiumsProductDao->orderBy('weight');
112 $premiumsProductDao->find();
6a488035 113
be2fb01f 114 $products = [];
a275d4b6 115 while ($premiumsProductDao->fetch()) {
6a488035 116 $productDAO = new CRM_Contribute_DAO_Product();
a275d4b6 117 $productDAO->id = $premiumsProductDao->product_id;
6a488035
TO
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 }
be2fb01f 135 $options = $temp = [];
6a488035
TO
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 /**
fe482240 154 * Build Premium B im Contribution Pages.
6a488035 155 *
c490a46a
CW
156 * @param CRM_Core_Form $form
157 * @param int $productID
158 * @param int $premiumProductID
6a488035 159 */
00be9182 160 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
6a488035
TO
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] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id, NULL);
be2fb01f 175 $options = $temp = [];
6a488035
TO
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->addGroup($radio, 'selectProduct', NULL);
185
186 $form->assign('showRadio', TRUE);
187 $form->assign('showSelectOptions', TRUE);
188 $form->assign('products', $products);
189 $form->assign('preview', TRUE);
190 }
191
192 /**
100fef9d 193 * Delete premium associated w/ contribution page.
6a488035 194 *
100fef9d 195 * @param int $contributionPageID
6a488035 196 */
00be9182 197 public static function deletePremium($contributionPageID) {
6a488035
TO
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
be2fb01f 205 $params = [
6a488035
TO
206 'entity_id' => $contributionPageID,
207 'entity_table' => 'civicrm_contribution_page',
be2fb01f 208 ];
6a488035
TO
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 /**
fe482240 225 * Retrieve premium product and their options.
6a488035 226 *
a6c01b45
CW
227 * @return array
228 * product and option arrays
6a488035 229 */
00be9182 230 public static function getPremiumProductInfo() {
6a488035 231 if (!self::$productInfo) {
be2fb01f 232 $products = $options = [];
6a488035
TO
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
be2fb01f 249 self::$productInfo = [$products, $options];
6a488035
TO
250 }
251 return self::$productInfo;
252 }
96025800 253
6a488035 254}