Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Product information. |
6a488035 | 37 | * @var array |
6a488035 TO |
38 | */ |
39 | private static $productInfo; | |
40 | ||
41 | /** | |
fe482240 | 42 | * Class constructor. |
665e5ec7 | 43 | */ |
00be9182 | 44 | public function __construct() { |
6a488035 TO |
45 | parent::__construct(); |
46 | } | |
47 | ||
48 | /** | |
fe482240 | 49 | * Fetch object based on array of properties. |
6a488035 | 50 | * |
014c4014 TO |
51 | * @param array $params |
52 | * (reference ) an assoc array of name/value pairs. | |
53 | * @param array $defaults | |
54 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 55 | * |
16b10e64 | 56 | * @return CRM_Contribute_BAO_ManagePremium |
6a488035 | 57 | */ |
00be9182 | 58 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
59 | $premium = new CRM_Contribute_DAO_Product(); |
60 | $premium->copyValues($params); | |
61 | if ($premium->find(TRUE)) { | |
62 | CRM_Core_DAO::storeValues($premium, $defaults); | |
63 | return $premium; | |
64 | } | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | /** | |
fe482240 | 69 | * Update the is_active flag in the db. |
6a488035 | 70 | * |
014c4014 TO |
71 | * @param int $id |
72 | * Id of the database record. | |
73 | * @param bool $is_active | |
74 | * Value we want to set the is_active field. | |
6a488035 | 75 | * |
a6c01b45 | 76 | * @return Object |
b44e3f84 | 77 | * DAO object on success, null otherwise |
6a488035 | 78 | */ |
00be9182 | 79 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
80 | return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active); |
81 | } | |
82 | ||
83 | /** | |
fe482240 | 84 | * Delete financial Types. |
6a488035 | 85 | * |
100fef9d | 86 | * @param int $premiumID |
f4aaa82a | 87 | * |
6a488035 | 88 | */ |
00be9182 | 89 | public static function del($premiumID) { |
6a488035 TO |
90 | //check dependencies |
91 | ||
874c9be7 | 92 | //delete from financial Type table |
6a488035 TO |
93 | $premium = new CRM_Contribute_DAO_Premium(); |
94 | $premium->id = $premiumID; | |
95 | $premium->delete(); | |
96 | } | |
97 | ||
98 | /** | |
fe482240 | 99 | * Build Premium Block im Contribution Pages. |
6a488035 | 100 | * |
100fef9d CW |
101 | * @param CRM_Core_Form $form |
102 | * @param int $pageID | |
f4aaa82a | 103 | * @param bool $formItems |
100fef9d | 104 | * @param int $selectedProductID |
f4aaa82a EM |
105 | * @param null $selectedOption |
106 | * | |
6a488035 | 107 | */ |
00be9182 | 108 | public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) { |
6a488035 TO |
109 | $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct')); |
110 | ||
111 | $dao = new CRM_Contribute_DAO_Premium(); | |
112 | $dao->entity_table = 'civicrm_contribution_page'; | |
113 | $dao->entity_id = $pageID; | |
114 | $dao->premiums_active = 1; | |
926c7c4c | 115 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, 'add'); |
7fb041e3 E |
116 | $addWhere = "financial_type_id IN (0)"; |
117 | if (!empty($financialTypes)) { | |
40c655aa | 118 | $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")"; |
7fb041e3 | 119 | } |
6a488035 TO |
120 | |
121 | if ($dao->find(TRUE)) { | |
122 | $premiumID = $dao->id; | |
123 | $premiumBlock = array(); | |
124 | CRM_Core_DAO::storeValues($dao, $premiumBlock); | |
125 | ||
126 | $dao = new CRM_Contribute_DAO_PremiumsProduct(); | |
127 | $dao->premiums_id = $premiumID; | |
7fb041e3 | 128 | $dao->whereAdd($addWhere); |
6a488035 TO |
129 | $dao->orderBy('weight'); |
130 | $dao->find(); | |
131 | ||
132 | $products = array(); | |
133 | $radio = array(); | |
134 | while ($dao->fetch()) { | |
135 | $productDAO = new CRM_Contribute_DAO_Product(); | |
136 | $productDAO->id = $dao->product_id; | |
137 | $productDAO->is_active = 1; | |
138 | if ($productDAO->find(TRUE)) { | |
139 | if ($selectedProductID != NULL) { | |
140 | if ($selectedProductID == $productDAO->id) { | |
141 | if ($selectedOption) { | |
142 | $productDAO->options = ts('Selected Option') . ': ' . $selectedOption; | |
143 | } | |
144 | else { | |
145 | $productDAO->options = NULL; | |
146 | } | |
147 | CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); | |
148 | } | |
149 | } | |
150 | else { | |
151 | CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); | |
152 | } | |
153 | } | |
154 | $options = $temp = array(); | |
155 | $temp = explode(',', $productDAO->options); | |
156 | foreach ($temp as $value) { | |
157 | $options[trim($value)] = trim($value); | |
158 | } | |
159 | if ($temp[0] != '') { | |
160 | $form->addElement('select', 'options_' . $productDAO->id, NULL, $options); | |
161 | } | |
162 | } | |
163 | if (count($products)) { | |
164 | $form->assign('showPremium', $formItems); | |
165 | $form->assign('showSelectOptions', $formItems); | |
166 | $form->assign('products', $products); | |
167 | $form->assign('premiumBlock', $premiumBlock); | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | /** | |
fe482240 | 173 | * Build Premium B im Contribution Pages. |
6a488035 | 174 | * |
c490a46a CW |
175 | * @param CRM_Core_Form $form |
176 | * @param int $productID | |
177 | * @param int $premiumProductID | |
f4aaa82a | 178 | * |
6a488035 | 179 | */ |
00be9182 | 180 | public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) { |
6a488035 TO |
181 | if ($premiumProductID) { |
182 | $dao = new CRM_Contribute_DAO_PremiumsProduct(); | |
183 | $dao->id = $premiumProductID; | |
184 | $dao->find(TRUE); | |
185 | $productID = $dao->product_id; | |
186 | } | |
187 | $productDAO = new CRM_Contribute_DAO_Product(); | |
188 | $productDAO->id = $productID; | |
189 | $productDAO->is_active = 1; | |
190 | if ($productDAO->find(TRUE)) { | |
191 | CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); | |
192 | } | |
193 | ||
194 | $radio[$productDAO->id] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id, NULL); | |
195 | $options = $temp = array(); | |
196 | $temp = explode(',', $productDAO->options); | |
197 | foreach ($temp as $value) { | |
198 | $options[$value] = $value; | |
199 | } | |
200 | if ($temp[0] != '') { | |
201 | $form->add('select', 'options_' . $productDAO->id, NULL, $options); | |
202 | } | |
203 | ||
204 | $form->addGroup($radio, 'selectProduct', NULL); | |
205 | ||
206 | $form->assign('showRadio', TRUE); | |
207 | $form->assign('showSelectOptions', TRUE); | |
208 | $form->assign('products', $products); | |
209 | $form->assign('preview', TRUE); | |
210 | } | |
211 | ||
212 | /** | |
100fef9d | 213 | * Delete premium associated w/ contribution page. |
6a488035 | 214 | * |
100fef9d | 215 | * @param int $contributionPageID |
f4aaa82a | 216 | * |
6a488035 | 217 | */ |
00be9182 | 218 | public static function deletePremium($contributionPageID) { |
6a488035 TO |
219 | if (!$contributionPageID) { |
220 | return; | |
221 | } | |
222 | ||
223 | //need to delete entries from civicrm_premiums | |
224 | //as well as from civicrm_premiums_product, CRM-4586 | |
225 | ||
226 | $params = array( | |
227 | 'entity_id' => $contributionPageID, | |
228 | 'entity_table' => 'civicrm_contribution_page', | |
229 | ); | |
230 | ||
231 | $premium = new CRM_Contribute_DAO_Premium(); | |
232 | $premium->copyValues($params); | |
233 | $premium->find(); | |
234 | while ($premium->fetch()) { | |
235 | //lets delete from civicrm_premiums_product | |
236 | $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct(); | |
237 | $premiumsProduct->premiums_id = $premium->id; | |
238 | $premiumsProduct->delete(); | |
239 | ||
240 | //now delete premium | |
241 | $premium->delete(); | |
242 | } | |
243 | } | |
244 | ||
245 | /** | |
fe482240 | 246 | * Retrieve premium product and their options. |
6a488035 | 247 | * |
a6c01b45 CW |
248 | * @return array |
249 | * product and option arrays | |
6a488035 | 250 | */ |
00be9182 | 251 | public static function getPremiumProductInfo() { |
6a488035 TO |
252 | if (!self::$productInfo) { |
253 | $products = $options = array(); | |
254 | ||
255 | $dao = new CRM_Contribute_DAO_Product(); | |
256 | $dao->is_active = 1; | |
257 | $dao->find(); | |
258 | ||
259 | while ($dao->fetch()) { | |
260 | $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )"; | |
261 | $opts = explode(',', $dao->options); | |
262 | foreach ($opts as $k => $v) { | |
263 | $ops[$k] = trim($v); | |
264 | } | |
265 | if ($ops[0] != '') { | |
266 | $options[$dao->id] = $opts; | |
267 | } | |
268 | } | |
269 | ||
270 | self::$productInfo = array($products, $options); | |
271 | } | |
272 | return self::$productInfo; | |
273 | } | |
96025800 | 274 | |
6a488035 | 275 | } |