| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium { |
| 36 | |
| 37 | /** |
| 38 | * product information |
| 39 | * @var array |
| 40 | * @static |
| 41 | */ |
| 42 | private static $productInfo; |
| 43 | |
| 44 | /** |
| 45 | * class constructor |
| 46 | */ |
| 47 | function __construct() { |
| 48 | parent::__construct(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 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 |
| 57 | * |
| 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 |
| 60 | * |
| 61 | * @return object CRM_Contribute_BAO_ManagePremium object |
| 62 | * @access public |
| 63 | * @static |
| 64 | */ |
| 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); |
| 70 | return $premium; |
| 71 | } |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * update the is_active flag in the db |
| 77 | * |
| 78 | * @param int $id id of the database record |
| 79 | * @param boolean $is_active value we want to set the is_active field |
| 80 | * |
| 81 | * @return Object DAO object on sucess, null otherwise |
| 82 | * @static |
| 83 | */ |
| 84 | static function setIsActive($id, $is_active) { |
| 85 | return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Premium', $id, 'premiums_active ', $is_active); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Function to delete financial Types |
| 90 | * |
| 91 | * @param int $contributionTypeId |
| 92 | * @static |
| 93 | */ |
| 94 | static function del($premiumID) { |
| 95 | //check dependencies |
| 96 | |
| 97 | //delete from financial Type table |
| 98 | $premium = new CRM_Contribute_DAO_Premium(); |
| 99 | $premium->id = $premiumID; |
| 100 | $premium->delete(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Function to build Premium Block im Contribution Pages |
| 105 | * |
| 106 | * @param int $pageId |
| 107 | * @static |
| 108 | */ |
| 109 | static function buildPremiumBlock(&$form, $pageID, $formItems = FALSE, $selectedProductID = NULL, $selectedOption = NULL) { |
| 110 | $form->add('hidden', "selectProduct", $selectedProductID, array('id' => 'selectProduct')); |
| 111 | |
| 112 | $dao = new CRM_Contribute_DAO_Premium(); |
| 113 | $dao->entity_table = 'civicrm_contribution_page'; |
| 114 | $dao->entity_id = $pageID; |
| 115 | $dao->premiums_active = 1; |
| 116 | |
| 117 | if ($dao->find(TRUE)) { |
| 118 | $premiumID = $dao->id; |
| 119 | $premiumBlock = array(); |
| 120 | CRM_Core_DAO::storeValues($dao, $premiumBlock); |
| 121 | |
| 122 | $dao = new CRM_Contribute_DAO_PremiumsProduct(); |
| 123 | $dao->premiums_id = $premiumID; |
| 124 | $dao->orderBy('weight'); |
| 125 | $dao->find(); |
| 126 | |
| 127 | $products = array(); |
| 128 | $radio = array(); |
| 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; |
| 138 | } |
| 139 | else { |
| 140 | $productDAO->options = NULL; |
| 141 | } |
| 142 | CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); |
| 143 | } |
| 144 | } |
| 145 | else { |
| 146 | CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); |
| 147 | } |
| 148 | } |
| 149 | $options = $temp = array(); |
| 150 | $temp = explode(',', $productDAO->options); |
| 151 | foreach ($temp as $value) { |
| 152 | $options[trim($value)] = trim($value); |
| 153 | } |
| 154 | if ($temp[0] != '') { |
| 155 | $form->addElement('select', 'options_' . $productDAO->id, NULL, $options); |
| 156 | } |
| 157 | } |
| 158 | if (count($products)) { |
| 159 | $form->assign('showPremium', $formItems); |
| 160 | $form->assign('showSelectOptions', $formItems); |
| 161 | $form->assign('products', $products); |
| 162 | $form->assign('premiumBlock', $premiumBlock); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Function to build Premium B im Contribution Pages |
| 169 | * |
| 170 | * @param int $pageId |
| 171 | * @static |
| 172 | */ |
| 173 | function buildPremiumPreviewBlock($form, $productID, $premiumProductID = NULL) { |
| 174 | if ($premiumProductID) { |
| 175 | $dao = new CRM_Contribute_DAO_PremiumsProduct(); |
| 176 | $dao->id = $premiumProductID; |
| 177 | $dao->find(TRUE); |
| 178 | $productID = $dao->product_id; |
| 179 | } |
| 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]); |
| 185 | } |
| 186 | |
| 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; |
| 192 | } |
| 193 | if ($temp[0] != '') { |
| 194 | $form->add('select', 'options_' . $productDAO->id, NULL, $options); |
| 195 | } |
| 196 | |
| 197 | $form->addGroup($radio, 'selectProduct', NULL); |
| 198 | |
| 199 | $form->assign('showRadio', TRUE); |
| 200 | $form->assign('showSelectOptions', TRUE); |
| 201 | $form->assign('products', $products); |
| 202 | $form->assign('preview', TRUE); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Function to delete premium associated w/ contribution page. |
| 207 | * |
| 208 | * @param int $contribution page id |
| 209 | * @static |
| 210 | */ |
| 211 | static function deletePremium($contributionPageID) { |
| 212 | if (!$contributionPageID) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | //need to delete entries from civicrm_premiums |
| 217 | //as well as from civicrm_premiums_product, CRM-4586 |
| 218 | |
| 219 | $params = array( |
| 220 | 'entity_id' => $contributionPageID, |
| 221 | 'entity_table' => 'civicrm_contribution_page', |
| 222 | ); |
| 223 | |
| 224 | $premium = new CRM_Contribute_DAO_Premium(); |
| 225 | $premium->copyValues($params); |
| 226 | $premium->find(); |
| 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(); |
| 232 | |
| 233 | //now delete premium |
| 234 | $premium->delete(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Funtion to retrieve premium product and their options |
| 240 | * |
| 241 | * @return array product and option arrays |
| 242 | * @static |
| 243 | * @access public |
| 244 | */ |
| 245 | static function getPremiumProductInfo() { |
| 246 | if (!self::$productInfo) { |
| 247 | $products = $options = array(); |
| 248 | |
| 249 | $dao = new CRM_Contribute_DAO_Product(); |
| 250 | $dao->is_active = 1; |
| 251 | $dao->find(); |
| 252 | |
| 253 | while ($dao->fetch()) { |
| 254 | $products[$dao->id] = $dao->name . " ( " . $dao->sku . " )"; |
| 255 | $opts = explode(',', $dao->options); |
| 256 | foreach ($opts as $k => $v) { |
| 257 | $ops[$k] = trim($v); |
| 258 | } |
| 259 | if ($ops[0] != '') { |
| 260 | $options[$dao->id] = $opts; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | self::$productInfo = array($products, $options); |
| 265 | } |
| 266 | return self::$productInfo; |
| 267 | } |
| 268 | } |
| 269 | |