CRM-19447: Added doc-blocks to unit test methods.
[civicrm-core.git] / CRM / Activity / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 |
c73475ea 12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
6a488035
TO
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 |
c73475ea
WA
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
b6c94f42 35 * This file is for activity search.
6a488035 36 */
3efb5b86 37class CRM_Activity_Form_Search extends CRM_Core_Form_Search {
6a488035 38
6a488035 39 /**
fe482240 40 * The params that are sent to the query.
6a488035
TO
41 *
42 * @var array
6a488035
TO
43 */
44 protected $_queryParams;
45
6a488035 46 /**
fe482240 47 * Are we restricting ourselves to a single contact.
6a488035 48 *
6a488035
TO
49 * @var boolean
50 */
51 protected $_single = FALSE;
52
53 /**
fe482240 54 * Are we restricting ourselves to a single contact.
6a488035 55 *
6a488035
TO
56 * @var boolean
57 */
58 protected $_limit = NULL;
59
6a488035 60 /**
fe482240 61 * Prefix for the controller.
6a488035
TO
62 */
63 protected $_prefix = "activity_";
64
6a488035 65 /**
fe482240 66 * The saved search ID retrieved from the GET vars.
6a488035
TO
67 *
68 * @var int
6a488035
TO
69 */
70 protected $_ssID;
71
72 /**
fe482240 73 * Processing needed for buildForm and later.
6a488035 74 */
00be9182 75 public function preProcess() {
6a488035
TO
76 $this->set('searchFormName', 'Search');
77
c490a46a 78 // set the button names
6a488035 79 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
80 $this->_actionButtonName = $this->getButtonName('next', 'action');
81
82 $this->_done = FALSE;
83 $this->defaults = array();
84
c490a46a
CW
85 // we allow the controller to set force/reset externally, useful when we are being
86 // driven by the wizard framework
a3d827a7 87 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean');
353ffa53
TO
88 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
89 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
6a488035
TO
90 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
91
92 $this->assign("context", $this->_context);
93
94 // get user submitted values
95 // get it from controller only if form has been submitted, else preProcess has set this
96 if (!empty($_POST) && !$this->controller->isModal()) {
97 $this->_formValues = $this->controller->exportValues($this->_name);
98 }
99 else {
100 $this->_formValues = $this->get('formValues');
101 }
102
103 if (empty($this->_formValues)) {
104 if (isset($this->_ssID)) {
105 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
106 }
107 }
108
109 if ($this->_force) {
110 $this->postProcess();
111 $this->set('force', 0);
112 }
113
114 $sortID = NULL;
115 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
116 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
117 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
118 );
119 }
120
121 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
122 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
123 $this->_action,
124 NULL,
125 $this->_single,
126 $this->_limit,
127 $this->_context
128 );
129 $prefix = NULL;
130 if ($this->_context == 'user') {
131 $prefix = $this->_prefix;
132 }
133
134 $this->assign("{$prefix}limit", $this->_limit);
135 $this->assign("{$prefix}single", $this->_single);
136
137 $controller = new CRM_Core_Selector_Controller($selector,
138 $this->get(CRM_Utils_Pager::PAGE_ID),
139 $sortID,
140 CRM_Core_Action::VIEW,
141 $this,
142 CRM_Core_Selector_Controller::TRANSFER,
143 $prefix
144 );
145 $controller->setEmbedded(TRUE);
146 $controller->moveFromSessionToTemplate();
147
148 $this->assign('summary', $this->get('summary'));
149 }
150
151 /**
fe482240 152 * Build the form object.
6a488035 153 */
00be9182 154 public function buildQuickForm() {
3efb5b86 155 parent::buildQuickForm();
e597fc33 156 $this->addSortNameField();
6a488035
TO
157
158 CRM_Activity_BAO_Query::buildSearchForm($this);
159
6a488035
TO
160 $rows = $this->get('rows');
161 if (is_array($rows)) {
162 if (!$this->_single) {
8d36b801 163 $this->addRowSelectors($rows);
6a488035
TO
164 }
165
6a488035
TO
166 $permission = CRM_Core_Permission::getPermission();
167
34197a55 168 $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles($permission));
6a488035
TO
169 }
170
6a488035
TO
171 }
172
173 /**
174 * The post processing of the form gets done here.
175 *
176 * Key things done during post processing are
177 * - check for reset or next request. if present, skip post procesing.
178 * - now check if user requested running a saved search, if so, then
179 * the form values associated with the saved search are used for searching.
180 * - if user has done a submit with new values the regular post submissing is
181 * done.
2e2605fe 182 *
6a488035
TO
183 * The processing consists of using a Selector / Controller framework for getting the
184 * search results.
6a488035 185 */
00be9182 186 public function postProcess() {
6a488035
TO
187 if ($this->_done) {
188 return;
189 }
190
191 $this->_done = TRUE;
192
193 if (!empty($_POST)) {
194 $this->_formValues = $this->controller->exportValues($this->_name);
0b38e8f1 195 $specialParams = array(
196 'activity_type_id',
197 'status_id',
0b38e8f1 198 );
199 $changeNames = array('status_id' => 'activity_status_id');
200 CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames);
6a488035
TO
201 }
202
203 $this->fixFormValues();
204
205 if (isset($this->_ssID) && empty($_POST)) {
206 // if we are editing / running a saved search and the form has not been posted
207 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
208 }
6a488035 209
d43b88cc
CW
210 // We don't show test records in summaries or dashboards
211 if (empty($this->_formValues['activity_test']) && $this->_force) {
6a488035
TO
212 $this->_formValues["activity_test"] = 0;
213 }
28c666be 214
c94d39fd 215 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
216
217 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
218
219 $this->set('formValues', $this->_formValues);
220 $this->set('queryParams', $this->_queryParams);
221
222 $buttonName = $this->controller->getButtonName();
e341bbee 223 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
224 // check actionName and if next, then do not repeat a search, since we are going to the next page
225 // hack, make sure we reset the task values
95ea77ec 226 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
227 $formName = $stateMachine->getTaskFormName();
228 $this->controller->resetPage($formName);
229 return;
230 }
231
232 $sortID = NULL;
233 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
234 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
235 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
236 );
237 }
238
239 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
240
241 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
242 $this->_action,
243 NULL,
244 $this->_single,
245 $this->_limit,
246 $this->_context
247 );
248 $selector->setKey($this->controller->_key);
249
250 $prefix = NULL;
251 if ($this->_context == 'basic' || $this->_context == 'user') {
252 $prefix = $this->_prefix;
253 }
254
255 $controller = new CRM_Core_Selector_Controller($selector,
256 $this->get(CRM_Utils_Pager::PAGE_ID),
257 $sortID,
258 CRM_Core_Action::VIEW,
259 $this,
260 CRM_Core_Selector_Controller::SESSION,
261 $prefix
262 );
263 $controller->setEmbedded(TRUE);
264 $query = &$selector->getQuery();
265
266 if ($this->_context == 'user') {
267 $query->setSkipPermission(TRUE);
268 }
269 $controller->run();
270 }
271
00be9182 272 public function fixFormValues() {
6a488035
TO
273 if (!$this->_force) {
274 return;
275 }
0370ad20 276
6a488035
TO
277 $status = CRM_Utils_Request::retrieve('status', 'String', $this);
278 if ($status) {
97b002d7
CW
279 $this->_formValues['activity_status_id'] = $status;
280 $this->_defaults['activity_status_id'] = $status;
6a488035
TO
281 }
282
a3d827a7 283 $survey = CRM_Utils_Request::retrieve('survey', 'Positive');
300c3253 284
6a488035 285 if ($survey) {
9231464f
KJ
286 $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey;
287 $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues);
288 $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
289
c56c5190 290 // since checkbox are replaced by multiple select option
291 $this->_formValues['activity_type_id'] = $activity_type_id;
292 $this->_defaults['activity_type_id'] = $activity_type_id;
6a488035
TO
293 }
294 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
295
296 if ($cid) {
297 $cid = CRM_Utils_Type::escape($cid, 'Integer');
298 if ($cid > 0) {
299 $this->_formValues['contact_id'] = $cid;
300
301 $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this);
302
303 if ($activity_role) {
304 $this->_formValues['activity_role'] = $activity_role;
305 }
306 else {
6a488035
TO
307 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
308 }
309 // also assign individual mode to the template
310 $this->_single = TRUE;
311 }
312 }
8ef12e64 313
4e636a74 314 // Added for membership search
8ef12e64 315
1273d77c 316 $signupType = CRM_Utils_Request::retrieve('signupType', 'Positive');
8ef12e64 317
4e636a74 318 if ($signupType) {
2e2acd1f 319 $this->_formValues['activity_role'] = 1;
2e2acd1f 320 $this->_defaults['activity_role'] = 1;
4e636a74 321 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
0370ad20
AH
322
323 $renew = CRM_Utils_Array::key('Membership Renewal', $activityTypes);
324 $signup = CRM_Utils_Array::key('Membership Signup', $activityTypes);
8ef12e64 325
4e636a74
AH
326 switch ($signupType) {
327 case 3: // signups and renewals
328 $this->_formValues['activity_type_id'][$renew] = 1;
329 $this->_defaults['activity_type_id'][$renew] = 1;
330 case 1: // signups only
331 $this->_formValues['activity_type_id'][$signup] = 1;
332 $this->_defaults['activity_type_id'][$signup] = 1;
333 break;
8ef12e64 334
4e636a74
AH
335 case 2: // renewals only
336 $this->_formValues['activity_type_id'][$renew] = 1;
337 $this->_defaults['activity_type_id'][$renew] = 1;
338 break;
339 }
340 }
8ef12e64 341
1273d77c 342 $dateLow = CRM_Utils_Request::retrieve('dateLow', 'String');
8ef12e64 343
4e636a74 344 if ($dateLow) {
0370ad20
AH
345 $dateLow = date('m/d/Y', strtotime($dateLow));
346 $this->_formValues['activity_date_relative'] = 0;
347 $this->_defaults['activity_date_relative'] = 0;
4e636a74
AH
348 $this->_formValues['activity_date_low'] = $dateLow;
349 $this->_defaults['activity_date_low'] = $dateLow;
350 }
8ef12e64 351
1273d77c 352 $dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String');
8ef12e64 353
4e636a74
AH
354 if ($dateHigh) {
355 // Activity date time assumes midnight at the beginning of the date
356 // This sets it to almost midnight at the end of the date
32864ccf 357 /* if ($dateHigh <= 99999999) {
e70a7fc0 358 $dateHigh = 1000000 * $dateHigh + 235959;
0370ad20
AH
359 } */
360 $dateHigh = date('m/d/Y', strtotime($dateHigh));
361 $this->_formValues['activity_date_relative'] = 0;
362 $this->_defaults['activity_date_relative'] = 0;
4e636a74
AH
363 $this->_formValues['activity_date_high'] = $dateHigh;
364 $this->_defaults['activity_date_high'] = $dateHigh;
8ef12e64 365 }
19c7dd92 366
2c8d0277
DG
367 // Enable search activity by custom value
368 $requestParams = CRM_Utils_Request::exportValues();
369 foreach (array_keys($requestParams) as $key) {
370 if (substr($key, 0, 7) != 'custom_') {
371 continue;
372 }
373 elseif (empty($requestParams[$key])) {
374 continue;
375 }
376 $customValue = CRM_Utils_Request::retrieve($key, 'String', $this);
377 if ($customValue) {
378 $this->_formValues[$key] = $customValue;
379 $this->_defaults[$key] = $customValue;
380 }
381 }
9231464f
KJ
382
383 if (!empty($this->_defaults)) {
384 $this->setDefaults($this->_defaults);
385 }
6a488035
TO
386 }
387
ffd93213
EM
388 /**
389 * @return null
390 */
00be9182 391 public function getFormValues() {
6a488035
TO
392 return NULL;
393 }
394
395 /**
396 * Return a descriptive name for the page, used in wizard header
397 *
398 * @return string
6a488035
TO
399 */
400 public function getTitle() {
401 return ts('Find Activities');
402 }
96025800 403
6a488035 404}