Merge pull request #23080 from eileenmcnaughton/isset
[civicrm-core.git] / CRM / Member / Form / Task.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 use Civi\Api4\Membership;
19
20 /**
21 * Class for member form task actions.
22 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
23 */
24 class CRM_Member_Form_Task extends CRM_Core_Form_Task {
25
26 /**
27 * The array that holds all the member ids.
28 *
29 * @var array
30 */
31 protected $_memberIds;
32
33 /**
34 * Build all the data structures needed to build the form.
35 *
36 * @param
37 *
38 * @return void
39 * @throws \CRM_Core_Exception
40 */
41 public function preProcess() {
42 self::preProcessCommon($this);
43 }
44
45 /**
46 * @param \CRM_Core_Form_Task $form
47 *
48 * @throws \CRM_Core_Exception
49 */
50 public static function preProcessCommon(&$form) {
51 $form->_memberIds = [];
52
53 $values = $form->getSearchFormValues();
54
55 $form->_task = $values['task'];
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 }
60
61 $ids = $form->getSelectedIDs($values);
62
63 if (!$ids) {
64 $queryParams = $form->get('queryParams');
65 $sortOrder = NULL;
66 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
67 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
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;
87 $form->setNextUrl('member');
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() {
95 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_memberIds,
96 'civicrm_membership'
97 );
98 }
99
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
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
141 }