CRM-14106 - Regex targeting the first part of if statements
[civicrm-core.git] / CRM / Admin / Form / PaymentProcessor.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: PaymentProcessor.php 9702 2007-05-29 23:57:16Z lobo $
33 *
34 */
35
36 /**
37 * This class generates form components for Payment Processor
38 *
39 */
40 class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
41 protected $_id = NULL;
42
43 protected $_testID = NULL;
44
45 protected $_fields = NULL;
46
47 protected $_ppDAO;
48
49 function preProcess() {
50 if(!CRM_Core_Permission::check('administer payment processors')) {
51 CRM_Core_Error::fatal('You do not have permission to administer payment processors');
52 }
53 parent::preProcess();
54
55 CRM_Utils_System::setTitle(ts('Settings - Payment Processor'));
56
57 // get the payment processor meta information
58
59 if ($this->_id) {
60 $this->_ppType = CRM_Utils_Request::retrieve('pp', 'String', $this, FALSE, NULL);
61 if (!$this->_ppType) {
62 $this->_ppType = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_PaymentProcessor',
63 $this->_id,
64 'payment_processor_type_id'
65 );
66 }
67 $this->set('pp', $this->_ppType);
68 }
69 else {
70 $this->_ppType = CRM_Utils_Request::retrieve('pp', 'String', $this, TRUE, NULL);
71 }
72
73 $this->assign('ppType', $this->_ppType);
74 $ppTypeName = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_PaymentProcessorType',
75 $this->_ppType,
76 'name'
77 );
78 $this->assign('ppTypeName', $ppTypeName );
79
80 $this->_ppDAO = new CRM_Financial_DAO_PaymentProcessorType( );
81 $this->_ppDAO->id = $this->_ppType;
82
83 if (!$this->_ppDAO->find(TRUE)) {
84 CRM_Core_Error::fatal(ts('Could not find payment processor meta information'));
85 }
86
87 if ($this->_id) {
88 $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor',
89 "reset=1&action=update&id={$this->_id}",
90 FALSE, NULL, FALSE
91 );
92 }
93 else {
94 $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor',
95 "reset=1&action=add",
96 FALSE, NULL, FALSE
97 );
98 }
99
100 //CRM-4129
101 $destination = CRM_Utils_Request::retrieve('civicrmDestination', 'String', $this);
102 if ($destination) {
103 $destination = urlencode($destination);
104 $refreshURL .= "&civicrmDestination=$destination";
105 }
106
107 $this->assign('refreshURL', $refreshURL);
108
109 $this->assign('is_recur', $this->_ppDAO->is_recur);
110
111 $this->_fields = array(
112 array(
113 'name' => 'user_name',
114 'label' => $this->_ppDAO->user_name_label,
115 ),
116 array(
117 'name' => 'password',
118 'label' => $this->_ppDAO->password_label,
119 ),
120 array(
121 'name' => 'signature',
122 'label' => $this->_ppDAO->signature_label,
123 ),
124 array(
125 'name' => 'subject',
126 'label' => $this->_ppDAO->subject_label,
127 ),
128 array(
129 'name' => 'url_site',
130 'label' => ts('Site URL'),
131 'rule' => 'url',
132 'msg' => ts('Enter a valid URL'),
133 ),
134 );
135
136 if ($this->_ppDAO->is_recur) {
137 $this->_fields[] = array(
138 'name' => 'url_recur',
139 'label' => ts('Recurring Payments URL'),
140 'rule' => 'url',
141 'msg' => ts('Enter a valid URL'),
142 );
143 }
144
145 if (!empty($this->_ppDAO->url_button_default)) {
146 $this->_fields[] = array(
147 'name' => 'url_button',
148 'label' => ts('Button URL'),
149 'rule' => 'url',
150 'msg' => ts('Enter a valid URL'),
151 );
152 }
153
154 if (!empty($this->_ppDAO->url_api_default)) {
155 $this->_fields[] = array(
156 'name' => 'url_api',
157 'label' => ts('API URL'),
158 'rule' => 'url',
159 'msg' => ts('Enter a valid URL'),
160 );
161 }
162 }
163
164 /**
165 * Function to build the form
166 *
167 * @return void
168 * @access public
169 */
170 public function buildQuickForm($check = FALSE) {
171 parent::buildQuickForm();
172
173 if ($this->_action & CRM_Core_Action::DELETE) {
174 return;
175 }
176
177 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessor');
178
179 $this->add('text', 'name', ts('Name'),
180 $attributes['name'], TRUE
181 );
182
183 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Financial_DAO_PaymentProcessor', $this->_id));
184
185 $this->add('text', 'description', ts('Description'),
186 $attributes['description']
187 );
188
189 $types = CRM_Core_PseudoConstant::paymentProcessorType();
190 $this->add( 'select', 'payment_processor_type_id', ts('Payment Processor Type'), $types, true,
191 array('onchange' => "reload(true)")
192 );
193
194 // Financial Account of account type asset CRM-11515
195 $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
196 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType));
197 if ($fcount = count($financialAccount)) {
198 $this->assign('financialAccount', $fcount);
199 }
200 $this->add('select', 'financial_account_id', ts('Financial Account'),
201 array('' => ts('- select -')) + $financialAccount,
202 true
203 );
204 // is this processor active ?
205 $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?'));
206 $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?'));
207
208
209 foreach ($this->_fields as $field) {
210 if (empty($field['label'])) {
211 continue;
212 }
213
214 $this->add('text', $field['name'],
215 $field['label'], $attributes[$field['name']]
216 );
217 $this->add('text', "test_{$field['name']}",
218 $field['label'], $attributes[$field['name']]
219 );
220 if (!empty($field['rule'])) {
221 $this->addRule($field['name'], $field['msg'], $field['rule']);
222 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
223 }
224 }
225
226 $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule'));
227 }
228
229 static function formRule($fields) {
230
231 // make sure that at least one of live or test is present
232 // and we have at least name and url_site
233 // would be good to make this processor specific
234 $errors = array();
235
236 if (!(self::checkSection($fields, $errors) ||
237 self::checkSection($fields, $errors, 'test')
238 )) {
239 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
240 }
241
242 if (!empty($errors)) {
243 return $errors;
244 }
245
246 return empty($errors) ? TRUE : $errors;
247 }
248
249 static function checkSection(&$fields, &$errors, $section = NULL) {
250 $names = array('user_name');
251
252 $present = FALSE;
253 $allPresent = TRUE;
254 foreach ($names as $name) {
255 if ($section) {
256 $name = "{$section}_$name";
257 }
258 if (!empty($fields[$name])) {
259 $present = TRUE;
260 }
261 else {
262 $allPresent = FALSE;
263 }
264 }
265
266 if ($present) {
267 if (!$allPresent) {
268 $errors['_qf_default'] = ts('You must have at least the user_name specified');
269 }
270 }
271 return $present;
272 }
273
274 function setDefaultValues() {
275 $defaults = array();
276 if ($this->_ppType) {
277 $defaults['payment_processor_type_id'] = $this->_ppType;
278 }
279 if (!$this->_id) {
280 $defaults['is_active'] = $defaults['is_default'] = 1;
281 $defaults['url_site'] = $this->_ppDAO->url_site_default;
282 $defaults['url_api'] = $this->_ppDAO->url_api_default;
283 $defaults['url_recur'] = $this->_ppDAO->url_recur_default;
284 $defaults['url_button'] = $this->_ppDAO->url_button_default;
285 $defaults['test_url_site'] = $this->_ppDAO->url_site_test_default;
286 $defaults['test_url_api'] = $this->_ppDAO->url_api_test_default;
287 $defaults['test_url_recur'] = $this->_ppDAO->url_recur_test_default;
288 $defaults['test_url_button'] = $this->_ppDAO->url_button_test_default;
289 return $defaults;
290 }
291 $domainID = CRM_Core_Config::domainID();
292
293 $dao = new CRM_Financial_DAO_PaymentProcessor( );
294 $dao->id = $this->_id;
295 $dao->domain_id = $domainID;
296 if (!$dao->find(TRUE)) {
297 return $defaults;
298 }
299
300 CRM_Core_DAO::storeValues($dao, $defaults);
301
302 // now get testID
303 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
304 $testDAO->name = $dao->name;
305 $testDAO->is_test = 1;
306 $testDAO->domain_id = $domainID;
307 if ($testDAO->find(TRUE)) {
308 $this->_testID = $testDAO->id;
309
310 foreach ($this->_fields as $field) {
311 $testName = "test_{$field['name']}";
312 $defaults[$testName] = $testDAO->{$field['name']};
313 }
314 }
315 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($dao->id, 'civicrm_payment_processor', 'financial_account_id');
316
317 return $defaults;
318 }
319
320 /**
321 * Function to process the form
322 *
323 * @access public
324 *
325 * @return Void
326 */
327 public function postProcess() {
328 CRM_Utils_System::flushCache( 'CRM_Financial_DAO_PaymentProcessor' );
329
330 if ($this->_action & CRM_Core_Action::DELETE) {
331 CRM_Financial_BAO_PaymentProcessor::del($this->_id);
332 CRM_Core_Session::setStatus("", ts('Payment Processor Deleted.'), "success");
333 return;
334 }
335
336 $values = $this->controller->exportValues($this->_name);
337 $domainID = CRM_Core_Config::domainID();
338
339 if (!empty($values['is_default'])) {
340 $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
341 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
342 }
343
344 $this->updatePaymentProcessor($values, $domainID, FALSE);
345 $this->updatePaymentProcessor($values, $domainID, TRUE);
346 CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');
347 }
348
349 /**
350 * Save a payment processor
351 *
352 * @return Void
353 */
354 function updatePaymentProcessor(&$values, $domainID, $test) {
355 $dao = new CRM_Financial_DAO_PaymentProcessor( );
356
357 $dao->id = $test ? $this->_testID : $this->_id;
358 $dao->domain_id = $domainID;
359 $dao->is_test = $test;
360 $dao->is_default = CRM_Utils_Array::value('is_default', $values, 0);
361
362 $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
363
364 $dao->name = $values['name'];
365 $dao->description = $values['description'];
366 $dao->payment_processor_type_id = $values['payment_processor_type_id'];
367
368 foreach ($this->_fields as $field) {
369 $fieldName = $test ? "test_{$field['name']}" : $field['name'];
370 $dao->{$field['name']} = trim(CRM_Utils_Array::value($fieldName, $values));
371 if (empty($dao->{$field['name']})) {
372 $dao->{$field['name']} = 'null';
373 }
374 }
375
376 // also copy meta fields from the info DAO
377 $dao->is_recur = $this->_ppDAO->is_recur;
378 $dao->billing_mode = $this->_ppDAO->billing_mode;
379 $dao->class_name = $this->_ppDAO->class_name;
380 $dao->payment_type = $this->_ppDAO->payment_type;
381
382 $dao->save();
383
384 //CRM-11515
385
386 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
387 $params = array(
388 'entity_table' => 'civicrm_payment_processor',
389 'entity_id' => $dao->id,
390 'account_relationship' => $relationTypeId,
391 'financial_account_id' => $values['financial_account_id']
392 );
393 CRM_Financial_BAO_FinancialTypeAccount::add($params);
394 }
395 }