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_';
51 * Explicitly declare the entity api name.
53 public function getDefaultEntity() {
54 return 'Contribution';
58 * Processing needed for buildForm and later.
60 * @throws \CiviCRM_API3_Exception
61 * @throws \CRM_Core_Exception
63 public function preProcess() {
64 // SearchFormName is deprecated & to be removed - the replacement is for the task to
65 // call $this->form->getSearchFormValues()
66 // A couple of extensions use it.
67 $this->set('searchFormName', 'Search');
69 $this->_actionButtonName
= $this->getButtonName('next', 'action');
75 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
76 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
84 if ($this->_context
=== 'user') {
85 $prefix = $this->_prefix
;
88 $this->assign("{$prefix}limit", $this->_limit
);
89 $this->assign("{$prefix}single", $this->_single
);
91 $controller = new CRM_Core_Selector_Controller($selector,
92 $this->get(CRM_Utils_Pager
::PAGE_ID
),
94 CRM_Core_Action
::VIEW
,
96 CRM_Core_Selector_Controller
::TRANSFER
,
100 $controller->setEmbedded(TRUE);
101 $controller->moveFromSessionToTemplate();
103 $this->assign('contributionSummary', $this->get('summary'));
112 public function setDefaultValues() {
113 $this->setDeprecatedDefaults();
114 $this->_defaults
= parent
::setDefaultValues();
116 $this->_defaults
= array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults
);
118 if (empty($this->_defaults
['contribution_status_id']) && !$this->_force
) {
119 // In force mode only parameters from the url will be used. When visible/ explicit this is a useful default.
120 $this->_defaults
['contribution_status_id'][1] = CRM_Core_PseudoConstant
::getKey(
121 'CRM_Contribute_BAO_Contribution',
122 'contribution_status_id',
127 // The membership or contribution id could be set on the form if viewing
128 // an embedded block on ParticipantView or MembershipView.
129 $membershipId = CRM_Utils_Request
::retrieve('memberId', 'Positive', $this);
130 if (isset($membershipId)) {
131 $this->_defaults
['contribution_membership_id'] = $membershipId;
133 $participantId = CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
134 if (isset($participantId)) {
135 $this->_defaults
['contribution_participant_id'] = $participantId;
138 return $this->_defaults
;
142 * Build the form object.
144 * @throws \CRM_Core_Exception
145 * @throws \CiviCRM_API3_Exception
147 public function buildQuickForm() {
148 if ($this->isFormInViewOrEditMode()) {
149 parent
::buildQuickForm();
150 $this->addContactSearchFields();
152 CRM_Contribute_BAO_Query
::buildSearchForm($this);
155 $rows = $this->get('rows');
156 if (is_array($rows)) {
157 if (!$this->_single
) {
158 $this->addRowSelectors($rows);
161 $permission = CRM_Core_Permission
::getPermission();
163 $queryParams = $this->get('queryParams');
164 $taskParams['softCreditFiltering'] = FALSE;
165 if (!empty($queryParams)) {
166 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($queryParams);
168 $tasks = CRM_Contribute_Task
::permissionedTaskTitles($permission, $taskParams);
169 $this->addTaskMenu($tasks);
175 * Get the label for the sortName field if email searching is on.
177 * (email searching is a setting under search preferences).
181 protected function getSortNameLabelWithEmail() {
182 return ts('Contributor Name or Email');
186 * Get the label for the sortName field if email searching is off.
188 * (email searching is a setting under search preferences).
192 protected function getSortNameLabelWithOutEmail() {
193 return ts('Contributor Name');
197 * Get the label for the tag field.
199 * We do this in a function so the 'ts' wraps the whole string to allow
200 * better translation.
204 protected function getTagLabel() {
205 return ts('Contributor Tag(s)');
209 * Get the label for the group field.
213 protected function getGroupLabel() {
214 return ts('Contributor Group(s)');
218 * Get the label for the group field.
222 protected function getContactTypeLabel() {
223 return ts('Contributor Contact Type');
227 * The post processing of the form gets done here.
229 * Key things done during post processing are
230 * - check for reset or next request. if present, skip post processing.
231 * - now check if user requested running a saved search, if so, then
232 * the form values associated with the saved search are used for searching.
233 * - if user has done a submit with new values the regular post submission is
235 * The processing consists of using a Selector / Controller framework for getting the
238 * @throws \CRM_Core_Exception
240 public function postProcess() {
247 $this->setFormValues();
248 // @todo - stop changing formValues - respect submitted form values, change a working array.
249 $this->fixFormValues();
251 // We don't show test records in summaries or dashboards
252 if (empty($this->_formValues
['contribution_test']) && $this->_force
&& !empty($this->_context
) && $this->_context
=== 'dashboard') {
253 // @todo - stop changing formValues - respect submitted form values, change a working array.
254 $this->_formValues
['contribution_test'] = 0;
258 'contribution_amount_low',
259 'contribution_amount_high',
261 if (isset($this->_formValues
[$f])) {
262 // @todo - stop changing formValues - respect submitted form values, change a working array.
263 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
267 if (!empty($_POST)) {
270 'contribution_soft_credit_type_id',
271 'contribution_status_id',
272 'contribution_trxn_id',
273 'contribution_product_id',
275 'payment_instrument_id',
276 'contribution_batch_id',
278 // @todo - stop changing formValues - respect submitted form values, change a working array.
279 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams);
281 // @todo - stop changing formValues - respect submitted form values, change a working array.
282 $tags = $this->_formValues
['contact_tags'] ??
NULL;
283 if ($tags && !is_array($tags)) {
284 // @todo - stop changing formValues - respect submitted form values, change a working array.
285 unset($this->_formValues
['contact_tags']);
286 $this->_formValues
['contact_tags'][$tags] = 1;
289 if ($tags && is_array($tags)) {
290 unset($this->_formValues
['contact_tags']);
291 foreach ($tags as $notImportant => $tagID) {
292 // @todo - stop changing formValues - respect submitted form values, change a working array.
293 $this->_formValues
['contact_tags'][$tagID] = 1;
297 $group = $this->_formValues
['group'] ??
NULL;
298 if ($group && !is_array($group)) {
299 // @todo - stop changing formValues - respect submitted form values, change a working array.
300 unset($this->_formValues
['group']);
301 $this->_formValues
['group'][$group] = 1;
304 if ($group && is_array($group)) {
305 unset($this->_formValues
['group']);
306 foreach ($group as $groupID) {
307 // @todo - stop changing formValues - respect submitted form values, change a working array.
308 $this->_formValues
['group'][$groupID] = 1;
313 // @todo - stop changing formValues - respect submitted form values, change a working array.
314 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
316 $this->set('queryParams', $this->_queryParams
);
318 $buttonName = $this->controller
->getButtonName();
319 if ($buttonName == $this->_actionButtonName
) {
320 // check actionName and if next, then do not repeat a search, since we are going to the next page
322 // hack, make sure we reset the task values
323 $stateMachine = $this->controller
->getStateMachine();
324 $formName = $stateMachine->getTaskFormName();
325 $this->controller
->resetPage($formName);
329 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
330 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
337 $selector->setKey($this->controller
->_key
);
340 if ($this->_context
=== 'basic' ||
$this->_context
=== 'user') {
341 $prefix = $this->_prefix
;
344 $controller = new CRM_Core_Selector_Controller($selector,
345 $this->get(CRM_Utils_Pager
::PAGE_ID
),
347 CRM_Core_Action
::VIEW
,
349 CRM_Core_Selector_Controller
::SESSION
,
352 $controller->setEmbedded(TRUE);
354 $query = &$selector->getQuery();
355 if ($this->_context
=== 'user') {
356 $query->setSkipPermission(TRUE);
363 * Use values from $_GET if force is set to TRUE.
365 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
367 * @throws \CRM_Core_Exception
369 public function fixFormValues() {
370 if (!$this->_force
) {
374 $status = CRM_Utils_Request
::retrieve('status', 'String');
376 $this->_formValues
['contribution_status_id'] = [$status => 1];
377 $this->_defaults
['contribution_status_id'] = [$status => 1];
380 $pcpid = (array) CRM_Utils_Request
::retrieve('pcpid', 'String', $this);
382 // Add new pcpid to the tail of the array...
383 foreach ($pcpid as $pcpIdList) {
384 $this->_formValues
['contribution_pcp_made_through_id'][] = $pcpIdList;
386 // and avoid any duplicate
387 $this->_formValues
['contribution_pcp_made_through_id'] = array_unique($this->_formValues
['contribution_pcp_made_through_id']);
390 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
392 // skip cid (contact id of membership/participant record) to get associated payments for membership/participant record,
393 // contribution record may be on different contact id.
394 $skip_cid = CRM_Utils_Request
::retrieve('skip_cid', 'Boolean', $this, FALSE, FALSE);
396 if ($cid && !$skip_cid) {
397 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
399 $this->_formValues
['contact_id'] = $cid;
400 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
403 // also assign individual mode to the template
404 $this->_single
= TRUE;
408 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
412 $test = CRM_Utils_Request
::retrieve('test', 'Boolean');
414 $test = CRM_Utils_Type
::escape($test, 'Boolean');
415 $this->_formValues
['contribution_test'] = $test;
418 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
420 $this->_formValues
['contribution_recur_id'] = $recur;
421 $this->_formValues
['contribution_recurring'] = 1;
424 //check for contribution page id.
425 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
426 if ($contribPageId) {
427 $this->_formValues
['contribution_page_id'] = $contribPageId;
432 * Return a descriptive name for the page, used in wizard header.
436 public function getTitle() {
437 return ts('Find Contributions');
441 * Set the metadata for the form.
443 * @throws \CiviCRM_API3_Exception
445 protected function setSearchMetadata() {
446 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query
::getSearchFieldMetadata()]);
447 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur
::getContributionRecurSearchFieldMetadata()]);
451 * Handling for url params that are deprecated.
453 * @throws \CRM_Core_Exception
455 protected function setDeprecatedDefaults() {
456 $lowReceiveDate = CRM_Utils_Request
::retrieve('start', 'Timestamp');
457 if (!empty($lowReceiveDate)) {
458 $this->_formValues
['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
459 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_low not start');
461 $highReceiveDate = CRM_Utils_Request
::retrieve('end', 'Timestamp');
462 if (!empty($highReceiveDate)) {
463 $this->_formValues
['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
464 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_high not end');
466 //check for contribution page id.
467 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
468 if ($contribPageId) {
469 CRM_Core_Error
::deprecatedFunctionWarning('pass contribution_page_id');
470 $this->_formValues
['contribution_page_id'] = $contribPageId;