Merge pull request #13158 from elisseck/dev/core/544
[civicrm-core.git] / CRM / Activity / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This file is for activity search.
36 */
37 class CRM_Activity_Form_Search extends CRM_Core_Form_Search {
38
39 /**
40 * The params that are sent to the query.
41 *
42 * @var array
43 */
44 protected $_queryParams;
45
46 /**
47 * Are we restricting ourselves to a single contact.
48 *
49 * @var boolean
50 */
51 protected $_single = FALSE;
52
53 /**
54 * Are we restricting ourselves to a single contact.
55 *
56 * @var boolean
57 */
58 protected $_limit = NULL;
59
60 /**
61 * Prefix for the controller.
62 */
63 protected $_prefix = "activity_";
64
65 /**
66 * The saved search ID retrieved from the GET vars.
67 *
68 * @var int
69 */
70 protected $_ssID;
71
72 /**
73 * @return string
74 */
75 public function getDefaultEntity() {
76 return 'Activity';
77 }
78
79 /**
80 * Processing needed for buildForm and later.
81 */
82 public function preProcess() {
83 $this->set('searchFormName', 'Search');
84
85 // set the button names
86 $this->_searchButtonName = $this->getButtonName('refresh');
87 $this->_actionButtonName = $this->getButtonName('next', 'action');
88
89 $this->_done = FALSE;
90
91 $this->loadStandardSearchOptionsFromUrl();
92
93 // get user submitted values
94 // get it from controller only if form has been submitted, else preProcess has set this
95 if (!empty($_POST) && !$this->controller->isModal()) {
96 $this->_formValues = $this->controller->exportValues($this->_name);
97 }
98 else {
99 $this->_formValues = $this->get('formValues');
100
101 if ($this->_force) {
102 // If we force the search then merge form values with url values
103 // and set submit values to form values.
104 // @todo this is not good security practice. Instead define the fields in metadata & use
105 // getEntityDefaults.
106 $this->_formValues = array_merge((array) $this->_formValues, CRM_Utils_Request::exportValues());
107 $this->_submitValues = $this->_formValues;
108 }
109 }
110
111 if (empty($this->_formValues)) {
112 if (isset($this->_ssID)) {
113 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
114 }
115 }
116
117 if ($this->_force) {
118 $this->postProcess();
119 $this->set('force', 0);
120 }
121
122 $sortID = NULL;
123 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
124 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
125 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
126 );
127 }
128
129 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
130 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
131 $this->_action,
132 NULL,
133 $this->_single,
134 $this->_limit,
135 $this->_context
136 );
137 $prefix = NULL;
138 if ($this->_context == 'user') {
139 $prefix = $this->_prefix;
140 }
141
142 $this->assign("{$prefix}limit", $this->_limit);
143 $this->assign("{$prefix}single", $this->_single);
144
145 $controller = new CRM_Core_Selector_Controller($selector,
146 $this->get(CRM_Utils_Pager::PAGE_ID),
147 $sortID,
148 CRM_Core_Action::VIEW,
149 $this,
150 CRM_Core_Selector_Controller::TRANSFER,
151 $prefix
152 );
153 $controller->setEmbedded(TRUE);
154 $controller->moveFromSessionToTemplate();
155
156 $this->assign('summary', $this->get('summary'));
157 }
158
159 /**
160 * Build the form object.
161 */
162 public function buildQuickForm() {
163 parent::buildQuickForm();
164 $this->addSortNameField();
165
166 CRM_Activity_BAO_Query::buildSearchForm($this);
167
168 $rows = $this->get('rows');
169 if (is_array($rows)) {
170 if (!$this->_single) {
171 $this->addRowSelectors($rows);
172 }
173
174 $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
175 }
176
177 }
178
179 /**
180 * The post processing of the form gets done here.
181 *
182 * Key things done during post processing are
183 * - check for reset or next request. if present, skip post procesing.
184 * - now check if user requested running a saved search, if so, then
185 * the form values associated with the saved search are used for searching.
186 * - if user has done a submit with new values the regular post submissing is
187 * done.
188 *
189 * The processing consists of using a Selector / Controller framework for getting the
190 * search results.
191 */
192 public function postProcess() {
193 if ($this->_done) {
194 return;
195 }
196
197 $this->_done = TRUE;
198
199 if (!empty($_POST)) {
200 $this->_formValues = $this->controller->exportValues($this->_name);
201 $specialParams = array(
202 'activity_type_id',
203 'status_id',
204 'priority_id',
205 'activity_text',
206 );
207 $changeNames = array(
208 'status_id' => 'activity_status_id',
209 'priority_id' => 'activity_priority_id',
210 );
211
212 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames);
213 }
214 $this->fixFormValues();
215
216 if (isset($this->_ssID) && empty($_POST)) {
217 // if we are editing / running a saved search and the form has not been posted
218 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
219 }
220
221 // We don't show test records in summaries or dashboards
222 if (empty($this->_formValues['activity_test']) && $this->_force) {
223 $this->_formValues["activity_test"] = 0;
224 }
225
226 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
227
228 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
229
230 $this->set('formValues', $this->_formValues);
231 $this->set('queryParams', $this->_queryParams);
232
233 $buttonName = $this->controller->getButtonName();
234 if ($buttonName == $this->_actionButtonName) {
235 // check actionName and if next, then do not repeat a search, since we are going to the next page
236 // hack, make sure we reset the task values
237 $stateMachine = $this->controller->getStateMachine();
238 $formName = $stateMachine->getTaskFormName();
239 $this->controller->resetPage($formName);
240 return;
241 }
242
243 $sortID = NULL;
244 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
245 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
246 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
247 );
248 }
249
250 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
251
252 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
253 $this->_action,
254 NULL,
255 $this->_single,
256 $this->_limit,
257 $this->_context
258 );
259 $selector->setKey($this->controller->_key);
260
261 $prefix = NULL;
262 if ($this->_context == 'basic' || $this->_context == 'user') {
263 $prefix = $this->_prefix;
264 }
265
266 $controller = new CRM_Core_Selector_Controller($selector,
267 $this->get(CRM_Utils_Pager::PAGE_ID),
268 $sortID,
269 CRM_Core_Action::VIEW,
270 $this,
271 CRM_Core_Selector_Controller::SESSION,
272 $prefix
273 );
274 $controller->setEmbedded(TRUE);
275 $query = &$selector->getQuery();
276
277 if ($this->_context == 'user') {
278 $query->setSkipPermission(TRUE);
279 }
280 $controller->run();
281 }
282
283 public function fixFormValues() {
284 if (!$this->_force) {
285 return;
286 }
287
288 $status = CRM_Utils_Request::retrieve('status', 'String', $this);
289 if ($status) {
290 $this->_formValues['activity_status_id'] = $status;
291 $this->_defaults['activity_status_id'] = $status;
292 }
293
294 $survey = CRM_Utils_Request::retrieve('survey', 'Positive');
295
296 if ($survey) {
297 $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey;
298 $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues);
299 $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
300
301 // since checkbox are replaced by multiple select option
302 $this->_formValues['activity_type_id'] = $activity_type_id;
303 $this->_defaults['activity_type_id'] = $activity_type_id;
304 }
305 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
306
307 if ($cid) {
308 $cid = CRM_Utils_Type::escape($cid, 'Integer');
309 if ($cid > 0) {
310 $this->_formValues['contact_id'] = $cid;
311
312 $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this);
313
314 if ($activity_role) {
315 $this->_formValues['activity_role'] = $activity_role;
316 }
317 else {
318 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
319 }
320 // also assign individual mode to the template
321 $this->_single = TRUE;
322 }
323 }
324
325 // Enable search activity by custom value
326 // @todo this is not good security practice. Instead define entity fields in metadata &
327 // use getEntity Defaults
328 $requestParams = CRM_Utils_Request::exportValues();
329 foreach (array_keys($requestParams) as $key) {
330 if (substr($key, 0, 7) != 'custom_') {
331 continue;
332 }
333 elseif (empty($requestParams[$key])) {
334 continue;
335 }
336 $customValue = CRM_Utils_Request::retrieve($key, 'String', $this);
337 if ($customValue) {
338 $this->_formValues[$key] = $customValue;
339 $this->_defaults[$key] = $customValue;
340 }
341 }
342
343 if (!empty($this->_defaults)) {
344 $this->setDefaults($this->_defaults);
345 }
346 }
347
348 /**
349 * @return null
350 */
351 public function getFormValues() {
352 return NULL;
353 }
354
355 /**
356 * This virtual function is used to set the default values of various form elements.
357 *
358 * @return array|NULL
359 * reference to the array of default values
360 */
361 public function setDefaultValues() {
362 return array_merge($this->getEntityDefaults($this->getDefaultEntity()), (array) $this->_formValues);
363 }
364
365 /**
366 * Return a descriptive name for the page, used in wizard header
367 *
368 * @return string
369 */
370 public function getTitle() {
371 return ts('Find Activities');
372 }
373
374 protected function getEntityMetadata() {
375 return CRM_Activity_BAO_Query::getSearchFieldMetadata();
376 }
377
378 }