Merge pull request #4880 from totten/master-cs3
[civicrm-core.git] / CRM / Contribute / BAO / Premium.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium {
36
37 /**
38 * Product information
39 * @var array
40 * @static
41 */
42 private static $productInfo;
43
44 /**
45 * Class constructor
46 */
47 public function __construct() {
48 parent::__construct();
49 }
50
51 /**
52 * Fetch object based on array of properties
53 *
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $defaults
57 * (reference ) an assoc array to hold the flattened values.
58 *
59 * @return CRM_Contribute_BAO_ManagePremium object
60 * @static
61 */
62 public static function retrieve(&$params, &$defaults) {
63 $premium = new CRM_Contribute_DAO_Product();
64 $premium->copyValues($params);
65 if ($premium->find(TRUE)) {
66 CRM_Core_DAO::storeValues($premium, $defaults);
67 return $premium;
68 }
69 return NULL;
70 }
71
72 /**
73 * Update the is_active flag in the db
74 *
75 * @param int $id
76 * Id of the database record.
77 * @param bool $is_active
78 * Value we want to set the is_active field.
79 *
80 * @return Object DAO object on sucess, null otherwise
81 * @static
82 */
83 public static function setIsActive($id, $is_active) {
84 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
85 }
86
87 /**
88 * Delete financial Types
89 *
90 * @param int $premiumID
91 *
92 * @static
93 */
94 public static function del($premiumID) {
95 //check dependencies
96
97 //delete from financial Type table
98 $premium = new CRM_Contribute_DAO_Premium();
99 $premium->id = $premiumID;
100 $premium->delete();
101 }
102
103 /**
104 * Build Premium Block im Contribution Pages
105 *
106 * @param CRM_Core_Form $form
107 * @param int $pageID
108 * @param bool $formItems
109 * @param int $selectedProductID
110 * @param null $selectedOption
111 *
112 * @static
113 */
114 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
115 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
116
117 $dao = new CRM_Contribute_DAO_Premium();
118 $dao->entity_table = 'civicrm_contribution_page';
119 $dao->entity_id = $pageID;
120 $dao->premiums_active = 1;
121
122 if ($dao->find(TRUE)) {
123 $premiumID = $dao->id;
124 $premiumBlock = array();
125 CRM_Core_DAO::storeValues($dao, $premiumBlock);
126
127 $dao = new CRM_Contribute_DAO_PremiumsProduct();
128 $dao->premiums_id = $premiumID;
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 /**
173 * Build Premium B im Contribution Pages
174 *
175 * @param CRM_Core_Form $form
176 * @param int $productID
177 * @param int $premiumProductID
178 *
179 * @static
180 */
181 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
182 if ($premiumProductID) {
183 $dao = new CRM_Contribute_DAO_PremiumsProduct();
184 $dao->id = $premiumProductID;
185 $dao->find(TRUE);
186 $productID = $dao->product_id;
187 }
188 $productDAO = new CRM_Contribute_DAO_Product();
189 $productDAO->id = $productID;
190 $productDAO->is_active = 1;
191 if ($productDAO->find(TRUE)) {
192 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
193 }
194
195 $radio[$productDAO->id] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id, NULL);
196 $options = $temp = array();
197 $temp = explode(',', $productDAO->options);
198 foreach ($temp as $value) {
199 $options[$value] = $value;
200 }
201 if ($temp[0] != '') {
202 $form->add('select', 'options_' . $productDAO->id, NULL, $options);
203 }
204
205 $form->addGroup($radio, 'selectProduct', NULL);
206
207 $form->assign('showRadio', TRUE);
208 $form->assign('showSelectOptions', TRUE);
209 $form->assign('products', $products);
210 $form->assign('preview', TRUE);
211 }
212
213 /**
214 * Delete premium associated w/ contribution page.
215 *
216 * @param int $contributionPageID
217 *
218 * @static
219 */
220 public static function deletePremium($contributionPageID) {
221 if (!$contributionPageID) {
222 return;
223 }
224
225 //need to delete entries from civicrm_premiums
226 //as well as from civicrm_premiums_product, CRM-4586
227
228 $params = array(
229 'entity_id' => $contributionPageID,
230 'entity_table' => 'civicrm_contribution_page',
231 );
232
233 $premium = new CRM_Contribute_DAO_Premium();
234 $premium->copyValues($params);
235 $premium->find();
236 while ($premium->fetch()) {
237 //lets delete from civicrm_premiums_product
238 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
239 $premiumsProduct->premiums_id = $premium->id;
240 $premiumsProduct->delete();
241
242 //now delete premium
243 $premium->delete();
244 }
245 }
246
247 /**
248 * Retrieve premium product and their options
249 *
250 * @return array product and option arrays
251 * @static
252 */
253 public static function getPremiumProductInfo() {
254 if (!self::$productInfo) {
255 $products = $options = array();
256
257 $dao = new CRM_Contribute_DAO_Product();
258 $dao->is_active = 1;
259 $dao->find();
260
261 while ($dao->fetch()) {
262 $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )";
263 $opts = explode(',', $dao->options);
264 foreach ($opts as $k => $v) {
265 $ops[$k] = trim($v);
266 }
267 if ($ops[0] != '') {
268 $options[$dao->id] = $opts;
269 }
270 }
271
272 self::$productInfo = array($products, $options);
273 }
274 return self::$productInfo;
275 }
276 }