Merge pull request #2033 from JoeMurray/master
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessorType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * Takes a bunch of params that are needed to match certain criteria and
51 * retrieves the relevant objects. Typically the valid params are only
52 * contact_id. We'll tweak this function to be more full featured over a period
53 * of time. This is the inverse function of create. It also stores all the retrieved
54 * values in the default array
55 *
56 * @param array $params (reference ) an assoc array of name/value pairs
57 * @param array $defaults (reference ) an assoc array to hold the flattened values
58 *
59 * @return object CRM_Core_BAO_LocaationType object on success, null otherwise
60 * @access public
61 * @static
62 */
63 static function retrieve(&$params, &$defaults) {
64 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
65 $paymentProcessorType->copyValues($params);
66 if ($paymentProcessorType->find(TRUE)) {
67 CRM_Core_DAO::storeValues($paymentProcessorType, $defaults);
68 return $paymentProcessorType;
69 }
70 return NULL;
71 }
72
73 /**
74 * update the is_active flag in the db
75 *
76 * @param int $id id of the database record
77 * @param boolean $is_active value we want to set the is_active field
78 *
79 * @return Object DAO object on sucess, null otherwise
80 *
81 * @access public
82 * @static
83 */
84 static function setIsActive($id, $is_active) {
85 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active);
86 }
87
88 /**
89 * retrieve the default payment processor
90 *
91 * @param NULL
92 *
93 * @return object The default payment processor object on success,
94 * null otherwise
95 * @static
96 * @access public
97 */
98 static function &getDefault() {
99 if (self::$_defaultPaymentProcessorType == NULL) {
100 $params = array('is_default' => 1);
101 $defaults = array();
102 self::$_defaultPaymentProcessorType = self::retrieve($params, $defaults);
103 }
104 return self::$_defaultPaymentProcessorType;
105 }
106
107 /**
108 * Function to add the payment-processor type in the db
109 *
110 * @param array $params (reference ) an assoc array of name/value pairs
111 * @param array $ids the array that holds all the db ids
112 *
113 * @return object CRM_Financial_DAO_PaymentProcessorType
114 * @access public
115 * @static
116 *
117 */
118 static function create(&$params) {
119 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
120 $paymentProcessorType->copyValues($params);
121
122 /*
123 // adapted from CRM_Core_Extensions_Payment::install
124 foreach (array(
125 'class_name',
126 'title',
127 'name',
128 'description',
129 'user_name_label',
130 'password_label',
131 'signature_label',
132 'subject_label',
133 'url_site_default',
134 'url_api_default',
135 'url_recur_default',
136 'url_site_test_default',
137 'url_api_test_default',
138 'url_recur_test_default',
139 'url_button_default',
140 'url_button_test_default',
141 'billing_mode',
142 'is_recur',
143 'payment_type'
144 ) as $trimmable) {
145 if (isset($paymentProcessorType->{$trimmable})) {
146 $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable});
147 }
148 }
149 */
150
151 if (isset($paymentProcessorType->billing_mode)) {
152 // ugh unidirectional manipulation
153 if (!is_numeric($paymentProcessorType->billing_mode)) {
154 $billingModes = array_flip(self::buildOptions('billing_mode'));
155 if (array_key_exists($paymentProcessorType->billing_mode, $billingModes)) {
156 $paymentProcessorType->billing_mode = $billingModes[$paymentProcessorType->billing_mode];
157 }
158 }
159 if (!array_key_exists($paymentProcessorType->billing_mode, self::buildOptions('billing_mode'))) {
160 throw new Exception("Unrecognized billing_mode");
161 }
162 }
163
164 // FIXME handle is_default
165 if (!empty($paymentProcessorType->id)) {
166 $ppByName = self::getAllPaymentProcessorTypes('name');
167 if (array_key_exists($paymentProcessorType->name, $ppByName)) {
168 if ($ppByName[$paymentProcessorType->name] != $paymentProcessorType->id) {
169 CRM_Core_Error::fatal('This payment processor type already exists.');
170 }
171 }
172 }
173
174 return $paymentProcessorType->save();
175 }
176
177 /**
178 * Function to delete payment processor
179 *
180 * @param int $paymentProcessorTypeId ID of the processor to be deleted.
181 *
182 * @access public
183 * @static
184 */
185 static function del($paymentProcessorTypeId) {
186 $query = "
187 SELECT pp.id processor_id
188 FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt
189 WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1";
190
191 $params = array(1 => array($paymentProcessorTypeId, 'Integer'));
192 $dao = CRM_Core_DAO::executeQuery($query, $params);
193
194 if ($dao->fetch()) {
195 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');
196 return;
197 }
198
199 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
200 $paymentProcessorType->id = $paymentProcessorTypeId;
201 if ($paymentProcessorType->delete()) {
202 CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.<br>'), '', 'success');
203 return TRUE;
204 }
205 }
206
207 static private function getAllPaymentProcessorTypes($attr) {
208 $ppt = array();
209 $dao = new CRM_Financial_DAO_PaymentProcessorType();
210 $dao->find();
211 while ($dao->fetch()) {
212 $ppt[$dao->$attr] = $dao->id;
213 }
214 return $ppt;
215 }
216
217 /**
218 * Get options for a given field.
219 * @see CRM_Core_DAO::buildOptions
220 *
221 * @param String $fieldName
222 * @param String $context: @see CRM_Core_DAO::buildOptionsContext
223 * @param Array $props: whatever is known about this dao object
224 */
225 public static function buildOptions($fieldName, $context = NULL, $props = array()) {
226 $params = array();
227 // Special logic for fields whose options depend on context or properties
228 switch ($fieldName) {
229 // These options are not in the db
230 case 'billing_mode':
231 return array(
232 CRM_Core_Payment::BILLING_MODE_FORM => 'form',
233 CRM_Core_Payment::BILLING_MODE_BUTTON => 'button',
234 CRM_Core_Payment::BILLING_MODE_NOTIFY => 'notify',
235 );
236 }
237 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
238 }
239 }
240