3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 class CRM_Contribute_BAO_Premium
extends CRM_Contribute_DAO_Premium
{
36 * Product information.
40 private static $productInfo;
45 public function __construct() {
46 parent
::__construct();
50 * Fetch object based on array of properties.
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
57 * @return CRM_Contribute_DAO_Product
59 public static function retrieve(&$params, &$defaults) {
60 $premium = new CRM_Contribute_DAO_Product();
61 $premium->copyValues($params);
62 if ($premium->find(TRUE)) {
63 CRM_Core_DAO
::storeValues($premium, $defaults);
70 * Update the is_active flag in the db.
73 * Id of the database record.
74 * @param bool $is_active
75 * Value we want to set the is_active field.
78 * DAO object on success, null otherwise
80 public 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
89 public static function del($premiumID) {
90 $premium = new CRM_Contribute_DAO_Premium();
91 $premium->id
= $premiumID;
96 * Build Premium Block im Contribution Pages.
98 * @param CRM_Core_Form $form
100 * @param bool $formItems
101 * @param int $selectedProductID
102 * @param string $selectedOption
104 public static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) {
105 $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct'));
107 $dao = new CRM_Contribute_DAO_Premium();
108 $dao->entity_table
= 'civicrm_contribution_page';
109 $dao->entity_id
= $pageID;
110 $dao->premiums_active
= 1;
111 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
112 $addWhere = "financial_type_id IN (0)";
113 if (!empty($financialTypes)) {
114 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
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->whereAdd($addWhere);
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
175 public function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) {
176 if ($premiumProductID) {
177 $dao = new CRM_Contribute_DAO_PremiumsProduct();
178 $dao->id
= $premiumProductID;
180 $productID = $dao->product_id
;
182 $productDAO = new CRM_Contribute_DAO_Product();
183 $productDAO->id
= $productID;
184 $productDAO->is_active
= 1;
185 if ($productDAO->find(TRUE)) {
186 CRM_Core_DAO
::storeValues($productDAO, $products[$productDAO->id
]);
189 $radio[$productDAO->id
] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id
, NULL);
190 $options = $temp = array();
191 $temp = explode(',', $productDAO->options
);
192 foreach ($temp as $value) {
193 $options[$value] = $value;
195 if ($temp[0] != '') {
196 $form->add('select', 'options_' . $productDAO->id
, NULL, $options);
199 $form->addGroup($radio, 'selectProduct', NULL);
201 $form->assign('showRadio', TRUE);
202 $form->assign('showSelectOptions', TRUE);
203 $form->assign('products', $products);
204 $form->assign('preview', TRUE);
208 * Delete premium associated w/ contribution page.
210 * @param int $contributionPageID
212 public static function deletePremium($contributionPageID) {
213 if (!$contributionPageID) {
217 //need to delete entries from civicrm_premiums
218 //as well as from civicrm_premiums_product, CRM-4586
221 'entity_id' => $contributionPageID,
222 'entity_table' => 'civicrm_contribution_page',
225 $premium = new CRM_Contribute_DAO_Premium();
226 $premium->copyValues($params);
228 while ($premium->fetch()) {
229 //lets delete from civicrm_premiums_product
230 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
231 $premiumsProduct->premiums_id
= $premium->id
;
232 $premiumsProduct->delete();
240 * Retrieve premium product and their options.
243 * product and option arrays
245 public 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;