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