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