-
Notifications
You must be signed in to change notification settings - Fork 3
/
manualdirectdebit.php
executable file
·390 lines (340 loc) · 13.8 KB
/
manualdirectdebit.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
require_once 'manualdirectdebit.civix.php';
use CRM_ManualDirectDebit_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function manualdirectdebit_civicrm_config(&$config) {
_manualdirectdebit_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function manualdirectdebit_civicrm_install() {
_manualdirectdebit_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function manualdirectdebit_civicrm_enable() {
_manualdirectdebit_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_permission().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_permission/
*/
function manualdirectdebit_civicrm_permission(&$permissions) {
$permissionsPrefix = 'CiviCRM : ';
$permissions['can manage direct debit batches'] = $permissionsPrefix . ts('Can manage Direct Debit Batches');
$permissions['administer ManualDirectDebit'] = [
E::ts('MembershipExtras: administer Manual Direct Debit'),
E::ts('Perform all Manual Direct Debit administration tasks in CiviCRM'),
];
}
/**
* Implements hook_civicrm_alterAPIPermissions().
*
*/
function manualdirectdebit_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
if ($entity == 'manual_direct_debit' && $action == 'deletemandate') {
$permissions[$entity][$action] = ['administer ManualDirectDebit'];
}
}
/**
* Implements hook_civicrm_navigationMenu().
*
*/
function manualdirectdebit_civicrm_navigationMenu(&$menu) {
$directDebitMenuItem = [
'name' => ts('Direct Debit'),
'url' => NULL,
'permission' => 'administer CiviCRM, administer ManualDirectDebit',
'operator' => 'OR',
'separator' => NULL,
];
_manualdirectdebit_civix_insert_navigation_menu($menu, 'Administer/', $directDebitMenuItem);
$subMenuItems = [
[
'name' => ts('Direct Debit Codes'),
'url' => 'civicrm/admin/options/direct_debit_codes',
'permission' => 'administer CiviCRM, administer ManualDirectDebit',
'operator' => 'OR',
'separator' => NULL,
],
[
'name' => ts('Direct Debit Configuration'),
'url' => 'civicrm/admin/direct_debit_configuration',
'permission' => 'administer CiviCRM, administer ManualDirectDebit',
'operator' => 'OR',
'separator' => NULL,
],
[
'name' => ts('Direct Debit Originator Number'),
'url' => 'civicrm/admin/options/direct_debit_originator_number?reset=1',
'permission' => 'administer CiviCRM, administer ManualDirectDebit',
'operator' => 'OR',
'separator' => NULL,
],
];
foreach ($subMenuItems as $menuItem) {
_manualdirectdebit_civix_insert_navigation_menu($menu, 'Administer/' . $directDebitMenuItem['name'], $menuItem);
}
}
/**
* Implements hook_civicrm_postProcess().
*/
function manualdirectdebit_civicrm_postProcess($formName, &$form) {
$action = $form->getAction();
$mandateID = $form->getSubmitValue('mandate_id');
$paymentInstrumentId = CRM_Utils_Array::value('payment_instrument_id', $form->getVar('_submitValues'));
$isDirectDebit = CRM_ManualDirectDebit_Common_DirectDebitDataProvider::isPaymentMethodDirectDebit($paymentInstrumentId);
switch (TRUE) {
case $formName == 'CRM_Contribute_Form_UpdateSubscription' && $action == CRM_Core_Action::UPDATE:
$manualDirectDebit = new CRM_ManualDirectDebit_Hook_PostProcess_RecurContribution_DirectDebitMandate($form);
$manualDirectDebit->saveMandateData();
break;
case $formName == 'CRM_Member_Form_Membership' && $action == CRM_Core_Action::ADD && $isDirectDebit:
$manualDirectDebit = new CRM_ManualDirectDebit_Hook_PostProcess_Membership_DirectDebitMandate($form);
$manualDirectDebit->saveMandateData();
break;
case $formName == 'CRM_Member_Form_MembershipRenewal' && $action == CRM_Core_Action::RENEW && $isDirectDebit:
$manualDirectDebit = new CRM_ManualDirectDebit_Hook_PostProcess_Membership_DirectDebitMandate($form);
$manualDirectDebit->saveMandateData();
break;
case $formName == 'CRM_Contribute_Form_Contribution' && $action == CRM_Core_Action::ADD:
case $action == CRM_Core_Action::ADD && !empty($mandateID) && $isDirectDebit:
case $action == CRM_Core_Action::UPDATE && !empty($mandateID) && $isDirectDebit:
$manualDirectDebit = new CRM_ManualDirectDebit_Hook_PostProcess_Contribution_DirectDebitMandate($form);
$manualDirectDebit->setCurrentContactId($form->getVar('_contactID'));
$manualDirectDebit->saveMandateData();
break;
case $formName == 'CRM_Contact_Form_CustomData':
$isDirectDebitGroup = CRM_ManualDirectDebit_Common_DirectDebitDataProvider::isDirectDebitCustomGroup($form->getVar('_groupID'));
if ($isDirectDebitGroup) {
$manualDirectDebit = new CRM_ManualDirectDebit_Hook_PostProcess_Contribution_DirectDebitMandate($form);
$manualDirectDebit->run();
}
break;
}
}
/**
* Implements hook_civicrm_pageRun().
*/
function manualdirectdebit_civicrm_pageRun(&$page) {
switch (get_class($page)) {
case 'CRM_Contribute_Page_ContributionRecur':
$injectCustomGroup = new CRM_ManualDirectDebit_Hook_PageRun_ContributionRecur_DirectDebitFieldsInjector($page);
$injectCustomGroup->inject();
break;
case 'CRM_Contact_Page_View_CustomData':
$path = CRM_Utils_System::currentPath();
$multiRecordDisplay = CRM_Utils_Request::retrieveValue('multiRecordDisplay', 'String', '');
$mode = CRM_Utils_Request::retrieveValue('mode', 'String', '');
$mandateStorageManager = new CRM_ManualDirectDebit_Common_MandateStorageManager();
$pageProcessor = new CRM_ManualDirectDebit_Hook_PageRun_ViewCustomData($path, $multiRecordDisplay, $mode, $mandateStorageManager, $page);
$pageProcessor->process();
break;
case 'CRM_Contribute_Page_Tab':
$contributionId = $page->getVar('_id');
$pageProcessor = new CRM_ManualDirectDebit_Hook_PageRun_TabPage();
$pageProcessor->setContributionId($contributionId);
$pageProcessor->hideDirectDebitFields();
CRM_Core_Resources::singleton()
->addScriptFile('uk.co.compucorp.manualdirectdebit', 'js/openContribution.js')
->addScriptFile('uk.co.compucorp.manualdirectdebit', 'js/paymentMethodMandateSelection.js');
break;
case 'CRM_Member_Page_Tab':
case 'CRM_Event_Page_Tab':
CRM_Core_Resources::singleton()
->addScriptFile('uk.co.compucorp.manualdirectdebit', 'js/paymentMethodMandateSelection.js');
break;
case CRM_ManualDirectDebit_Page_BatchList::class:
CRM_Core_Resources::singleton()->addStyleFile(E::LONG_NAME, 'css/batchSearch.css');
break;
}
}
function _manualdirectdebit_getContactType($contactId) {
return civicrm_api3('Contact', 'getvalue', [
'return' => 'contact_type',
'id' => $contactId,
]);
}
/**
* Implements hook_civicrm_custom().
*/
function manualdirectdebit_civicrm_custom($op, $groupID, $entityID, &$params) {
if (CRM_ManualDirectDebit_Common_DirectDebitDataProvider::isDirectDebitCustomGroup($groupID)) {
if (in_array($op, ['create', 'edit', 'update'])) {
$mandateDataGenerator = new CRM_ManualDirectDebit_Hook_Custom_DataGenerator($entityID, $params);
$mandateDataGenerator->generateMandateData();
}
if ($op == 'edit' || $op == 'update') {
$mandateStorageManager = new CRM_ManualDirectDebit_Common_MandateStorageManager();
$cancellationChecker = new CRM_ManualDirectDebit_Hook_Custom_CancellationBatchChecker($entityID, $params, $mandateStorageManager);
$cancellationChecker->process();
}
}
}
/**
* Implements hook_civicrm_postSave_civicrm_contribution().
*/
function manualdirectdebit_civicrm_postSave_civicrm_contribution($dao) {
$mandateContributionConnector = CRM_ManualDirectDebit_Hook_MandateContributionConnector::getInstance();
$mandateContributionConnector->setContributionProperties($dao);
}
/**
* Implements hook_civicrm_post().
*/
function manualdirectdebit_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if ($op == 'create' && $objectName == 'Contribution') {
$postContributionHook = new CRM_ManualDirectDebit_Hook_Post_Contribution($objectId);
$postContributionHook->process();
}
}
/**
* Implements hook_civicrm_buildForm().
*/
function manualdirectdebit_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Activity_Form_ActivityLinks') {
$openContributionId = CRM_Utils_Request::retrieveValue('openContribution', 'Integer', FALSE);
if ($openContributionId) {
$form->add('hidden', 'optionContributionId', $openContributionId);
}
}
if ($formName == 'CRM_Contact_Form_CustomData') {
if (CRM_ManualDirectDebit_Common_DirectDebitDataProvider::isDirectDebitCustomGroup($form->getVar('_groupID'))) {
$customData = new CRM_ManualDirectDebit_Hook_BuildForm_CustomData($form);
$customData->run();
}
}
if ($formName == 'CRM_Custom_Form_CustomDataByType') {
$customDataByType = new CRM_ManualDirectDebit_Hook_BuildForm_CustomDataByType($form);
$customDataByType->run();
}
if ($formName === 'CRM_Member_Form_Membership' || $formName === 'CRM_Member_Form_MembershipRenewal') {
$formBuilder = new CRM_ManualDirectDebit_Hook_BuildForm_Membership($form);
$formBuilder->buildForm();
}
if ($formName === 'CRM_Financial_Form_Payment') {
$formBuilder = new CRM_ManualDirectDebit_Hook_BuildForm_Payment($form);
$formBuilder->buildForm();
}
if ($formName === 'CRM_Contribute_Form_UpdateSubscription') {
$formBuilder = new CRM_ManualDirectDebit_Hook_BuildForm_UpdateSubscription($form);
$formBuilder->buildForm();
}
}
/**
* Implements hook_civicrm_validateForm().
*/
function manualdirectdebit_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName == 'CRM_Member_Form_Membership' || $formName === 'CRM_Member_Form_MembershipRenewal') {
$directDebitValidator = new CRM_ManualDirectDebit_Hook_ValidateForm_MandateValidator($form, $fields, $errors);
$directDebitValidator->checkValidation();
}
}
/**
* Implements hook_civicrm_postSave_civicrm_membership_payment().
*/
function manualdirectdebit_civicrm_postSave_civicrm_membership_payment($dao) {
$mandateCreator = new CRM_ManualDirectDebit_Hook_PostSave_MembershipPayment_MandateCreator($dao);
$mandateCreator->assignMandateForContributions();
}
/**
* Implements hook_civicrm_links().
*/
function manualdirectdebit_civicrm_links($op, $objectName, $objectId, &$links, &$mask, &$values) {
$linkProvider = new CRM_ManualDirectDebit_Hook_Links_LinkProvider($links);
if ($objectName == 'Contribution' && $op == 'contribution.selector.recurring') {
$linkProvider->alterRecurContributionLinks($values, $objectId);
}
if ($objectName == 'Batch') {
$linkProvider->alterBatchLinks($objectId);
}
}
/**
* Implements hook_membershipextras_postOfflineAutoRenewal().
*/
function manualdirectdebit_membershipextras_postOfflineAutoRenewal($membershipId, $recurContributionId, $previousRecurContributionId) {
$activity = new CRM_ManualDirectDebit_Hook_PostOfflineAutoRenewal_Activity($recurContributionId);
$activity->process();
$mandate = new CRM_ManualDirectDebit_Hook_PostOfflineAutoRenewal_Mandate($recurContributionId, $previousRecurContributionId);
$mandate->process();
}
/**
* Implements hook_membershipextras_calculateContributionReceiveDate().
*/
function manualdirectdebit_membershipextras_calculateContributionReceiveDate($contributionNumber, &$receiveDate, $contributionCreationParams) {
$settingsManager = new CRM_ManualDirectDebit_Common_SettingsManager();
switch ($contributionNumber) {
case 1:
$receiveDateCalculator = new CRM_ManualDirectDebit_Hook_CalculateContributionReceiveDate_FirstContribution(
$receiveDate,
$contributionCreationParams,
$settingsManager
);
$receiveDateCalculator->process();
break;
case 2:
$receiveDateCalculator = new CRM_ManualDirectDebit_Hook_CalculateContributionReceiveDate_SecondContribution(
$receiveDate,
$contributionCreationParams,
$settingsManager
);
$receiveDateCalculator->process();
break;
default:
$receiveDateCalculator = new CRM_ManualDirectDebit_Hook_CalculateContributionReceiveDate_OtherContribution(
$receiveDate,
$contributionCreationParams,
$settingsManager
);
$receiveDateCalculator->process();
}
}
function manualdirectdebit_civicrm_searchTasks($objectName, &$tasks) {
if ($objectName == 'contribution') {
$tasks[] = [
'title' => 'Send Direct Debit Notifications',
'class' => 'CRM_ManualDirectDebit_Form_Email_Contribution',
'result' => FALSE,
];
}
if ($objectName == 'membership') {
$tasks[] = [
'title' => 'Send Direct Debit Notifications',
'class' => 'CRM_ManualDirectDebit_Form_Email_Membership',
'result' => FALSE,
];
$tasks[] = [
'title' => 'Print Direct Debit Letters',
'class' => 'CRM_ManualDirectDebit_Form_PrintMergeDocument',
'result' => FALSE,
];
}
}
function manualdirectdebit_civicrm_container($container) {
$priorityHigherThanCoreServices = -1;
$container->findDefinition('dispatcher')
->addMethodCall('addListener',
array('civi.token.render', '_manualdirectdebit_civicrm_tokenRenderEventListener', $priorityHigherThanCoreServices));
}
function _manualdirectdebit_civicrm_tokenRenderEventListener($event) {
$tokenRenderListener = new CRM_ManualDirectDebit_Event_Listener_TokenRender($event);
$tokenRenderListener->replaceDirectDebitTokens();
}
/**
* Implements hook_civicrm_queryObjects().
*/
function manualdirectdebit_civicrm_queryObjects(&$queryObjects, $type) {
if ($type === 'Contact') {
$queryObjects[] = new CRM_ManualDirectDebit_Hook_QueryObjects_Contribution();
}
}