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