Merge pull request #4772 from jitendrapurohit/CRM-15750
[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 function __construct() {
48 parent::__construct();
49 }
50
51 /**
52 * Fetch object based on array of properties
53 *
54 * @param array $params (reference ) an assoc array of name/value pairs
55 * @param array $defaults (reference ) an assoc array to hold the flattened values
56 *
57 * @return CRM_Contribute_BAO_ManagePremium object
58 * @access public
59 * @static
60 */
61 static function retrieve(&$params, &$defaults) {
62 $premium = new CRM_Contribute_DAO_Product();
63 $premium->copyValues($params);
64 if ($premium->find(TRUE)) {
65 CRM_Core_DAO::storeValues($premium, $defaults);
66 return $premium;
67 }
68 return NULL;
69 }
70
71 /**
72 * Update the is_active flag in the db
73 *
74 * @param int $id id of the database record
75 * @param boolean $is_active value we want to set the is_active field
76 *
77 * @return Object DAO object on sucess, null otherwise
78 * @static
79 */
80 static function setIsActive($id, $is_active) {
81 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
82 }
83
84 /**
85 * Delete financial Types
86 *
87 * @param int $premiumID
88 *
89 * @static
90 */
91 static function del($premiumID) {
92 //check dependencies
93
94 //delete from financial Type table
95 $premium = new CRM_Contribute_DAO_Premium();
96 $premium->id = $premiumID;
97 $premium->delete();
98 }
99
100 /**
101 * Build Premium Block im Contribution Pages
102 *
103 * @param CRM_Core_Form $form
104 * @param int $pageID
105 * @param bool $formItems
106 * @param int $selectedProductID
107 * @param null $selectedOption
108 *
109 * @static
110 */
111 static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
112 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
113
114 $dao = new CRM_Contribute_DAO_Premium();
115 $dao->entity_table = 'civicrm_contribution_page';
116 $dao->entity_id = $pageID;
117 $dao->premiums_active = 1;
118
119 if ($dao->find(TRUE)) {
120 $premiumID = $dao->id;
121 $premiumBlock = array();
122 CRM_Core_DAO::storeValues($dao, $premiumBlock);
123
124 $dao = new CRM_Contribute_DAO_PremiumsProduct();
125 $dao->premiums_id = $premiumID;
126 $dao->orderBy('weight');
127 $dao->find();
128
129 $products = array();
130 $radio = array();
131 while ($dao->fetch()) {
132 $productDAO = new CRM_Contribute_DAO_Product();
133 $productDAO->id = $dao->product_id;
134 $productDAO->is_active = 1;
135 if ($productDAO->find(TRUE)) {
136 if ($selectedProductID != NULL) {
137 if ($selectedProductID == $productDAO->id) {
138 if ($selectedOption) {
139 $productDAO->options = ts('Selected Option') . ': ' . $selectedOption;
140 }
141 else {
142 $productDAO->options = NULL;
143 }
144 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
145 }
146 }
147 else {
148 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
149 }
150 }
151 $options = $temp = array();
152 $temp = explode(',', $productDAO->options);
153 foreach ($temp as $value) {
154 $options[trim($value)] = trim($value);
155 }
156 if ($temp[0] != '') {
157 $form->addElement('select', 'options_' . $productDAO->id, NULL, $options);
158 }
159 }
160 if (count($products)) {
161 $form->assign('showPremium', $formItems);
162 $form->assign('showSelectOptions', $formItems);
163 $form->assign('products', $products);
164 $form->assign('premiumBlock', $premiumBlock);
165 }
166 }
167 }
168
169 /**
170 * Build Premium B im Contribution Pages
171 *
172 * @param CRM_Core_Form $form
173 * @param int $productID
174 * @param int $premiumProductID
175 *
176 * @static
177 */
178 function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
179 if ($premiumProductID) {
180 $dao = new CRM_Contribute_DAO_PremiumsProduct();
181 $dao->id = $premiumProductID;
182 $dao->find(TRUE);
183 $productID = $dao->product_id;
184 }
185 $productDAO = new CRM_Contribute_DAO_Product();
186 $productDAO->id = $productID;
187 $productDAO->is_active = 1;
188 if ($productDAO->find(TRUE)) {
189 CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]);
190 }
191
192 $radio[$productDAO->id] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id, NULL);
193 $options = $temp = array();
194 $temp = explode(',', $productDAO->options);
195 foreach ($temp as $value) {
196 $options[$value] = $value;
197 }
198 if ($temp[0] != '') {
199 $form->add('select', 'options_' . $productDAO->id, NULL, $options);
200 }
201
202 $form->addGroup($radio, 'selectProduct', NULL);
203
204 $form->assign('showRadio', TRUE);
205 $form->assign('showSelectOptions', TRUE);
206 $form->assign('products', $products);
207 $form->assign('preview', TRUE);
208 }
209
210 /**
211 * Delete premium associated w/ contribution page.
212 *
213 * @param int $contributionPageID
214 *
215 * @static
216 */
217 static function deletePremium($contributionPageID) {
218 if (!$contributionPageID) {
219 return;
220 }
221
222 //need to delete entries from civicrm_premiums
223 //as well as from civicrm_premiums_product, CRM-4586
224
225 $params = array(
226 'entity_id' => $contributionPageID,
227 'entity_table' => 'civicrm_contribution_page',
228 );
229
230 $premium = new CRM_Contribute_DAO_Premium();
231 $premium->copyValues($params);
232 $premium->find();
233 while ($premium->fetch()) {
234 //lets delete from civicrm_premiums_product
235 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
236 $premiumsProduct->premiums_id = $premium->id;
237 $premiumsProduct->delete();
238
239 //now delete premium
240 $premium->delete();
241 }
242 }
243
244 /**
245 * Retrieve premium product and their options
246 *
247 * @return array product and option arrays
248 * @static
249 * @access public
250 */
251 static function getPremiumProductInfo() {
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 }
274 }
275