3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 use Civi\Token\TokenProcessor
;
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * This is a shared parent class for form task actions.
22 abstract class CRM_Core_Form_Task
extends CRM_Core_Form
{
25 * The task being performed
32 * The additional clause that we restrict the search with
36 protected $_componentClause = NULL;
39 * The array that holds all the component ids
43 protected $_componentIds;
51 * The array that holds all the case ids
58 * The array that holds all the contact ids
65 * Must be set to entity table name (eg. civicrm_participant) by child class
69 public static $tableName = NULL;
72 * Must be set to entity shortname (eg. event)
76 public static $entityShortname = NULL;
84 * ['contact_id' => 4, 'participant_id' => 6, 'schema' => ['contactId' => 5, 'participantId' => 6],
91 * Set where the browser should be directed to next.
93 * @param string $pathPart
95 * @throws \CRM_Core_Exception
97 public function setNextUrl(string $pathPart) {
98 //set the context for redirection for any task actions
99 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
100 $urlParams = 'force=1';
101 if (CRM_Utils_Rule
::qfKey($qfKey)) {
102 $urlParams .= "&qfKey=$qfKey";
105 $session = CRM_Core_Session
::singleton();
106 $searchFormName = strtolower($this->get('searchFormName') ??
'');
107 if ($searchFormName === 'search') {
108 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/' . $pathPart . '/search', $urlParams));
111 $session->replaceUserContext(CRM_Utils_System
::url("civicrm/contact/search/$searchFormName",
118 * Get the ids the user has selected or FALSE if selection has not been used.
120 * @param array $values
124 public function getSelectedIDs(array $values) {
125 if ($values['radio_ts'] === 'ts_sel') {
127 foreach ($values as $name => $value) {
128 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
129 $ids[] = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
138 * Build all the data structures needed to build the form.
140 * @throws \CRM_Core_Exception
142 public function preProcess() {
143 self
::preProcessCommon($this);
147 * Common pre-processing function.
149 * @param CRM_Core_Form_Task $form
151 * @throws \CRM_Core_Exception
153 public static function preProcessCommon(&$form) {
154 $form->_entityIds
= [];
156 $searchFormValues = $form->getSearchFormValues();
158 $form->_task
= $searchFormValues['task'];
159 $isSelectedContacts = ($searchFormValues['radio_ts'] ??
NULL) === 'ts_sel';
160 $form->assign('isSelectedContacts', $isSelectedContacts);
162 if ($isSelectedContacts) {
163 foreach ($searchFormValues as $name => $value) {
164 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
165 $entityIds[] = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
170 $queryParams = $form->get('queryParams');
172 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
173 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
176 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE, $form->getQueryMode());
177 $query->_distinctComponentClause
= $form->getDistinctComponentClause();
178 $query->_groupByComponentClause
= $form->getGroupByComponentClause();
179 $result = $query->searchQuery(0, 0, $sortOrder);
180 $selector = $form->getEntityAliasField();
181 while ($result->fetch()) {
182 $entityIds[] = $result->$selector;
186 if (!empty($entityIds)) {
187 $form->_componentClause
= ' ' . $form->getTableName() . '.id IN ( ' . implode(',', $entityIds) . ' ) ';
188 $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds));
191 $form->_entityIds
= $form->_componentIds
= $entityIds;
193 // Some functions (eg. PDF letter tokens) rely on Ids being in specific fields rather than the generic $form->_entityIds
194 // So we set that specific field here (eg. for cases $form->_caseIds = $form->_entityIds).
195 // FIXME: This is really to handle legacy code that should probably be updated to use $form->_entityIds
196 $entitySpecificIdsName = '_' . $form::$entityShortname . 'Ids';
197 $form->$entitySpecificIdsName = $form->_entityIds
;
198 $form->setNextUrl($form::$entityShortname);
203 * Given the entity id, compute the contact id since its used for things like send email
204 * For example, for cases we need to override this function as the table name is civicrm_case_contact
206 public function setContactIDs() {
207 $this->_contactIds
= CRM_Core_DAO
::getContactIDsFromComponent($this->_entityIds
,
208 $this->getTableName()
213 * Add buttons to the form.
215 * @param string $title
216 * Title of the main button.
217 * @param string $nextType
218 * Button type for the form after processing.
219 * @param string $backType
220 * @param bool $submitOnce
222 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
231 'name' => ts('Cancel'),
237 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
238 * Should be overridden by child classes in most cases
242 public function getQueryMode() {
243 return $this->queryMode ?
: CRM_Contact_BAO_Query
::MODE_CONTACTS
;
247 * Given the component id, compute the contact id
248 * since it's used for things like send email.
250 * @todo At the moment this duplicates a similar function in CRM_Core_DAO
251 * because right now only the case component is using this. Since the
252 * default $orderBy is '' which is what the original does, others should be
253 * easily convertable as NFC.
254 * @todo The passed in variables should be class member variables. Shouldn't
255 * need to have passed in vars.
257 * @param $componentIDs
258 * @param string $tableName
259 * @param string $idField
263 public function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
266 if (empty($componentIDs)) {
270 $orderBy = $this->orderBy();
272 $IDs = implode(',', $componentIDs);
276 WHERE $idField IN ( $IDs ) $orderBy
279 $dao = CRM_Core_DAO
::executeQuery($query);
280 while ($dao->fetch()) {
281 $contactIDs[] = $dao->contact_id
;
287 * Default ordering for getContactIDsFromComponent. Subclasses can override.
290 * SQL fragment. Either return '' or a valid order clause including the
291 * words "ORDER BY", e.g. "ORDER BY `{$this->idField}`"
293 public function orderBy() {
298 * Get the submitted values for the form.
302 public function getSearchFormValues() {
303 if ($this->_action
=== CRM_Core_Action
::ADVANCED
) {
304 return $this->controller
->exportValues('Advanced');
306 if ($this->_action
=== CRM_Core_Action
::PROFILE
) {
307 return $this->controller
->exportValues('Builder');
309 if ($this->_action
== CRM_Core_Action
::COPY
) {
310 return $this->controller
->exportValues('Custom');
312 if ($this->get('entity') !== 'Contact') {
313 return $this->controller
->exportValues('Search');
315 return $this->controller
->exportValues('Basic');
319 * Get the name of the table for the relevant entity.
323 public function getTableName() {
324 CRM_Core_Error
::deprecatedFunctionWarning('function should be overridden');
325 return $this::$tableName;
329 * Get the clause for grouping by the component.
333 public function getDistinctComponentClause() {
334 return " ( " . $this->getTableName() . ".id )";
338 * Get the group by clause for the component.
342 public function getGroupByComponentClause() {
343 return " GROUP BY " . $this->getTableName() . ".id ";
347 * Get the group by clause for the component.
351 public function getEntityAliasField() {
352 CRM_Core_Error
::deprecatedFunctionWarning('function should be overridden');
353 return $this::$entityShortname . '_id';
357 * List available tokens for this form.
361 public function listTokens() {
362 $tokenProcessor = new TokenProcessor(Civi
::dispatcher(), ['schema' => $this->getTokenSchema()]);
363 return $tokenProcessor->listTokens();
367 * Get the token processor schema required to list any tokens for this task.
371 protected function getTokenSchema(): array {
372 return ['contactId'];
376 * Get the rows from the results.
380 protected function getRows(): array {
382 foreach ($this->getContactIDs() as $contactID) {
383 $rows[] = ['contact_id' => $contactID, 'schema' => ['contactId' => $contactID]];
389 * Get the relevant contact IDs.
393 protected function getContactIDs(): array {
394 return $this->_contactIds ??
[];