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 * 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
93 * @internal param int $contributionTypeId
96 static function del($premiumID) {
99 //delete from financial Type table
100 $premium = new CRM_Contribute_DAO_Premium();
101 $premium->id
= $premiumID;
106 * Function to build Premium Block im Contribution Pages
108 * @param $form CRM_Core_Form
110 * @param bool $formItems
111 * @param null $selectedProductID
112 * @param null $selectedOption
114 * @internal param int $pageId
117 static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
118 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
120 $dao = new CRM_Contribute_DAO_Premium();
121 $dao->entity_table
= 'civicrm_contribution_page';
122 $dao->entity_id
= $pageID;
123 $dao->premiums_active
= 1;
125 if ($dao->find(TRUE)) {
126 $premiumID = $dao->id
;
127 $premiumBlock = array();
128 CRM_Core_DAO
::storeValues($dao, $premiumBlock);
130 $dao = new CRM_Contribute_DAO_PremiumsProduct();
131 $dao->premiums_id
= $premiumID;
132 $dao->orderBy('weight');
137 while ($dao->fetch()) {
138 $productDAO = new CRM_Contribute_DAO_Product();
139 $productDAO->id
= $dao->product_id
;
140 $productDAO->is_active
= 1;
141 if ($productDAO->find(TRUE)) {
142 if ($selectedProductID != NULL) {
143 if ($selectedProductID == $productDAO->id
) {
144 if ($selectedOption) {
145 $productDAO->options
= ts('Selected Option') . ': ' . $selectedOption;
148 $productDAO->options
= NULL;
150 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
154 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
157 $options = $temp = array();
158 $temp = explode(',', $productDAO->options
);
159 foreach ($temp as $value) {
160 $options[trim($value)] = trim($value);
162 if ($temp[0] != '') {
163 $form->addElement('select', 'options_' . $productDAO->id
, NULL, $options);
166 if (count($products)) {
167 $form->assign('showPremium', $formItems);
168 $form->assign('showSelectOptions', $formItems);
169 $form->assign('products', $products);
170 $form->assign('premiumBlock', $premiumBlock);
176 * Function to build Premium B im Contribution Pages
178 * @param $form CRM_Core_Form
180 * @param null $premiumProductID
182 * @internal param int $pageId
185 function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
186 if ($premiumProductID) {
187 $dao = new CRM_Contribute_DAO_PremiumsProduct();
188 $dao->id
= $premiumProductID;
190 $productID = $dao->product_id
;
192 $productDAO = new CRM_Contribute_DAO_Product();
193 $productDAO->id
= $productID;
194 $productDAO->is_active
= 1;
195 if ($productDAO->find(TRUE)) {
196 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
199 $radio[$productDAO->id
] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id
, NULL);
200 $options = $temp = array();
201 $temp = explode(',', $productDAO->options
);
202 foreach ($temp as $value) {
203 $options[$value] = $value;
205 if ($temp[0] != '') {
206 $form->add('select', 'options_' . $productDAO->id
, NULL, $options);
209 $form->addGroup($radio, 'selectProduct', NULL);
211 $form->assign('showRadio', TRUE);
212 $form->assign('showSelectOptions', TRUE);
213 $form->assign('products', $products);
214 $form->assign('preview', TRUE);
218 * Function to delete premium associated w/ contribution page.
220 * @param $contributionPageID
222 * @internal param int $contribution page id
225 static function deletePremium($contributionPageID) {
226 if (!$contributionPageID) {
230 //need to delete entries from civicrm_premiums
231 //as well as from civicrm_premiums_product, CRM-4586
234 'entity_id' => $contributionPageID,
235 'entity_table' => 'civicrm_contribution_page',
238 $premium = new CRM_Contribute_DAO_Premium();
239 $premium->copyValues($params);
241 while ($premium->fetch()) {
242 //lets delete from civicrm_premiums_product
243 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
244 $premiumsProduct->premiums_id
= $premium->id
;
245 $premiumsProduct->delete();
253 * Function to retrieve premium product and their options
255 * @return array product and option arrays
259 static function getPremiumProductInfo() {
260 if (!self
::$productInfo) {
261 $products = $options = array();
263 $dao = new CRM_Contribute_DAO_Product();
267 while ($dao->fetch()) {
268 $products[$dao->id
] = $dao->name
. " ( " . $dao->sku
. " )";
269 $opts = explode(',', $dao->options
);
270 foreach ($opts as $k => $v) {
274 $options[$dao->id
] = $opts;
278 self
::$productInfo = array($products, $options);
280 return self
::$productInfo;