-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
membershiputils.php
246 lines (226 loc) · 8.31 KB
/
membershiputils.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
<?php
require_once 'membershiputils.civix.php';
// phpcs:disable
use Civi\Api4\MembershipStatus;
use CRM_Membershiputils_ExtensionUtil as E;
use Symfony\Component\DependencyInjection\{ContainerBuilder, Definition};
// phpcs:enable
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function membershiputils_civicrm_config(&$config) {
_membershiputils_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_container().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_container/
*/
function membershiputils_civicrm_container(ContainerBuilder $container) {
$resource_hooks = new Definition('CRM_Membershiputils_ResourceHooks');
$resource_hooks->addTag('kernel.event_subscriber');
$container->setDefinition('membershiputils_resource_hooks', $resource_hooks);
$process_hooks = new Definition('CRM_Membershiputils_ProcessHooks');
$process_hooks->addTag('kernel.event_subscriber');
$container->setDefinition('membershiputils_process_hooks', $process_hooks);
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function membershiputils_civicrm_install() {
_membershiputils_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function membershiputils_civicrm_enable() {
_membershiputils_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed
*/
function membershiputils_civicrm_managed(&$entities) {
$entities[] = [
'module' => 'au.com.agileware.membershiputils',
'name' => 'duplicate',
'entity' => 'MembershipStatus',
'update' => 'never',
'cleanup' => 'never',
'params' => [
'version' => 3,
'name' => 'Duplicate',
'label' => 'Duplicate',
'is_current_member' => 0,
'is_active' => 1,
'is_admin' => 1,
'is_default' => 0,
'is_reserved' => 0,
'weight' => 999,
],
];
$entities[] = [
'module' => 'au.com.agileware.membershiputils',
'name' => 'notrenewing',
'entity' => 'MembershipStatus',
'update' => 'never',
'cleanup' => 'never',
'params' => [
'version' => 3,
'name' => 'Not Renewing',
'label' => 'Not Renewing',
'is_current_member' => 0,
'is_active' => 1,
'is_admin' => 1,
'is_default' => 0,
'is_reserved' => 0,
'weight' => 999,
],
];
$entities[] = [
'module' => 'au.com.agileware.membershiputils',
'name' => 'findduplicatememberships',
'entity' => 'Job',
'update' => 'always',
'params' => [
'version' => 3,
'name' => 'Find Duplicate Memberships',
'description' => 'Find duplicate memberships and set their Membership Status to Duplicate.',
'api_entity' => 'membershiputils',
'api_action' => 'Findduplicatememberships',
'parameters' => '',
'run_frequency' => 'Always',
'is_active' => 0,
],
];
$entities[] = [
'module' => 'au.com.agileware.membershiputils',
'name' => 'adjustmembershipenddate',
'entity' => 'Job',
'update' => 'always',
'params' => [
'version' => 3,
'name' => 'Set Membership End Date to end of the month',
'description' => 'Set the Membership End Date to the end of month. Applies to New, Current and Grace memberships only.',
'api_entity' => 'membershiputils',
'api_action' => 'Adjustmembershipenddate',
'parameters' => '',
'run_frequency' => 'Daily',
'is_active' => 0,
],
];
$entities[] = [
'module' => 'au.com.agileware.membershiputils',
'name' => 'specificmembershipenddate',
'entity' => 'Job',
'update' => 'always',
'params' => [
'version' => 3,
'name' => 'Set Membership End Date to a specific date',
'description' => 'Set Membership End Date to a specific date. Applies to New, Current and Grace memberships only.',
'api_entity' => 'membershiputils',
'api_action' => 'Specificmembershipenddate',
'parameters' => '',
'run_frequency' => 'Daily',
'is_active' => 0,
],
];
}
/**
* Adjust the end date to end of the month
*/
function membershiputils_adjustmembershipenddate($end_date) {
// Bizarrely, CiviCRM may pass in end_date in either of these two formats
if (strpos($end_date, '-') == FALSE) {
$date_format = 'Ymd';
// Return only the date part of a MySQL datetime value
$end_date = substr($end_date, 0, 8);
}
else {
$date_format = 'Y-m-d';
}
// Use the current end date as default
$new_end_date = date_create_from_format($date_format, $end_date);
// Adjust the end date to the last day of the month
if (Civi::settings()->get('adjust_membership_end_date')) {
$end_date = date_create_from_format($date_format, $end_date);
$start_month = date_create($end_date->format('Y-m') . '-01');
$new_end_date = date_modify($start_month, '+1 month -1 day');
}
elseif (Civi::settings()->get('use_specific_membership_end_date')) {
// Otherwise set a specific end date
$new_end_date = date_create_from_format('Y-m-d', Civi::settings()
->get('specific_membership_end_date'));
}
// Return the date in the same format it was received
return $new_end_date->format($date_format);
}
// @TODO Move these hooks to CRM_Membershiputils_AdjustmentHooks
function membershiputils_civicrm_pre($op, $objectName, $id, &$params) {
static $membershipStatuses = NULL;
// If a Membership is being created or edited and the end date has been set then adjust
// Skip when 'null' is set as the end_date as this indicates a Lifetime membership term with on end date
if('Membership' !== $objectName
|| ('edit' != $op && 'create' != $op)
|| empty($params['end_date']) || $params['end_date'] === 'null'
) {
return;
}
$membershipStatuses ??= MembershipStatus::get(FALSE)
->addSelect('id', 'name')
->execute()
->indexBy('id')
->getArrayCopy();
$currentStatus = $membershipStatuses[$params['status_id']]['name'];
// @TODO Should this just check if the membership status is an active type?
// Adjust membership end date if new, current, grace
if (Civi::settings()->get('adjust_membership_end_date')
&& ($currentStatus == 'New' || $currentStatus == 'Current' || $currentStatus == 'Grace')) {
$params['end_date'] = membershiputils_adjustmembershipenddate($params['end_date']);
}
// set specific membership end date if new or current
elseif (Civi::settings()->get('use_specific_membership_end_date')
&& ($currentStatus == 'New' || $currentStatus == 'Current')) {
$params['end_date'] = membershiputils_adjustmembershipenddate($params['end_date']);
}
}
function membershiputils_civicrm_post($op, $objectName, $id, &$params) {
// This function is required to work around weirdness in CiviCRM which sometimes does not set the membership dates, they may be NULL
if ('Membership' == $objectName && 'create' == $op) {
if (Civi::settings()->get('adjust_membership_end_date') || Civi::settings()->get('use_specific_membership_end_date')) {
// If end date has not been set then try to calculate it now
if (!$params->end_date) {
$dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($params->membership_type_id, NULL, NULL, NULL);
CRM_Core_DAO::setFieldValue('CRM_Member_DAO_Membership', $id, 'join_date', $dates['join_date'], 'id');
CRM_Core_DAO::setFieldValue('CRM_Member_DAO_Membership', $id, 'start_date', $dates['start_date'], 'id');
CRM_Core_DAO::setFieldValue('CRM_Member_DAO_Membership', $id, 'end_date', $dates['end_date'], 'id');
}
}
}
}
/**
* Implements hook_civicrm_navigationMenu().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
*/
function membershiputils_civicrm_navigationMenu(&$menu) {
_membershiputils_civix_insert_navigation_menu($menu, 'Administer', [
'label' => E::ts('Membership Utils Settings'),
'name' => 'membershiputils_settings',
'url' => 'civicrm/admin/setting/membershiputils',
'permission' => 'administer CiviCRM',
'operator' => 'OR',
'separator' => 0,
]);
_membershiputils_civix_navigationMenu($menu);
}