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