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 or FALSE if selection has not been used.
106 * @param array $values
110 public function getSelectedIDs(array $values) {
111 if ($values['radio_ts'] === 'ts_sel') {
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
);
124 * Build all the data structures needed to build the form.
126 * @throws \CRM_Core_Exception
128 public function preProcess() {
129 self
::preProcessCommon($this);
133 * Common pre-processing function.
135 * @param CRM_Core_Form_Task $form
137 * @throws \CRM_Core_Exception
139 public static function preProcessCommon(&$form) {
140 $form->_entityIds
= [];
142 $searchFormValues = $form->getSearchFormValues();
144 $form->_task
= $searchFormValues['task'];
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
);
155 $queryParams = $form->get('queryParams');
157 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
158 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
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;
171 if (!empty($entityIds)) {
172 $form->_componentClause
= ' ' . $form->getTableName() . '.id IN ( ' . implode(',', $entityIds) . ' ) ';
173 $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds));
176 $form->_entityIds
= $form->_componentIds
= $entityIds;
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);
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
191 public function setContactIDs() {
192 $this->_contactIds
= CRM_Core_DAO
::getContactIDsFromComponent($this->_entityIds
,
193 $this->getTableName()
198 * Add buttons to the form.
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
207 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
216 'name' => ts('Cancel'),
222 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
223 * Should be overridden by child classes in most cases
227 public function getQueryMode() {
228 return $this->queryMode ?
: CRM_Contact_BAO_Query
::MODE_CONTACTS
;
232 * Given the component id, compute the contact id
233 * since it's used for things like send email.
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.
242 * @param $componentIDs
243 * @param string $tableName
244 * @param string $idField
248 public function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
251 if (empty($componentIDs)) {
255 $orderBy = $this->orderBy();
257 $IDs = implode(',', $componentIDs);
261 WHERE $idField IN ( $IDs ) $orderBy
264 $dao = CRM_Core_DAO
::executeQuery($query);
265 while ($dao->fetch()) {
266 $contactIDs[] = $dao->contact_id
;
272 * Default ordering for getContactIDsFromComponent. Subclasses can override.
275 * SQL fragment. Either return '' or a valid order clause including the
276 * words "ORDER BY", e.g. "ORDER BY `{$this->idField}`"
278 public function orderBy() {
283 * Get the submitted values for the form.
287 public function getSearchFormValues() {
288 if ($this->_action
=== CRM_Core_Action
::ADVANCED
) {
289 return $this->controller
->exportValues('Advanced');
291 if ($this->_action
=== CRM_Core_Action
::PROFILE
) {
292 return $this->controller
->exportValues('Builder');
294 if ($this->_action
== CRM_Core_Action
::COPY
) {
295 return $this->controller
->exportValues('Custom');
297 if ($this->get('entity') !== 'Contact') {
298 return $this->controller
->exportValues('Search');
300 return $this->controller
->exportValues('Basic');
304 * Get the name of the table for the relevant entity.
308 public function getTableName() {
309 CRM_Core_Error
::deprecatedFunctionWarning('function should be overridden');
310 return $this::$tableName;
314 * Get the clause for grouping by the component.
318 public function getDistinctComponentClause() {
319 return " ( " . $this->getTableName() . ".id )";
323 * Get the group by clause for the component.
327 public function getGroupByComponentClause() {
328 return " GROUP BY " . $this->getTableName() . ".id ";
332 * Get the group by clause for the component.
336 public function getEntityAliasField() {
337 CRM_Core_Error
::deprecatedFunctionWarning('function should be overridden');
338 return $this::$entityShortname . '_id';