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