Merge branch 4.5 into master
[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
81 * DAO object on sucess, null otherwise
82 * @static
83 */
84 public static function setIsActive($id, $is_active) {
85 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
86 }
87
88 /**
89 * Delete financial Types
90 *
91 * @param int $premiumID
92 *
93 * @static
94 */
95 public static function del($premiumID) {
96 //check dependencies
97
98 //delete from financial Type table
99 $premium = new CRM_Contribute_DAO_Premium();
100 $premium->id = $premiumID;
101 $premium->delete();
102 }
103
104 /**
105 * Build Premium Block im Contribution Pages
106 *
107 * @param CRM_Core_Form $form
108 * @param int $pageID
109 * @param bool $formItems
110 * @param int $selectedProductID
111 * @param null $selectedOption
112 *
113 * @static
114 */
115 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
116 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
117
118 $dao = new CRM_Contribute_DAO_Premium();
119 $dao->entity_table = 'civicrm_contribution_page';
120 $dao->entity_id = $pageID;
121 $dao->premiums_active = 1;
122
123 if ($dao->find(TRUE)) {
124 $premiumID = $dao->id;
125 $premiumBlock = array();
126 CRM_Core_DAO::storeValues($dao, $premiumBlock);
127
128 $dao = new CRM_Contribute_DAO_PremiumsProduct();
129 $dao->premiums_id = $premiumID;
130 $dao->orderBy('weight');
131 $dao->find();
132
133 $products = array();
134 $radio = array();
135 while ($dao->fetch()) {
136 $productDAO = new CRM_Contribute_DAO_Product();
137 $productDAO->id = $dao->product_id;
138 $productDAO->is_active = 1;
139 if ($productDAO->find(TRUE)) {
140 if ($selectedProductID != NULL) {
141 if ($selectedProductID == $productDAO->id) {
142 if ($selectedOption) {
143 $productDAO->options = ts('Selected Option') . ': ' . $selectedOption;
144 }
145 else {
146 $productDAO->options = NULL;
147 }
148 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
149 }
150 }
151 else {
152 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
153 }
154 }
155 $options = $temp = array();
156 $temp = explode(',', $productDAO->options);
157 foreach ($temp as $value) {
158 $options[trim($value)] = trim($value);
159 }
160 if ($temp[0] != '') {
161 $form->addElement('select', 'options_' . $productDAO->id, NULL, $options);
162 }
163 }
164 if (count($products)) {
165 $form->assign('showPremium', $formItems);
166 $form->assign('showSelectOptions', $formItems);
167 $form->assign('products', $products);
168 $form->assign('premiumBlock', $premiumBlock);
169 }
170 }
171 }
172
173 /**
174 * Build Premium B im Contribution Pages
175 *
176 * @param CRM_Core_Form $form
177 * @param int $productID
178 * @param int $premiumProductID
179 *
180 * @static
181 */
182 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
183 if ($premiumProductID) {
184 $dao = new CRM_Contribute_DAO_PremiumsProduct();
185 $dao->id = $premiumProductID;
186 $dao->find(TRUE);
187 $productID = $dao->product_id;
188 }
189 $productDAO = new CRM_Contribute_DAO_Product();
190 $productDAO->id = $productID;
191 $productDAO->is_active = 1;
192 if ($productDAO->find(TRUE)) {
193 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
194 }
195
196 $radio[$productDAO->id] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id, NULL);
197 $options = $temp = array();
198 $temp = explode(',', $productDAO->options);
199 foreach ($temp as $value) {
200 $options[$value] = $value;
201 }
202 if ($temp[0] != '') {
203 $form->add('select', 'options_' . $productDAO->id, NULL, $options);
204 }
205
206 $form->addGroup($radio, 'selectProduct', NULL);
207
208 $form->assign('showRadio', TRUE);
209 $form->assign('showSelectOptions', TRUE);
210 $form->assign('products', $products);
211 $form->assign('preview', TRUE);
212 }
213
214 /**
215 * Delete premium associated w/ contribution page.
216 *
217 * @param int $contributionPageID
218 *
219 * @static
220 */
221 public static function deletePremium($contributionPageID) {
222 if (!$contributionPageID) {
223 return;
224 }
225
226 //need to delete entries from civicrm_premiums
227 //as well as from civicrm_premiums_product, CRM-4586
228
229 $params = array(
230 'entity_id' => $contributionPageID,
231 'entity_table' => 'civicrm_contribution_page',
232 );
233
234 $premium = new CRM_Contribute_DAO_Premium();
235 $premium->copyValues($params);
236 $premium->find();
237 while ($premium->fetch()) {
238 //lets delete from civicrm_premiums_product
239 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
240 $premiumsProduct->premiums_id = $premium->id;
241 $premiumsProduct->delete();
242
243 //now delete premium
244 $premium->delete();
245 }
246 }
247
248 /**
249 * Retrieve premium product and their options
250 *
251 * @return array
252 * product and option arrays
253 * @static
254 */
255 public static function getPremiumProductInfo() {
256 if (!self::$productInfo) {
257 $products = $options = array();
258
259 $dao = new CRM_Contribute_DAO_Product();
260 $dao->is_active = 1;
261 $dao->find();
262
263 while ($dao->fetch()) {
264 $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )";
265 $opts = explode(',', $dao->options);
266 foreach ($opts as $k => $v) {
267 $ops[$k] = trim($v);
268 }
269 if ($ops[0] != '') {
270 $options[$dao->id] = $opts;
271 }
272 }
273
274 self::$productInfo = array($products, $options);
275 }
276 return self::$productInfo;
277 }
278 }