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 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
313 $this->set('queryParams', $this->_queryParams
);
315 $buttonName = $this->controller
->getButtonName();
316 if ($buttonName == $this->_actionButtonName
) {
317 // check actionName and if next, then do not repeat a search, since we are going to the next page
319 // hack, make sure we reset the task values
320 $stateMachine = $this->controller
->getStateMachine();
321 $formName = $stateMachine->getTaskFormName();
322 $this->controller
->resetPage($formName);
326 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
327 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
334 $selector->setKey($this->controller
->_key
);
337 if ($this->_context
=== 'basic' ||
$this->_context
=== 'user') {
338 $prefix = $this->_prefix
;
341 $controller = new CRM_Core_Selector_Controller($selector,
342 $this->get(CRM_Utils_Pager
::PAGE_ID
),
344 CRM_Core_Action
::VIEW
,
346 CRM_Core_Selector_Controller
::SESSION
,
349 $controller->setEmbedded(TRUE);
351 $query = &$selector->getQuery();
352 if ($this->_context
=== 'user') {
353 $query->setSkipPermission(TRUE);
360 * Use values from $_GET if force is set to TRUE.
362 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
364 * @throws \CRM_Core_Exception
366 public function fixFormValues() {
367 if (!$this->_force
) {
371 $status = CRM_Utils_Request
::retrieve('status', 'String');
373 $this->_formValues
['contribution_status_id'] = [$status => 1];
374 $this->_defaults
['contribution_status_id'] = [$status => 1];
377 $pcpid = (array) CRM_Utils_Request
::retrieve('pcpid', 'String', $this);
379 // Add new pcpid to the tail of the array...
380 foreach ($pcpid as $pcpIdList) {
381 $this->_formValues
['contribution_pcp_made_through_id'][] = $pcpIdList;
383 // and avoid any duplicate
384 $this->_formValues
['contribution_pcp_made_through_id'] = array_unique($this->_formValues
['contribution_pcp_made_through_id']);
387 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
390 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
392 $this->_formValues
['contact_id'] = $cid;
393 // @todo - why do we retrieve these when they are not used?
394 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
395 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
398 // also assign individual mode to the template
399 $this->_single
= TRUE;
403 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
407 $test = CRM_Utils_Request
::retrieve('test', 'Boolean');
409 $test = CRM_Utils_Type
::escape($test, 'Boolean');
410 $this->_formValues
['contribution_test'] = $test;
413 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
415 $this->_formValues
['contribution_recur_id'] = $recur;
416 $this->_formValues
['contribution_recurring'] = 1;
419 //check for contribution page id.
420 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
421 if ($contribPageId) {
422 $this->_formValues
['contribution_page_id'] = $contribPageId;
427 * Return a descriptive name for the page, used in wizard header.
431 public function getTitle() {
432 return ts('Find Contributions');
436 * Set the metadata for the form.
438 * @throws \CiviCRM_API3_Exception
440 protected function setSearchMetadata() {
441 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query
::getSearchFieldMetadata()]);
442 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur
::getContributionRecurSearchFieldMetadata()]);
446 * Handling for url params that are deprecated.
448 * @throws \CRM_Core_Exception
450 protected function setDeprecatedDefaults() {
451 $lowReceiveDate = CRM_Utils_Request
::retrieve('start', 'Timestamp');
452 if (!empty($lowReceiveDate)) {
453 $this->_formValues
['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
454 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_low not start');
456 $highReceiveDate = CRM_Utils_Request
::retrieve('end', 'Timestamp');
457 if (!empty($highReceiveDate)) {
458 $this->_formValues
['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
459 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_high not end');
461 //check for contribution page id.
462 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
463 if ($contribPageId) {
464 CRM_Core_Error
::deprecatedFunctionWarning('pass contribution_page_id');
465 $this->_formValues
['contribution_page_id'] = $contribPageId;