3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 function __construct() {
48 parent
::__construct();
52 * Fetch object based on array of properties
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
57 * @return CRM_Contribute_BAO_ManagePremium object
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);
72 * Update the is_active flag in the db
74 * @param int $id id of the database record
75 * @param boolean $is_active value we want to set the is_active field
77 * @return Object DAO object on sucess, null otherwise
80 static function setIsActive($id, $is_active) {
81 return CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
85 * Delete financial Types
87 * @param int $premiumID
91 static function del($premiumID) {
94 //delete from financial Type table
95 $premium = new CRM_Contribute_DAO_Premium();
96 $premium->id
= $premiumID;
101 * Build Premium Block im Contribution Pages
103 * @param CRM_Core_Form $form
105 * @param bool $formItems
106 * @param int $selectedProductID
107 * @param null $selectedOption
111 static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
112 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
114 $dao = new CRM_Contribute_DAO_Premium();
115 $dao->entity_table
= 'civicrm_contribution_page';
116 $dao->entity_id
= $pageID;
117 $dao->premiums_active
= 1;
119 if ($dao->find(TRUE)) {
120 $premiumID = $dao->id
;
121 $premiumBlock = array();
122 CRM_Core_DAO
::storeValues($dao, $premiumBlock);
124 $dao = new CRM_Contribute_DAO_PremiumsProduct();
125 $dao->premiums_id
= $premiumID;
126 $dao->orderBy('weight');
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;
142 $productDAO->options
= NULL;
144 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
148 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
151 $options = $temp = array();
152 $temp = explode(',', $productDAO->options
);
153 foreach ($temp as $value) {
154 $options[trim($value)] = trim($value);
156 if ($temp[0] != '') {
157 $form->addElement('select', 'options_' . $productDAO->id
, NULL, $options);
160 if (count($products)) {
161 $form->assign('showPremium', $formItems);
162 $form->assign('showSelectOptions', $formItems);
163 $form->assign('products', $products);
164 $form->assign('premiumBlock', $premiumBlock);
170 * Build Premium B im Contribution Pages
172 * @param CRM_Core_Form $form
173 * @param int $productID
174 * @param int $premiumProductID
178 function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
179 if ($premiumProductID) {
180 $dao = new CRM_Contribute_DAO_PremiumsProduct();
181 $dao->id
= $premiumProductID;
183 $productID = $dao->product_id
;
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
]);
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;
198 if ($temp[0] != '') {
199 $form->add('select', 'options_' . $productDAO->id
, NULL, $options);
202 $form->addGroup($radio, 'selectProduct', NULL);
204 $form->assign('showRadio', TRUE);
205 $form->assign('showSelectOptions', TRUE);
206 $form->assign('products', $products);
207 $form->assign('preview', TRUE);
211 * Delete premium associated w/ contribution page.
213 * @param int $contributionPageID
217 static function deletePremium($contributionPageID) {
218 if (!$contributionPageID) {
222 //need to delete entries from civicrm_premiums
223 //as well as from civicrm_premiums_product, CRM-4586
226 'entity_id' => $contributionPageID,
227 'entity_table' => 'civicrm_contribution_page',
230 $premium = new CRM_Contribute_DAO_Premium();
231 $premium->copyValues($params);
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();
245 * Retrieve premium product and their options
247 * @return array product and option arrays
251 static function getPremiumProductInfo() {
252 if (!self
::$productInfo) {
253 $products = $options = array();
255 $dao = new CRM_Contribute_DAO_Product();
259 while ($dao->fetch()) {
260 $products[$dao->id
] = $dao->name
. " ( " . $dao->sku
. " )";
261 $opts = explode(',', $dao->options
);
262 foreach ($opts as $k => $v) {
266 $options[$dao->id
] = $opts;
270 self
::$productInfo = array($products, $options);
272 return self
::$productInfo;