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