Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Product information. |
663b5e57 | 37 | * |
6a488035 | 38 | * @var array |
6a488035 TO |
39 | */ |
40 | private static $productInfo; | |
41 | ||
42 | /** | |
fe482240 | 43 | * Class constructor. |
665e5ec7 | 44 | */ |
00be9182 | 45 | public function __construct() { |
6a488035 TO |
46 | parent::__construct(); |
47 | } | |
48 | ||
49 | /** | |
fe482240 | 50 | * Fetch object based on array of properties. |
6a488035 | 51 | * |
014c4014 TO |
52 | * @param array $params |
53 | * (reference ) an assoc array of name/value pairs. | |
54 | * @param array $defaults | |
55 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 56 | * |
663b5e57 | 57 | * @return CRM_Contribute_DAO_Product |
6a488035 | 58 | */ |
00be9182 | 59 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
60 | $premium = new CRM_Contribute_DAO_Product(); |
61 | $premium->copyValues($params); | |
62 | if ($premium->find(TRUE)) { | |
63 | CRM_Core_DAO::storeValues($premium, $defaults); | |
64 | return $premium; | |
65 | } | |
66 | return NULL; | |
67 | } | |
68 | ||
69 | /** | |
fe482240 | 70 | * Update the is_active flag in the db. |
6a488035 | 71 | * |
014c4014 TO |
72 | * @param int $id |
73 | * Id of the database record. | |
74 | * @param bool $is_active | |
75 | * Value we want to set the is_active field. | |
6a488035 | 76 | * |
8a4fede3 | 77 | * @return bool |
78 | * true if we found and updated the object, else false | |
6a488035 | 79 | */ |
00be9182 | 80 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
81 | return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active); |
82 | } | |
83 | ||
84 | /** | |
fe482240 | 85 | * Delete financial Types. |
6a488035 | 86 | * |
100fef9d | 87 | * @param int $premiumID |
6a488035 | 88 | */ |
00be9182 | 89 | public static function del($premiumID) { |
6a488035 TO |
90 | $premium = new CRM_Contribute_DAO_Premium(); |
91 | $premium->id = $premiumID; | |
92 | $premium->delete(); | |
93 | } | |
94 | ||
95 | /** | |
fe482240 | 96 | * Build Premium Block im Contribution Pages. |
6a488035 | 97 | * |
100fef9d CW |
98 | * @param CRM_Core_Form $form |
99 | * @param int $pageID | |
f4aaa82a | 100 | * @param bool $formItems |
100fef9d | 101 | * @param int $selectedProductID |
663b5e57 | 102 | * @param string $selectedOption |
6a488035 | 103 | */ |
00be9182 | 104 | public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) { |
be2fb01f | 105 | $form->add('hidden', "selectProduct", $selectedProductID, ['id' => 'selectProduct']); |
6a488035 | 106 | |
a275d4b6 MW |
107 | $premiumDao = new CRM_Contribute_DAO_Premium(); |
108 | $premiumDao->entity_table = 'civicrm_contribution_page'; | |
109 | $premiumDao->entity_id = $pageID; | |
110 | $premiumDao->premiums_active = 1; | |
6a488035 | 111 | |
a275d4b6 MW |
112 | if ($premiumDao->find(TRUE)) { |
113 | $premiumID = $premiumDao->id; | |
be2fb01f | 114 | $premiumBlock = []; |
a275d4b6 MW |
115 | CRM_Core_DAO::storeValues($premiumDao, $premiumBlock); |
116 | ||
117 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD); | |
118 | $addWhere = "financial_type_id IN (0)"; | |
119 | if (!empty($financialTypes)) { | |
120 | $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")"; | |
121 | } | |
122 | $addWhere = "{$addWhere} OR financial_type_id IS NULL"; | |
6a488035 | 123 | |
a275d4b6 MW |
124 | $premiumsProductDao = new CRM_Contribute_DAO_PremiumsProduct(); |
125 | $premiumsProductDao->premiums_id = $premiumID; | |
126 | $premiumsProductDao->whereAdd($addWhere); | |
127 | $premiumsProductDao->orderBy('weight'); | |
128 | $premiumsProductDao->find(); | |
6a488035 | 129 | |
be2fb01f | 130 | $products = []; |
a275d4b6 | 131 | while ($premiumsProductDao->fetch()) { |
6a488035 | 132 | $productDAO = new CRM_Contribute_DAO_Product(); |
a275d4b6 | 133 | $productDAO->id = $premiumsProductDao->product_id; |
6a488035 TO |
134 | $productDAO->is_active = 1; |
135 | if ($productDAO->find(TRUE)) { | |
136 | if ($selectedProductID != NULL) { | |
137 | if ($selectedProductID == $productDAO->id) { | |
138 | if ($selectedOption) { | |
139 | $productDAO->options = ts('Selected Option') . ': ' . $selectedOption; | |
140 | } | |
141 | else { | |
142 | $productDAO->options = NULL; | |
143 | } | |
144 | CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); | |
145 | } | |
146 | } | |
147 | else { | |
148 | CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); | |
149 | } | |
150 | } | |
be2fb01f | 151 | $options = $temp = []; |
6a488035 TO |
152 | $temp = explode(',', $productDAO->options); |
153 | foreach ($temp as $value) { | |
154 | $options[trim($value)] = trim($value); | |
155 | } | |
156 | if ($temp[0] != '') { | |
157 | $form->addElement('select', 'options_' . $productDAO->id, NULL, $options); | |
158 | } | |
159 | } | |
160 | if (count($products)) { | |
161 | $form->assign('showPremium', $formItems); | |
162 | $form->assign('showSelectOptions', $formItems); | |
163 | $form->assign('products', $products); | |
164 | $form->assign('premiumBlock', $premiumBlock); | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | /** | |
fe482240 | 170 | * Build Premium B im Contribution Pages. |
6a488035 | 171 | * |
c490a46a CW |
172 | * @param CRM_Core_Form $form |
173 | * @param int $productID | |
174 | * @param int $premiumProductID | |
6a488035 | 175 | */ |
00be9182 | 176 | public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) { |
6a488035 TO |
177 | if ($premiumProductID) { |
178 | $dao = new CRM_Contribute_DAO_PremiumsProduct(); | |
179 | $dao->id = $premiumProductID; | |
180 | $dao->find(TRUE); | |
181 | $productID = $dao->product_id; | |
182 | } | |
183 | $productDAO = new CRM_Contribute_DAO_Product(); | |
184 | $productDAO->id = $productID; | |
185 | $productDAO->is_active = 1; | |
186 | if ($productDAO->find(TRUE)) { | |
187 | CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); | |
188 | } | |
189 | ||
190 | $radio[$productDAO->id] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id, NULL); | |
be2fb01f | 191 | $options = $temp = []; |
6a488035 TO |
192 | $temp = explode(',', $productDAO->options); |
193 | foreach ($temp as $value) { | |
194 | $options[$value] = $value; | |
195 | } | |
196 | if ($temp[0] != '') { | |
197 | $form->add('select', 'options_' . $productDAO->id, NULL, $options); | |
198 | } | |
199 | ||
200 | $form->addGroup($radio, 'selectProduct', NULL); | |
201 | ||
202 | $form->assign('showRadio', TRUE); | |
203 | $form->assign('showSelectOptions', TRUE); | |
204 | $form->assign('products', $products); | |
205 | $form->assign('preview', TRUE); | |
206 | } | |
207 | ||
208 | /** | |
100fef9d | 209 | * Delete premium associated w/ contribution page. |
6a488035 | 210 | * |
100fef9d | 211 | * @param int $contributionPageID |
6a488035 | 212 | */ |
00be9182 | 213 | public static function deletePremium($contributionPageID) { |
6a488035 TO |
214 | if (!$contributionPageID) { |
215 | return; | |
216 | } | |
217 | ||
218 | //need to delete entries from civicrm_premiums | |
219 | //as well as from civicrm_premiums_product, CRM-4586 | |
220 | ||
be2fb01f | 221 | $params = [ |
6a488035 TO |
222 | 'entity_id' => $contributionPageID, |
223 | 'entity_table' => 'civicrm_contribution_page', | |
be2fb01f | 224 | ]; |
6a488035 TO |
225 | |
226 | $premium = new CRM_Contribute_DAO_Premium(); | |
227 | $premium->copyValues($params); | |
228 | $premium->find(); | |
229 | while ($premium->fetch()) { | |
230 | //lets delete from civicrm_premiums_product | |
231 | $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct(); | |
232 | $premiumsProduct->premiums_id = $premium->id; | |
233 | $premiumsProduct->delete(); | |
234 | ||
235 | //now delete premium | |
236 | $premium->delete(); | |
237 | } | |
238 | } | |
239 | ||
240 | /** | |
fe482240 | 241 | * Retrieve premium product and their options. |
6a488035 | 242 | * |
a6c01b45 CW |
243 | * @return array |
244 | * product and option arrays | |
6a488035 | 245 | */ |
00be9182 | 246 | public static function getPremiumProductInfo() { |
6a488035 | 247 | if (!self::$productInfo) { |
be2fb01f | 248 | $products = $options = []; |
6a488035 TO |
249 | |
250 | $dao = new CRM_Contribute_DAO_Product(); | |
251 | $dao->is_active = 1; | |
252 | $dao->find(); | |
253 | ||
254 | while ($dao->fetch()) { | |
255 | $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )"; | |
256 | $opts = explode(',', $dao->options); | |
257 | foreach ($opts as $k => $v) { | |
258 | $ops[$k] = trim($v); | |
259 | } | |
260 | if ($ops[0] != '') { | |
261 | $options[$dao->id] = $opts; | |
262 | } | |
263 | } | |
264 | ||
be2fb01f | 265 | self::$productInfo = [$products, $options]; |
6a488035 TO |
266 | } |
267 | return self::$productInfo; | |
268 | } | |
96025800 | 269 | |
6a488035 | 270 | } |