Merge pull request #17368 from colemanw/api4suffix
[civicrm-core.git] / CRM / Activity / Form / Search.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 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This file is for activity search.
20 */
21 class CRM_Activity_Form_Search extends CRM_Core_Form_Search {
22
23 /**
24 * The params that are sent to the query.
25 *
26 * @var array
27 */
28 protected $_queryParams;
29
30 /**
31 * Are we restricting ourselves to a single contact.
32 *
33 * @var bool
34 */
35 protected $_single = FALSE;
36
37 /**
38 * Are we restricting ourselves to a single contact.
39 *
40 * @var bool
41 */
42 protected $_limit;
43
44 /**
45 * Prefix for the controller.
46 * @var string
47 */
48 protected $_prefix = 'activity_';
49
50 /**
51 * The saved search ID retrieved from the GET vars.
52 *
53 * @var int
54 */
55 protected $_ssID;
56
57 /**
58 * @return string
59 */
60 public function getDefaultEntity() {
61 return 'Activity';
62 }
63
64 /**
65 * Processing needed for buildForm and later.
66 *
67 * @throws \CRM_Core_Exception
68 * @throws \CiviCRM_API3_Exception
69 */
70 public function preProcess() {
71 $this->set('searchFormName', 'Search');
72
73 // set the button names
74 $this->_actionButtonName = $this->getButtonName('next', 'action');
75
76 $this->_done = FALSE;
77 $this->sortNameOnly = TRUE;
78
79 parent::preProcess();
80
81 if (empty($this->_formValues)) {
82 if (isset($this->_ssID)) {
83 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
84 }
85 }
86
87 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
88 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
89 $this->_action,
90 NULL,
91 $this->_single,
92 $this->_limit,
93 $this->_context
94 );
95 $prefix = NULL;
96 if ($this->_context == 'user') {
97 $prefix = $this->_prefix;
98 }
99
100 $this->assign("{$prefix}limit", $this->_limit);
101 $this->assign("{$prefix}single", $this->_single);
102
103 $controller = new CRM_Core_Selector_Controller($selector,
104 $this->get(CRM_Utils_Pager::PAGE_ID),
105 $this->getSortID(),
106 CRM_Core_Action::VIEW,
107 $this,
108 CRM_Core_Selector_Controller::TRANSFER,
109 $prefix
110 );
111 $controller->setEmbedded(TRUE);
112 $controller->moveFromSessionToTemplate();
113
114 $this->assign('summary', $this->get('summary'));
115 }
116
117 /**
118 * Build the form object.
119 *
120 * @throws \CRM_Core_Exception
121 * @throws \CiviCRM_API3_Exception
122 */
123 public function buildQuickForm() {
124 parent::buildQuickForm();
125 $this->addSortNameField();
126
127 CRM_Activity_BAO_Query::buildSearchForm($this);
128
129 $rows = $this->get('rows');
130 if (is_array($rows)) {
131 if (!$this->_single) {
132 $this->addRowSelectors($rows);
133 }
134
135 $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
136 }
137
138 }
139
140 /**
141 * The post processing of the form gets done here.
142 *
143 * Key things done during post processing are
144 * - check for reset or next request. if present, skip post procesing.
145 * - now check if user requested running a saved search, if so, then
146 * the form values associated with the saved search are used for searching.
147 * - if user has done a submit with new values the regular post submissing is
148 * done.
149 *
150 * The processing consists of using a Selector / Controller framework for getting the
151 * search results.
152 *
153 * @throws \CRM_Core_Exception
154 */
155 public function postProcess() {
156 if ($this->_done) {
157 return;
158 }
159
160 $this->_done = TRUE;
161 $this->setFormValues();
162 if (!empty($_POST)) {
163 $specialParams = [
164 'activity_type_id',
165 'priority_id',
166 ];
167 $changeNames = [
168 'priority_id' => 'activity_priority_id',
169 ];
170
171 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames);
172 }
173 $this->fixFormValues();
174
175 if (isset($this->_ssID) && empty($_POST)) {
176 // if we are editing / running a saved search and the form has not been posted
177 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
178 }
179
180 // We don't show test records in summaries or dashboards
181 if (empty($this->_formValues['activity_test']) && $this->_force) {
182 $this->_formValues["activity_test"] = 0;
183 }
184
185 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
186
187 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
188
189 $this->set('queryParams', $this->_queryParams);
190
191 $buttonName = $this->controller->getButtonName();
192 if ($buttonName == $this->_actionButtonName) {
193 // check actionName and if next, then do not repeat a search, since we are going to the next page
194 // hack, make sure we reset the task values
195 $stateMachine = $this->controller->getStateMachine();
196 $formName = $stateMachine->getTaskFormName();
197 $this->controller->resetPage($formName);
198 return;
199 }
200
201 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
202
203 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
204 $this->_action,
205 NULL,
206 $this->_single,
207 $this->_limit,
208 $this->_context
209 );
210 $selector->setKey($this->controller->_key);
211
212 $prefix = NULL;
213 if ($this->_context === 'basic' || $this->_context === 'user') {
214 $prefix = $this->_prefix;
215 }
216
217 $controller = new CRM_Core_Selector_Controller($selector,
218 $this->get(CRM_Utils_Pager::PAGE_ID),
219 $this->getSortID(),
220 CRM_Core_Action::VIEW,
221 $this,
222 CRM_Core_Selector_Controller::SESSION,
223 $prefix
224 );
225 $controller->setEmbedded(TRUE);
226 $query = &$selector->getQuery();
227
228 if ($this->_context === 'user') {
229 $query->setSkipPermission(TRUE);
230 }
231 $controller->run();
232 }
233
234 /**
235 * Probably more hackery than anything else.
236 *
237 * @throws \CRM_Core_Exception
238 */
239 public function fixFormValues() {
240 if (!$this->_force) {
241 return;
242 }
243
244 $status = CRM_Utils_Request::retrieve('status', 'String', $this);
245 if ($status) {
246 $this->_formValues['activity_status_id'] = $status;
247 $this->_defaults['activity_status_id'] = $status;
248 }
249
250 $survey = CRM_Utils_Request::retrieve('survey', 'Positive');
251
252 if ($survey) {
253 $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey;
254 $sid = $this->_formValues['activity_survey_id'] ?? NULL;
255 $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
256
257 // since checkbox are replaced by multiple select option
258 $this->_formValues['activity_type_id'] = $activity_type_id;
259 $this->_defaults['activity_type_id'] = $activity_type_id;
260 }
261 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
262
263 if ($cid) {
264 $cid = CRM_Utils_Type::escape($cid, 'Integer');
265 if ($cid > 0) {
266 $this->_formValues['contact_id'] = $cid;
267
268 $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this);
269
270 if ($activity_role) {
271 $this->_formValues['activity_role'] = $activity_role;
272 }
273 else {
274 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
275 }
276 // also assign individual mode to the template
277 $this->_single = TRUE;
278 }
279 }
280
281 // Enable search activity by custom value
282 // @todo this is not good security practice. Instead define entity fields in metadata &
283 // use getEntity Defaults
284 $requestParams = CRM_Utils_Request::exportValues();
285 foreach (array_keys($requestParams) as $key) {
286 if (substr($key, 0, 7) != 'custom_') {
287 continue;
288 }
289 elseif (empty($requestParams[$key])) {
290 continue;
291 }
292 $customValue = CRM_Utils_Request::retrieve($key, 'String', $this);
293 if ($customValue) {
294 $this->_formValues[$key] = $customValue;
295 $this->_defaults[$key] = $customValue;
296 }
297 }
298
299 if (!empty($this->_defaults)) {
300 $this->setDefaults($this->_defaults);
301 }
302 }
303
304 /**
305 * Return a descriptive name for the page, used in wizard header
306 *
307 * @return string
308 */
309 public function getTitle() {
310 return ts('Find Activities');
311 }
312
313 /**
314 * Get metadata for the entity fields.
315 *
316 * @return array
317 *
318 * @throws \CiviCRM_API3_Exception
319 */
320 protected function getEntityMetadata() {
321 return CRM_Activity_BAO_Query::getSearchFieldMetadata();
322 }
323
324 /**
325 * Set the metadata for the form.
326 */
327 protected function setSearchMetadata() {
328 $this->addSearchFieldMetadata(['Activity' => $this->getEntityMetadata()]);
329 }
330
331 }