Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-25-10-57-01
[civicrm-core.git] / CRM / Contribute / BAO / Premium.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 function __construct() {
48 parent::__construct();
49 }
50
51 /**
52 * Takes a bunch of params that are needed to match certain criteria and
53 * retrieves the relevant objects. Typically the valid params are only
54 * contact_id. We'll tweak this function to be more full featured over a period
55 * of time. This is the inverse function of create. It also stores all the retrieved
56 * values in the default array
57 *
58 * @param array $params (reference ) an assoc array of name/value pairs
59 * @param array $defaults (reference ) an assoc array to hold the flattened values
60 *
61 * @return object CRM_Contribute_BAO_ManagePremium object
62 * @access public
63 * @static
64 */
65 static function retrieve(&$params, &$defaults) {
66 $premium = new CRM_Contribute_DAO_Product();
67 $premium->copyValues($params);
68 if ($premium->find(TRUE)) {
69 CRM_Core_DAO::storeValues($premium, $defaults);
70 return $premium;
71 }
72 return NULL;
73 }
74
75 /**
76 * update the is_active flag in the db
77 *
78 * @param int $id id of the database record
79 * @param boolean $is_active value we want to set the is_active field
80 *
81 * @return Object DAO object on sucess, null otherwise
82 * @static
83 */
84 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 * Function to delete financial Types
90 *
91 * @param $premiumID
92 *
93 * @internal param int $contributionTypeId
94 * @static
95 */
96 static function del($premiumID) {
97 //check dependencies
98
99 //delete from financial Type table
100 $premium = new CRM_Contribute_DAO_Premium();
101 $premium->id = $premiumID;
102 $premium->delete();
103 }
104
105 /**
106 * Function to build Premium Block im Contribution Pages
107 *
108 * @param $form CRM_Core_Form
109 * @param $pageID
110 * @param bool $formItems
111 * @param null $selectedProductID
112 * @param null $selectedOption
113 *
114 * @internal param int $pageId
115 * @static
116 */
117 static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
118 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
119
120 $dao = new CRM_Contribute_DAO_Premium();
121 $dao->entity_table = 'civicrm_contribution_page';
122 $dao->entity_id = $pageID;
123 $dao->premiums_active = 1;
124
125 if ($dao->find(TRUE)) {
126 $premiumID = $dao->id;
127 $premiumBlock = array();
128 CRM_Core_DAO::storeValues($dao, $premiumBlock);
129
130 $dao = new CRM_Contribute_DAO_PremiumsProduct();
131 $dao->premiums_id = $premiumID;
132 $dao->orderBy('weight');
133 $dao->find();
134
135 $products = array();
136 $radio = array();
137 while ($dao->fetch()) {
138 $productDAO = new CRM_Contribute_DAO_Product();
139 $productDAO->id = $dao->product_id;
140 $productDAO->is_active = 1;
141 if ($productDAO->find(TRUE)) {
142 if ($selectedProductID != NULL) {
143 if ($selectedProductID == $productDAO->id) {
144 if ($selectedOption) {
145 $productDAO->options = ts('Selected Option') . ': ' . $selectedOption;
146 }
147 else {
148 $productDAO->options = NULL;
149 }
150 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
151 }
152 }
153 else {
154 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
155 }
156 }
157 $options = $temp = array();
158 $temp = explode(',', $productDAO->options);
159 foreach ($temp as $value) {
160 $options[trim($value)] = trim($value);
161 }
162 if ($temp[0] != '') {
163 $form->addElement('select', 'options_' . $productDAO->id, NULL, $options);
164 }
165 }
166 if (count($products)) {
167 $form->assign('showPremium', $formItems);
168 $form->assign('showSelectOptions', $formItems);
169 $form->assign('products', $products);
170 $form->assign('premiumBlock', $premiumBlock);
171 }
172 }
173 }
174
175 /**
176 * Function to build Premium B im Contribution Pages
177 *
178 * @param $form CRM_Core_Form
179 * @param $productID
180 * @param null $premiumProductID
181 *
182 * @internal param int $pageId
183 * @static
184 */
185 function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
186 if ($premiumProductID) {
187 $dao = new CRM_Contribute_DAO_PremiumsProduct();
188 $dao->id = $premiumProductID;
189 $dao->find(TRUE);
190 $productID = $dao->product_id;
191 }
192 $productDAO = new CRM_Contribute_DAO_Product();
193 $productDAO->id = $productID;
194 $productDAO->is_active = 1;
195 if ($productDAO->find(TRUE)) {
196 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
197 }
198
199 $radio[$productDAO->id] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id, NULL);
200 $options = $temp = array();
201 $temp = explode(',', $productDAO->options);
202 foreach ($temp as $value) {
203 $options[$value] = $value;
204 }
205 if ($temp[0] != '') {
206 $form->add('select', 'options_' . $productDAO->id, NULL, $options);
207 }
208
209 $form->addGroup($radio, 'selectProduct', NULL);
210
211 $form->assign('showRadio', TRUE);
212 $form->assign('showSelectOptions', TRUE);
213 $form->assign('products', $products);
214 $form->assign('preview', TRUE);
215 }
216
217 /**
218 * Function to delete premium associated w/ contribution page.
219 *
220 * @param $contributionPageID
221 *
222 * @internal param int $contribution page id
223 * @static
224 */
225 static function deletePremium($contributionPageID) {
226 if (!$contributionPageID) {
227 return;
228 }
229
230 //need to delete entries from civicrm_premiums
231 //as well as from civicrm_premiums_product, CRM-4586
232
233 $params = array(
234 'entity_id' => $contributionPageID,
235 'entity_table' => 'civicrm_contribution_page',
236 );
237
238 $premium = new CRM_Contribute_DAO_Premium();
239 $premium->copyValues($params);
240 $premium->find();
241 while ($premium->fetch()) {
242 //lets delete from civicrm_premiums_product
243 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
244 $premiumsProduct->premiums_id = $premium->id;
245 $premiumsProduct->delete();
246
247 //now delete premium
248 $premium->delete();
249 }
250 }
251
252 /**
253 * Function to retrieve premium product and their options
254 *
255 * @return array product and option arrays
256 * @static
257 * @access public
258 */
259 static function getPremiumProductInfo() {
260 if (!self::$productInfo) {
261 $products = $options = array();
262
263 $dao = new CRM_Contribute_DAO_Product();
264 $dao->is_active = 1;
265 $dao->find();
266
267 while ($dao->fetch()) {
268 $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )";
269 $opts = explode(',', $dao->options);
270 foreach ($opts as $k => $v) {
271 $ops[$k] = trim($v);
272 }
273 if ($ops[0] != '') {
274 $options[$dao->id] = $opts;
275 }
276 }
277
278 self::$productInfo = array($products, $options);
279 }
280 return self::$productInfo;
281 }
282 }
283