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 $this->set('searchFormName', 'Search');
66 $this->_actionButtonName
= $this->getButtonName('next', 'action');
72 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
73 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
81 if ($this->_context
=== 'user') {
82 $prefix = $this->_prefix
;
85 $this->assign("{$prefix}limit", $this->_limit
);
86 $this->assign("{$prefix}single", $this->_single
);
88 $controller = new CRM_Core_Selector_Controller($selector,
89 $this->get(CRM_Utils_Pager
::PAGE_ID
),
91 CRM_Core_Action
::VIEW
,
93 CRM_Core_Selector_Controller
::TRANSFER
,
97 $controller->setEmbedded(TRUE);
98 $controller->moveFromSessionToTemplate();
100 $this->assign('contributionSummary', $this->get('summary'));
109 public function setDefaultValues() {
110 $this->setDeprecatedDefaults();
111 $this->_defaults
= parent
::setDefaultValues();
113 $this->_defaults
= array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults
);
115 if (empty($this->_defaults
['contribution_status_id']) && !$this->_force
) {
116 // In force mode only parameters from the url will be used. When visible/ explicit this is a useful default.
117 $this->_defaults
['contribution_status_id'][1] = CRM_Core_PseudoConstant
::getKey(
118 'CRM_Contribute_BAO_Contribution',
119 'contribution_status_id',
124 // The membership or contribution id could be set on the form if viewing
125 // an embedded block on ParticipantView or MembershipView.
126 $membershipId = CRM_Utils_Request
::retrieve('memberId', 'Positive', $this);
127 if (isset($membershipId)) {
128 $this->_defaults
['contribution_membership_id'] = $membershipId;
130 $participantId = CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
131 if (isset($participantId)) {
132 $this->_defaults
['contribution_participant_id'] = $participantId;
135 return $this->_defaults
;
139 * Build the form object.
141 * @throws \CRM_Core_Exception
142 * @throws \CiviCRM_API3_Exception
144 public function buildQuickForm() {
145 if ($this->isFormInViewOrEditMode()) {
146 parent
::buildQuickForm();
147 $this->addContactSearchFields();
149 CRM_Contribute_BAO_Query
::buildSearchForm($this);
152 $rows = $this->get('rows');
153 if (is_array($rows)) {
154 if (!$this->_single
) {
155 $this->addRowSelectors($rows);
158 $permission = CRM_Core_Permission
::getPermission();
160 $queryParams = $this->get('queryParams');
161 $taskParams['softCreditFiltering'] = FALSE;
162 if (!empty($queryParams)) {
163 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($queryParams);
165 $tasks = CRM_Contribute_Task
::permissionedTaskTitles($permission, $taskParams);
166 $this->addTaskMenu($tasks);
172 * Get the label for the sortName field if email searching is on.
174 * (email searching is a setting under search preferences).
178 protected function getSortNameLabelWithEmail() {
179 return ts('Contributor Name or Email');
183 * Get the label for the sortName field if email searching is off.
185 * (email searching is a setting under search preferences).
189 protected function getSortNameLabelWithOutEmail() {
190 return ts('Contributor Name');
194 * Get the label for the tag field.
196 * We do this in a function so the 'ts' wraps the whole string to allow
197 * better translation.
201 protected function getTagLabel() {
202 return ts('Contributor Tag(s)');
206 * Get the label for the group field.
210 protected function getGroupLabel() {
211 return ts('Contributor Group(s)');
215 * Get the label for the group field.
219 protected function getContactTypeLabel() {
220 return ts('Contributor Contact Type');
224 * The post processing of the form gets done here.
226 * Key things done during post processing are
227 * - check for reset or next request. if present, skip post processing.
228 * - now check if user requested running a saved search, if so, then
229 * the form values associated with the saved search are used for searching.
230 * - if user has done a submit with new values the regular post submission is
232 * The processing consists of using a Selector / Controller framework for getting the
235 * @throws \CRM_Core_Exception
237 public function postProcess() {
244 $this->setFormValues();
245 // @todo - stop changing formValues - respect submitted form values, change a working array.
246 $this->fixFormValues();
248 // We don't show test records in summaries or dashboards
249 if (empty($this->_formValues
['contribution_test']) && $this->_force
&& !empty($this->_context
) && $this->_context
=== 'dashboard') {
250 // @todo - stop changing formValues - respect submitted form values, change a working array.
251 $this->_formValues
['contribution_test'] = 0;
255 'contribution_amount_low',
256 'contribution_amount_high',
258 if (isset($this->_formValues
[$f])) {
259 // @todo - stop changing formValues - respect submitted form values, change a working array.
260 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
264 if (!empty($_POST)) {
267 'contribution_soft_credit_type_id',
268 'contribution_status_id',
269 'contribution_trxn_id',
270 'contribution_product_id',
272 'payment_instrument_id',
273 'contribution_batch_id',
275 // @todo - stop changing formValues - respect submitted form values, change a working array.
276 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams);
278 // @todo - stop changing formValues - respect submitted form values, change a working array.
279 $tags = $this->_formValues
['contact_tags'] ??
NULL;
280 if ($tags && !is_array($tags)) {
281 // @todo - stop changing formValues - respect submitted form values, change a working array.
282 unset($this->_formValues
['contact_tags']);
283 $this->_formValues
['contact_tags'][$tags] = 1;
286 if ($tags && is_array($tags)) {
287 unset($this->_formValues
['contact_tags']);
288 foreach ($tags as $notImportant => $tagID) {
289 // @todo - stop changing formValues - respect submitted form values, change a working array.
290 $this->_formValues
['contact_tags'][$tagID] = 1;
294 $group = $this->_formValues
['group'] ??
NULL;
295 if ($group && !is_array($group)) {
296 // @todo - stop changing formValues - respect submitted form values, change a working array.
297 unset($this->_formValues
['group']);
298 $this->_formValues
['group'][$group] = 1;
301 if ($group && is_array($group)) {
302 unset($this->_formValues
['group']);
303 foreach ($group as $groupID) {
304 // @todo - stop changing formValues - respect submitted form values, change a working array.
305 $this->_formValues
['group'][$groupID] = 1;
310 // @todo - stop changing formValues - respect submitted form values, change a working array.
311 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
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);
393 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
395 $this->_formValues
['contact_id'] = $cid;
396 // @todo - why do we retrieve these when they are not used?
397 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
398 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
401 // also assign individual mode to the template
402 $this->_single
= TRUE;
406 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
410 $test = CRM_Utils_Request
::retrieve('test', 'Boolean');
412 $test = CRM_Utils_Type
::escape($test, 'Boolean');
413 $this->_formValues
['contribution_test'] = $test;
416 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
418 $this->_formValues
['contribution_recur_id'] = $recur;
419 $this->_formValues
['contribution_recurring'] = 1;
422 //check for contribution page id.
423 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
424 if ($contribPageId) {
425 $this->_formValues
['contribution_page_id'] = $contribPageId;
430 * Return a descriptive name for the page, used in wizard header.
434 public function getTitle() {
435 return ts('Find Contributions');
439 * Set the metadata for the form.
441 * @throws \CiviCRM_API3_Exception
443 protected function setSearchMetadata() {
444 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query
::getSearchFieldMetadata()]);
445 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur
::getContributionRecurSearchFieldMetadata()]);
449 * Handling for url params that are deprecated.
451 * @throws \CRM_Core_Exception
453 protected function setDeprecatedDefaults() {
454 $lowReceiveDate = CRM_Utils_Request
::retrieve('start', 'Timestamp');
455 if (!empty($lowReceiveDate)) {
456 $this->_formValues
['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
457 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_low not start');
459 $highReceiveDate = CRM_Utils_Request
::retrieve('end', 'Timestamp');
460 if (!empty($highReceiveDate)) {
461 $this->_formValues
['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
462 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_high not end');
464 //check for contribution page id.
465 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
466 if ($contribPageId) {
467 CRM_Core_Error
::deprecatedFunctionWarning('pass contribution_page_id');
468 $this->_formValues
['contribution_page_id'] = $contribPageId;