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