3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Contribute_BAO_Premium
extends CRM_Contribute_DAO_Premium
{
42 private static $productInfo;
47 public function __construct() {
48 parent
::__construct();
52 * Fetch object based on array of properties
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.
59 * @return CRM_Contribute_BAO_ManagePremium
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);
73 * Update the is_active flag in the db
76 * Id of the database record.
77 * @param bool $is_active
78 * Value we want to set the is_active field.
81 * DAO object on sucess, null otherwise
84 public static function setIsActive($id, $is_active) {
85 return CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
89 * Delete financial Types
91 * @param int $premiumID
95 public static function del($premiumID) {
98 //delete from financial Type table
99 $premium = new CRM_Contribute_DAO_Premium();
100 $premium->id
= $premiumID;
105 * Build Premium Block im Contribution Pages
107 * @param CRM_Core_Form $form
109 * @param bool $formItems
110 * @param int $selectedProductID
111 * @param null $selectedOption
115 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
116 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
118 $dao = new CRM_Contribute_DAO_Premium();
119 $dao->entity_table
= 'civicrm_contribution_page';
120 $dao->entity_id
= $pageID;
121 $dao->premiums_active
= 1;
123 if ($dao->find(TRUE)) {
124 $premiumID = $dao->id
;
125 $premiumBlock = array();
126 CRM_Core_DAO
::storeValues($dao, $premiumBlock);
128 $dao = new CRM_Contribute_DAO_PremiumsProduct();
129 $dao->premiums_id
= $premiumID;
130 $dao->orderBy('weight');
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;
146 $productDAO->options
= NULL;
148 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
152 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
155 $options = $temp = array();
156 $temp = explode(',', $productDAO->options
);
157 foreach ($temp as $value) {
158 $options[trim($value)] = trim($value);
160 if ($temp[0] != '') {
161 $form->addElement('select', 'options_' . $productDAO->id
, NULL, $options);
164 if (count($products)) {
165 $form->assign('showPremium', $formItems);
166 $form->assign('showSelectOptions', $formItems);
167 $form->assign('products', $products);
168 $form->assign('premiumBlock', $premiumBlock);
174 * Build Premium B im Contribution Pages
176 * @param CRM_Core_Form $form
177 * @param int $productID
178 * @param int $premiumProductID
182 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
183 if ($premiumProductID) {
184 $dao = new CRM_Contribute_DAO_PremiumsProduct();
185 $dao->id
= $premiumProductID;
187 $productID = $dao->product_id
;
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
]);
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;
202 if ($temp[0] != '') {
203 $form->add('select', 'options_' . $productDAO->id
, NULL, $options);
206 $form->addGroup($radio, 'selectProduct', NULL);
208 $form->assign('showRadio', TRUE);
209 $form->assign('showSelectOptions', TRUE);
210 $form->assign('products', $products);
211 $form->assign('preview', TRUE);
215 * Delete premium associated w/ contribution page.
217 * @param int $contributionPageID
221 public static function deletePremium($contributionPageID) {
222 if (!$contributionPageID) {
226 //need to delete entries from civicrm_premiums
227 //as well as from civicrm_premiums_product, CRM-4586
230 'entity_id' => $contributionPageID,
231 'entity_table' => 'civicrm_contribution_page',
234 $premium = new CRM_Contribute_DAO_Premium();
235 $premium->copyValues($params);
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();
249 * Retrieve premium product and their options
252 * product and option arrays
255 public static function getPremiumProductInfo() {
256 if (!self
::$productInfo) {
257 $products = $options = array();
259 $dao = new CRM_Contribute_DAO_Product();
263 while ($dao->fetch()) {
264 $products[$dao->id
] = $dao->name
. " ( " . $dao->sku
. " )";
265 $opts = explode(',', $dao->options
);
266 foreach ($opts as $k => $v) {
270 $options[$dao->id
] = $opts;
274 self
::$productInfo = array($products, $options);
276 return self
::$productInfo;