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 (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
60 public static function retrieve(&$params, &$defaults) {
61 $premium = new CRM_Contribute_DAO_Product();
62 $premium->copyValues($params);
63 if ($premium->find(TRUE)) {
64 CRM_Core_DAO
::storeValues($premium, $defaults);
71 * Update the is_active flag in the db
73 * @param int $id id of the database record
74 * @param boolean $is_active value we want to set the is_active field
76 * @return Object DAO object on sucess, null otherwise
79 public static function setIsActive($id, $is_active) {
80 return CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
84 * Delete financial Types
86 * @param int $premiumID
90 public static function del($premiumID) {
93 //delete from financial Type table
94 $premium = new CRM_Contribute_DAO_Premium();
95 $premium->id
= $premiumID;
100 * Build Premium Block im Contribution Pages
102 * @param CRM_Core_Form $form
104 * @param bool $formItems
105 * @param int $selectedProductID
106 * @param null $selectedOption
110 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
111 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
113 $dao = new CRM_Contribute_DAO_Premium();
114 $dao->entity_table
= 'civicrm_contribution_page';
115 $dao->entity_id
= $pageID;
116 $dao->premiums_active
= 1;
118 if ($dao->find(TRUE)) {
119 $premiumID = $dao->id
;
120 $premiumBlock = array();
121 CRM_Core_DAO
::storeValues($dao, $premiumBlock);
123 $dao = new CRM_Contribute_DAO_PremiumsProduct();
124 $dao->premiums_id
= $premiumID;
125 $dao->orderBy('weight');
130 while ($dao->fetch()) {
131 $productDAO = new CRM_Contribute_DAO_Product();
132 $productDAO->id
= $dao->product_id
;
133 $productDAO->is_active
= 1;
134 if ($productDAO->find(TRUE)) {
135 if ($selectedProductID != NULL) {
136 if ($selectedProductID == $productDAO->id
) {
137 if ($selectedOption) {
138 $productDAO->options
= ts('Selected Option') . ': ' . $selectedOption;
141 $productDAO->options
= NULL;
143 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
147 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
150 $options = $temp = array();
151 $temp = explode(',', $productDAO->options
);
152 foreach ($temp as $value) {
153 $options[trim($value)] = trim($value);
155 if ($temp[0] != '') {
156 $form->addElement('select', 'options_' . $productDAO->id
, NULL, $options);
159 if (count($products)) {
160 $form->assign('showPremium', $formItems);
161 $form->assign('showSelectOptions', $formItems);
162 $form->assign('products', $products);
163 $form->assign('premiumBlock', $premiumBlock);
169 * Build Premium B im Contribution Pages
171 * @param CRM_Core_Form $form
172 * @param int $productID
173 * @param int $premiumProductID
177 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
178 if ($premiumProductID) {
179 $dao = new CRM_Contribute_DAO_PremiumsProduct();
180 $dao->id
= $premiumProductID;
182 $productID = $dao->product_id
;
184 $productDAO = new CRM_Contribute_DAO_Product();
185 $productDAO->id
= $productID;
186 $productDAO->is_active
= 1;
187 if ($productDAO->find(TRUE)) {
188 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
191 $radio[$productDAO->id
] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id
, NULL);
192 $options = $temp = array();
193 $temp = explode(',', $productDAO->options
);
194 foreach ($temp as $value) {
195 $options[$value] = $value;
197 if ($temp[0] != '') {
198 $form->add('select', 'options_' . $productDAO->id
, NULL, $options);
201 $form->addGroup($radio, 'selectProduct', NULL);
203 $form->assign('showRadio', TRUE);
204 $form->assign('showSelectOptions', TRUE);
205 $form->assign('products', $products);
206 $form->assign('preview', TRUE);
210 * Delete premium associated w/ contribution page.
212 * @param int $contributionPageID
216 public static function deletePremium($contributionPageID) {
217 if (!$contributionPageID) {
221 //need to delete entries from civicrm_premiums
222 //as well as from civicrm_premiums_product, CRM-4586
225 'entity_id' => $contributionPageID,
226 'entity_table' => 'civicrm_contribution_page',
229 $premium = new CRM_Contribute_DAO_Premium();
230 $premium->copyValues($params);
232 while ($premium->fetch()) {
233 //lets delete from civicrm_premiums_product
234 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
235 $premiumsProduct->premiums_id
= $premium->id
;
236 $premiumsProduct->delete();
244 * Retrieve premium product and their options
246 * @return array product and option arrays
249 public static function getPremiumProductInfo() {
250 if (!self
::$productInfo) {
251 $products = $options = array();
253 $dao = new CRM_Contribute_DAO_Product();
257 while ($dao->fetch()) {
258 $products[$dao->id
] = $dao->name
. " ( " . $dao->sku
. " )";
259 $opts = explode(',', $dao->options
);
260 foreach ($opts as $k => $v) {
264 $options[$dao->id
] = $opts;
268 self
::$productInfo = array($products, $options);
270 return self
::$productInfo;