Merge pull request #23213 from eileenmcnaughton/post
[civicrm-core.git] / CRM / Event / 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\Participant;
19
20 /**
21 * Class for event form task actions.
22 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
23 */
24 class CRM_Event_Form_Task extends CRM_Core_Form_Task {
25
26 /**
27 * The array that holds all the participant ids.
28 *
29 * @var array
30 */
31 protected $_participantIds;
32
33 /**
34 * Rows to act on.
35 *
36 * Each row will have a participant ID & a contact ID using
37 * the keys the token processor expects.
38 *
39 * @var array
40 */
41 protected $rows = [];
42
43 /**
44 * Build all the data structures needed to build the form.
45 *
46 * @param
47 *
48 * @return void
49 */
50 public function preProcess() {
51 self::preProcessCommon($this);
52 }
53
54 /**
55 * @param CRM_Core_Form_Task $form
56 */
57 public static function preProcessCommon(&$form) {
58 $form->_participantIds = [];
59
60 $values = $form->getSearchFormValues();
61
62 $form->_task = $values['task'];
63 $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
64 if (!array_key_exists($form->_task, $tasks)) {
65 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
66 }
67
68 $ids = $form->getSelectedIDs($values);
69
70 if (!$ids) {
71 $queryParams = $form->get('queryParams');
72 $sortOrder = NULL;
73 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
74 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
75 }
76
77 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
78 CRM_Contact_BAO_Query::MODE_EVENT
79 );
80 $query->_distinctComponentClause = "civicrm_participant.id";
81 $query->_groupByComponentClause = " GROUP BY civicrm_participant.id ";
82 $result = $query->searchQuery(0, 0, $sortOrder);
83 while ($result->fetch()) {
84 $ids[] = $result->participant_id;
85 }
86 }
87
88 if (!empty($ids)) {
89 $form->_componentClause = ' civicrm_participant.id IN ( ' . implode(',', $ids) . ' ) ';
90 $form->assign('totalSelectedParticipants', count($ids));
91 }
92
93 $form->_participantIds = $form->_componentIds = $ids;
94
95 $form->setNextUrl('event');
96 }
97
98 /**
99 * Get the participant IDs.
100 *
101 * @return array
102 */
103 public function getIDs(): array {
104 return $this->_participantIds;
105 }
106
107 /**
108 * Given the participant id, compute the contact id
109 * since its used for things like send email
110 */
111 public function setContactIDs(): void {
112 $this->_contactIds = $this->getContactIDs();
113 }
114
115 /**
116 * Get the relevant contact IDs.
117 *
118 * @return array
119 */
120 protected function getContactIDs(): array {
121 if (isset($this->_contactIds)) {
122 return $this->_contactIds;
123 }
124 foreach ($this->getRows() as $row) {
125 $this->_contactIds[] = $row['contact_id'];
126 }
127 return $this->_contactIds;
128 }
129
130 /**
131 * Simple shell that derived classes can call to add buttons to.
132 * the form with a customized title for the main Submit
133 *
134 * @param string $title
135 * Title of the main button.
136 * @param string $nextType
137 * @param string $backType
138 * @param bool $submitOnce
139 *
140 * @return void
141 */
142 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
143 $this->addButtons([
144 [
145 'type' => $nextType,
146 'name' => $title,
147 'isDefault' => TRUE,
148 ],
149 [
150 'type' => $backType,
151 'name' => ts('Cancel'),
152 ],
153 ]);
154 }
155
156 /**
157 * Get the rows form the search, keyed to make the token processor happy.
158 *
159 * @throws \API_Exception
160 */
161 protected function getRows(): array {
162 if (empty($this->rows)) {
163 // checkPermissions set to false - in case form is bypassing in some way.
164 $participants = Participant::get(FALSE)
165 ->addWhere('id', 'IN', $this->getIDs())
166 ->setSelect(['id', 'contact_id'])->execute();
167 foreach ($participants as $participant) {
168 $this->rows[] = [
169 'contact_id' => $participant['contact_id'],
170 'participant_id' => $participant['id'],
171 'schema' => [
172 'contactId' => $participant['contact_id'],
173 'participantId' => $participant['id'],
174 ],
175 ];
176 }
177 }
178 return $this->rows;
179 }
180
181 /**
182 * Get the token processor schema required to list any tokens for this task.
183 *
184 * @return array
185 */
186 public function getTokenSchema(): array {
187 return ['participantId', 'contactId', 'eventId'];
188 }
189
190 }