Merge pull request #23893 from eileenmcnaughton/user_two
[civicrm-core.git] / CRM / Member / Form / Task.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
4f036b71
EM
18use Civi\Api4\Membership;
19
6a488035 20/**
31aaf096
MW
21 * Class for member form task actions.
22 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
6a488035 23 */
31aaf096 24class CRM_Member_Form_Task extends CRM_Core_Form_Task {
6a488035
TO
25
26 /**
fe482240 27 * The array that holds all the member ids.
6a488035
TO
28 *
29 * @var array
30 */
31 protected $_memberIds;
32
33 /**
fe482240 34 * Build all the data structures needed to build the form.
6a488035
TO
35 *
36 * @param
37 *
38 * @return void
7da29bae 39 * @throws \CRM_Core_Exception
6a488035 40 */
00be9182 41 public function preProcess() {
6a488035
TO
42 self::preProcessCommon($this);
43 }
44
bb3a214a 45 /**
2d09a0c3 46 * @param \CRM_Core_Form_Task $form
7da29bae 47 *
48 * @throws \CRM_Core_Exception
bb3a214a 49 */
2b089ce1 50 public static function preProcessCommon(&$form) {
be2fb01f 51 $form->_memberIds = [];
6a488035 52
2d09a0c3 53 $values = $form->getSearchFormValues();
6a488035
TO
54
55 $form->_task = $values['task'];
e335ab3b
MW
56 $tasks = CRM_Member_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
57 if (!array_key_exists($form->_task, $tasks)) {
58 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
59 }
6a488035 60
56752ec0 61 $ids = $form->getSelectedIDs($values);
62
63 if (!$ids) {
6a488035 64 $queryParams = $form->get('queryParams');
b09fe5ed 65 $sortOrder = NULL;
353ffa53 66 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
481a74f4 67 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
6a488035
TO
68 }
69 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
70 CRM_Contact_BAO_Query::MODE_MEMBER
71 );
72 $query->_distinctComponentClause = ' civicrm_membership.id';
73 $query->_groupByComponentClause = ' GROUP BY civicrm_membership.id ';
74 $result = $query->searchQuery(0, 0, $sortOrder);
75
76 while ($result->fetch()) {
77 $ids[] = $result->membership_id;
78 }
79 }
80
81 if (!empty($ids)) {
82 $form->_componentClause = ' civicrm_membership.id IN ( ' . implode(',', $ids) . ' ) ';
83 $form->assign('totalSelectedMembers', count($ids));
84 }
85
86 $form->_memberIds = $form->_componentIds = $ids;
188bf7c3 87 $form->setNextUrl('member');
6a488035
TO
88 }
89
90 /**
91 * Given the membership id, compute the contact id
92 * since its used for things like send email
93 */
94 public function setContactIDs() {
41d0a2e7 95 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_memberIds,
6a488035
TO
96 'civicrm_membership'
97 );
98 }
99
4f036b71
EM
100 /**
101 * @return array
102 */
103 protected function getIDS() {
104 return $this->_memberIds;
105 }
106
107 /**
108 * Get the rows form the search, keyed to make the token processor happy.
109 *
110 * @throws \API_Exception
111 */
112 protected function getRows(): array {
113 if (empty($this->rows)) {
114 // checkPermissions set to false - in case form is bypassing in some way.
115 $memberships = Membership::get(FALSE)
116 ->addWhere('id', 'IN', $this->getIDs())
117 ->setSelect(['id', 'contact_id'])->execute();
118 foreach ($memberships as $membership) {
119 $this->rows[] = [
120 'contact_id' => $membership['contact_id'],
121 'membership_id' => $membership['id'],
122 'schema' => [
123 'contactId' => $membership['contact_id'],
124 'membershipId' => $membership['id'],
125 ],
126 ];
127 }
128 }
129 return $this->rows;
130 }
131
601c941f
EM
132 /**
133 * Get the token processor schema required to list any tokens for this task.
134 *
135 * @return array
136 */
137 public function getTokenSchema(): array {
138 return ['membershipId', 'contactId'];
139 }
140
6a488035 141}