CRM-13981 fix - Migrate "In Honor of" to Soft Credits
[civicrm-core.git] / CRM / Contribute / Form / Search.php
CommitLineData
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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * Files required
38 */
39
40/**
41 * advanced search, extends basic search
42 */
43class CRM_Contribute_Form_Search extends CRM_Core_Form {
44
45 /**
46 * Are we forced to run a search
47 *
48 * @var int
49 * @access protected
50 */
51 protected $_force;
52
53 /**
54 * name of search button
55 *
56 * @var string
57 * @access protected
58 */
59 protected $_searchButtonName;
60
61 /**
62 * name of print button
63 *
64 * @var string
65 * @access protected
66 */
67 protected $_printButtonName;
68
69 /**
70 * name of action button
71 *
72 * @var string
73 * @access protected
74 */
75 protected $_actionButtonName;
76
77 /**
78 * form values that we will be using
79 *
80 * @var array
f1751139 81 * @access public
6a488035 82 */
f1751139 83 public $_formValues;
6a488035
TO
84
85 /**
86 * the params that are sent to the query
87 *
88 * @var array
89 * @access protected
90 */
91 protected $_queryParams;
92
93 /**
94 * have we already done this search
95 *
96 * @access protected
97 * @var boolean
98 */
99 protected $_done;
100
101 /**
102 * are we restricting ourselves to a single contact
103 *
104 * @access protected
105 * @var boolean
106 */
107 protected $_single = FALSE;
108
109 /**
110 * are we restricting ourselves to a single contact
111 *
112 * @access protected
113 * @var boolean
114 */
115 protected $_limit = NULL;
116
117 /**
118 * what context are we being invoked from
119 *
120 * @access protected
121 * @var string
122 */
123 protected $_context = NULL;
124
125 protected $_defaults;
126
127 /**
128 * prefix for the controller
129 *
130 */
131 protected $_prefix = "contribute_";
132
133 /**
134 * processing needed for buildForm and later
135 *
136 * @return void
137 * @access public
138 */ function preProcess() {
139 $this->set('searchFormName', 'Search');
140
141 /**
142 * set the button names
143 */
144 $this->_searchButtonName = $this->getButtonName('refresh');
145 $this->_printButtonName = $this->getButtonName('next', 'print');
146 $this->_actionButtonName = $this->getButtonName('next', 'action');
147
148 $this->_done = FALSE;
149 $this->defaults = array();
150
151 /*
152 * we allow the controller to set force/reset externally, useful when we are being
153 * driven by the wizard framework
154 */
ed106721 155
6a488035
TO
156 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
157 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
158 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
159 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
160
161 $this->assign("context", $this->_context);
162
163 // get user submitted values
164 // get it from controller only if form has been submitted, else preProcess has set this
165 if (!empty($_POST)) {
166 $this->_formValues = $this->controller->exportValues($this->_name);
167 }
168 else {
169 $this->_formValues = $this->get('formValues');
170 }
171
172 //membership ID
173 $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
174 if (isset($memberShipId)) {
175 $this->_formValues['contribution_membership_id'] = $memberShipId;
176 }
177 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
178 if (isset($participantId)) {
179 $this->_formValues['contribution_participant_id'] = $participantId;
180 }
181
182 if ($this->_force) {
183 $this->postProcess();
184 $this->set('force', 0);
185 }
186
187 $sortID = NULL;
188 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
189 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
190 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
191 );
192 }
193
194 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
195 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
196 $this->_action,
197 NULL,
198 $this->_single,
199 $this->_limit,
200 $this->_context
201 );
202 $prefix = NULL;
203 if ($this->_context == 'user') {
204 $prefix = $this->_prefix;
205 }
206
207 $this->assign("{$prefix}limit", $this->_limit);
208 $this->assign("{$prefix}single", $this->_single);
209
210 $controller = new CRM_Core_Selector_Controller($selector,
211 $this->get(CRM_Utils_Pager::PAGE_ID),
212 $sortID,
213 CRM_Core_Action::VIEW,
214 $this,
215 CRM_Core_Selector_Controller::TRANSFER,
216 $prefix
217 );
218
219 $controller->setEmbedded(TRUE);
220 $controller->moveFromSessionToTemplate();
221
222 $this->assign('contributionSummary', $this->get('summary'));
223 }
224
225 function setDefaultValues() {
226 if (!CRM_Utils_Array::value('contribution_status',
227 $this->_defaults
228 )) {
229 $this->_defaults['contribution_status'][1] = 1;
230 }
231 return $this->_defaults;
232 }
233
234 /**
235 * Build the form
236 *
237 * @access public
238 *
239 * @return void
240 */
241 function buildQuickForm() {
242 // text for sort_name
243 $this->addElement('text',
244 'sort_name',
245 ts('Contributor Name or Email'),
246 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact',
247 'sort_name'
248 )
249 );
250
251 $this->_group = CRM_Core_PseudoConstant::group();
252
253 // multiselect for groups
254 if ($this->_group) {
255 $this->add('select', 'group', ts('Groups'), $this->_group, FALSE,
256 array('id' => 'group', 'multiple' => 'multiple', 'title' => ts('- select -'))
257 );
258 }
259
260 // multiselect for tags
261 require_once 'CRM/Core/BAO/Tag.php';
262 $contactTags = CRM_Core_BAO_Tag::getTags();
263
264 if ($contactTags) {
265 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
266 array('id' => 'contact_tags', 'multiple' => 'multiple', 'title' => ts('- select -'))
267 );
268 }
269
270 CRM_Contribute_BAO_Query::buildSearchForm($this);
271
272 /*
273 * add form checkboxes for each row. This is needed out here to conform to QF protocol
274 * of all elements being declared in builQuickForm
275 */
ed106721 276
6a488035
TO
277 $rows = $this->get('rows');
278 if (is_array($rows)) {
279 if (!$this->_single) {
280 $this->addElement('checkbox',
281 'toggleSelect',
282 NULL,
283 NULL,
284 array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);")
285 );
286 foreach ($rows as $row) {
287 $this->addElement('checkbox', $row['checkbox'],
288 NULL, NULL,
289 array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');")
290 );
291 }
292 }
293
294 $total = $cancel = 0;
295
296 $permission = CRM_Core_Permission::getPermission();
297
298 $tasks = array('' => ts('- actions -')) + CRM_Contribute_Task::permissionedTaskTitles($permission);
299 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
300 $this->add('submit', $this->_actionButtonName, ts('Go'),
301 array(
302 'class' => 'form-submit',
303 'id' => 'Go',
304 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
305 )
306 );
307
308 $this->add('submit', $this->_printButtonName, ts('Print'),
309 array(
310 'class' => 'form-submit',
311 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 1);",
312 )
313 );
314
315 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
316 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
317 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('onclick' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );"));
318 }
319
320 // add buttons
321 $this->addButtons(array(
322 array(
323 'type' => 'refresh',
324 'name' => ts('Search'),
325 'isDefault' => TRUE,
326 ),
327 )
328 );
329 }
330
331 /**
332 * The post processing of the form gets done here.
333 *
334 * Key things done during post processing are
335 * - check for reset or next request. if present, skip post procesing.
336 * - now check if user requested running a saved search, if so, then
337 * the form values associated with the saved search are used for searching.
338 * - if user has done a submit with new values the regular post submissing is
339 * done.
340 * The processing consists of using a Selector / Controller framework for getting the
341 * search results.
342 *
343 * @param
344 *
345 * @return void
346 * @access public
347 */
348 function postProcess() {
349 if ($this->_done) {
350 return;
351 }
352
353 $this->_done = TRUE;
354
355 if (!empty($_POST)) {
356 $this->_formValues = $this->controller->exportValues($this->_name);
357 }
358
359 $this->fixFormValues();
360
d43b88cc
CW
361 // We don't show test records in summaries or dashboards
362 if (empty($this->_formValues['contribution_test']) && $this->_force) {
6a488035
TO
363 $this->_formValues["contribution_test"] = 0;
364 }
365
366 foreach (array(
367 'contribution_amount_low', 'contribution_amount_high') as $f) {
368 if (isset($this->_formValues[$f])) {
369 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
370 }
371 }
ed106721 372
6a488035 373 $config = CRM_Core_Config::singleton();
1f0d8c92
PN
374 // CRM-13848
375 $financialType = CRM_Utils_Array::value('financial_type_id', $this->_formValues);
376 if ($financialType && is_array($financialType)) {
377 unset($this->_formValues['financial_type_id']);
378 foreach($financialType as $notImportant => $typeID) {
379 $this->_formValues['financial_type_id'][$typeID] = 1;
380 }
381 }
382
6a488035
TO
383 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
384 if ($tags && !is_array($tags)) {
385 unset($this->_formValues['contact_tags']);
386 $this->_formValues['contact_tags'][$tags] = 1;
387 }
ed106721 388
6a488035
TO
389 if ($tags && is_array($tags)) {
390 unset($this->_formValues['contact_tags']);
391 foreach($tags as $notImportant => $tagID) {
1f0d8c92 392 $this->_formValues['contact_tags'][$tagID] = 1;
6a488035
TO
393 }
394 }
ed106721
DL
395
396
6a488035
TO
397 if (!$config->groupTree) {
398 $group = CRM_Utils_Array::value('group', $this->_formValues);
399 if ($group && !is_array($group)) {
400 unset($this->_formValues['group']);
401 $this->_formValues['group'][$group] = 1;
402 }
ed106721 403
6a488035
TO
404 if ($group && is_array($group)) {
405 unset($this->_formValues['group']);
406 foreach($group as $notImportant => $groupID) {
1f0d8c92 407 $this->_formValues['group'][$groupID] = 1;
6a488035
TO
408 }
409 }
ed106721 410
6a488035
TO
411 }
412
413 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
414
415 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
416
417 $this->set('formValues', $this->_formValues);
418 $this->set('queryParams', $this->_queryParams);
419
420 $buttonName = $this->controller->getButtonName();
421 if ($buttonName == $this->_actionButtonName || $buttonName == $this->_printButtonName) {
422 // check actionName and if next, then do not repeat a search, since we are going to the next page
423
424 // hack, make sure we reset the task values
ed106721 425 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
426 $formName = $stateMachine->getTaskFormName();
427 $this->controller->resetPage($formName);
428 return;
429 }
430
431
432 $sortID = NULL;
433 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
434 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
435 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
436 );
437 }
438
439 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
440 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
441 $this->_action,
442 NULL,
443 $this->_single,
444 $this->_limit,
445 $this->_context
446 );
447 $selector->setKey($this->controller->_key);
448
449 $prefix = NULL;
450 if ($this->_context == 'basic' || $this->_context == 'user') {
451 $prefix = $this->_prefix;
452 }
453
454 $controller = new CRM_Core_Selector_Controller($selector,
455 $this->get(CRM_Utils_Pager::PAGE_ID),
456 $sortID,
457 CRM_Core_Action::VIEW,
458 $this,
459 CRM_Core_Selector_Controller::SESSION,
460 $prefix
461 );
462 $controller->setEmbedded(TRUE);
463
464 $query = &$selector->getQuery();
465 if ($this->_context == 'user') {
466 $query->setSkipPermission(TRUE);
467 }
468 $summary = &$query->summaryContribution($this->_context);
469 $this->set('summary', $summary);
470 $this->assign('contributionSummary', $summary);
471 $controller->run();
472 }
473
474 function fixFormValues() {
475 // if this search has been forced
476 // then see if there are any get values, and if so over-ride the post values
477 // note that this means that GET over-rides POST :)
478
479 if (!$this->_force) {
480 return;
481 }
482
483 $status = CRM_Utils_Request::retrieve('status', 'String',
484 CRM_Core_DAO::$_nullObject
485 );
486 if ($status) {
487 $this->_formValues['contribution_status_id'] = array($status => 1);
488 $this->_defaults['contribution_status_id'] = array($status => 1);
489 }
490
491 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
492
493 if ($cid) {
494 $cid = CRM_Utils_Type::escape($cid, 'Integer');
495 if ($cid > 0) {
496 $this->_formValues['contact_id'] = $cid;
497 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
498 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
499 'sort_name'
500 );
501 // also assign individual mode to the template
502 $this->_single = TRUE;
503 }
504 }
505
506 $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp',
507 CRM_Core_DAO::$_nullObject
508 );
509 if ($lowDate) {
510 $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp');
511 $date = CRM_Utils_Date::setDateDefaults($lowDate);
512 $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0];
513 }
514
515 $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp',
516 CRM_Core_DAO::$_nullObject
517 );
518 if ($highDate) {
519 $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp');
520 $date = CRM_Utils_Date::setDateDefaults($highDate);
521 $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0];
522 }
523
524 if ($highDate || $lowDate) {
525 //set the Choose Date Range value
526 $this->_formValues['contribution_date_relative'] = 0;
527 }
528
529 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
530 $this
531 );
532
533 $test = CRM_Utils_Request::retrieve('test', 'Boolean',
534 CRM_Core_DAO::$_nullObject
535 );
536 if (isset($test)) {
537 $test = CRM_Utils_Type::escape($test, 'Boolean');
538 $this->_formValues['contribution_test'] = $test;
539 }
540 //Recurring id
541 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
542 if ($recur) {
543 $this->_formValues['contribution_recur_id'] = $recur;
544 $this->_formValues['contribution_recurring'] = 1;
545 }
546
547 //check for contribution page id.
548 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
549 if ($contribPageId) {
550 $this->_formValues['contribution_page_id'] = $contribPageId;
551 }
552
553 //give values to default.
554 $this->_defaults = $this->_formValues;
555 }
556
557 /**
558 * Return a descriptive name for the page, used in wizard header
559 *
560 * @return string
561 * @access public
562 */
563 public function getTitle() {
564 return ts('Find Contributions');
565 }
566}
567