Merge pull request #18891 from civicrm/5.31
[civicrm-core.git] / ext / contributioncancelactions / contributioncancelactions.php
1 <?php
2
3 require_once 'contributioncancelactions.civix.php';
4 // phpcs:disable
5 use CRM_Contributioncancelactions_ExtensionUtil as E;
6 // phpcs:enable
7 use Civi\Api4\LineItem;
8
9 /**
10 * Implements hook_civicrm_preProcess().
11 *
12 * This enacts the following
13 * - find and cancel any related pending memberships
14 * - (not yet implemented) find and cancel any related pending participant records
15 * - (not yet implemented) find any related pledge payment records. Remove the contribution id.
16 *
17 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_post
18 */
19 function contributioncancelactions_civicrm_post($op, $objectName, $objectId, $objectRef) {
20 if ($op === 'edit' && $objectName === 'Contribution') {
21 if ('Cancelled' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $objectRef->contribution_status_id)) {
22 // Find and cancel any pending memberships.
23 $connectedMemberships = (array) LineItem::get(FALSE)->setWhere([
24 ['contribution_id', '=', $objectId],
25 ['entity_table', '=', 'civicrm_membership'],
26 ])->execute()->indexBy('entity_id');
27 if (empty($connectedMemberships)) {
28 return;
29 }
30 // @todo we don't have v4 membership api yet so v3 for now.
31 $connectedMemberships = array_keys(civicrm_api3('Membership', 'get', [
32 'status_id' => 'Pending',
33 'id' => ['IN' => array_keys($connectedMemberships)],
34 ])['values']);
35 if (empty($connectedMemberships)) {
36 return;
37 }
38 foreach ($connectedMemberships as $membershipID) {
39 civicrm_api3('Membership', 'create', ['status_id' => 'Cancelled', 'id' => $membershipID, 'is_override' => 1]);
40 }
41 }
42 }
43 }