3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 class CRM_Contribute_BAO_Premium
extends CRM_Contribute_DAO_Premium
{
36 * Product information.
39 private static $productInfo;
44 public function __construct() {
45 parent
::__construct();
49 * Fetch object based on array of properties.
51 * @param array $params
52 * (reference ) an assoc array of name/value pairs.
53 * @param array $defaults
54 * (reference ) an assoc array to hold the flattened values.
56 * @return CRM_Contribute_BAO_ManagePremium
58 public static function retrieve(&$params, &$defaults) {
59 $premium = new CRM_Contribute_DAO_Product();
60 $premium->copyValues($params);
61 if ($premium->find(TRUE)) {
62 CRM_Core_DAO
::storeValues($premium, $defaults);
69 * Update the is_active flag in the db.
72 * Id of the database record.
73 * @param bool $is_active
74 * Value we want to set the is_active field.
77 * DAO object on success, 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
89 public static function del($premiumID) {
92 //delete from financial Type table
93 $premium = new CRM_Contribute_DAO_Premium();
94 $premium->id
= $premiumID;
99 * Build Premium Block im Contribution Pages.
101 * @param CRM_Core_Form $form
103 * @param bool $formItems
104 * @param int $selectedProductID
105 * @param null $selectedOption
108 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
109 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
111 $dao = new CRM_Contribute_DAO_Premium();
112 $dao->entity_table
= 'civicrm_contribution_page';
113 $dao->entity_id
= $pageID;
114 $dao->premiums_active
= 1;
115 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
116 $addWhere = "financial_type_id IN (0)";
117 if (!empty($financialTypes)) {
118 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
121 if ($dao->find(TRUE)) {
122 $premiumID = $dao->id
;
123 $premiumBlock = array();
124 CRM_Core_DAO
::storeValues($dao, $premiumBlock);
126 $dao = new CRM_Contribute_DAO_PremiumsProduct();
127 $dao->premiums_id
= $premiumID;
128 $dao->whereAdd($addWhere);
129 $dao->orderBy('weight');
134 while ($dao->fetch()) {
135 $productDAO = new CRM_Contribute_DAO_Product();
136 $productDAO->id
= $dao->product_id
;
137 $productDAO->is_active
= 1;
138 if ($productDAO->find(TRUE)) {
139 if ($selectedProductID != NULL) {
140 if ($selectedProductID == $productDAO->id
) {
141 if ($selectedOption) {
142 $productDAO->options
= ts('Selected Option') . ': ' . $selectedOption;
145 $productDAO->options
= NULL;
147 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
151 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
154 $options = $temp = array();
155 $temp = explode(',', $productDAO->options
);
156 foreach ($temp as $value) {
157 $options[trim($value)] = trim($value);
159 if ($temp[0] != '') {
160 $form->addElement('select', 'options_' . $productDAO->id
, NULL, $options);
163 if (count($products)) {
164 $form->assign('showPremium', $formItems);
165 $form->assign('showSelectOptions', $formItems);
166 $form->assign('products', $products);
167 $form->assign('premiumBlock', $premiumBlock);
173 * Build Premium B im Contribution Pages.
175 * @param CRM_Core_Form $form
176 * @param int $productID
177 * @param int $premiumProductID
180 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
181 if ($premiumProductID) {
182 $dao = new CRM_Contribute_DAO_PremiumsProduct();
183 $dao->id
= $premiumProductID;
185 $productID = $dao->product_id
;
187 $productDAO = new CRM_Contribute_DAO_Product();
188 $productDAO->id
= $productID;
189 $productDAO->is_active
= 1;
190 if ($productDAO->find(TRUE)) {
191 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
194 $radio[$productDAO->id
] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id
, NULL);
195 $options = $temp = array();
196 $temp = explode(',', $productDAO->options
);
197 foreach ($temp as $value) {
198 $options[$value] = $value;
200 if ($temp[0] != '') {
201 $form->add('select', 'options_' . $productDAO->id
, NULL, $options);
204 $form->addGroup($radio, 'selectProduct', NULL);
206 $form->assign('showRadio', TRUE);
207 $form->assign('showSelectOptions', TRUE);
208 $form->assign('products', $products);
209 $form->assign('preview', TRUE);
213 * Delete premium associated w/ contribution page.
215 * @param int $contributionPageID
218 public static function deletePremium($contributionPageID) {
219 if (!$contributionPageID) {
223 //need to delete entries from civicrm_premiums
224 //as well as from civicrm_premiums_product, CRM-4586
227 'entity_id' => $contributionPageID,
228 'entity_table' => 'civicrm_contribution_page',
231 $premium = new CRM_Contribute_DAO_Premium();
232 $premium->copyValues($params);
234 while ($premium->fetch()) {
235 //lets delete from civicrm_premiums_product
236 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
237 $premiumsProduct->premiums_id
= $premium->id
;
238 $premiumsProduct->delete();
246 * Retrieve premium product and their options.
249 * product and option arrays
251 public 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;