Merge pull request #17390 from mattwire/api3activitydatetimedefault
[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 * Build all the data structures needed to build the form.
78 *
79 * @throws \CRM_Core_Exception
80 */
81 public function preProcess() {
82 self::preProcessCommon($this);
83 }
84
85 /**
86 * Common pre-processing function.
87 *
88 * @param CRM_Core_Form_Task $form
89 *
90 * @throws \CRM_Core_Exception
91 */
92 public static function preProcessCommon(&$form) {
93 $form->_entityIds = [];
94
95 $searchFormValues = $form->controller->exportValues($form->get('searchFormName'));
96
97 $form->_task = $searchFormValues['task'];
98 $className = 'CRM_' . ucfirst($form::$entityShortname) . '_Task';
99 $entityTasks = $className::tasks();
100 $form->assign('taskName', $entityTasks[$form->_task]);
101
102 $entityIds = [];
103 if ($searchFormValues['radio_ts'] == 'ts_sel') {
104 foreach ($searchFormValues as $name => $value) {
105 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
106 $entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
107 }
108 }
109 }
110 else {
111 $queryParams = $form->get('queryParams');
112 $sortOrder = NULL;
113 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
114 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
115 }
116
117 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE, $form->getQueryMode());
118 $query->_distinctComponentClause = " ( " . $form::$tableName . ".id )";
119 $query->_groupByComponentClause = " GROUP BY " . $form::$tableName . ".id ";
120 $result = $query->searchQuery(0, 0, $sortOrder);
121 $selector = $form::$entityShortname . '_id';
122 while ($result->fetch()) {
123 $entityIds[] = $result->$selector;
124 }
125 }
126
127 if (!empty($entityIds)) {
128 $form->_componentClause = ' ' . $form::$tableName . '.id IN ( ' . implode(',', $entityIds) . ' ) ';
129 $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds));
130 }
131
132 $form->_entityIds = $form->_componentIds = $entityIds;
133
134 // Some functions (eg. PDF letter tokens) rely on Ids being in specific fields rather than the generic $form->_entityIds
135 // So we set that specific field here (eg. for cases $form->_caseIds = $form->_entityIds).
136 // FIXME: This is really to handle legacy code that should probably be updated to use $form->_entityIds
137 $entitySpecificIdsName = '_' . $form::$entityShortname . 'Ids';
138 $form->$entitySpecificIdsName = $form->_entityIds;
139
140 //set the context for redirection for any task actions
141 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
142 $urlParams = 'force=1';
143 if (CRM_Utils_Rule::qfKey($qfKey)) {
144 $urlParams .= "&qfKey=$qfKey";
145 }
146
147 $session = CRM_Core_Session::singleton();
148 $searchFormName = strtolower($form->get('searchFormName'));
149 if ($searchFormName == 'search') {
150 $session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $form::$entityShortname . '/search', $urlParams));
151 }
152 else {
153 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
154 $urlParams
155 ));
156 }
157 }
158
159 /**
160 * Given the entity id, compute the contact id since its used for things like send email
161 * For example, for cases we need to override this function as the table name is civicrm_case_contact
162 */
163 public function setContactIDs() {
164 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_entityIds,
165 $this::$tableName
166 );
167 }
168
169 /**
170 * Add buttons to the form.
171 *
172 * @param string $title
173 * Title of the main button.
174 * @param string $nextType
175 * Button type for the form after processing.
176 * @param string $backType
177 * @param bool $submitOnce
178 */
179 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
180 $this->addButtons([
181 [
182 'type' => $nextType,
183 'name' => $title,
184 'isDefault' => TRUE,
185 ],
186 [
187 'type' => $backType,
188 'name' => ts('Cancel'),
189 ],
190 ]);
191 }
192
193 /**
194 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
195 * Should be overridden by child classes in most cases
196 *
197 * @return int
198 */
199 public function getQueryMode() {
200 return $this->queryMode ?: CRM_Contact_BAO_Query::MODE_CONTACTS;
201 }
202
203 /**
204 * Given the component id, compute the contact id
205 * since it's used for things like send email.
206 *
207 * @todo At the moment this duplicates a similar function in CRM_Core_DAO
208 * because right now only the case component is using this. Since the
209 * default $orderBy is '' which is what the original does, others should be
210 * easily convertable as NFC.
211 * @todo The passed in variables should be class member variables. Shouldn't
212 * need to have passed in vars.
213 *
214 * @param $componentIDs
215 * @param string $tableName
216 * @param string $idField
217 *
218 * @return array
219 */
220 public function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
221 $contactIDs = [];
222
223 if (empty($componentIDs)) {
224 return $contactIDs;
225 }
226
227 $orderBy = $this->orderBy();
228
229 $IDs = implode(',', $componentIDs);
230 $query = "
231 SELECT contact_id
232 FROM $tableName
233 WHERE $idField IN ( $IDs ) $orderBy
234 ";
235
236 $dao = CRM_Core_DAO::executeQuery($query);
237 while ($dao->fetch()) {
238 $contactIDs[] = $dao->contact_id;
239 }
240 return $contactIDs;
241 }
242
243 /**
244 * Default ordering for getContactIDsFromComponent. Subclasses can override.
245 *
246 * @return string
247 * SQL fragment. Either return '' or a valid order clause including the
248 * words "ORDER BY", e.g. "ORDER BY `{$this->idField}`"
249 */
250 public function orderBy() {
251 return '';
252 }
253
254 }