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 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * This is a shared parent class for form task actions.
20 abstract class CRM_Core_Form_Task
extends CRM_Core_Form
{
23 * The task being performed
30 * The additional clause that we restrict the search with
34 protected $_componentClause = NULL;
37 * The array that holds all the component ids
41 protected $_componentIds;
49 * The array that holds all the case ids
56 * The array that holds all the contact ids
63 * Must be set to entity table name (eg. civicrm_participant) by child class
67 public static $tableName = NULL;
70 * Must be set to entity shortname (eg. event)
74 public static $entityShortname = NULL;
77 * Set where the browser should be directed to next.
79 * @param string $pathPart
81 * @throws \CRM_Core_Exception
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";
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));
97 $session->replaceUserContext(CRM_Utils_System
::url("civicrm/contact/search/$searchFormName",
104 * Get the ids the user has selected.
106 * @param array $values
110 public function getSelectedIDs(array $values): array {
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
);
121 * Build all the data structures needed to build the form.
123 * @throws \CRM_Core_Exception
125 public function preProcess() {
126 self
::preProcessCommon($this);
130 * Common pre-processing function.
132 * @param CRM_Core_Form_Task $form
134 * @throws \CRM_Core_Exception
136 public static function preProcessCommon(&$form) {
137 $form->_entityIds
= [];
139 $searchFormValues = $form->getSearchFormValues();
141 $form->_task
= $searchFormValues['task'];
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
);
152 $queryParams = $form->get('queryParams');
154 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
155 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
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;
168 if (!empty($entityIds)) {
169 $form->_componentClause
= ' ' . $form->getTableName() . '.id IN ( ' . implode(',', $entityIds) . ' ) ';
170 $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds));
173 $form->_entityIds
= $form->_componentIds
= $entityIds;
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);
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
188 public function setContactIDs() {
189 $this->_contactIds
= CRM_Core_DAO
::getContactIDsFromComponent($this->_entityIds
,
190 $this->getTableName()
195 * Add buttons to the form.
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
204 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
213 'name' => ts('Cancel'),
219 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
220 * Should be overridden by child classes in most cases
224 public function getQueryMode() {
225 return $this->queryMode ?
: CRM_Contact_BAO_Query
::MODE_CONTACTS
;
229 * Given the component id, compute the contact id
230 * since it's used for things like send email.
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.
239 * @param $componentIDs
240 * @param string $tableName
241 * @param string $idField
245 public function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
248 if (empty($componentIDs)) {
252 $orderBy = $this->orderBy();
254 $IDs = implode(',', $componentIDs);
258 WHERE $idField IN ( $IDs ) $orderBy
261 $dao = CRM_Core_DAO
::executeQuery($query);
262 while ($dao->fetch()) {
263 $contactIDs[] = $dao->contact_id
;
269 * Default ordering for getContactIDsFromComponent. Subclasses can override.
272 * SQL fragment. Either return '' or a valid order clause including the
273 * words "ORDER BY", e.g. "ORDER BY `{$this->idField}`"
275 public function orderBy() {
280 * Get the submitted values for the form.
284 public function getSearchFormValues() {
285 if ($this->_action
=== CRM_Core_Action
::ADVANCED
) {
286 return $this->controller
->exportValues('Advanced');
288 if ($this->_action
=== CRM_Core_Action
::PROFILE
) {
289 return $this->controller
->exportValues('Builder');
291 if ($this->_action
== CRM_Core_Action
::COPY
) {
292 return $this->controller
->exportValues('Custom');
294 if ($this->get('entity') !== 'Contact') {
295 return $this->controller
->exportValues('Search');
297 return $this->controller
->exportValues('Basic');
301 * Get the name of the table for the relevant entity.
305 public function getTableName() {
306 CRM_Core_Error
::deprecatedFunctionWarning('function should be overridden');
307 return $this::$tableName;
311 * Get the clause for grouping by the component.
315 public function getDistinctComponentClause() {
316 return " ( " . $this->getTableName() . ".id )";
320 * Get the group by clause for the component.
324 public function getGroupByComponentClause() {
325 return " GROUP BY " . $this->getTableName() . ".id ";
329 * Get the group by clause for the component.
333 public function getEntityAliasField() {
334 CRM_Core_Error
::deprecatedFunctionWarning('function should be overridden');
335 return $this::$entityShortname . '_id';