Merge pull request #4822 from kurund/indentation-fixes
[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 (reference ) an assoc array of name/value pairs
53 * @param array $defaults (reference ) an assoc array to hold the flattened values
54 *
55 * @return CRM_Contribute_BAO_ManagePremium object
56 * @static
57 */
58 public static function retrieve(&$params, &$defaults) {
59 $premium = new CRM_Contribute_DAO_Product();
60 $premium->copyValues($params);
61 if ($premium->find(TRUE)) {
62 $premium->product_name = $premium->name;
63 CRM_Core_DAO::storeValues($premium, $defaults);
64 return $premium;
65 }
66 return NULL;
67 }
68
69 /**
70 * Update the is_active flag in the db
71 *
72 * @param int $id id of the database record
73 * @param boolean $is_active value we want to set the is_active field
74 *
75 * @return Object DAO object on sucess, null otherwise
76 * @static
77 */
78 public static function setIsActive($id, $is_active) {
79 if (!$is_active) {
80 $dao = new CRM_Contribute_DAO_PremiumsProduct();
81 $dao->product_id = $id;
82 $dao->delete();
83 }
84 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Product', $id, 'is_active', $is_active);
85 }
86
87 /**
88 * add the financial types
89 *
90 * @param array $params reference array contains the values submitted by the form
91 * @param array $ids reference array contains the id
92 *
93 * @static
94 *
95 * @return object
96 */
97 public static function add(&$params, &$ids) {
98 // CRM-14283 - strip protocol and domain from image URLs
99 $image_type = array('image', 'thumbnail');
100 foreach ($image_type as $key) {
101 if (isset($params[$key])) {
102 $parsedURL = explode('/', $params[$key]);
103 $pathComponents = array_slice($parsedURL, 3);
104 $params[$key] = '/' . implode('/', $pathComponents);
105 }
106 }
107
108 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
109 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
110
111 // action is taken depending upon the mode
112 $premium = new CRM_Contribute_DAO_Product();
113 $premium->copyValues($params);
114
115 $premium->id = CRM_Utils_Array::value('premium', $ids);
116
117 // set currency for CRM-1496
118 if (!isset($premium->currency)) {
119 $config = CRM_Core_Config::singleton();
120 $premium->currency = $config->defaultCurrency;
121 }
122
123 $premium->save();
124 return $premium;
125 }
126
127 /**
128 * Delete premium Types
129 *
130 * @param int $productID
131 * @static
132 */
133
134 public static function del($productID) {
135 //check dependencies
136 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
137 $premiumsProduct->product_id = $productID;
138 if ($premiumsProduct->find(TRUE)) {
139 $session = CRM_Core_Session::singleton();
140 $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');
141 CRM_Core_Session::setStatus($message);
142 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/contribute/managePremiums', 'reset=1&action=browse'));
143 }
144
145 //delete from financial Type table
146 $premium = new CRM_Contribute_DAO_Product();
147 $premium->id = $productID;
148 $premium->delete();
149 }
150 }