Merge pull request #21311 from eileenmcnaughton/poof
[civicrm-core.git] / CRM / Core / Form / Task.php
CommitLineData
888da08c
MW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
888da08c 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 |
888da08c
MW
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
888da08c
MW
15 */
16
17/**
18 * This is a shared parent class for form task actions.
19 */
20abstract class CRM_Core_Form_Task extends CRM_Core_Form {
21
22 /**
23 * The task being performed
24 *
25 * @var int
26 */
27 protected $_task;
28
29 /**
30 * The additional clause that we restrict the search with
31 *
32 * @var string
33 */
34 protected $_componentClause = NULL;
35
36 /**
37 * The array that holds all the component ids
38 *
39 * @var array
40 */
41 protected $_componentIds;
42
d5fd18f6 43 /**
44 * @var int
45 */
46 protected $queryMode;
47
888da08c
MW
48 /**
49 * The array that holds all the case ids
50 *
51 * @var array
52 */
53 public $_entityIds;
54
55 /**
56 * The array that holds all the contact ids
57 *
58 * @var array
59 */
60 public $_contactIds;
61
fe841bdf
MW
62 /**
63 * Must be set to entity table name (eg. civicrm_participant) by child class
64 *
65 * @var string
66 */
518fa0ee 67 public static $tableName = NULL;
fe841bdf
MW
68
69 /**
70 * Must be set to entity shortname (eg. event)
71 *
72 * @var string
73 */
518fa0ee 74 public static $entityShortname = NULL;
888da08c 75
d85de2e4 76 /**
77 * Set where the browser should be directed to next.
78 *
79 * @param string $pathPart
80 *
81 * @throws \CRM_Core_Exception
82 */
faa31fa6 83 public function setNextUrl(string $pathPart) {
d85de2e4 84 //set the context for redirection for any task actions
85 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
86 $urlParams = 'force=1';
87 if (CRM_Utils_Rule::qfKey($qfKey)) {
88 $urlParams .= "&qfKey=$qfKey";
89 }
90
91 $session = CRM_Core_Session::singleton();
92 $searchFormName = strtolower($this->get('searchFormName'));
93 if ($searchFormName === 'search') {
94 $session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $pathPart . '/search', $urlParams));
95 }
96 else {
97 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
98 $urlParams
99 ));
100 }
101 }
102
6c9b622c 103 /**
56752ec0 104 * Get the ids the user has selected or FALSE if selection has not been used.
6c9b622c 105 *
106 * @param array $values
107 *
56752ec0 108 * @return array|bool
6c9b622c 109 */
56752ec0 110 public function getSelectedIDs(array $values) {
111 if ($values['radio_ts'] === 'ts_sel') {
112 $ids = [];
113 foreach ($values as $name => $value) {
114 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
115 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
116 }
6c9b622c 117 }
56752ec0 118 return $ids;
6c9b622c 119 }
56752ec0 120 return FALSE;
6c9b622c 121 }
122
888da08c
MW
123 /**
124 * Build all the data structures needed to build the form.
6e3090fb
MW
125 *
126 * @throws \CRM_Core_Exception
888da08c
MW
127 */
128 public function preProcess() {
6e3090fb
MW
129 self::preProcessCommon($this);
130 }
131
132 /**
133 * Common pre-processing function.
134 *
a0174743 135 * @param CRM_Core_Form_Task $form
6e3090fb
MW
136 *
137 * @throws \CRM_Core_Exception
138 */
fe841bdf 139 public static function preProcessCommon(&$form) {
be2fb01f 140 $form->_entityIds = [];
888da08c 141
2d09a0c3 142 $searchFormValues = $form->getSearchFormValues();
888da08c 143
fe841bdf 144 $form->_task = $searchFormValues['task'];
888da08c 145
be2fb01f 146 $entityIds = [];
fe841bdf
MW
147 if ($searchFormValues['radio_ts'] == 'ts_sel') {
148 foreach ($searchFormValues as $name => $value) {
888da08c 149 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
fe841bdf 150 $entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
888da08c
MW
151 }
152 }
153 }
154 else {
6e3090fb 155 $queryParams = $form->get('queryParams');
888da08c 156 $sortOrder = NULL;
6e3090fb
MW
157 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
158 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
888da08c
MW
159 }
160
a0174743 161 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE, $form->getQueryMode());
ab3e9e4c 162 $query->_distinctComponentClause = $form->getDistinctComponentClause();
163 $query->_groupByComponentClause = $form->getGroupByComponentClause();
888da08c 164 $result = $query->searchQuery(0, 0, $sortOrder);
ab3e9e4c 165 $selector = $form->getEntityAliasField();
888da08c 166 while ($result->fetch()) {
fe841bdf 167 $entityIds[] = $result->$selector;
888da08c
MW
168 }
169 }
170
fe841bdf 171 if (!empty($entityIds)) {
ab3e9e4c 172 $form->_componentClause = ' ' . $form->getTableName() . '.id IN ( ' . implode(',', $entityIds) . ' ) ';
fe841bdf 173 $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds));
888da08c
MW
174 }
175
fe841bdf 176 $form->_entityIds = $form->_componentIds = $entityIds;
888da08c 177
39fecc8a
MW
178 // Some functions (eg. PDF letter tokens) rely on Ids being in specific fields rather than the generic $form->_entityIds
179 // So we set that specific field here (eg. for cases $form->_caseIds = $form->_entityIds).
180 // FIXME: This is really to handle legacy code that should probably be updated to use $form->_entityIds
181 $entitySpecificIdsName = '_' . $form::$entityShortname . 'Ids';
182 $form->$entitySpecificIdsName = $form->_entityIds;
d85de2e4 183 $form->setNextUrl($form::$entityShortname);
39fecc8a 184
888da08c
MW
185 }
186
187 /**
6e3090fb
MW
188 * Given the entity id, compute the contact id since its used for things like send email
189 * For example, for cases we need to override this function as the table name is civicrm_case_contact
888da08c
MW
190 */
191 public function setContactIDs() {
9e7fec13 192 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_entityIds,
ab3e9e4c 193 $this->getTableName()
888da08c
MW
194 );
195 }
196
197 /**
f14a62d1 198 * Add buttons to the form.
888da08c
MW
199 *
200 * @param string $title
201 * Title of the main button.
202 * @param string $nextType
203 * Button type for the form after processing.
204 * @param string $backType
205 * @param bool $submitOnce
206 */
207 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
be2fb01f 208 $this->addButtons([
518fa0ee
SL
209 [
210 'type' => $nextType,
211 'name' => $title,
212 'isDefault' => TRUE,
213 ],
214 [
215 'type' => $backType,
216 'name' => ts('Cancel'),
217 ],
218 ]);
888da08c
MW
219 }
220
a0174743
MW
221 /**
222 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
223 * Should be overridden by child classes in most cases
224 *
225 * @return int
226 */
227 public function getQueryMode() {
d5fd18f6 228 return $this->queryMode ?: CRM_Contact_BAO_Query::MODE_CONTACTS;
a0174743
MW
229 }
230
2e9051c7
D
231 /**
232 * Given the component id, compute the contact id
233 * since it's used for things like send email.
234 *
235 * @todo At the moment this duplicates a similar function in CRM_Core_DAO
236 * because right now only the case component is using this. Since the
237 * default $orderBy is '' which is what the original does, others should be
238 * easily convertable as NFC.
239 * @todo The passed in variables should be class member variables. Shouldn't
240 * need to have passed in vars.
241 *
242 * @param $componentIDs
243 * @param string $tableName
244 * @param string $idField
245 *
246 * @return array
247 */
248 public function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
249 $contactIDs = [];
250
251 if (empty($componentIDs)) {
252 return $contactIDs;
253 }
254
255 $orderBy = $this->orderBy();
256
257 $IDs = implode(',', $componentIDs);
258 $query = "
259SELECT contact_id
260 FROM $tableName
261 WHERE $idField IN ( $IDs ) $orderBy
262";
263
264 $dao = CRM_Core_DAO::executeQuery($query);
265 while ($dao->fetch()) {
266 $contactIDs[] = $dao->contact_id;
267 }
268 return $contactIDs;
269 }
270
271 /**
272 * Default ordering for getContactIDsFromComponent. Subclasses can override.
273 *
274 * @return string
275 * SQL fragment. Either return '' or a valid order clause including the
276 * words "ORDER BY", e.g. "ORDER BY `{$this->idField}`"
277 */
278 public function orderBy() {
279 return '';
280 }
281
986ac53f 282 /**
283 * Get the submitted values for the form.
284 *
285 * @return array
286 */
2d09a0c3 287 public function getSearchFormValues() {
986ac53f 288 if ($this->_action === CRM_Core_Action::ADVANCED) {
289 return $this->controller->exportValues('Advanced');
290 }
291 if ($this->_action === CRM_Core_Action::PROFILE) {
292 return $this->controller->exportValues('Builder');
293 }
294 if ($this->_action == CRM_Core_Action::COPY) {
295 return $this->controller->exportValues('Custom');
296 }
2d09a0c3 297 if ($this->get('entity') !== 'Contact') {
298 return $this->controller->exportValues('Search');
299 }
986ac53f 300 return $this->controller->exportValues('Basic');
301 }
302
ab3e9e4c 303 /**
304 * Get the name of the table for the relevant entity.
305 *
306 * @return string
307 */
308 public function getTableName() {
309 CRM_Core_Error::deprecatedFunctionWarning('function should be overridden');
310 return $this::$tableName;
311 }
312
313 /**
314 * Get the clause for grouping by the component.
315 *
316 * @return string
317 */
318 public function getDistinctComponentClause() {
319 return " ( " . $this->getTableName() . ".id )";
320 }
321
322 /**
323 * Get the group by clause for the component.
324 *
325 * @return string
326 */
327 public function getGroupByComponentClause() {
328 return " GROUP BY " . $this->getTableName() . ".id ";
329 }
330
331 /**
332 * Get the group by clause for the component.
333 *
334 * @return string
335 */
336 public function getEntityAliasField() {
337 CRM_Core_Error::deprecatedFunctionWarning('function should be overridden');
338 return $this::$entityShortname . '_id';
339 }
340
888da08c 341}