Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-11-03-17-29-16
[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 if (!$this->_single) {
247 $this->addElement('checkbox', 'toggleSelect', NULL, NULL,
248 array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);")
249 );
250 foreach ($rows as $row) {
251 $this->addElement('checkbox', $row['checkbox'],
252 NULL, NULL,
253 array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');")
254 );
255 }
256 }
257
258 $permission = CRM_Core_Permission::getPermission();
259
260 $tasks = array('' => ts('- actions -')) + CRM_Activity_Task::permissionedTaskTitles($permission);
261
262 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
263 $this->add('submit', $this->_actionButtonName, ts('Go'),
264 array(
265 'class' => 'form-submit',
266 'id' => 'Go',
267 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
268 )
269 );
270
271 $this->add('submit', $this->_printButtonName, ts('Print'),
272 array(
273 'class' => 'form-submit',
274 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 1);",
275 )
276 );
277
278 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
279 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
280 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all',
281 array('onchange' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );")
282 );
283 }
284
285 // add buttons
286 $this->addButtons(array(
287 array(
288 'type' => 'refresh',
289 'name' => ts('Search'),
290 'isDefault' => TRUE,
291 ),
292 ));
293 }
294
295 /**
296 * The post processing of the form gets done here.
297 *
298 * Key things done during post processing are
299 * - check for reset or next request. if present, skip post procesing.
300 * - now check if user requested running a saved search, if so, then
301 * the form values associated with the saved search are used for searching.
302 * - if user has done a submit with new values the regular post submissing is
303 * done.
304 * The processing consists of using a Selector / Controller framework for getting the
305 * search results.
306 *
307 * @param
308 *
309 * @return void
310 * @access public
311 */
312 function postProcess() {
313 if ($this->_done) {
314 return;
315 }
316
317 $this->_done = TRUE;
318
319 if (!empty($_POST)) {
320 $this->_formValues = $this->controller->exportValues($this->_name);
321 }
322
323 $this->fixFormValues();
324
325 if (isset($this->_ssID) && empty($_POST)) {
326 // if we are editing / running a saved search and the form has not been posted
327 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
328 }
329
330 // We don't show test records in summaries or dashboards
331 if (empty($this->_formValues['activity_test']) && $this->_force) {
332 $this->_formValues["activity_test"] = 0;
333 }
334
335 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
336
337 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
338
339 $this->set('formValues', $this->_formValues);
340 $this->set('queryParams', $this->_queryParams);
341
342 $buttonName = $this->controller->getButtonName();
343 if ($buttonName == $this->_actionButtonName || $buttonName == $this->_printButtonName) {
344 // check actionName and if next, then do not repeat a search, since we are going to the next page
345 // hack, make sure we reset the task values
346 $stateMachine = $this->controller->getStateMachine();
347 $formName = $stateMachine->getTaskFormName();
348 $this->controller->resetPage($formName);
349 return;
350 }
351
352 $sortID = NULL;
353 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
354 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
355 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
356 );
357 }
358
359 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
360
361 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
362 $this->_action,
363 NULL,
364 $this->_single,
365 $this->_limit,
366 $this->_context
367 );
368 $selector->setKey($this->controller->_key);
369
370 $prefix = NULL;
371 if ($this->_context == 'basic' || $this->_context == 'user') {
372 $prefix = $this->_prefix;
373 }
374
375 $controller = new CRM_Core_Selector_Controller($selector,
376 $this->get(CRM_Utils_Pager::PAGE_ID),
377 $sortID,
378 CRM_Core_Action::VIEW,
379 $this,
380 CRM_Core_Selector_Controller::SESSION,
381 $prefix
382 );
383 $controller->setEmbedded(TRUE);
384 $query = &$selector->getQuery();
385
386 if ($this->_context == 'user') {
387 $query->setSkipPermission(TRUE);
388 }
389 $controller->run();
390 }
391
392 function fixFormValues() {
393 if (!$this->_force) {
394 return;
395 }
396
397 $status = CRM_Utils_Request::retrieve('status', 'String', $this);
398 if ($status) {
399 $this->_formValues['activity_status'] = $status;
400 $this->_defaults['activity_status'] = $status;
401 }
402
403 $survey = CRM_Utils_Request::retrieve('survey', 'Positive', CRM_Core_DAO::$_nullObject);
404
405 if ($survey) {
406 $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey;
407 $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues);
408 $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
409
410 $this->_formValues['activity_type_id'][$activity_type_id] = 1;
411 $this->_defaults['activity_type_id'][$activity_type_id] = 1;
412 }
413 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
414
415 if ($cid) {
416 $cid = CRM_Utils_Type::escape($cid, 'Integer');
417 if ($cid > 0) {
418 $this->_formValues['contact_id'] = $cid;
419
420 $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this);
421
422 if ($activity_role) {
423 $this->_formValues['activity_role'] = $activity_role;
424 }
425 else {
426 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
427 }
428 // also assign individual mode to the template
429 $this->_single = TRUE;
430 }
431 }
432
433 // Added for membership search
434
435 $signupType = CRM_Utils_Request::retrieve('signupType', 'Positive',
436 CRM_Core_DAO::$_nullObject
437 );
438
439 if ($signupType) {
440 //$this->_formValues['activity_type_id'] = array();
441 $this->_formValues['activity_role'] = 1;
442 $this->_defaults['activity_role'] = 1;
443 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
444
445 $renew = CRM_Utils_Array::key('Membership Renewal', $activityTypes);
446 $signup = CRM_Utils_Array::key('Membership Signup', $activityTypes);
447
448 switch ($signupType) {
449 case 3: // signups and renewals
450 $this->_formValues['activity_type_id'][$renew] = 1;
451 $this->_defaults['activity_type_id'][$renew] = 1;
452 case 1: // signups only
453 $this->_formValues['activity_type_id'][$signup] = 1;
454 $this->_defaults['activity_type_id'][$signup] = 1;
455 break;
456
457 case 2: // renewals only
458 $this->_formValues['activity_type_id'][$renew] = 1;
459 $this->_defaults['activity_type_id'][$renew] = 1;
460 break;
461 }
462 }
463
464 $dateLow = CRM_Utils_Request::retrieve('dateLow', 'String',
465 CRM_Core_DAO::$_nullObject
466 );
467
468 if ($dateLow) {
469 $dateLow = date('m/d/Y', strtotime($dateLow));
470 $this->_formValues['activity_date_relative'] = 0;
471 $this->_defaults['activity_date_relative'] = 0;
472 $this->_formValues['activity_date_low'] = $dateLow;
473 $this->_defaults['activity_date_low'] = $dateLow;
474 }
475
476 $dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String',
477 CRM_Core_DAO::$_nullObject
478 );
479
480 if ($dateHigh) {
481 // Activity date time assumes midnight at the beginning of the date
482 // This sets it to almost midnight at the end of the date
483 /* if ($dateHigh <= 99999999) {
484 $dateHigh = 1000000 * $dateHigh + 235959;
485 } */
486 $dateHigh = date('m/d/Y', strtotime($dateHigh));
487 $this->_formValues['activity_date_relative'] = 0;
488 $this->_defaults['activity_date_relative'] = 0;
489 $this->_formValues['activity_date_high'] = $dateHigh;
490 $this->_defaults['activity_date_high'] = $dateHigh;
491 }
492
493 if (!empty($this->_defaults)) {
494 $this->setDefaults($this->_defaults);
495 }
496 }
497
498 function getFormValues() {
499 return NULL;
500 }
501
502 /**
503 * Return a descriptive name for the page, used in wizard header
504 *
505 * @return string
506 * @access public
507 */
508 public function getTitle() {
509 return ts('Find Activities');
510 }
511 }
512