Merge pull request #16077 from civicrm/5.21
[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 protected $_id = NULL;
23
24 protected $_fields = NULL;
25
26 public function preProcess() {
27 parent::preProcess();
28
29 $this->_fields = [
30 [
31 'name' => 'name',
32 'label' => ts('Name'),
33 'required' => TRUE,
34 ],
35 [
36 'name' => 'title',
37 'label' => ts('Title'),
38 'required' => TRUE,
39 ],
40 [
41 'name' => 'billing_mode',
42 'label' => ts('Billing Mode'),
43 'required' => TRUE,
44 'rule' => 'positiveInteger',
45 'msg' => ts('Enter a positive integer'),
46 ],
47 [
48 'name' => 'description',
49 'label' => ts('Description'),
50 ],
51 [
52 'name' => 'user_name_label',
53 'label' => ts('User Name Label'),
54 ],
55 [
56 'name' => 'password_label',
57 'label' => ts('Password Label'),
58 ],
59 [
60 'name' => 'signature_label',
61 'label' => ts('Signature Label'),
62 ],
63 [
64 'name' => 'subject_label',
65 'label' => ts('Subject Label'),
66 ],
67 [
68 'name' => 'class_name',
69 'label' => ts('PHP class name'),
70 'required' => TRUE,
71 ],
72 [
73 'name' => 'url_site_default',
74 'label' => ts('Live Site URL'),
75 'required' => TRUE,
76 'rule' => 'url',
77 'msg' => ts('Enter a valid URL'),
78 ],
79 [
80 'name' => 'url_api_default',
81 'label' => ts('Live API URL'),
82 'required' => FALSE,
83 'rule' => 'url',
84 'msg' => ts('Enter a valid URL'),
85 ],
86 [
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'),
92 ],
93 [
94 'name' => 'url_button_default',
95 'label' => ts('Live Button URL'),
96 'rule' => 'url',
97 'msg' => ts('Enter a valid URL'),
98 ],
99 [
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'),
105 ],
106 [
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'),
112 ],
113 [
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'),
119 ],
120 [
121 'name' => 'url_button_test_default',
122 'label' => ts('Test Button URL'),
123 'rule' => 'url',
124 'msg' => ts('Enter a valid URL'),
125 ],
126 ];
127 }
128
129 /**
130 * Build the form object.
131 *
132 * @param bool $check
133 */
134 public function buildQuickForm($check = FALSE) {
135 parent::buildQuickForm();
136
137 if ($this->_action & CRM_Core_Action::DELETE) {
138 return;
139 }
140
141 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessorType');
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 );
148 if (!empty($field['rule'])) {
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
159 /**
160 * @return array
161 */
162 public function setDefaultValues() {
163 $defaults = [];
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
174 $dao = new CRM_Financial_DAO_PaymentProcessorType();
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 /**
187 * Process the form submission.
188 */
189 public function postProcess() {
190 CRM_Utils_System::flushCache();
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
199 if (!empty($values['is_default'])) {
200 $query = "
201 UPDATE civicrm_payment_processor SET is_default = 0";
202 CRM_Core_DAO::executeQuery($query);
203 }
204
205 $dao = new CRM_Financial_DAO_PaymentProcessorType();
206
207 $dao->id = $this->_id;
208 $dao->is_default = CRM_Utils_Array::value('is_default', $values, 0);
209 $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
210 $dao->is_recur = CRM_Utils_Array::value('is_recur', $values, 0);
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 }
223
224 }