Merge branch 4.5 into master
[civicrm-core.git] / CRM / Contribute / BAO / ManagePremiums.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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_ManagePremiums extends CRM_Contribute_DAO_Product {
36
37 /**
38 * Static holder for the default LT
39 */
40 static $_defaultContributionType = NULL;
41
42 /**
43 * Class constructor
44 */
45 public function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Fetch object based on array of properties
51 *
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.
56 *
57 * @return CRM_Contribute_BAO_ManagePremium object
58 * @static
59 */
60 public static function retrieve(&$params, &$defaults) {
61 $premium = new CRM_Contribute_DAO_Product();
62 $premium->copyValues($params);
63 if ($premium->find(TRUE)) {
64 $premium->product_name = $premium->name;
65 CRM_Core_DAO::storeValues($premium, $defaults);
66 return $premium;
67 }
68 return NULL;
69 }
70
71 /**
72 * Update the is_active flag in the db
73 *
74 * @param int $id
75 * Id of the database record.
76 * @param bool $is_active
77 * Value we want to set the is_active field.
78 *
79 * @return Object
80 * DAO object on sucess, null otherwise
81 * @static
82 */
83 public static function setIsActive($id, $is_active) {
84 if (!$is_active) {
85 $dao = new CRM_Contribute_DAO_PremiumsProduct();
86 $dao->product_id = $id;
87 $dao->delete();
88 }
89 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Product', $id, 'is_active', $is_active);
90 }
91
92 /**
93 * add the financial types
94 *
95 * @param array $params
96 * Reference array contains the values submitted by the form.
97 * @param array $ids
98 * Reference array contains the id.
99 *
100 * @static
101 *
102 * @return object
103 */
104 public static function add(&$params, &$ids) {
105 // CRM-14283 - strip protocol and domain from image URLs
106 $image_type = array('image', 'thumbnail');
107 foreach ($image_type as $key) {
108 if (isset($params[$key])) {
109 $parsedURL = explode('/', $params[$key]);
110 $pathComponents = array_slice($parsedURL, 3);
111 $params[$key] = '/' . implode('/', $pathComponents);
112 }
113 }
114
115 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
116 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
117
118 // action is taken depending upon the mode
119 $premium = new CRM_Contribute_DAO_Product();
120 $premium->copyValues($params);
121
122 $premium->id = CRM_Utils_Array::value('premium', $ids);
123
124 // set currency for CRM-1496
125 if (!isset($premium->currency)) {
126 $config = CRM_Core_Config::singleton();
127 $premium->currency = $config->defaultCurrency;
128 }
129
130 $premium->save();
131 return $premium;
132 }
133
134 /**
135 * Delete premium Types
136 *
137 * @param int $productID
138 * @static
139 */
140
141 public static function del($productID) {
142 //check dependencies
143 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
144 $premiumsProduct->product_id = $productID;
145 if ($premiumsProduct->find(TRUE)) {
146 $session = CRM_Core_Session::singleton();
147 $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');
148 CRM_Core_Session::setStatus($message);
149 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/contribute/managePremiums', 'reset=1&action=browse'));
150 }
151
152 //delete from financial Type table
153 $premium = new CRM_Contribute_DAO_Product();
154 $premium->id = $productID;
155 $premium->delete();
156 }
157 }