-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeigndonors.php
293 lines (262 loc) · 10.7 KB
/
foreigndonors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
require_once 'foreigndonors.civix.php';
use CRM_Foreigndonors_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function foreigndonors_civicrm_config(&$config) {
_foreigndonors_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function foreigndonors_civicrm_install() {
_foreigndonors_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function foreigndonors_civicrm_enable() {
_foreigndonors_civix_civicrm_enable();
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
// */
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
function foreigndonors_civicrm_navigationMenu(&$menu) {
_foreigndonors_civix_insert_navigation_menu($menu, 'Mailings', array(
'label' => E::ts('New subliminal message'),
'name' => 'mailing_subliminal_message',
'url' => 'civicrm/mailing/subliminal',
'permission' => 'access CiviMail',
'operator' => 'OR',
'separator' => 0,
));
_foreigndonors_civix_navigationMenu($menu);
} // */
function foreigndonors_civicrm_alterEntitySettingsFolders(&$folders) {
static $configured = FALSE;
if ($configured) {
return;
}
$configured = TRUE;
$extRoot = dirname(__FILE__) . DIRECTORY_SEPARATOR;
$extDir = $extRoot . 'settings';
if (!in_array($extDir, $folders)) {
$folders[] = $extDir;
}
}
function foreigndonors_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Contribute_Form_Contribution_Main' || $formName == 'CRM_Event_Form_Registration_Register') {
$formId = $form->get('id');
// Get domain ID
// Necessary to modify text for QLD domain
// and add prohibited checkbox for NSW and ACT domains
$domainId = CRM_Core_Config::domainID();
// If the form doesn't have the setting ticked, we don't need to do anything
if ($formName == 'CRM_Contribute_Form_Contribution_Main' && !_foreigndonors_checkEnabled($formId, 'contribution_page')) {
return;
}
if ($formName == 'CRM_Event_Form_Registration_Register' && !_foreigndonors_checkEnabled($formId, 'event_fee')) {
return;
}
if ($formName == 'CRM_Event_Form_Registration_Register' && !$form->_values['event']['is_monetary']) {
return;
}
$form->assign('domainId', $domainId);
$form->assign('foreignPageId', $formId);
// As a safeguard against people completing NSW contrib forms through the wrong domain
// We're going to get the financial type of the page/event and if it starts with 'NSW'
// we know to inject the prohibited donor checkbox, regardless of domain
$finType = [];
if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
$finType = \Civi\Api4\ContributionPage::get(FALSE)
->addSelect('financial_type_id:label')
->addWhere('id', '=', $formId)
->setLimit(1)
->execute();
}
else {
$finType = \Civi\Api4\Event::get(FALSE)
->addSelect('financial_type_id:label')
->addWhere('id', '=', $formId)
->setLimit(1)
->execute();
}
$isNSW = substr($finType[0]['financial_type_id:label'], 0, 3) === 'NSW';
$isACT = substr($finType[0]['financial_type_id:label'], 0, 3) === 'ACT';
$form->assign('addNSWProhibitedDonor', $isNSW || $domainId == 8);
$form->assign('addACTProhibitedDonor', $isACT || $domainId == 9);
// Add the checkbox to the public form
// Have to use different language for Queensland
// Inject a prohibited donor checkbox for NSW and ACT
if ($domainId == 7 && !$isNSW && !$isACT) {
$label = "I am an Australian Citizen or Permanent Resident, and not a QLD prohibited donor";
$form->add('Checkbox', 'foreigndonor', ts('I am an Australian Citizen or Permanent Resident, and not a QLD prohibited donor'));
$form->addRule('foreigndonor', ts('You must affirm you are not a prohibited donor as per State and Federal legislation'), 'required', NULL, 'client');
}
elseif ($domainId == 8 || $isNSW ) {
$label = "I am an Australian Citizen or Permanent Resident and am not a foreign donor";
$form->add('Checkbox', 'foreigndonor', ts('I am an Australian Citizen or Permanent Resident and am not a foreign donor'));
$form->addRule('foreigndonor', ts('You must affirm you are an Australian Citizen or Permanent Resident'), 'required', NULL, 'client');
$form->add('Checkbox', 'prohibiteddonor', ts('I am not a prohibited donor as per NSW electoral legislation'));
$form->addRule('prohibiteddonor', ts('You must affirm you are not a prohibited donor as per NSW electoral legislation'), 'required', NULL, 'client');
}
elseif ($domainId == 9 || $isACT ) {
$label = "I am an Australian Citizen or Permanent Resident and am not a foreign donor";
$form->add('Checkbox', 'foreigndonor', ts('I am an Australian Citizen or Permanent Resident and am not a foreign donor'));
$form->addRule('foreigndonor', ts('You must affirm you are an Australian Citizen or Permanent Resident'), 'required', NULL, 'client');
$form->add('Checkbox', 'prohibiteddonor', ts('I am not a prohibited donor as per ACT electoral legislation'));
$form->addRule('prohibiteddonor', ts('You must affirm you are not a prohibited donor as per ACT electoral legislation'), 'required', NULL, 'client');
}
else {
$label = "I am an Australian Citizen or Permanent Resident";
$form->add('Checkbox', 'foreigndonor', ts('I am an Australian Citizen or Permanent Resident'));
$form->addRule('foreigndonor', ts('You must affirm you are an Australian Citizen or Permanent Resident'), 'required', NULL, 'client');
}
$templatePath = realpath(dirname(__FILE__) . '/templates');
if ($formName == 'CRM_Event_Form_Registration_Register') {
CRM_Core_Resources::singleton()->addScriptFile('au.org.greens.foreigndonors', 'js/event-form.js');
}
else {
CRM_Core_Resources::singleton()->addScriptFIle('au.org.greens.foreigndonors', 'js/contrib-form.js');
}
CRM_Core_Resources::singleton()->addVars('foreigndonors', ['label' => $label, 'domainId' => $domainId, 'boostrapjs' => E::url('js/bootstrap.min.js')]);
CRM_Core_Resources::singleton()->addScriptFile('au.org.greens.foreigndonors', 'js/help_text.js');
CRM_Core_Region::instance('form-bottom')->add(array(
'template' => "{$templatePath}/foreigndonors.tpl",
));
return;
}
}
function foreigndonors_civicrm_post($op, $objectName, $id, &$params) {
$entity_id = $id;
$check_enabled = FALSE;
$contribution_id = 0;
// Check for Contribution generated through Contribution Page
if ($objectName == 'Contribution' && $op == 'create') {
$result = civicrm_api3('Contribution', 'get', array(
'return' => ['contribution_page_id'],
'id' => $entity_id,
));
$contrib_page_id = $result['values'][$entity_id]['contribution_page_id'];
if (!empty($contrib_page_id)) {
if (_foreigndonors_checkEnabled($contrib_page_id, 'contribution_page')) {
$check_enabled = TRUE;
$contribution_id = $entity_id;
}
}
}
elseif ($objectName == 'ParticipantPayment' && $op == 'create') {
// Check for a Contribution generated through an event registration payment
// Workaround for Civi core bug (see #13739)
$entity_id = $params->id;
// Get the participant ID linked to the payment
$result = civicrm_api3('ParticipantPayment', 'get', [
'return' => ['participant_id'],
'id' => $entity_id,
]);
// Get the event ID linked to the participant ID
$participant_id = $result['values'][$entity_id]['participant_id'];
$result = civicrm_api3('Participant', 'get', [
'return' => ['event_id'],
'id' => $participant_id,
]);
$event_id = $result['values'][$participant_id]['event_id'];
// Now check if event has foreign donor check set
if (_foreigndonors_checkEnabled($event_id, 'event_fee')) {
$check_enabled = TRUE;
// Get the contribution ID linked to the payment
$result = civicrm_api3('ParticipantPayment', 'get', [
'return' => ['contribution_id'],
'id' => $entity_id,
]);
$contribution_id = $result['values'][$entity_id]['contribution_id'];
}
}
if ($check_enabled) {
if (CRM_Core_Transaction::isActive()) {
CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, 'foreigndonors_record_submission', [$contribution_id]);
}
else {
foreigndonors_record_submission($contribution_id);
}
}
}
/**
* Record that the end user has ticketed the foreign donor check box and or the prohibited donor checkbox
* @param int $contribution_id
*/
function foreigndonors_record_submission($contribution_id) {
// Finally, if need be, update the Contribution record
// to record foreign donor check
$customFieldId = _foreigndonors_get_customFieldId();
$params = [
'id' => $contribution_id,
'custom_' . $customFieldId => 1,
];
$domainId = CRM_Core_Config::domainID();
$finType = \Civi\Api4\Contribution::get(FALSE)
->addSelect('financial_type_id:label')
->addWhere('id', '=', $contribution_id)
->setLimit(1)
->execute();
if ($domainId == 8 || substr($finType[0]['financial_type_id:label'], 0, 3) === 'NSW' ||
$domainId == 9 || substr($finType[0]['financial_type_id:label'], 0, 3) === 'ACT') {
$prohibitedCustomFieldId = _foreigndonors_get_prohibited_donor_customFieldId();
if (!empty($prohibitedCustomFieldId)) {
$params['custom_' . $prohibitedCustomFieldId] = 'No';
}
}
$result = civicrm_api3('Contribution', 'create', $params);
}
function _foreigndonors_checkEnabled($formId, $type) {
$result = civicrm_api3('entity_setting', 'get', array(
'entity_id' => $formId,
'entity_type' => $type,
'key' => 'au.org.greens.foreigndonors',
'name' => 'foreign_donors_check',
));
if (!empty($result['values'][$formId]['foreign_donors_check'])) {
return 1;
}
else {
return 0;
}
}
function _foreigndonors_get_customFieldId() {
$result = civicrm_api3('CustomField', 'get', array(
'name' => 'Foreign_donor_declaration_recorded',
));
if (!empty($result['id'])) {
return $result['id'];
}
else {
return 0;
}
}
function _foreigndonors_get_prohibited_donor_customFieldId() {
$result = civicrm_api3('CustomField', 'get', [
'name' => 'Are_you_a_prohibited_donor_',
]);
if (!empty($result['id'])) {
return $result['id'];
}
else {
return 0;
}
}