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;
261 // We don't show template records in summaries or dashboards
262 if (empty($this->_formValues
['is_template']) && $this->_force
&& !empty($this->_context
) && ($this->_context
=== 'dashboard' ||
$this->_context
=== 'contribution')) {
263 // @todo - stop changing formValues - respect submitted form values, change a working array.
264 $this->_formValues
['is_template'] = 0;
268 'contribution_amount_low',
269 'contribution_amount_high',
271 if (isset($this->_formValues
[$f])) {
272 // @todo - stop changing formValues - respect submitted form values, change a working array.
273 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
277 if (!empty($_POST)) {
280 'contribution_soft_credit_type_id',
281 'contribution_status_id',
282 'contribution_trxn_id',
283 'contribution_product_id',
285 'payment_instrument_id',
286 'contribution_batch_id',
288 // @todo - stop changing formValues - respect submitted form values, change a working array.
289 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams);
291 // @todo - stop changing formValues - respect submitted form values, change a working array.
292 $tags = $this->_formValues
['contact_tags'] ??
NULL;
293 if ($tags && !is_array($tags)) {
294 // @todo - stop changing formValues - respect submitted form values, change a working array.
295 unset($this->_formValues
['contact_tags']);
296 $this->_formValues
['contact_tags'][$tags] = 1;
299 if ($tags && is_array($tags)) {
300 unset($this->_formValues
['contact_tags']);
301 foreach ($tags as $notImportant => $tagID) {
302 // @todo - stop changing formValues - respect submitted form values, change a working array.
303 $this->_formValues
['contact_tags'][$tagID] = 1;
307 $group = $this->_formValues
['group'] ??
NULL;
308 if ($group && !is_array($group)) {
309 // @todo - stop changing formValues - respect submitted form values, change a working array.
310 unset($this->_formValues
['group']);
311 $this->_formValues
['group'][$group] = 1;
314 if ($group && is_array($group)) {
315 unset($this->_formValues
['group']);
316 foreach ($group as $groupID) {
317 // @todo - stop changing formValues - respect submitted form values, change a working array.
318 $this->_formValues
['group'][$groupID] = 1;
323 // @todo - stop changing formValues - respect submitted form values, change a working array.
324 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
326 $this->set('queryParams', $this->_queryParams
);
328 $buttonName = $this->controller
->getButtonName();
329 if ($buttonName == $this->_actionButtonName
) {
330 // check actionName and if next, then do not repeat a search, since we are going to the next page
332 // hack, make sure we reset the task values
333 $stateMachine = $this->controller
->getStateMachine();
334 $formName = $stateMachine->getTaskFormName();
335 $this->controller
->resetPage($formName);
339 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
340 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
347 $selector->setKey($this->controller
->_key
);
350 if ($this->_context
=== 'basic' ||
$this->_context
=== 'user') {
351 $prefix = $this->_prefix
;
354 $controller = new CRM_Core_Selector_Controller($selector,
355 $this->get(CRM_Utils_Pager
::PAGE_ID
),
357 CRM_Core_Action
::VIEW
,
359 CRM_Core_Selector_Controller
::SESSION
,
362 $controller->setEmbedded(TRUE);
364 $query = &$selector->getQuery();
365 if ($this->_context
=== 'user') {
366 $query->setSkipPermission(TRUE);
373 * Use values from $_GET if force is set to TRUE.
375 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
377 * @throws \CRM_Core_Exception
379 public function fixFormValues() {
380 if (!$this->_force
) {
384 $status = CRM_Utils_Request
::retrieve('status', 'String');
386 $this->_formValues
['contribution_status_id'] = [$status => 1];
387 $this->_defaults
['contribution_status_id'] = [$status => 1];
390 $pcpid = (array) CRM_Utils_Request
::retrieve('pcpid', 'String', $this);
392 // Add new pcpid to the tail of the array...
393 foreach ($pcpid as $pcpIdList) {
394 $this->_formValues
['contribution_pcp_made_through_id'][] = $pcpIdList;
396 // and avoid any duplicate
397 $this->_formValues
['contribution_pcp_made_through_id'] = array_unique($this->_formValues
['contribution_pcp_made_through_id']);
400 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
402 // skip cid (contact id of membership/participant record) to get associated payments for membership/participant record,
403 // contribution record may be on different contact id.
404 $skip_cid = CRM_Utils_Request
::retrieve('skip_cid', 'Boolean', $this, FALSE, FALSE);
406 if ($cid && !$skip_cid) {
407 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
409 $this->_formValues
['contact_id'] = $cid;
410 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
413 // also assign individual mode to the template
414 $this->_single
= TRUE;
418 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
422 $test = CRM_Utils_Request
::retrieve('test', 'Boolean');
424 $test = CRM_Utils_Type
::escape($test, 'Boolean');
425 $this->_formValues
['contribution_test'] = $test;
428 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
430 $this->_formValues
['contribution_recur_id'] = $recur;
431 $this->_formValues
['contribution_recurring'] = 1;
434 //check for contribution page id.
435 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
436 if ($contribPageId) {
437 $this->_formValues
['contribution_page_id'] = $contribPageId;
442 * Return a descriptive name for the page, used in wizard header.
446 public function getTitle() {
447 return ts('Find Contributions');
451 * Set the metadata for the form.
453 * @throws \CiviCRM_API3_Exception
455 protected function setSearchMetadata() {
456 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query
::getSearchFieldMetadata()]);
457 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur
::getContributionRecurSearchFieldMetadata()]);
461 * Handling for url params that are deprecated.
463 * @throws \CRM_Core_Exception
465 protected function setDeprecatedDefaults() {
466 $lowReceiveDate = CRM_Utils_Request
::retrieve('start', 'Timestamp');
467 if (!empty($lowReceiveDate)) {
468 $this->_formValues
['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
469 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_low not start');
471 $highReceiveDate = CRM_Utils_Request
::retrieve('end', 'Timestamp');
472 if (!empty($highReceiveDate)) {
473 $this->_formValues
['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
474 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_high not end');
476 //check for contribution page id.
477 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
478 if ($contribPageId) {
479 CRM_Core_Error
::deprecatedFunctionWarning('pass contribution_page_id');
480 $this->_formValues
['contribution_page_id'] = $contribPageId;