Merge pull request #856 from dlobo/CRM-12594
[civicrm-core.git] / CRM / Activity / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 */
42class 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('With (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
6a488035
TO
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 }
6a488035 329
28c666be
CW
330 // we don't show test activities in Contact Summary / User Dashboard
331 if (empty($this->_formValues['activity_test']) && $this->_single) {
6a488035
TO
332 $this->_formValues["activity_test"] = 0;
333 }
28c666be 334
6a488035
TO
335 if (!CRM_Utils_Array::value('activity_contact_name', $this->_formValues) && !CRM_Utils_Array::value('contact_id', $this->_formValues)) {
336 $this->_formValues['activity_role'] = NULL;
337 }
338 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
339
340 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
341
342 $this->set('formValues', $this->_formValues);
343 $this->set('queryParams', $this->_queryParams);
344
345 $buttonName = $this->controller->getButtonName();
346 if ($buttonName == $this->_actionButtonName || $buttonName == $this->_printButtonName) {
347 // check actionName and if next, then do not repeat a search, since we are going to the next page
348 // hack, make sure we reset the task values
349 $stateMachine = &$this->controller->getStateMachine();
350 $formName = $stateMachine->getTaskFormName();
351 $this->controller->resetPage($formName);
352 return;
353 }
354
355 $sortID = NULL;
356 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
357 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
358 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
359 );
360 }
361
362 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
363
364 $selector = new CRM_Activity_Selector_Search($this->_queryParams,
365 $this->_action,
366 NULL,
367 $this->_single,
368 $this->_limit,
369 $this->_context
370 );
371 $selector->setKey($this->controller->_key);
372
373 $prefix = NULL;
374 if ($this->_context == 'basic' || $this->_context == 'user') {
375 $prefix = $this->_prefix;
376 }
377
378 $controller = new CRM_Core_Selector_Controller($selector,
379 $this->get(CRM_Utils_Pager::PAGE_ID),
380 $sortID,
381 CRM_Core_Action::VIEW,
382 $this,
383 CRM_Core_Selector_Controller::SESSION,
384 $prefix
385 );
386 $controller->setEmbedded(TRUE);
387 $query = &$selector->getQuery();
388
389 if ($this->_context == 'user') {
390 $query->setSkipPermission(TRUE);
391 }
392 $controller->run();
393 }
394
6a488035
TO
395 function fixFormValues() {
396 if (!$this->_force) {
397 return;
398 }
399 $status = CRM_Utils_Request::retrieve('status', 'String', $this);
400 if ($status) {
401 $this->_formValues['activity_status'] = $status;
402 $this->_defaults['activity_status'] = $status;
403 }
404
300c3253
KJ
405 $survey = CRM_Utils_Request::retrieve('survey', 'Positive', CRM_Core_DAO::$_nullObject);
406
6a488035 407 if ($survey) {
9231464f
KJ
408 $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey;
409 $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues);
410 $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
411
412 $this->_formValues['activity_type_id'][$activity_type_id] = 1;
413 $this->_defaults['activity_type_id'][$activity_type_id] = 1;
6a488035
TO
414 }
415 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
416
417 if ($cid) {
418 $cid = CRM_Utils_Type::escape($cid, 'Integer');
419 if ($cid > 0) {
420 $this->_formValues['contact_id'] = $cid;
421
422 $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this);
423
424 if ($activity_role) {
425 $this->_formValues['activity_role'] = $activity_role;
426 }
427 else {
6a488035
TO
428 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
429 }
430 // also assign individual mode to the template
431 $this->_single = TRUE;
432 }
433 }
9231464f
KJ
434
435 if (!empty($this->_defaults)) {
436 $this->setDefaults($this->_defaults);
437 }
438
6a488035
TO
439 }
440
441 function getFormValues() {
442 return NULL;
443 }
444
445 /**
446 * Return a descriptive name for the page, used in wizard header
447 *
448 * @return string
449 * @access public
450 */
451 public function getTitle() {
452 return ts('Find Activities');
453 }
454}
455