Annotate DAO files with COMPONENT, exclude disabled components' entities from APIv4...
[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 or FALSE if selection has not been used.
105 *
106 * @param array $values
107 *
108 * @return array|bool
109 */
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 }
117 }
118 return $ids;
119 }
120 return FALSE;
121 }
122
123 /**
124 * Build all the data structures needed to build the form.
125 *
126 * @throws \CRM_Core_Exception
127 */
128 public function preProcess() {
129 self::preProcessCommon($this);
130 }
131
132 /**
133 * Common pre-processing function.
134 *
135 * @param CRM_Core_Form_Task $form
136 *
137 * @throws \CRM_Core_Exception
138 */
139 public static function preProcessCommon(&$form) {
140 $form->_entityIds = [];
141
142 $searchFormValues = $form->getSearchFormValues();
143
144 $form->_task = $searchFormValues['task'];
145
146 $entityIds = [];
147 if ($searchFormValues['radio_ts'] == 'ts_sel') {
148 foreach ($searchFormValues as $name => $value) {
149 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
150 $entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
151 }
152 }
153 }
154 else {
155 $queryParams = $form->get('queryParams');
156 $sortOrder = NULL;
157 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
158 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
159 }
160
161 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE, $form->getQueryMode());
162 $query->_distinctComponentClause = $form->getDistinctComponentClause();
163 $query->_groupByComponentClause = $form->getGroupByComponentClause();
164 $result = $query->searchQuery(0, 0, $sortOrder);
165 $selector = $form->getEntityAliasField();
166 while ($result->fetch()) {
167 $entityIds[] = $result->$selector;
168 }
169 }
170
171 if (!empty($entityIds)) {
172 $form->_componentClause = ' ' . $form->getTableName() . '.id IN ( ' . implode(',', $entityIds) . ' ) ';
173 $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds));
174 }
175
176 $form->_entityIds = $form->_componentIds = $entityIds;
177
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;
183 $form->setNextUrl($form::$entityShortname);
184
185 }
186
187 /**
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
190 */
191 public function setContactIDs() {
192 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_entityIds,
193 $this->getTableName()
194 );
195 }
196
197 /**
198 * Add buttons to the form.
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) {
208 $this->addButtons([
209 [
210 'type' => $nextType,
211 'name' => $title,
212 'isDefault' => TRUE,
213 ],
214 [
215 'type' => $backType,
216 'name' => ts('Cancel'),
217 ],
218 ]);
219 }
220
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() {
228 return $this->queryMode ?: CRM_Contact_BAO_Query::MODE_CONTACTS;
229 }
230
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 = "
259 SELECT 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
282 /**
283 * Get the submitted values for the form.
284 *
285 * @return array
286 */
287 public function getSearchFormValues() {
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 }
297 if ($this->get('entity') !== 'Contact') {
298 return $this->controller->exportValues('Search');
299 }
300 return $this->controller->exportValues('Basic');
301 }
302
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
341 }