Merge pull request #7182 from totten/4.6-gencode-mem
[civicrm-core.git] / CRM / Contribute / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
c5a11b83 35 * Advanced search, extends basic search.
6a488035 36 */
3efb5b86 37class CRM_Contribute_Form_Search extends CRM_Core_Form_Search {
6a488035 38
6a488035 39 /**
fe482240 40 * The params that are sent to the query.
6a488035
TO
41 *
42 * @var array
6a488035
TO
43 */
44 protected $_queryParams;
45
6a488035 46 /**
fe482240 47 * Are we restricting ourselves to a single contact.
6a488035 48 *
6a488035
TO
49 * @var boolean
50 */
51 protected $_single = FALSE;
52
53 /**
fe482240 54 * Are we restricting ourselves to a single contact.
6a488035 55 *
6a488035
TO
56 * @var boolean
57 */
58 protected $_limit = NULL;
59
6a488035
TO
60 protected $_defaults;
61
62 /**
fe482240 63 * Prefix for the controller.
6a488035
TO
64 */
65 protected $_prefix = "contribute_";
66
67 /**
fe482240 68 * Processing needed for buildForm and later.
95ea96be 69 */
acb1052e 70 public function preProcess() {
6a488035
TO
71 $this->set('searchFormName', 'Search');
72
6a488035 73 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
74 $this->_actionButtonName = $this->getButtonName('next', 'action');
75
76 $this->_done = FALSE;
c5a11b83 77 // @todo - is this an error - $this->_defaults is used.
6a488035
TO
78 $this->defaults = array();
79
80 /*
81 * we allow the controller to set force/reset externally, useful when we are being
82 * driven by the wizard framework
83 */
ed106721 84
353ffa53
TO
85 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
86 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
87 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
6a488035
TO
88 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
89
90 $this->assign("context", $this->_context);
91
92 // get user submitted values
93 // get it from controller only if form has been submitted, else preProcess has set this
94 if (!empty($_POST)) {
95 $this->_formValues = $this->controller->exportValues($this->_name);
96 }
97 else {
98 $this->_formValues = $this->get('formValues');
99 }
100
101 //membership ID
102 $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this);
103 if (isset($memberShipId)) {
104 $this->_formValues['contribution_membership_id'] = $memberShipId;
105 }
106 $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
107 if (isset($participantId)) {
108 $this->_formValues['contribution_participant_id'] = $participantId;
109 }
110
111 if ($this->_force) {
112 $this->postProcess();
113 $this->set('force', 0);
114 }
115
116 $sortID = NULL;
117 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
118 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
119 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
120 );
121 }
122
c94d39fd 123 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
6a488035
TO
124 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
125 $this->_action,
126 NULL,
127 $this->_single,
128 $this->_limit,
129 $this->_context
130 );
131 $prefix = NULL;
132 if ($this->_context == 'user') {
133 $prefix = $this->_prefix;
134 }
135
136 $this->assign("{$prefix}limit", $this->_limit);
137 $this->assign("{$prefix}single", $this->_single);
138
139 $controller = new CRM_Core_Selector_Controller($selector,
140 $this->get(CRM_Utils_Pager::PAGE_ID),
141 $sortID,
142 CRM_Core_Action::VIEW,
143 $this,
144 CRM_Core_Selector_Controller::TRANSFER,
145 $prefix
146 );
147
148 $controller->setEmbedded(TRUE);
149 $controller->moveFromSessionToTemplate();
150
151 $this->assign('contributionSummary', $this->get('summary'));
152 }
153
c5a11b83
EM
154 /**
155 * Set defaults.
156 *
157 * @return array
158 */
00be9182 159 public function setDefaultValues() {
874c9be7 160 if (empty($this->_defaults['contribution_status'])) {
6a488035
TO
161 $this->_defaults['contribution_status'][1] = 1;
162 }
163 return $this->_defaults;
164 }
165
166 /**
fe482240 167 * Build the form object.
6a488035 168 */
00be9182 169 public function buildQuickForm() {
3efb5b86 170 parent::buildQuickForm();
6a488035
TO
171 // text for sort_name
172 $this->addElement('text',
173 'sort_name',
174 ts('Contributor Name or Email'),
175 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact',
176 'sort_name'
177 )
178 );
179
24431f7b 180 $this->_group = CRM_Core_PseudoConstant::nestedGroup();
6a488035
TO
181
182 // multiselect for groups
183 if ($this->_group) {
184 $this->add('select', 'group', ts('Groups'), $this->_group, FALSE,
ab345ca5 185 array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
6a488035
TO
186 );
187 }
188
189 // multiselect for tags
6a488035
TO
190 $contactTags = CRM_Core_BAO_Tag::getTags();
191
192 if ($contactTags) {
193 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
ab345ca5 194 array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
6a488035
TO
195 );
196 }
197
198 CRM_Contribute_BAO_Query::buildSearchForm($this);
199
6a488035
TO
200 $rows = $this->get('rows');
201 if (is_array($rows)) {
202 if (!$this->_single) {
8d36b801 203 $this->addRowSelectors($rows);
6a488035
TO
204 }
205
6a488035
TO
206 $permission = CRM_Core_Permission::getPermission();
207
c410490a
DS
208 $queryParams = $this->get('queryParams');
209 $softCreditFiltering = FALSE;
210 if (!empty($queryParams)) {
211 $softCreditFiltering = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
212 }
34197a55
CW
213 $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $softCreditFiltering);
214 $this->addTaskMenu($tasks);
6a488035
TO
215 }
216
6a488035
TO
217 }
218
219 /**
220 * The post processing of the form gets done here.
221 *
222 * Key things done during post processing are
c5a11b83 223 * - check for reset or next request. if present, skip post processing.
6a488035
TO
224 * - now check if user requested running a saved search, if so, then
225 * the form values associated with the saved search are used for searching.
c5a11b83 226 * - if user has done a submit with new values the regular post submission is
6a488035
TO
227 * done.
228 * The processing consists of using a Selector / Controller framework for getting the
229 * search results.
6a488035 230 */
00be9182 231 public function postProcess() {
6a488035
TO
232 if ($this->_done) {
233 return;
234 }
235
236 $this->_done = TRUE;
237
238 if (!empty($_POST)) {
239 $this->_formValues = $this->controller->exportValues($this->_name);
240 }
241
242 $this->fixFormValues();
243
d43b88cc 244 // We don't show test records in summaries or dashboards
b899db8f 245 if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') {
6a488035
TO
246 $this->_formValues["contribution_test"] = 0;
247 }
248
249 foreach (array(
353ffa53 250 'contribution_amount_low',
acb1052e 251 'contribution_amount_high',
353ffa53 252 ) as $f) {
6a488035
TO
253 if (isset($this->_formValues[$f])) {
254 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
255 }
256 }
ed106721 257
6a488035 258 $config = CRM_Core_Config::singleton();
a2be2b19 259 if (!empty($_POST)) {
afa0b07c 260 $specialParams = array(
261 'financial_type_id',
262 'contribution_soft_credit_type_id',
263 'contribution_status_id',
264 'contribution_source',
acb1052e 265 'contribution_trxn_id',
b5a37491 266 'invoice_id',
afa0b07c 267 );
268 foreach ($specialParams as $element) {
3c151c70 269 $value = CRM_Utils_Array::value($element, $this->_formValues);
270 if ($value) {
9ab34172 271 if (is_array($value)) {
272 $this->_formValues[$element] = array('IN' => $value);
273 }
e51b7372 274 else {
275 $this->_formValues[$element] = array('LIKE' => "%$value%");
276 }
a2be2b19 277 }
1f0d8c92 278 }
1f0d8c92 279
a2be2b19
PN
280 $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues);
281 if ($tags && !is_array($tags)) {
282 unset($this->_formValues['contact_tags']);
283 $this->_formValues['contact_tags'][$tags] = 1;
284 }
ed106721 285
a2be2b19
PN
286 if ($tags && is_array($tags)) {
287 unset($this->_formValues['contact_tags']);
22e263ad 288 foreach ($tags as $notImportant => $tagID) {
a2be2b19
PN
289 $this->_formValues['contact_tags'][$tagID] = 1;
290 }
6a488035 291 }
ed106721 292
a2be2b19
PN
293 if (!$config->groupTree) {
294 $group = CRM_Utils_Array::value('group', $this->_formValues);
295 if ($group && !is_array($group)) {
296 unset($this->_formValues['group']);
297 $this->_formValues['group'][$group] = 1;
298 }
ed106721 299
a2be2b19
PN
300 if ($group && is_array($group)) {
301 unset($this->_formValues['group']);
22e263ad 302 foreach ($group as $notImportant => $groupID) {
a2be2b19
PN
303 $this->_formValues['group'][$groupID] = 1;
304 }
6a488035
TO
305 }
306 }
6a488035
TO
307 }
308
c94d39fd 309 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
310
311 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
312
313 $this->set('formValues', $this->_formValues);
314 $this->set('queryParams', $this->_queryParams);
315
316 $buttonName = $this->controller->getButtonName();
e341bbee 317 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
318 // check actionName and if next, then do not repeat a search, since we are going to the next page
319
320 // hack, make sure we reset the task values
ed106721 321 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
322 $formName = $stateMachine->getTaskFormName();
323 $this->controller->resetPage($formName);
324 return;
325 }
326
6a488035
TO
327 $sortID = NULL;
328 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
329 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
330 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
331 );
332 }
333
334 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
335 $selector = new CRM_Contribute_Selector_Search($this->_queryParams,
336 $this->_action,
337 NULL,
338 $this->_single,
339 $this->_limit,
340 $this->_context
341 );
342 $selector->setKey($this->controller->_key);
343
344 $prefix = NULL;
345 if ($this->_context == 'basic' || $this->_context == 'user') {
346 $prefix = $this->_prefix;
347 }
348
349 $controller = new CRM_Core_Selector_Controller($selector,
350 $this->get(CRM_Utils_Pager::PAGE_ID),
351 $sortID,
352 CRM_Core_Action::VIEW,
353 $this,
354 CRM_Core_Selector_Controller::SESSION,
355 $prefix
356 );
357 $controller->setEmbedded(TRUE);
358
359 $query = &$selector->getQuery();
360 if ($this->_context == 'user') {
361 $query->setSkipPermission(TRUE);
362 }
363 $summary = &$query->summaryContribution($this->_context);
364 $this->set('summary', $summary);
365 $this->assign('contributionSummary', $summary);
366 $controller->run();
367 }
368
c5a11b83
EM
369 /**
370 * Use values from $_GET if force is set to TRUE.
371 *
372 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
373 */
00be9182 374 public function fixFormValues() {
6a488035
TO
375 if (!$this->_force) {
376 return;
377 }
378
379 $status = CRM_Utils_Request::retrieve('status', 'String',
380 CRM_Core_DAO::$_nullObject
381 );
382 if ($status) {
383 $this->_formValues['contribution_status_id'] = array($status => 1);
384 $this->_defaults['contribution_status_id'] = array($status => 1);
385 }
386
387 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
388
389 if ($cid) {
390 $cid = CRM_Utils_Type::escape($cid, 'Integer');
391 if ($cid > 0) {
392 $this->_formValues['contact_id'] = $cid;
c5a11b83 393 // @todo - why do we retrieve these when they are not used?
6a488035
TO
394 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
395 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
396 'sort_name'
397 );
398 // also assign individual mode to the template
399 $this->_single = TRUE;
400 }
401 }
402
403 $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp',
404 CRM_Core_DAO::$_nullObject
405 );
406 if ($lowDate) {
407 $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp');
408 $date = CRM_Utils_Date::setDateDefaults($lowDate);
409 $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0];
410 }
411
412 $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp',
413 CRM_Core_DAO::$_nullObject
414 );
415 if ($highDate) {
416 $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp');
417 $date = CRM_Utils_Date::setDateDefaults($highDate);
418 $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0];
419 }
420
421 if ($highDate || $lowDate) {
422 //set the Choose Date Range value
423 $this->_formValues['contribution_date_relative'] = 0;
424 }
425
426 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
427 $this
428 );
429
430 $test = CRM_Utils_Request::retrieve('test', 'Boolean',
431 CRM_Core_DAO::$_nullObject
432 );
433 if (isset($test)) {
434 $test = CRM_Utils_Type::escape($test, 'Boolean');
435 $this->_formValues['contribution_test'] = $test;
436 }
437 //Recurring id
438 $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE);
439 if ($recur) {
440 $this->_formValues['contribution_recur_id'] = $recur;
441 $this->_formValues['contribution_recurring'] = 1;
442 }
443
444 //check for contribution page id.
445 $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this);
446 if ($contribPageId) {
447 $this->_formValues['contribution_page_id'] = $contribPageId;
448 }
449
450 //give values to default.
451 $this->_defaults = $this->_formValues;
452 }
453
454 /**
c5a11b83 455 * Return a descriptive name for the page, used in wizard header.
6a488035
TO
456 *
457 * @return string
6a488035
TO
458 */
459 public function getTitle() {
460 return ts('Find Contributions');
461 }
96025800 462
6a488035 463}