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