-
Notifications
You must be signed in to change notification settings - Fork 8
/
membershipextras.php
executable file
·465 lines (402 loc) · 17.8 KB
/
membershipextras.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
require_once 'membershipextras.civix.php';
use CRM_MembershipExtras_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function membershipextras_civicrm_config(&$config) {
_membershipextras_civix_civicrm_config($config);
Civi::dispatcher()->addListener('civi.api.prepare', ['CRM_MembershipExtras_Hook_Config_APIWrapper_PaymentAPI', 'preApiCall']);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function membershipextras_civicrm_install() {
_membershipextras_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function membershipextras_civicrm_enable() {
_membershipextras_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_navigationMenu().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu/
*/
function membershipextras_civicrm_navigationMenu(&$menu) {
$paymentPlanSettingsMenuItem = [
'name' => 'payment_plan_settings',
'label' => ts('Payment Plan Settings'),
'url' => 'civicrm/admin/payment_plan_settings',
'permission' => 'administer CiviCRM,administer MembershipExtras',
'operator' => 'OR',
'separator' => NULL,
];
_membershipextras_civix_insert_navigation_menu($menu, 'Administer/CiviContribute', $paymentPlanSettingsMenuItem);
$automatedMembershipUpgradeRulesMenuItem = [
'name' => 'automated_membership_upgrade_rules',
'label' => ts('Membership Automated Upgrade Rules'),
'url' => 'civicrm/admin/member/automated-upgrade-rules?reset=1',
'permission' => 'administer CiviCRM,administer MembershipExtras',
'operator' => 'OR',
'separator' => 2,
];
_membershipextras_civix_insert_navigation_menu($menu, 'Administer/CiviMember', $automatedMembershipUpgradeRulesMenuItem);
$paymentSchemeMenuItem = [
'name' => 'membership_payment_scheme',
'label' => ts('Payment Schemes'),
'url' => 'civicrm/member/admin/payment-schemes?reset=1',
'permission' => 'administer CiviCRM,administer MembershipExtras',
'operator' => 'OR',
'separator' => NULL,
];
_membershipextras_civix_insert_navigation_menu($menu, 'Administer/CiviContribute', $paymentSchemeMenuItem);
}
/**
* Implements hook_civicrm_pre().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_pre
*/
function membershipextras_civicrm_pre($op, $objectName, $id, &$params) {
/**
* We store the contribution ID in a static variable because we
* need it for CRM_MembershipExtras_Hook_PreEdit_Membership class
* to be able to determine the correct recurring contribution ID in
* case there was more than one recurring contribution.
* It is not that pretty solution but there is no much
* options for now.
*/
static $contributionID = NULL;
static $lineItemCount = [];
if ($op === 'edit' && $objectName === 'Contribution') {
$contributionID = $id;
}
if ($objectName === 'Membership' && $op == 'edit') {
$paymentType = Civi::$statics[E::LONG_NAME]['paymentType'] ?? '';
$membershipPreHook = new CRM_MembershipExtras_Hook_Pre_MembershipEdit($id, $params, $contributionID, $paymentType);
$membershipPreHook->preProcess();
}
static $isFirstPaymentPlanContribution = TRUE;
$isPaymentPlanPayment = CRM_MembershipExtras_Helper_InstalmentSchedule::isPaymentPlanPayment();
$isContributionCreation = ($objectName === 'Contribution' && $op === 'create');
if ($isContributionCreation) {
$lineItems = CRM_Utils_Array::value('line_item', $params, []);
$lineItemCount[] = !empty($lineItems) ? count(array_values($lineItems)[0]) : 1;
}
if ($isContributionCreation && $isPaymentPlanPayment && $isFirstPaymentPlanContribution) {
$paymentPlanProcessor = new CRM_MembershipExtras_Hook_Pre_MembershipPaymentPlanProcessor_Contribution($params);
$paymentPlanProcessor->createPaymentPlan();
$paymentPlanProcessor->setContributionToPayLater();
$isFirstPaymentPlanContribution = FALSE;
}
static $firstPaymentPlanContributionId;
$lineItemContributionCreation = $objectName === 'LineItem' && $op === 'create' && !empty($params['contribution_id']);
$firstPaymentPlanContributionLineItemCreation = ($lineItemContributionCreation && (empty($firstPaymentPlanContributionId) || $firstPaymentPlanContributionId == $params['contribution_id']));
if ($firstPaymentPlanContributionLineItemCreation && $isPaymentPlanPayment) {
if (isset($lineItemCount[0])) {
$params['lineItemCount'] = $lineItemCount[0];
}
$paymentPlanProcessor = new CRM_MembershipExtras_Hook_Pre_MembershipPaymentPlanProcessor_LineItem($params);
$paymentPlanProcessor->alterLineItemParameters();
$firstPaymentPlanContributionId = $params['contribution_id'];
}
if ($objectName == 'ContributionRecur') {
$contributionRecurPreHook = new CRM_MembershipExtras_Hook_Pre_ContributionRecur($op, $id, $params);
$contributionRecurPreHook->preProcess();
}
if ($objectName === 'Contribution') {
$contributionPreHook = new CRM_MembershipExtras_Hook_Pre_Contribution($op, $id, $params);
$contributionPreHook->preProcess();
}
}
/**
* Implements hook_civicrm_post().
*/
function membershipextras_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if ($objectName === 'EntityFinancialTrxn') {
$entityFinancialTrxnHook = new CRM_MembershipExtras_Hook_Post_EntityFinancialTrxn($objectRef);
$entityFinancialTrxnHook->updatePaymentPlanStatus();
}
if ($objectName === 'ContributionRecur') {
$contributionRecurPostHook = new CRM_MembershipExtras_Hook_Post_ContributionRecur($objectRef);
$contributionRecurPostHook->postProcess();
}
if ($objectName == 'MembershipPayment') {
$membershipPaymentPostHook = new CRM_MembershipExtras_Hook_Post_MembershipPayment($op, $objectId, $objectRef);
$membershipPaymentPostHook->postProcess();
}
}
/**
* Implements hook_civicrm_postProcess().
*/
function membershipextras_civicrm_postProcess($formName, &$form) {
$isAddAction = $form->getAction() & CRM_Core_Action::ADD;
$isRenewAction = $form->getAction() & CRM_Core_Action::RENEW;
if (($formName === 'CRM_Member_Form_Membership' && $isAddAction) || ($formName === 'CRM_Member_Form_MembershipRenewal' && $isRenewAction)) {
$contributionIsPaymentPlan =
CRM_Utils_Request::retrieve('contribution_type_toggle', 'String') === 'payment_plan';
if (!$contributionIsPaymentPlan) {
return;
}
$operation = $isAddAction ? 'creation' : 'renewal';
$paymentPlanProcessor = new CRM_MembershipExtras_Hook_PostProcess_MembershipPaymentPlanProcessor($formName, $form, $operation);
$paymentPlanProcessor->postProcess();
if ($formName == 'CRM_Member_Form_Membership') {
$offlineAutoRenewProcessor = new CRM_MembershipExtras_Hook_PostProcess_MembershipOfflineAutoRenewProcessor($form);
$offlineAutoRenewProcessor->postProcess();
}
}
if ($formName === 'CRM_Contribute_Form_UpdateSubscription') {
$postProcessFormHook = new CRM_MembershipExtras_Hook_PostProcess_UpdateSubscription($form);
$postProcessFormHook->postProcess();
}
if ($formName === 'CRM_Member_Form_MembershipType') {
$membershipTypeHook = new CRM_MembershipExtras_Hook_PostProcess_UpdateMembershipTypeColour($form);
$membershipTypeHook->process();
$membershipTypeProRataCalculationHook = new CRM_MembershipExtras_Hook_PostProcess_MembershipTypeSetting($form);
$membershipTypeProRataCalculationHook->process();
}
}
/**
* Implements hook_civicrm_buildForm().
*/
function membershipextras_civicrm_buildForm($formName, &$form) {
if ($formName === 'CRM_Member_Form_Membership' && ($form->getAction() & CRM_Core_Action::UPDATE)) {
$membershipEdit = new CRM_MembershipExtras_Hook_BuildForm_MembershipEdit($form);
$membershipEdit->buildForm();
}
if (
($formName === 'CRM_Member_Form_Membership' && ($form->getAction() & CRM_Core_Action::ADD))
|| ($formName === 'CRM_Member_Form_MembershipRenewal' && ($form->getAction() & CRM_Core_Action::RENEW))
) {
$membershipHook = new CRM_MembershipExtras_Hook_BuildForm_MembershipPaymentPlan($form);
$membershipHook->buildForm();
}
if ($formName === 'CRM_Member_Form_MembershipStatus') {
$membershipStatusHook = new CRM_MembershipExtras_Hook_BuildForm_MembershipStatus();
$membershipStatusHook->buildForm($form);
}
if ($formName === 'CRM_Contribute_Form_UpdateSubscription') {
$updateFormHook = new CRM_MembershipExtras_Hook_BuildForm_UpdateSubscription($form);
$updateFormHook->buildForm();
}
if ($formName === 'CRM_Member_Form_MembershipType') {
$membershipTypeColourHook = new CRM_MembershipExtras_Hook_BuildForm_MembershipType_Colour($form);
$membershipTypeColourHook->buildForm();
$membershipSettingsHook = new CRM_MembershipExtras_Hook_BuildForm_MembershipType_Setting($form);
$membershipSettingsHook->buildForm();
}
if ($formName === 'CRM_Contribute_Form_Contribution') {
$contributionEditHook = new CRM_MembershipExtras_Hook_BuildForm_ContributionEdit();
$contributionEditHook->buildForm();
}
if ($formName == 'CRM_Contribute_Form_AdditionalPayment') {
Civi::$statics[E::LONG_NAME]['paymentType'] = $form->getVar('_paymentType');
}
if ($formName === 'CRM_Price_Form_Option' || $formName === 'CRM_Price_Form_Field') {
$priceFieldHook = new CRM_MembershipExtras_Hook_BuildForm_PriceOptionEdit($form);
$priceFieldHook->buildForm();
}
}
/**
* Implements hook_civicrm_pageRun().
*
* @link https://docs.civicrm.org/dev/en/master/hooks/hook_civicrm_pageRun/
*/
function membershipextras_civicrm_pageRun($page) {
$hooks = [
new CRM_MembershipExtras_Hook_PageRun_MembershipTypePageColourUpdate(),
new CRM_MembershipExtras_Hook_PageRun_MemberPageTabColourUpdate(),
new CRM_MembershipExtras_Hook_PageRun_MemberPageDashboardColourUpdate(),
new CRM_MembershipExtras_Hook_PageRun_MemberPageTab(),
];
foreach ($hooks as $hook) {
$hook->handle($page);
}
if (get_class($page) === 'CRM_MembershipExtras_Page_EditContributionRecurLineItems') {
CRM_Core_Resources::singleton()->addStyleFile(
CRM_MembershipExtras_ExtensionUtil::LONG_NAME,
'css/style.css',
1
);
CRM_Core_Resources::singleton()->addScriptFile(
CRM_MembershipExtras_ExtensionUtil::LONG_NAME,
'js/CurrentPeriodLineItemHandler.js',
1,
'page-header'
)->addScriptFile(
CRM_MembershipExtras_ExtensionUtil::LONG_NAME,
'js/NextPeriodLineItemHandler.js',
1,
'page-header'
);
}
if (get_class($page) === 'CRM_Member_Page_Tab' && CRM_Utils_System::currentPath() === 'civicrm/contact/view/membership') {
CRM_Core_Resources::singleton()->addStyleFile(
CRM_MembershipExtras_ExtensionUtil::LONG_NAME,
'css/membershipView.css',
1
);
}
if ($page instanceof CRM_Contribute_Page_ContributionRecur) {
$recurViewPage = new CRM_MembershipExtras_Hook_PageRun_ContributionRecurViewPage();
$recurViewPage->handle($page);
}
if ($page instanceof CRM_Contribute_Page_Tab || $page instanceof CRM_Member_Page_RecurringContributions) {
$hook = new CRM_MembershipExtras_Hook_PageRun_ContributionTab();
$hook->handle($page);
}
}
/**
* Implements hook_civicrm_validateForm().
*/
function membershipextras_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName === 'CRM_Member_Form_MembershipType') {
$membershipType = new CRM_MembershipExtras_Hook_ValidateForm_MembershipType($fields, $errors);
$membershipType->validate();
}
$formAction = $form->getAction();
$isNewMembershipForm = ($formName === 'CRM_Member_Form_Membership' && ($formAction & CRM_Core_Action::ADD));
$isPaymentPlanPayment = CRM_MembershipExtras_Helper_InstalmentSchedule::isPaymentPlanPayment();
if ($isNewMembershipForm && $isPaymentPlanPayment) {
$paymentPlanValidateHook = new CRM_MembershipExtras_Hook_ValidateForm_MembershipPaymentPlan($form, $fields, $errors);
$paymentPlanValidateHook->validate();
}
$isMembershipUpdateForm = $formName === 'CRM_Member_Form_Membership' && ($formAction & CRM_Core_Action::UPDATE);
if ($isMembershipUpdateForm) {
$membershipUpdateValidationHook = new CRM_MembershipExtras_Hook_ValidateForm_MembershipUpdate($form, $fields, $errors);
$membershipUpdateValidationHook->validate();
}
$isUpdateSubcriptionForm = $formName === 'CRM_Contribute_Form_UpdateSubscription' && ($formAction & CRM_Core_Action::UPDATE);
if ($isUpdateSubcriptionForm) {
$subscriptionUpdateValidationHook = new CRM_MembershipExtras_Hook_ValidateForm_UpdateSubscription($form, $fields, $errors);
$subscriptionUpdateValidationHook->validate();
}
}
/**
* Implements hook_civicrm_alterCalculatedMembershipStatus().
*/
function membershipextras_civicrm_alterCalculatedMembershipStatus(&$calculatedStatus, $arguments, $membership) {
$alterMembershipStatusHook = new CRM_MembershipExtras_Hook_Alter_CalculatedMembershipStatus();
$alterMembershipStatusHook->alterMembershipStatus($calculatedStatus, $arguments, $membership);
}
/**
* Implements hook_civicrm_links().
*/
function membershipextras_civicrm_links($op, $objectName, $objectId, &$links, &$mask, &$values) {
if ($op == 'contribution.selector.recurring' && $objectName == 'Contribution') {
$recurContribuLinksHook = new CRM_MembershipExtras_Hook_Links_RecurringContribution($objectId, $links, $mask);
$recurContribuLinksHook->alterLinks();
}
if ($op == 'contribution.selector.row' && $objectName == 'Contribution') {
if (CRM_Core_Permission::check('edit contributions')) {
$links[] = [
'name' => 'Duplicate As New Pending Contribution',
'url' => 'civicrm/contribution/duplicate-contribution',
'qs' => 'reset=1&crid=%%id%%',
'title' => 'Duplicate As New Pending Contribution',
];
}
}
// hide memberships 'Cancel Auto-renewal` action.
if (in_array($op, ['membership.tab.row', 'membership.selector.row']) && $objectName == 'Membership') {
$cancelAutorenewalActionName = ts('Cancel Auto-renewal');
$cancelAutoRenewActionIndex = array_search($cancelAutorenewalActionName, array_column($links, 'name'));
if ($cancelAutoRenewActionIndex !== FALSE) {
unset($links[$cancelAutoRenewActionIndex]);
}
}
}
/**
* Implements hook_civicrm_alterContent().
*/
function membershipextras_civicrm_alterContent(&$content, $context, $tplName, &$object) {
if ($tplName == 'CRM/Member/Page/Tab.tpl') {
$memberTabPage = new CRM_MembershipExtras_Hook_AlterContent_MemberTabPage($content);
$memberTabPage->alterContent();
}
}
function membershipextras_civicrm_preProcess($formName, $form) {
if ($formName === 'CRM_Contribute_Form_ContributionView') {
$preProcessor = new CRM_MembershipExtras_Hook_PreProcess_ContributionView($form);
$preProcessor->preProcess();
}
}
/**
* Implements alterMailParams hook().
*
* @param array $params
* @param $context
*/
function membershipextras_civicrm_alterMailParams(&$params, $context) {
$alterMailParamsHook = new CRM_MembershipExtras_Hook_Alter_MailParamsHandler($params);
$alterMailParamsHook->handle();
}
/**
* Implements hook_civicrm_permission().
*
*/
function membershipextras_civicrm_permission(&$permissions) {
$permissions += [
'administer MembershipExtras' => [
E::ts('MembershipExtras: administer Membership Extras'),
E::ts('Perform all Membership Extras administration tasks in CiviCRM'),
],
];
}
/**
* Implements hook_civicrm_alterAPIPermissions().
*
*/
function membershipextras_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
// PaymentSchedule API end-point actions are not dangerous, so
// no need to check permissions.
if ($entity == 'payment_schedule') {
$params['check_permissions'] = FALSE;
}
if ($entity == 'contribution_recur_line_item') {
$permissions['contribution_recur_line_item'][$action] = ['edit contributions'];
}
// PriceFieldValue API automatically does a join on PrieField
// API, in which Civi does a permission check again without the context
// parameter being passed down,thus resulting in permission failure for any
// PriceFieldValue API call,thus we here store it in a static variable
// to make sure it is clear that this extra API join is actually coming from
// Membershipextras context.
static $isMembershipextrasContext = FALSE;
if (!empty($params['context']) && $params['context'] == 'Membershipextras') {
$isMembershipextrasContext = TRUE;
}
// These are the APIs that we call from the Manage installments page JS file, except
// 'PriceField' which is called from an automatic join that Civi does when you call 'PriceFieldValue' API.
// These entities have no default permissions, thus Civi forces 'administer CiviCRM' permission on them if called
// using AJAX, we here reduce their permissions to 'access CiviContribute' if they are called from Membershipextras context.
// 'access CiviContribute' is used instead of `edit contributions' because the actions we perform on them from the manage installment
// page are just 'get' operations, and not 'create' or 'delete'.
$isManageInstallmentAPIs = in_array($entity, ['entity_financial_account', 'financial_account', 'financial_type', 'price_field', 'price_field_value']);
$isManageInstallmentAPIsAllowedActions = in_array($action, ['get', 'getsingle']);
if ($isMembershipextrasContext && $isManageInstallmentAPIs && $isManageInstallmentAPIsAllowedActions) {
$permissions[$entity][$action] = ['access CiviContribute'];
}
}
/**
* Implements fieldOptions hook().
*
* @param string $entity
* @param string $field
* @param array $options
* @param array $params
*/
function membershipextras_civicrm_fieldOptions($entity, $field, &$options, $params) {
if (in_array($entity, ['FinancialItem', 'LineItem']) && $field == 'entity_table') {
$options['civicrm_contribution_recur'] = ts('Recurring Contribution');
}
}