Merge pull request #22484 from civicrm/5.46
[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
88aae6d4
A
26 /**
27 * @var bool
28 */
29 public $submitOnce = TRUE;
30
00be9182 31 public function preProcess() {
6a488035
TO
32 parent::preProcess();
33
be2fb01f
CW
34 $this->_fields = [
35 [
6a488035
TO
36 'name' => 'name',
37 'label' => ts('Name'),
38 'required' => TRUE,
be2fb01f
CW
39 ],
40 [
6a488035
TO
41 'name' => 'title',
42 'label' => ts('Title'),
43 'required' => TRUE,
be2fb01f
CW
44 ],
45 [
6a488035
TO
46 'name' => 'billing_mode',
47 'label' => ts('Billing Mode'),
48 'required' => TRUE,
49 'rule' => 'positiveInteger',
50 'msg' => ts('Enter a positive integer'),
be2fb01f
CW
51 ],
52 [
6a488035
TO
53 'name' => 'description',
54 'label' => ts('Description'),
be2fb01f
CW
55 ],
56 [
6a488035
TO
57 'name' => 'user_name_label',
58 'label' => ts('User Name Label'),
be2fb01f
CW
59 ],
60 [
6a488035
TO
61 'name' => 'password_label',
62 'label' => ts('Password Label'),
be2fb01f
CW
63 ],
64 [
6a488035
TO
65 'name' => 'signature_label',
66 'label' => ts('Signature Label'),
be2fb01f
CW
67 ],
68 [
6a488035
TO
69 'name' => 'subject_label',
70 'label' => ts('Subject Label'),
be2fb01f
CW
71 ],
72 [
6a488035
TO
73 'name' => 'class_name',
74 'label' => ts('PHP class name'),
75 'required' => TRUE,
be2fb01f
CW
76 ],
77 [
6a488035
TO
78 'name' => 'url_site_default',
79 'label' => ts('Live Site URL'),
80 'required' => TRUE,
81 'rule' => 'url',
82 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
83 ],
84 [
6a488035
TO
85 'name' => 'url_api_default',
86 'label' => ts('Live API URL'),
87 'required' => FALSE,
88 'rule' => 'url',
89 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
90 ],
91 [
6a488035
TO
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'),
be2fb01f
CW
97 ],
98 [
6a488035
TO
99 'name' => 'url_button_default',
100 'label' => ts('Live Button URL'),
101 'rule' => 'url',
102 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
103 ],
104 [
6a488035
TO
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'),
be2fb01f
CW
110 ],
111 [
6a488035
TO
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'),
be2fb01f
CW
117 ],
118 [
6a488035
TO
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'),
be2fb01f
CW
124 ],
125 [
6a488035
TO
126 'name' => 'url_button_test_default',
127 'label' => ts('Test Button URL'),
128 'rule' => 'url',
129 'msg' => ts('Enter a valid URL'),
be2fb01f
CW
130 ],
131 ];
6a488035
TO
132 }
133
134 /**
eceb18cc 135 * Build the form object.
6a488035 136 *
dd244018 137 * @param bool $check
6a488035
TO
138 */
139 public function buildQuickForm($check = FALSE) {
140 parent::buildQuickForm();
141
142 if ($this->_action & CRM_Core_Action::DELETE) {
143 return;
144 }
145
481a74f4 146 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessorType');
6a488035
TO
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 );
a7488080 153 if (!empty($field['rule'])) {
6a488035
TO
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
e0ef6999
EM
164 /**
165 * @return array
166 */
00be9182 167 public function setDefaultValues() {
be2fb01f 168 $defaults = [];
6a488035
TO
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
481a74f4 179 $dao = new CRM_Financial_DAO_PaymentProcessorType();
6a488035
TO
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 /**
eceb18cc 192 * Process the form submission.
6a488035
TO
193 */
194 public function postProcess() {
d7d6c461 195 CRM_Utils_System::flushCache();
6a488035
TO
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
a7488080 204 if (!empty($values['is_default'])) {
6a488035
TO
205 $query = "
206UPDATE civicrm_payment_processor SET is_default = 0";
33621c4f 207 CRM_Core_DAO::executeQuery($query);
6a488035
TO
208 }
209
481a74f4 210 $dao = new CRM_Financial_DAO_PaymentProcessorType();
6a488035 211
353ffa53 212 $dao->id = $this->_id;
6a488035 213 $dao->is_default = CRM_Utils_Array::value('is_default', $values, 0);
353ffa53
TO
214 $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
215 $dao->is_recur = CRM_Utils_Array::value('is_recur', $values, 0);
6a488035
TO
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 }
96025800 228
6a488035 229}