Merge pull request #15937 from seamuslee001/dev_core_1405
[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->_searchButtonName = $this->getButtonName('refresh');
75 $this->_actionButtonName = $this->getButtonName('next', 'action');
76
77 $this->_done = FALSE;
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 $sortID = NULL;
88 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
89 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
90 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
91 );
92 }
93
94 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
95 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
96 $this->_action,
97 NULL,
98 $this->_single,
99 $this->_limit,
100 $this->_context
101 );
102 $prefix = NULL;
103 if ($this->_context == 'user') {
104 $prefix = $this->_prefix;
105 }
106
107 $this->assign("{$prefix}limit", $this->_limit);
108 $this->assign("{$prefix}single", $this->_single);
109
110 $controller = new CRM_Core_Selector_Controller($selector,
111 $this->get(CRM_Utils_Pager::PAGE_ID),
112 $sortID,
113 CRM_Core_Action::VIEW,
114 $this,
115 CRM_Core_Selector_Controller::TRANSFER,
116 $prefix
117 );
118 $controller->setEmbedded(TRUE);
119 $controller->moveFromSessionToTemplate();
120
121 $this->assign('summary', $this->get('summary'));
122 }
123
124 /**
125 * Build the form object.
126 *
127 * @throws \CRM_Core_Exception
128 * @throws \CiviCRM_API3_Exception
129 */
130 public function buildQuickForm() {
131 parent::buildQuickForm();
132 $this->addSortNameField();
133
134 CRM_Activity_BAO_Query::buildSearchForm($this);
135
136 $rows = $this->get('rows');
137 if (is_array($rows)) {
138 if (!$this->_single) {
139 $this->addRowSelectors($rows);
140 }
141
142 $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
143 }
144
145 }
146
147 /**
148 * The post processing of the form gets done here.
149 *
150 * Key things done during post processing are
151 * - check for reset or next request. if present, skip post procesing.
152 * - now check if user requested running a saved search, if so, then
153 * the form values associated with the saved search are used for searching.
154 * - if user has done a submit with new values the regular post submissing is
155 * done.
156 *
157 * The processing consists of using a Selector / Controller framework for getting the
158 * search results.
159 *
160 * @throws \CRM_Core_Exception
161 */
162 public function postProcess() {
163 if ($this->_done) {
164 return;
165 }
166
167 $this->_done = TRUE;
168 $this->setFormValues();
169 if (!empty($_POST)) {
170 $specialParams = [
171 'activity_type_id',
172 'priority_id',
173 ];
174 $changeNames = [
175 'priority_id' => 'activity_priority_id',
176 ];
177
178 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames);
179 }
180 $this->fixFormValues();
181
182 if (isset($this->_ssID) && empty($_POST)) {
183 // if we are editing / running a saved search and the form has not been posted
184 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
185 }
186
187 // We don't show test records in summaries or dashboards
188 if (empty($this->_formValues['activity_test']) && $this->_force) {
189 $this->_formValues["activity_test"] = 0;
190 }
191
192 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
193
194 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
195
196 $this->set('queryParams', $this->_queryParams);
197
198 $buttonName = $this->controller->getButtonName();
199 if ($buttonName == $this->_actionButtonName) {
200 // check actionName and if next, then do not repeat a search, since we are going to the next page
201 // hack, make sure we reset the task values
202 $stateMachine = $this->controller->getStateMachine();
203 $formName = $stateMachine->getTaskFormName();
204 $this->controller->resetPage($formName);
205 return;
206 }
207
208 $sortID = NULL;
209 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
210 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
211 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
212 );
213 }
214
215 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
216
217 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
218 $this->_action,
219 NULL,
220 $this->_single,
221 $this->_limit,
222 $this->_context
223 );
224 $selector->setKey($this->controller->_key);
225
226 $prefix = NULL;
227 if ($this->_context === 'basic' || $this->_context === 'user') {
228 $prefix = $this->_prefix;
229 }
230
231 $controller = new CRM_Core_Selector_Controller($selector,
232 $this->get(CRM_Utils_Pager::PAGE_ID),
233 $sortID,
234 CRM_Core_Action::VIEW,
235 $this,
236 CRM_Core_Selector_Controller::SESSION,
237 $prefix
238 );
239 $controller->setEmbedded(TRUE);
240 $query = &$selector->getQuery();
241
242 if ($this->_context === 'user') {
243 $query->setSkipPermission(TRUE);
244 }
245 $controller->run();
246 }
247
248 /**
249 * Probably more hackery than anything else.
250 *
251 * @throws \CRM_Core_Exception
252 */
253 public function fixFormValues() {
254 if (!$this->_force) {
255 return;
256 }
257
258 $status = CRM_Utils_Request::retrieve('status', 'String', $this);
259 if ($status) {
260 $this->_formValues['activity_status_id'] = $status;
261 $this->_defaults['activity_status_id'] = $status;
262 }
263
264 $survey = CRM_Utils_Request::retrieve('survey', 'Positive');
265
266 if ($survey) {
267 $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey;
268 $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues);
269 $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
270
271 // since checkbox are replaced by multiple select option
272 $this->_formValues['activity_type_id'] = $activity_type_id;
273 $this->_defaults['activity_type_id'] = $activity_type_id;
274 }
275 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
276
277 if ($cid) {
278 $cid = CRM_Utils_Type::escape($cid, 'Integer');
279 if ($cid > 0) {
280 $this->_formValues['contact_id'] = $cid;
281
282 $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this);
283
284 if ($activity_role) {
285 $this->_formValues['activity_role'] = $activity_role;
286 }
287 else {
288 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
289 }
290 // also assign individual mode to the template
291 $this->_single = TRUE;
292 }
293 }
294
295 // Enable search activity by custom value
296 // @todo this is not good security practice. Instead define entity fields in metadata &
297 // use getEntity Defaults
298 $requestParams = CRM_Utils_Request::exportValues();
299 foreach (array_keys($requestParams) as $key) {
300 if (substr($key, 0, 7) != 'custom_') {
301 continue;
302 }
303 elseif (empty($requestParams[$key])) {
304 continue;
305 }
306 $customValue = CRM_Utils_Request::retrieve($key, 'String', $this);
307 if ($customValue) {
308 $this->_formValues[$key] = $customValue;
309 $this->_defaults[$key] = $customValue;
310 }
311 }
312
313 if (!empty($this->_defaults)) {
314 $this->setDefaults($this->_defaults);
315 }
316 }
317
318 /**
319 * Return a descriptive name for the page, used in wizard header
320 *
321 * @return string
322 */
323 public function getTitle() {
324 return ts('Find Activities');
325 }
326
327 /**
328 * Get metadata for the entity fields.
329 *
330 * @return array
331 *
332 * @throws \CiviCRM_API3_Exception
333 */
334 protected function getEntityMetadata() {
335 return CRM_Activity_BAO_Query::getSearchFieldMetadata();
336 }
337
338 /**
339 * Set the metadata for the form.
340 */
341 protected function setSearchMetadata() {
342 $this->addSearchFieldMetadata(['Activity' => $this->getEntityMetadata()]);
343 }
344
345 }