-
Notifications
You must be signed in to change notification settings - Fork 1
/
membershipperiodtracker.php
347 lines (268 loc) · 12.2 KB
/
membershipperiodtracker.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
<?php
require_once 'membershipperiodtracker.civix.php';
use CRM_Membershipperiodtracker_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function membershipperiodtracker_civicrm_config(&$config) {
_membershipperiodtracker_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function membershipperiodtracker_civicrm_xmlMenu(&$files) {
_membershipperiodtracker_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function membershipperiodtracker_civicrm_install() {
_membershipperiodtracker_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function membershipperiodtracker_civicrm_postInstall() {
_membershipperiodtracker_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function membershipperiodtracker_civicrm_uninstall() {
_membershipperiodtracker_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function membershipperiodtracker_civicrm_enable() {
_membershipperiodtracker_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function membershipperiodtracker_civicrm_disable() {
_membershipperiodtracker_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function membershipperiodtracker_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _membershipperiodtracker_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function membershipperiodtracker_civicrm_managed(&$entities) {
_membershipperiodtracker_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types.
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function membershipperiodtracker_civicrm_caseTypes(&$caseTypes) {
_membershipperiodtracker_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
*/
function membershipperiodtracker_civicrm_angularModules(&$angularModules) {
_membershipperiodtracker_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function membershipperiodtracker_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_membershipperiodtracker_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implementation hook_civicrm_post()
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_post
*
* in this hook, we are tracking/recording Membership creation/renewal Periods and/or Contribution.
*
* @since 1.0
* @throws CiviCRM_API3_Exception
*/
function membershipperiodtracker_civicrm_post($op, $objectName, $objectId, &$objectRef) {
// IDE Help
/** @var array( [id], [start_date], [end_date], [contact_id], [membership_id], [membership_type_id], [contribution_id] ) $params */
/********************************* Saving Membership create/renewal/extending date History *******************************/
// When user created/renewed Membership(Entity) but not yet saved Contribution
if ( $objectName == 'Membership' ) {
/** @var CRM_Member_DAO_Membership $objectRef */
// Parameters for Membership Period.
$params = array();
$params['end_date'] = $objectRef->end_date;
$params['contact_id'] = $objectRef->contact_id;
$params['membership_id'] = $objectId;
$params['membership_type_id'] = $objectRef->membership_type_id;
$params['contribution_id'] = null; // Contribution not yet saved so can't get the Contribution Id here. look at: *** Update Contribution ID ***
if ( $op == 'create' ) {
$params['start_date'] = $objectRef->start_date;
CRM_MembershipPeriodTracker_BAO_MembershipPeriod::create( $params );
} elseif ( $op == 'edit' ) {
try {
// We are getting Membership Log so we can crack out start_date if the Membership was edited for renewal/or extending date.
$membershipLogResult = civicrm_api3('MembershipLog', 'get', array(
'sequential' => 1,
'membership_id' => $objectId,
'end_date' => $objectRef->end_date, // Make sure right Log get selected by means we will only get one item/Log
) );
$membershipLogCount = $membershipLogResult['count'];
} catch (CiviCRM_API3_Exception $e) {
return;
}
// This Log will give me the Last changes was made, so i can get the start date of the Membership($objectRef->membership_id) renewal
$lastMembershipLog = $membershipLogResult['values'][$membershipLogCount - 1];
$params['start_date'] = $lastMembershipLog['start_date'];
// Prevent duplicate date populating // Note: We are not going to prevent extending date by using Editing features (if its needed u can ask me)
// Here by meaning duplicate is: When user edited Membership field except/but Membership field that are associated(Foreign Key) with our Table(civicrm_membership_period).
try {
$duplicatePeriodCount = civicrm_api3( 'MembershipPeriod', 'getcount', $params );
} catch (CiviCRM_API3_Exception $e) {
$duplicatePeriodCount = 1; // Don't wanna take risk!
}
if ( $duplicatePeriodCount == 0 ) { // Populate only when there is no same Period with same(as now) data.
CRM_MembershipPeriodTracker_BAO_MembershipPeriod::create( $params );
}
}
}
/************************ Update Contribution ID ***************************/
// When user create or renew Membership(Entity) with checked `Record Membership Payment?`
// It will run when Membership created/renewed and saved all data
if ( $objectName == 'MembershipPayment' && $op == 'create' ) {
/** @var CRM_Member_DAO_MembershipPayment $objectRef */
// there is no way to be this Membership(from Membership Payment Obj ref) empty or does not exist!!!
$membershipParams = array(
'id' => $objectRef->membership_id,
);
try {
// Payment was made for this Membership($objectRef->membership_id)
$membership = civicrm_api3( 'Membership', 'get', $membershipParams )['values'][$objectRef->membership_id];
// Get Membership Log for cracking start_date and end_date
$membershipLogResult = civicrm_api3( 'MembershipLog', 'get', array(
'sequential' => 1,
'membership_id' => $objectRef->membership_id,
'membership_type_id' => $membership['membership_type_id'],
'options' => array( 'sort' => 'start_date desc', 'limit' => 1 ), // Not using end_date because end_date can be null when Membership type is Lifetime
) );
} catch (CiviCRM_API3_Exception $e) {
return;
}
// This Log will give me the Last changes was made in the Membership, so i can get the start and end date of the Membership($objectRef->membership_id) renewal
$lastMembershipLog = $membershipLogResult['values'][0]; // Result is sequential array
try {
// We are gonna update our last added Period with added Contribution ID
// So thats why i am getting the last added Period ID using nearly all data except Contribution Id by doing this it will return only one result/id, look at getvalue Parameters to understand
$lastAddedPeriodId = civicrm_api3( 'MembershipPeriod', 'getvalue', array(
'return' => "id",
'start_date' => $lastMembershipLog['start_date'],
'end_date' => $lastMembershipLog['end_date'],
'contact_id' => $membership['contact_id'],
'membership_id' => $objectRef->membership_id,
'membership_type_id' => $membership['membership_type_id'],
) );
} catch (CiviCRM_API3_Exception $e) {
return; // Get out of here!
}
// Parameters for Membership Period.
$params = array();
// Collection Membership Period data
$params['id'] = $lastAddedPeriodId;
$params['start_date'] = $lastMembershipLog['start_date'];
$params['end_date'] = $lastMembershipLog['end_date'];
$params['contact_id'] = $membership['contact_id'];
$params['membership_id'] = $objectRef->membership_id;
$params['membership_type_id'] = $membership['membership_type_id'];
$params['contribution_id'] = $objectRef->contribution_id;
// It will update WHERE $params['id'] eq $lastAddedPeriodId
CRM_MembershipPeriodTracker_BAO_MembershipPeriod::create( $params );
}
}
// TODO add backward compatibility for tabset using tabs
/**
* Implementation hook_civicrm_tabset()
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_tabset
*
* in this hook, added our own Tab called Membership Period History in Contact
*
* @since 1.0
* @throws CiviCRM_API3_Exception
*/
function membershipperiodtracker_civicrm_tabset($tabsetName, &$tabs, $context) {
// When Contact View TabSet we are going to add our own Tab called Membership Period History
if ( $tabsetName == 'civicrm/contact/view' && isset( $context['contact_id'] ) ){
$contactId = $context['contact_id'];
$url = CRM_Utils_System::url( 'civicrm/contact/view/membershipperiodhistory',
"reset=1&force=1&cid=$contactId" );
$periodHistoryCount = civicrm_api3('MembershipPeriod', 'getcount', array( 'contact_id' => $contactId ) );
$tab = array(
'id' => 'membership_period_history',
'title' => 'Membership Period History',
'url' => $url,
'valid' => true,
'active' => true,
'class' => 'livePage',
'current' => false,
'weight' => 31, // After Membership
'count' => $periodHistoryCount
);
$tabs[] = $tab;
}
}
function membershipperiodtracker_civicrm_pageRun(&$page) {
/** @var CRM_Core_Page $page */
$pageName = $page->getVar('_name');
// Add our Membership Period History to Membership Tab Page(CRM/Member/Page/Tab.php) Ajax Response Update tabs ajaxResponse[updateTabs]
// So our tab get updated automatically when Memberships Tab
if ($pageName == 'CRM_Member_Page_Tab') {
/** @var CRM_Member_Page_Tab $page */
$page->ajaxResponse['updateTabs']['#tab_membership_period_history'] = civicrm_api3( 'MembershipPeriod', 'getcount', array( 'contact_id' => $page->_contactId ) );
}
}
/**
* Setup All mine Entity
* Implements hook_civicrm_entityTypes().
*/
function membershipperiodtracker_civicrm_entityTypes(&$entityTypes) {
$entityTypes[] = array (
'name' => 'MembershipPeriod',
'class' => 'CRM_MembershipPeriodTracker_DAO_MembershipPeriod',
'table' => 'civicrm_membership_period',
);
}