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