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