Merge pull request #14667 from hoegrammer/master
[civicrm-core.git] / CRM / Admin / Form / PaymentProcessorType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
ce064e4f 19 * This class generates form components for Location Type.
6a488035
TO
20 */
21class CRM_Admin_Form_PaymentProcessorType extends CRM_Admin_Form {
e57fc8ca 22 public $_id = NULL;
6a488035
TO
23
24 protected $_fields = NULL;
25
00be9182 26 public function preProcess() {
6a488035
TO
27 parent::preProcess();
28
be2fb01f
CW
29 $this->_fields = [
30 [
6a488035
TO
31 'name' => 'name',
32 'label' => ts('Name'),
33 'required' => TRUE,
be2fb01f
CW
34 ],
35 [
6a488035
TO
36 'name' => 'title',
37 'label' => ts('Title'),
38 'required' => TRUE,
be2fb01f
CW
39 ],
40 [
6a488035
TO
41 'name' => 'billing_mode',
42 'label' => ts('Billing Mode'),
43 'required' => TRUE,
44 'rule' => 'positiveInteger',
45 'msg' => ts('Enter a positive integer'),
be2fb01f
CW
46 ],
47 [
6a488035
TO
48 'name' => 'description',
49 'label' => ts('Description'),
be2fb01f
CW
50 ],
51 [
6a488035
TO
52 'name' => 'user_name_label',
53 'label' => ts('User Name Label'),
be2fb01f
CW
54 ],
55 [
6a488035
TO
56 'name' => 'password_label',
57 'label' => ts('Password Label'),
be2fb01f
CW
58 ],
59 [
6a488035
TO
60 'name' => 'signature_label',
61 'label' => ts('Signature Label'),
be2fb01f
CW
62 ],
63 [
6a488035
TO
64 'name' => 'subject_label',
65 'label' => ts('Subject Label'),
be2fb01f
CW
66 ],
67 [
6a488035
TO
68 'name' => 'class_name',
69 'label' => ts('PHP class name'),
70 'required' => TRUE,
be2fb01f
CW
71 ],
72 [
6a488035
TO
73 'name' => 'url_site_default',
74 'label' => ts('Live Site URL'),
75 'required' => TRUE,
76 'rule' => 'url',
77 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
78 ],
79 [
6a488035
TO
80 'name' => 'url_api_default',
81 'label' => ts('Live API URL'),
82 'required' => FALSE,
83 'rule' => 'url',
84 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
85 ],
86 [
6a488035
TO
87 'name' => 'url_recur_default',
88 'label' => ts('Live Recurring Payments URL'),
89 'required' => TRUE,
90 'rule' => 'url',
91 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
92 ],
93 [
6a488035
TO
94 'name' => 'url_button_default',
95 'label' => ts('Live Button URL'),
96 'rule' => 'url',
97 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
98 ],
99 [
6a488035
TO
100 'name' => 'url_site_test_default',
101 'label' => ts('Test Site URL'),
102 'required' => TRUE,
103 'rule' => 'url',
104 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
105 ],
106 [
6a488035
TO
107 'name' => 'url_api_test_default',
108 'label' => ts('Test API URL'),
109 'required' => FALSE,
110 'rule' => 'url',
111 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
112 ],
113 [
6a488035
TO
114 'name' => 'url_recur_test_default',
115 'label' => ts('Test Recurring Payments URL'),
116 'required' => TRUE,
117 'rule' => 'url',
118 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
119 ],
120 [
6a488035
TO
121 'name' => 'url_button_test_default',
122 'label' => ts('Test Button URL'),
123 'rule' => 'url',
124 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
125 ],
126 ];
6a488035
TO
127 }
128
129 /**
eceb18cc 130 * Build the form object.
6a488035 131 *
dd244018 132 * @param bool $check
6a488035
TO
133 */
134 public function buildQuickForm($check = FALSE) {
135 parent::buildQuickForm();
136
137 if ($this->_action & CRM_Core_Action::DELETE) {
138 return;
139 }
140
481a74f4 141 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessorType');
6a488035
TO
142
143 foreach ($this->_fields as $field) {
144 $required = CRM_Utils_Array::value('required', $field, FALSE);
145 $this->add('text', $field['name'],
146 $field['label'], $attributes['name'], $required
147 );
a7488080 148 if (!empty($field['rule'])) {
6a488035
TO
149 $this->addRule($field['name'], $field['msg'], $field['rule']);
150 }
151 }
152
153 // is this processor active ?
154 $this->add('checkbox', 'is_active', ts('Is this Payment Processor Type active?'));
155 $this->add('checkbox', 'is_default', ts('Is this Payment Processor Type the default?'));
156 $this->add('checkbox', 'is_recur', ts('Does this Payment Processor Type support recurring donations?'));
157 }
158
e0ef6999
EM
159 /**
160 * @return array
161 */
00be9182 162 public function setDefaultValues() {
be2fb01f 163 $defaults = [];
6a488035
TO
164
165 if (!$this->_id) {
166 $defaults['is_active'] = $defaults['is_default'] = 1;
167 $defaults['user_name_label'] = ts('User Name');
168 $defaults['password_label'] = ts('Password');
169 $defaults['signature_label'] = ts('Signature');
170 $defaults['subject_label'] = ts('Subject');
171 return $defaults;
172 }
173
481a74f4 174 $dao = new CRM_Financial_DAO_PaymentProcessorType();
6a488035
TO
175 $dao->id = $this->_id;
176
177 if (!$dao->find(TRUE)) {
178 return $defaults;
179 }
180
181 CRM_Core_DAO::storeValues($dao, $defaults);
182
183 return $defaults;
184 }
185
186 /**
eceb18cc 187 * Process the form submission.
6a488035
TO
188 */
189 public function postProcess() {
d7d6c461 190 CRM_Utils_System::flushCache();
6a488035
TO
191
192 if ($this->_action & CRM_Core_Action::DELETE) {
193 CRM_Financial_BAO_PaymentProcessorType::del($this->_id);
194 return;
195 }
196
197 $values = $this->controller->exportValues($this->_name);
198
a7488080 199 if (!empty($values['is_default'])) {
6a488035
TO
200 $query = "
201UPDATE civicrm_payment_processor SET is_default = 0";
33621c4f 202 CRM_Core_DAO::executeQuery($query);
6a488035
TO
203 }
204
481a74f4 205 $dao = new CRM_Financial_DAO_PaymentProcessorType();
6a488035 206
353ffa53 207 $dao->id = $this->_id;
6a488035 208 $dao->is_default = CRM_Utils_Array::value('is_default', $values, 0);
353ffa53
TO
209 $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
210 $dao->is_recur = CRM_Utils_Array::value('is_recur', $values, 0);
6a488035
TO
211
212 $dao->name = $values['name'];
213 $dao->description = $values['description'];
214
215 foreach ($this->_fields as $field) {
216 $dao->{$field['name']} = trim($values[$field['name']]);
217 if (empty($dao->{$field['name']})) {
218 $dao->{$field['name']} = 'null';
219 }
220 }
221 $dao->save();
222 }
96025800 223
6a488035 224}