Merge pull request #4773 from civicrm/version-fix
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessorType.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_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentProcessorType {
36
37 /**
38 * Static holder for the default payment processor
39 */
40 static $_defaultPaymentProcessorType = NULL;
41
42 /**
43 * Class constructor
44 */
45 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_Core_BAO_LocaationType object on success, null otherwise
56 * @access public
57 * @static
58 */
59 static function retrieve(&$params, &$defaults) {
60 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
61 $paymentProcessorType->copyValues($params);
62 if ($paymentProcessorType->find(TRUE)) {
63 CRM_Core_DAO::storeValues($paymentProcessorType, $defaults);
64 return $paymentProcessorType;
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 *
77 * @access public
78 * @static
79 */
80 static function setIsActive($id, $is_active) {
81 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active);
82 }
83
84 /**
85 * Retrieve the default payment processor
86 *
87 * @param NULL
88 *
89 * @return object The default payment processor object on success,
90 * null otherwise
91 * @static
92 * @access public
93 */
94 static function &getDefault() {
95 if (self::$_defaultPaymentProcessorType == NULL) {
96 $params = array('is_default' => 1);
97 $defaults = array();
98 self::$_defaultPaymentProcessorType = self::retrieve($params, $defaults);
99 }
100 return self::$_defaultPaymentProcessorType;
101 }
102
103 /**
104 * Add the payment-processor type in the db
105 *
106 * @param array $params (reference ) an assoc array of name/value pairs
107 *
108 * @throws Exception
109 * @return CRM_Financial_DAO_PaymentProcessorType
110 * @access public
111 * @static
112 */
113 static function create(&$params) {
114 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
115 $paymentProcessorType->copyValues($params);
116
117 /*
118 // adapted from CRM_Core_Extensions_Payment::install
119 foreach (array(
120 'class_name',
121 'title',
122 'name',
123 'description',
124 'user_name_label',
125 'password_label',
126 'signature_label',
127 'subject_label',
128 'url_site_default',
129 'url_api_default',
130 'url_recur_default',
131 'url_site_test_default',
132 'url_api_test_default',
133 'url_recur_test_default',
134 'url_button_default',
135 'url_button_test_default',
136 'billing_mode',
137 'is_recur',
138 'payment_type'
139 ) as $trimmable) {
140 if (isset($paymentProcessorType->{$trimmable})) {
141 $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable});
142 }
143 }
144 */
145
146 if (isset($paymentProcessorType->billing_mode)) {
147 // ugh unidirectional manipulation
148 if (!is_numeric($paymentProcessorType->billing_mode)) {
149 $billingModes = array_flip(self::buildOptions('billing_mode'));
150 if (array_key_exists($paymentProcessorType->billing_mode, $billingModes)) {
151 $paymentProcessorType->billing_mode = $billingModes[$paymentProcessorType->billing_mode];
152 }
153 }
154 if (!array_key_exists($paymentProcessorType->billing_mode, self::buildOptions('billing_mode'))) {
155 throw new Exception("Unrecognized billing_mode");
156 }
157 }
158
159 // FIXME handle is_default
160 if (!empty($paymentProcessorType->id)) {
161 $ppByName = self::getAllPaymentProcessorTypes('name');
162 if (array_key_exists($paymentProcessorType->name, $ppByName)) {
163 if ($ppByName[$paymentProcessorType->name] != $paymentProcessorType->id) {
164 CRM_Core_Error::fatal('This payment processor type already exists.');
165 }
166 }
167 }
168
169 return $paymentProcessorType->save();
170 }
171
172 /**
173 * Delete payment processor
174 *
175 * @param int $paymentProcessorTypeId ID of the processor to be deleted.
176 *
177 * @return bool
178 * @access public
179 * @static
180 */
181 static function del($paymentProcessorTypeId) {
182 $query = "
183 SELECT pp.id processor_id
184 FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt
185 WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1";
186
187 $params = array(1 => array($paymentProcessorTypeId, 'Integer'));
188 $dao = CRM_Core_DAO::executeQuery($query, $params);
189
190 if ($dao->fetch()) {
191 CRM_Core_Session::setStatus(ts('There is a Payment Processor associated with selected Payment Processor type, hence it can not be deleted.'), ts('Deletion Error'), 'error');
192 return;
193 }
194
195 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
196 $paymentProcessorType->id = $paymentProcessorTypeId;
197 if ($paymentProcessorType->delete()) {
198 CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.<br>'), '', 'success');
199 return TRUE;
200 }
201 }
202
203 /**
204 * @param $attr
205 *
206 * @return array
207 */
208 static private function getAllPaymentProcessorTypes($attr) {
209 $ppt = array();
210 $dao = new CRM_Financial_DAO_PaymentProcessorType();
211 $dao->find();
212 while ($dao->fetch()) {
213 $ppt[$dao->$attr] = $dao->id;
214 }
215 return $ppt;
216 }
217
218 }
219