3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Contribute_BAO_Premium
extends CRM_Contribute_DAO_Premium
{
42 private static $productInfo;
47 function __construct() {
48 parent
::__construct();
52 * Takes a bunch of params that are needed to match certain criteria and
53 * retrieves the relevant objects. Typically the valid params are only
54 * contact_id. We'll tweak this function to be more full featured over a period
55 * of time. This is the inverse function of create. It also stores all the retrieved
56 * values in the default array
58 * @param array $params (reference ) an assoc array of name/value pairs
59 * @param array $defaults (reference ) an assoc array to hold the flattened values
61 * @return object CRM_Contribute_BAO_ManagePremium object
65 static function retrieve(&$params, &$defaults) {
66 $premium = new CRM_Contribute_DAO_Product();
67 $premium->copyValues($params);
68 if ($premium->find(TRUE)) {
69 CRM_Core_DAO
::storeValues($premium, $defaults);
76 * update the is_active flag in the db
78 * @param int $id id of the database record
79 * @param boolean $is_active value we want to set the is_active field
81 * @return Object DAO object on sucess, null otherwise
84 static function setIsActive($id, $is_active) {
85 return CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active);
89 * Function to delete financial Types
91 * @param int $contributionTypeId
94 static function del($premiumID) {
97 //delete from financial Type table
98 $premium = new CRM_Contribute_DAO_Premium();
99 $premium->id
= $premiumID;
104 * Function to build Premium Block im Contribution Pages
109 static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
110 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
112 $dao = new CRM_Contribute_DAO_Premium();
113 $dao->entity_table
= 'civicrm_contribution_page';
114 $dao->entity_id
= $pageID;
115 $dao->premiums_active
= 1;
117 if ($dao->find(TRUE)) {
118 $premiumID = $dao->id
;
119 $premiumBlock = array();
120 CRM_Core_DAO
::storeValues($dao, $premiumBlock);
122 $dao = new CRM_Contribute_DAO_PremiumsProduct();
123 $dao->premiums_id
= $premiumID;
124 $dao->orderBy('weight');
129 while ($dao->fetch()) {
130 $productDAO = new CRM_Contribute_DAO_Product();
131 $productDAO->id
= $dao->product_id
;
132 $productDAO->is_active
= 1;
133 if ($productDAO->find(TRUE)) {
134 if ($selectedProductID != NULL) {
135 if ($selectedProductID == $productDAO->id
) {
136 if ($selectedOption) {
137 $productDAO->options
= ts('Selected Option') . ': ' . $selectedOption;
140 $productDAO->options
= NULL;
142 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
146 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
149 $options = $temp = array();
150 $temp = explode(',', $productDAO->options
);
151 foreach ($temp as $value) {
152 $options[trim($value)] = trim($value);
154 if ($temp[0] != '') {
155 $form->addElement('select', 'options_' . $productDAO->id
, NULL, $options);
158 if (count($products)) {
159 $form->assign('showPremium', $formItems);
160 $form->assign('showSelectOptions', $formItems);
161 $form->assign('products', $products);
162 $form->assign('premiumBlock', $premiumBlock);
168 * Function to build Premium B im Contribution Pages
173 function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
174 if ($premiumProductID) {
175 $dao = new CRM_Contribute_DAO_PremiumsProduct();
176 $dao->id
= $premiumProductID;
178 $productID = $dao->product_id
;
180 $productDAO = new CRM_Contribute_DAO_Product();
181 $productDAO->id
= $productID;
182 $productDAO->is_active
= 1;
183 if ($productDAO->find(TRUE)) {
184 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
187 $radio[$productDAO->id
] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id
, NULL);
188 $options = $temp = array();
189 $temp = explode(',', $productDAO->options
);
190 foreach ($temp as $value) {
191 $options[$value] = $value;
193 if ($temp[0] != '') {
194 $form->add('select', 'options_' . $productDAO->id
, NULL, $options);
197 $form->addGroup($radio, 'selectProduct', NULL);
199 $form->assign('showRadio', TRUE);
200 $form->assign('showSelectOptions', TRUE);
201 $form->assign('products', $products);
202 $form->assign('preview', TRUE);
206 * Function to delete premium associated w/ contribution page.
208 * @param int $contribution page id
211 static function deletePremium($contributionPageID) {
212 if (!$contributionPageID) {
216 //need to delete entries from civicrm_premiums
217 //as well as from civicrm_premiums_product, CRM-4586
220 'entity_id' => $contributionPageID,
221 'entity_table' => 'civicrm_contribution_page',
224 $premium = new CRM_Contribute_DAO_Premium();
225 $premium->copyValues($params);
227 while ($premium->fetch()) {
228 //lets delete from civicrm_premiums_product
229 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
230 $premiumsProduct->premiums_id
= $premium->id
;
231 $premiumsProduct->delete();
239 * Funtion to retrieve premium product and their options
241 * @return array product and option arrays
245 static function getPremiumProductInfo() {
246 if (!self
::$productInfo) {
247 $products = $options = array();
249 $dao = new CRM_Contribute_DAO_Product();
253 while ($dao->fetch()) {
254 $products[$dao->id
] = $dao->name
. " ( " . $dao->sku
. " )";
255 $opts = explode(',', $dao->options
);
256 foreach ($opts as $k => $v) {
260 $options[$dao->id
] = $opts;
264 self
::$productInfo = array($products, $options);
266 return self
::$productInfo;