Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | ||
6a488035 TO |
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'); | |
6a488035 TO |
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() { | |
18d2d5e2 | 226 | $this->addElement('text', 'sort_name', ts('Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); |
6a488035 TO |
227 | |
228 | CRM_Activity_BAO_Query::buildSearchForm($this); | |
229 | ||
230 | /* | |
231 | * add form checkboxes for each row. This is needed out here to conform to QF protocol | |
232 | * of all elements being declared in builQuickForm | |
233 | */ | |
234 | ||
235 | $rows = $this->get('rows'); | |
236 | if (is_array($rows)) { | |
b9599ed0 | 237 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); |
6a488035 TO |
238 | if (!$this->_single) { |
239 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, | |
240 | array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);") | |
241 | ); | |
242 | foreach ($rows as $row) { | |
243 | $this->addElement('checkbox', $row['checkbox'], | |
244 | NULL, NULL, | |
245 | array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');") | |
246 | ); | |
247 | } | |
248 | } | |
249 | ||
6a488035 TO |
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 | ||
6a488035 TO |
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('onchange' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );") | |
267 | ); | |
268 | } | |
269 | ||
270 | // add buttons | |
271 | $this->addButtons(array( | |
272 | array( | |
273 | 'type' => 'refresh', | |
274 | 'name' => ts('Search'), | |
275 | 'isDefault' => TRUE, | |
276 | ), | |
277 | )); | |
278 | } | |
279 | ||
280 | /** | |
281 | * The post processing of the form gets done here. | |
282 | * | |
283 | * Key things done during post processing are | |
284 | * - check for reset or next request. if present, skip post procesing. | |
285 | * - now check if user requested running a saved search, if so, then | |
286 | * the form values associated with the saved search are used for searching. | |
287 | * - if user has done a submit with new values the regular post submissing is | |
288 | * done. | |
289 | * The processing consists of using a Selector / Controller framework for getting the | |
290 | * search results. | |
291 | * | |
292 | * @param | |
293 | * | |
294 | * @return void | |
295 | * @access public | |
296 | */ | |
297 | function postProcess() { | |
298 | if ($this->_done) { | |
299 | return; | |
300 | } | |
301 | ||
302 | $this->_done = TRUE; | |
303 | ||
304 | if (!empty($_POST)) { | |
305 | $this->_formValues = $this->controller->exportValues($this->_name); | |
306 | } | |
307 | ||
308 | $this->fixFormValues(); | |
309 | ||
310 | if (isset($this->_ssID) && empty($_POST)) { | |
311 | // if we are editing / running a saved search and the form has not been posted | |
312 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
313 | } | |
6a488035 | 314 | |
d43b88cc CW |
315 | // We don't show test records in summaries or dashboards |
316 | if (empty($this->_formValues['activity_test']) && $this->_force) { | |
6a488035 TO |
317 | $this->_formValues["activity_test"] = 0; |
318 | } | |
28c666be | 319 | |
6a488035 TO |
320 | CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); |
321 | ||
322 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
323 | ||
324 | $this->set('formValues', $this->_formValues); | |
325 | $this->set('queryParams', $this->_queryParams); | |
326 | ||
327 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 328 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
329 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
330 | // hack, make sure we reset the task values | |
95ea77ec | 331 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
332 | $formName = $stateMachine->getTaskFormName(); |
333 | $this->controller->resetPage($formName); | |
334 | return; | |
335 | } | |
336 | ||
337 | $sortID = NULL; | |
338 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
339 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
340 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
341 | ); | |
342 | } | |
343 | ||
344 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
345 | ||
346 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, | |
347 | $this->_action, | |
348 | NULL, | |
349 | $this->_single, | |
350 | $this->_limit, | |
351 | $this->_context | |
352 | ); | |
353 | $selector->setKey($this->controller->_key); | |
354 | ||
355 | $prefix = NULL; | |
356 | if ($this->_context == 'basic' || $this->_context == 'user') { | |
357 | $prefix = $this->_prefix; | |
358 | } | |
359 | ||
360 | $controller = new CRM_Core_Selector_Controller($selector, | |
361 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
362 | $sortID, | |
363 | CRM_Core_Action::VIEW, | |
364 | $this, | |
365 | CRM_Core_Selector_Controller::SESSION, | |
366 | $prefix | |
367 | ); | |
368 | $controller->setEmbedded(TRUE); | |
369 | $query = &$selector->getQuery(); | |
370 | ||
371 | if ($this->_context == 'user') { | |
372 | $query->setSkipPermission(TRUE); | |
373 | } | |
374 | $controller->run(); | |
375 | } | |
376 | ||
6a488035 TO |
377 | function fixFormValues() { |
378 | if (!$this->_force) { | |
379 | return; | |
380 | } | |
0370ad20 | 381 | |
6a488035 TO |
382 | $status = CRM_Utils_Request::retrieve('status', 'String', $this); |
383 | if ($status) { | |
384 | $this->_formValues['activity_status'] = $status; | |
385 | $this->_defaults['activity_status'] = $status; | |
386 | } | |
387 | ||
300c3253 KJ |
388 | $survey = CRM_Utils_Request::retrieve('survey', 'Positive', CRM_Core_DAO::$_nullObject); |
389 | ||
6a488035 | 390 | if ($survey) { |
9231464f KJ |
391 | $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey; |
392 | $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues); | |
393 | $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id'); | |
394 | ||
395 | $this->_formValues['activity_type_id'][$activity_type_id] = 1; | |
396 | $this->_defaults['activity_type_id'][$activity_type_id] = 1; | |
6a488035 TO |
397 | } |
398 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
399 | ||
400 | if ($cid) { | |
401 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
402 | if ($cid > 0) { | |
403 | $this->_formValues['contact_id'] = $cid; | |
404 | ||
405 | $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this); | |
406 | ||
407 | if ($activity_role) { | |
408 | $this->_formValues['activity_role'] = $activity_role; | |
409 | } | |
410 | else { | |
6a488035 TO |
411 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name'); |
412 | } | |
413 | // also assign individual mode to the template | |
414 | $this->_single = TRUE; | |
415 | } | |
416 | } | |
8ef12e64 | 417 | |
4e636a74 | 418 | // Added for membership search |
8ef12e64 | 419 | |
4e636a74 AH |
420 | $signupType = CRM_Utils_Request::retrieve('signupType', 'Positive', |
421 | CRM_Core_DAO::$_nullObject | |
422 | ); | |
8ef12e64 | 423 | |
4e636a74 AH |
424 | if ($signupType) { |
425 | //$this->_formValues['activity_type_id'] = array(); | |
2e2acd1f | 426 | $this->_formValues['activity_role'] = 1; |
2e2acd1f | 427 | $this->_defaults['activity_role'] = 1; |
4e636a74 | 428 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); |
0370ad20 AH |
429 | |
430 | $renew = CRM_Utils_Array::key('Membership Renewal', $activityTypes); | |
431 | $signup = CRM_Utils_Array::key('Membership Signup', $activityTypes); | |
8ef12e64 | 432 | |
4e636a74 AH |
433 | switch ($signupType) { |
434 | case 3: // signups and renewals | |
435 | $this->_formValues['activity_type_id'][$renew] = 1; | |
436 | $this->_defaults['activity_type_id'][$renew] = 1; | |
437 | case 1: // signups only | |
438 | $this->_formValues['activity_type_id'][$signup] = 1; | |
439 | $this->_defaults['activity_type_id'][$signup] = 1; | |
440 | break; | |
8ef12e64 | 441 | |
4e636a74 AH |
442 | case 2: // renewals only |
443 | $this->_formValues['activity_type_id'][$renew] = 1; | |
444 | $this->_defaults['activity_type_id'][$renew] = 1; | |
445 | break; | |
446 | } | |
447 | } | |
8ef12e64 | 448 | |
0370ad20 | 449 | $dateLow = CRM_Utils_Request::retrieve('dateLow', 'String', |
4e636a74 AH |
450 | CRM_Core_DAO::$_nullObject |
451 | ); | |
8ef12e64 | 452 | |
4e636a74 | 453 | if ($dateLow) { |
0370ad20 AH |
454 | $dateLow = date('m/d/Y', strtotime($dateLow)); |
455 | $this->_formValues['activity_date_relative'] = 0; | |
456 | $this->_defaults['activity_date_relative'] = 0; | |
4e636a74 AH |
457 | $this->_formValues['activity_date_low'] = $dateLow; |
458 | $this->_defaults['activity_date_low'] = $dateLow; | |
459 | } | |
8ef12e64 | 460 | |
0370ad20 | 461 | $dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String', |
4e636a74 AH |
462 | CRM_Core_DAO::$_nullObject |
463 | ); | |
8ef12e64 | 464 | |
4e636a74 AH |
465 | if ($dateHigh) { |
466 | // Activity date time assumes midnight at the beginning of the date | |
467 | // This sets it to almost midnight at the end of the date | |
0370ad20 | 468 | /* if ($dateHigh <= 99999999) { |
4e636a74 | 469 | $dateHigh = 1000000 * $dateHigh + 235959; |
0370ad20 AH |
470 | } */ |
471 | $dateHigh = date('m/d/Y', strtotime($dateHigh)); | |
472 | $this->_formValues['activity_date_relative'] = 0; | |
473 | $this->_defaults['activity_date_relative'] = 0; | |
4e636a74 AH |
474 | $this->_formValues['activity_date_high'] = $dateHigh; |
475 | $this->_defaults['activity_date_high'] = $dateHigh; | |
8ef12e64 | 476 | } |
9231464f KJ |
477 | |
478 | if (!empty($this->_defaults)) { | |
479 | $this->setDefaults($this->_defaults); | |
480 | } | |
6a488035 TO |
481 | } |
482 | ||
483 | function getFormValues() { | |
484 | return NULL; | |
485 | } | |
486 | ||
487 | /** | |
488 | * Return a descriptive name for the page, used in wizard header | |
489 | * | |
490 | * @return string | |
491 | * @access public | |
492 | */ | |
493 | public function getTitle() { | |
494 | return ts('Find Activities'); | |
495 | } | |
496 | } | |
497 |