Commit | Line | Data |
---|---|---|
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 | ||
c32592e1 EM |
18 | use Civi\Api4\Participant; |
19 | ||
6a488035 | 20 | /** |
31aaf096 MW |
21 | * Class for event form task actions. |
22 | * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions. | |
6a488035 | 23 | */ |
31aaf096 | 24 | class CRM_Event_Form_Task extends CRM_Core_Form_Task { |
6a488035 TO |
25 | |
26 | /** | |
66f9e52b | 27 | * The array that holds all the participant ids. |
6a488035 TO |
28 | * |
29 | * @var array | |
30 | */ | |
31 | protected $_participantIds; | |
32 | ||
c32592e1 EM |
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 | ||
6a488035 | 43 | /** |
66f9e52b | 44 | * Build all the data structures needed to build the form. |
6a488035 TO |
45 | * |
46 | * @param | |
47 | * | |
48 | * @return void | |
6a488035 | 49 | */ |
00be9182 | 50 | public function preProcess() { |
6a488035 TO |
51 | self::preProcessCommon($this); |
52 | } | |
53 | ||
0cf587a7 | 54 | /** |
2d09a0c3 | 55 | * @param CRM_Core_Form_Task $form |
0cf587a7 | 56 | */ |
2b089ce1 | 57 | public static function preProcessCommon(&$form) { |
be2fb01f | 58 | $form->_participantIds = []; |
6a488035 | 59 | |
2d09a0c3 | 60 | $values = $form->getSearchFormValues(); |
6a488035 TO |
61 | |
62 | $form->_task = $values['task']; | |
8fd26836 MW |
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 | } | |
6a488035 | 67 | |
56752ec0 | 68 | $ids = $form->getSelectedIDs($values); |
69 | ||
70 | if (!$ids) { | |
6a488035 TO |
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 | ||
188bf7c3 | 95 | $form->setNextUrl('event'); |
6a488035 TO |
96 | } |
97 | ||
c32592e1 EM |
98 | /** |
99 | * Get the participant IDs. | |
100 | * | |
101 | * @return array | |
102 | */ | |
103 | public function getIDs(): array { | |
104 | return $this->_participantIds; | |
105 | } | |
106 | ||
6a488035 TO |
107 | /** |
108 | * Given the participant id, compute the contact id | |
109 | * since its used for things like send email | |
110 | */ | |
c32592e1 EM |
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) { | |
f34aa732 | 125 | $this->_contactIds[] = $row['contact_id']; |
c32592e1 EM |
126 | } |
127 | return $this->_contactIds; | |
6a488035 TO |
128 | } |
129 | ||
130 | /** | |
66f9e52b | 131 | * Simple shell that derived classes can call to add buttons to. |
6a488035 TO |
132 | * the form with a customized title for the main Submit |
133 | * | |
d4dd1e85 TO |
134 | * @param string $title |
135 | * Title of the main button. | |
da6b46f4 EM |
136 | * @param string $nextType |
137 | * @param string $backType | |
138 | * @param bool $submitOnce | |
139 | * | |
6a488035 | 140 | * @return void |
6a488035 | 141 | */ |
00be9182 | 142 | public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) { |
be2fb01f | 143 | $this->addButtons([ |
90b461f1 SL |
144 | [ |
145 | 'type' => $nextType, | |
146 | 'name' => $title, | |
147 | 'isDefault' => TRUE, | |
148 | ], | |
149 | [ | |
150 | 'type' => $backType, | |
151 | 'name' => ts('Cancel'), | |
152 | ], | |
153 | ]); | |
6a488035 | 154 | } |
96025800 | 155 | |
c32592e1 EM |
156 | /** |
157 | * Get the rows form the search, keyed to make the token processor happy. | |
158 | * | |
159 | * @throws \API_Exception | |
160 | */ | |
f34aa732 | 161 | protected function getRows(): array { |
c32592e1 EM |
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[] = [ | |
f34aa732 EM |
169 | 'contact_id' => $participant['contact_id'], |
170 | 'participant_id' => $participant['id'], | |
171 | 'schema' => [ | |
172 | 'contactId' => $participant['contact_id'], | |
173 | 'participantId' => $participant['id'], | |
174 | ], | |
c32592e1 EM |
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 | ||
6a488035 | 190 | } |