3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Advanced search, extends basic search.
21 class CRM_Contribute_Form_Search
extends CRM_Core_Form_Search
{
24 * The params that are sent to the query.
28 protected $_queryParams;
31 * Are we restricting ourselves to a single contact.
35 protected $_single = FALSE;
38 * Are we restricting ourselves to a single contact.
42 protected $_limit = NULL;
45 * Prefix for the controller.
48 protected $_prefix = 'contribute_';
53 public $submitOnce = TRUE;
56 * Explicitly declare the entity api name.
58 public function getDefaultEntity() {
59 return 'Contribution';
63 * Processing needed for buildForm and later.
65 * @throws \CiviCRM_API3_Exception
66 * @throws \CRM_Core_Exception
68 public function preProcess() {
69 // SearchFormName is deprecated & to be removed - the replacement is for the task to
70 // call $this->form->getSearchFormValues()
71 // A couple of extensions use it.
72 $this->set('searchFormName', 'Search');
74 $this->_actionButtonName
= $this->getButtonName('next', 'action');
80 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
81 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
89 if ($this->_context
=== 'user') {
90 $prefix = $this->_prefix
;
93 $this->assign("{$prefix}limit", $this->_limit
);
94 $this->assign("{$prefix}single", $this->_single
);
96 $controller = new CRM_Core_Selector_Controller($selector,
97 $this->get(CRM_Utils_Pager
::PAGE_ID
),
99 CRM_Core_Action
::VIEW
,
101 CRM_Core_Selector_Controller
::TRANSFER
,
105 $controller->setEmbedded(TRUE);
106 $controller->moveFromSessionToTemplate();
108 $this->assign('contributionSummary', $this->get('summary'));
117 public function setDefaultValues() {
118 $this->setDeprecatedDefaults();
119 $this->_defaults
= parent
::setDefaultValues();
121 $this->_defaults
= array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults
);
123 if (empty($this->_defaults
['contribution_status_id']) && !$this->_force
) {
124 // In force mode only parameters from the url will be used. When visible/ explicit this is a useful default.
125 $this->_defaults
['contribution_status_id'][1] = CRM_Core_PseudoConstant
::getKey(
126 'CRM_Contribute_BAO_Contribution',
127 'contribution_status_id',
132 // The membership or contribution id could be set on the form if viewing
133 // an embedded block on ParticipantView or MembershipView.
134 $membershipId = CRM_Utils_Request
::retrieve('memberId', 'Positive', $this);
135 if (isset($membershipId)) {
136 $this->_defaults
['contribution_membership_id'] = $membershipId;
138 $participantId = CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
139 if (isset($participantId)) {
140 $this->_defaults
['contribution_participant_id'] = $participantId;
143 return $this->_defaults
;
147 * Build the form object.
149 * @throws \CRM_Core_Exception
150 * @throws \CiviCRM_API3_Exception
152 public function buildQuickForm() {
153 if ($this->isFormInViewOrEditMode()) {
154 parent
::buildQuickForm();
155 $this->addContactSearchFields();
157 CRM_Contribute_BAO_Query
::buildSearchForm($this);
160 $rows = $this->get('rows');
161 if (is_array($rows)) {
162 if (!$this->_single
) {
163 $this->addRowSelectors($rows);
166 $permission = CRM_Core_Permission
::getPermission();
168 $queryParams = $this->get('queryParams');
169 $taskParams['softCreditFiltering'] = FALSE;
170 if (!empty($queryParams)) {
171 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($queryParams);
173 $tasks = CRM_Contribute_Task
::permissionedTaskTitles($permission, $taskParams);
174 $this->addTaskMenu($tasks);
180 * Get the label for the sortName field if email searching is on.
182 * (email searching is a setting under search preferences).
186 protected function getSortNameLabelWithEmail() {
187 return ts('Contributor Name or Email');
191 * Get the label for the sortName field if email searching is off.
193 * (email searching is a setting under search preferences).
197 protected function getSortNameLabelWithOutEmail() {
198 return ts('Contributor Name');
202 * Get the label for the tag field.
204 * We do this in a function so the 'ts' wraps the whole string to allow
205 * better translation.
209 protected function getTagLabel() {
210 return ts('Contributor Tag(s)');
214 * Get the label for the group field.
218 protected function getGroupLabel() {
219 return ts('Contributor Group(s)');
223 * Get the label for the group field.
227 protected function getContactTypeLabel() {
228 return ts('Contributor Contact Type');
232 * The post processing of the form gets done here.
234 * Key things done during post processing are
235 * - check for reset or next request. if present, skip post processing.
236 * - now check if user requested running a saved search, if so, then
237 * the form values associated with the saved search are used for searching.
238 * - if user has done a submit with new values the regular post submission is
240 * The processing consists of using a Selector / Controller framework for getting the
243 * @throws \CRM_Core_Exception
245 public function postProcess() {
252 $this->setFormValues();
253 // @todo - stop changing formValues - respect submitted form values, change a working array.
254 $this->fixFormValues();
256 // We don't show test records in summaries or dashboards
257 if (empty($this->_formValues
['contribution_test']) && $this->_force
&& !empty($this->_context
) && $this->_context
=== 'dashboard') {
258 // @todo - stop changing formValues - respect submitted form values, change a working array.
259 $this->_formValues
['contribution_test'] = 0;
263 'contribution_amount_low',
264 'contribution_amount_high',
266 if (isset($this->_formValues
[$f])) {
267 // @todo - stop changing formValues - respect submitted form values, change a working array.
268 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
272 if (!empty($_POST)) {
275 'contribution_soft_credit_type_id',
276 'contribution_status_id',
277 'contribution_trxn_id',
278 'contribution_product_id',
280 'payment_instrument_id',
281 'contribution_batch_id',
283 // @todo - stop changing formValues - respect submitted form values, change a working array.
284 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams);
286 // @todo - stop changing formValues - respect submitted form values, change a working array.
287 $tags = $this->_formValues
['contact_tags'] ??
NULL;
288 if ($tags && !is_array($tags)) {
289 // @todo - stop changing formValues - respect submitted form values, change a working array.
290 unset($this->_formValues
['contact_tags']);
291 $this->_formValues
['contact_tags'][$tags] = 1;
294 if ($tags && is_array($tags)) {
295 unset($this->_formValues
['contact_tags']);
296 foreach ($tags as $notImportant => $tagID) {
297 // @todo - stop changing formValues - respect submitted form values, change a working array.
298 $this->_formValues
['contact_tags'][$tagID] = 1;
302 $group = $this->_formValues
['group'] ??
NULL;
303 if ($group && !is_array($group)) {
304 // @todo - stop changing formValues - respect submitted form values, change a working array.
305 unset($this->_formValues
['group']);
306 $this->_formValues
['group'][$group] = 1;
309 if ($group && is_array($group)) {
310 unset($this->_formValues
['group']);
311 foreach ($group as $groupID) {
312 // @todo - stop changing formValues - respect submitted form values, change a working array.
313 $this->_formValues
['group'][$groupID] = 1;
318 // @todo - stop changing formValues - respect submitted form values, change a working array.
319 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
321 $this->set('queryParams', $this->_queryParams
);
323 $buttonName = $this->controller
->getButtonName();
324 if ($buttonName == $this->_actionButtonName
) {
325 // check actionName and if next, then do not repeat a search, since we are going to the next page
327 // hack, make sure we reset the task values
328 $stateMachine = $this->controller
->getStateMachine();
329 $formName = $stateMachine->getTaskFormName();
330 $this->controller
->resetPage($formName);
334 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
335 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
342 $selector->setKey($this->controller
->_key
);
345 if ($this->_context
=== 'basic' ||
$this->_context
=== 'user') {
346 $prefix = $this->_prefix
;
349 $controller = new CRM_Core_Selector_Controller($selector,
350 $this->get(CRM_Utils_Pager
::PAGE_ID
),
352 CRM_Core_Action
::VIEW
,
354 CRM_Core_Selector_Controller
::SESSION
,
357 $controller->setEmbedded(TRUE);
359 $query = &$selector->getQuery();
360 if ($this->_context
=== 'user') {
361 $query->setSkipPermission(TRUE);
368 * Use values from $_GET if force is set to TRUE.
370 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
372 * @throws \CRM_Core_Exception
374 public function fixFormValues() {
375 if (!$this->_force
) {
379 $status = CRM_Utils_Request
::retrieve('status', 'String');
381 $this->_formValues
['contribution_status_id'] = [$status => 1];
382 $this->_defaults
['contribution_status_id'] = [$status => 1];
385 $pcpid = (array) CRM_Utils_Request
::retrieve('pcpid', 'String', $this);
387 // Add new pcpid to the tail of the array...
388 foreach ($pcpid as $pcpIdList) {
389 $this->_formValues
['contribution_pcp_made_through_id'][] = $pcpIdList;
391 // and avoid any duplicate
392 $this->_formValues
['contribution_pcp_made_through_id'] = array_unique($this->_formValues
['contribution_pcp_made_through_id']);
395 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
397 // skip cid (contact id of membership/participant record) to get associated payments for membership/participant record,
398 // contribution record may be on different contact id.
399 $skip_cid = CRM_Utils_Request
::retrieve('skip_cid', 'Boolean', $this, FALSE, FALSE);
401 if ($cid && !$skip_cid) {
402 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
404 $this->_formValues
['contact_id'] = $cid;
405 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
408 // also assign individual mode to the template
409 $this->_single
= TRUE;
413 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
417 $test = CRM_Utils_Request
::retrieve('test', 'Boolean');
419 $test = CRM_Utils_Type
::escape($test, 'Boolean');
420 $this->_formValues
['contribution_test'] = $test;
423 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
425 $this->_formValues
['contribution_recur_id'] = $recur;
426 $this->_formValues
['contribution_recurring'] = 1;
429 //check for contribution page id.
430 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
431 if ($contribPageId) {
432 $this->_formValues
['contribution_page_id'] = $contribPageId;
437 * Return a descriptive name for the page, used in wizard header.
441 public function getTitle() {
442 return ts('Find Contributions');
446 * Set the metadata for the form.
448 * @throws \CiviCRM_API3_Exception
450 protected function setSearchMetadata() {
451 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query
::getSearchFieldMetadata()]);
452 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur
::getContributionRecurSearchFieldMetadata()]);
456 * Handling for url params that are deprecated.
458 * @throws \CRM_Core_Exception
460 protected function setDeprecatedDefaults() {
461 $lowReceiveDate = CRM_Utils_Request
::retrieve('start', 'Timestamp');
462 if (!empty($lowReceiveDate)) {
463 $this->_formValues
['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
464 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_low not start');
466 $highReceiveDate = CRM_Utils_Request
::retrieve('end', 'Timestamp');
467 if (!empty($highReceiveDate)) {
468 $this->_formValues
['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
469 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_high not end');
471 //check for contribution page id.
472 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
473 if ($contribPageId) {
474 CRM_Core_Error
::deprecatedFunctionWarning('pass contribution_page_id');
475 $this->_formValues
['contribution_page_id'] = $contribPageId;