Merge pull request #5106 from cividesk/CRM-14974-4.6
[civicrm-core.git] / CRM / Contribute / BAO / ManagePremiums.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contribute_BAO_ManagePremiums extends CRM_Contribute_DAO_Product {
36
37 /**
fe482240 38 * Static holder for the default LT.
6a488035
TO
39 */
40 static $_defaultContributionType = NULL;
41
42 /**
fe482240 43 * Class constructor.
6a488035 44 */
00be9182 45 public function __construct() {
6a488035
TO
46 parent::__construct();
47 }
48
49 /**
fe482240 50 * Fetch object based on array of properties.
6a488035 51 *
014c4014
TO
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.
6a488035 56 *
16b10e64 57 * @return CRM_Contribute_BAO_ManagePremium
6a488035 58 */
00be9182 59 public static function retrieve(&$params, &$defaults) {
6a488035
TO
60 $premium = new CRM_Contribute_DAO_Product();
61 $premium->copyValues($params);
62 if ($premium->find(TRUE)) {
63 $premium->product_name = $premium->name;
64 CRM_Core_DAO::storeValues($premium, $defaults);
65 return $premium;
66 }
67 return NULL;
68 }
69
70 /**
fe482240 71 * Update the is_active flag in the db.
6a488035 72 *
014c4014
TO
73 * @param int $id
74 * Id of the database record.
75 * @param bool $is_active
76 * Value we want to set the is_active field.
6a488035 77 *
a6c01b45
CW
78 * @return Object
79 * DAO object on sucess, null otherwise
6a488035 80 */
00be9182 81 public static function setIsActive($id, $is_active) {
6a488035
TO
82 if (!$is_active) {
83 $dao = new CRM_Contribute_DAO_PremiumsProduct();
84 $dao->product_id = $id;
85 $dao->delete();
86 }
87 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Product', $id, 'is_active', $is_active);
88 }
89
90 /**
fe482240 91 * add the financial types.
6a488035 92 *
014c4014
TO
93 * @param array $params
94 * Reference array contains the values submitted by the form.
95 * @param array $ids
96 * Reference array contains the id.
6a488035 97 *
6a488035
TO
98 *
99 * @return object
100 */
00be9182 101 public static function add(&$params, &$ids) {
ffc4d2d0
FG
102 // CRM-14283 - strip protocol and domain from image URLs
103 $image_type = array('image', 'thumbnail');
104 foreach ($image_type as $key) {
105 if (isset($params[$key])) {
106 $parsedURL = explode('/', $params[$key]);
107 $pathComponents = array_slice($parsedURL, 3);
108 $params[$key] = '/' . implode('/', $pathComponents);
109 }
110 }
6a488035
TO
111
112 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
113 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
114
115 // action is taken depending upon the mode
116 $premium = new CRM_Contribute_DAO_Product();
117 $premium->copyValues($params);
118
119 $premium->id = CRM_Utils_Array::value('premium', $ids);
120
121 // set currency for CRM-1496
122 if (!isset($premium->currency)) {
123 $config = CRM_Core_Config::singleton();
124 $premium->currency = $config->defaultCurrency;
125 }
126
127 $premium->save();
128 return $premium;
129 }
130
131 /**
fe482240 132 * Delete premium Types.
6a488035
TO
133 *
134 * @param int $productID
6a488035 135 */
00be9182 136 public static function del($productID) {
6a488035
TO
137 //check dependencies
138 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
139 $premiumsProduct->product_id = $productID;
140 if ($premiumsProduct->find(TRUE)) {
141 $session = CRM_Core_Session::singleton();
142 $message .= ts('This Premium is being linked to <a href=\'%1\'>Online Contribution page</a>. Please remove it in order to delete this Premium.', array(1 => CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1')), ts('Deletion Error'), 'error');
143 CRM_Core_Session::setStatus($message);
144 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/contribute/managePremiums', 'reset=1&action=browse'));
145 }
146
874c9be7 147 //delete from financial Type table
6a488035
TO
148 $premium = new CRM_Contribute_DAO_Product();
149 $premium->id = $productID;
150 $premium->delete();
151 }
96025800 152
6a488035 153}