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->_searchButtonName
= $this->getButtonName('refresh');
67 $this->_actionButtonName
= $this->getButtonName('next', 'action');
74 $memberShipId = CRM_Utils_Request
::retrieve('memberId', 'Positive', $this);
75 if (isset($memberShipId)) {
76 $this->_formValues
['contribution_membership_id'] = $memberShipId;
78 $participantId = CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
79 if (isset($participantId)) {
80 $this->_formValues
['contribution_participant_id'] = $participantId;
83 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
84 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
92 if ($this->_context
== 'user') {
93 $prefix = $this->_prefix
;
96 $this->assign("{$prefix}limit", $this->_limit
);
97 $this->assign("{$prefix}single", $this->_single
);
99 $controller = new CRM_Core_Selector_Controller($selector,
100 $this->get(CRM_Utils_Pager
::PAGE_ID
),
102 CRM_Core_Action
::VIEW
,
104 CRM_Core_Selector_Controller
::TRANSFER
,
108 $controller->setEmbedded(TRUE);
109 $controller->moveFromSessionToTemplate();
111 $this->assign('contributionSummary', $this->get('summary'));
120 public function setDefaultValues() {
121 $this->setDeprecatedDefaults();
122 $this->_defaults
= parent
::setDefaultValues();
124 $this->_defaults
= array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults
);
126 if (empty($this->_defaults
['contribution_status_id']) && !$this->_force
) {
127 // In force mode only parameters from the url will be used. When visible/ explicit this is a useful default.
128 $this->_defaults
['contribution_status_id'][1] = CRM_Core_PseudoConstant
::getKey(
129 'CRM_Contribute_BAO_Contribution',
130 'contribution_status_id',
134 return $this->_defaults
;
138 * Build the form object.
140 * @throws \CRM_Core_Exception
141 * @throws \CiviCRM_API3_Exception
143 public function buildQuickForm() {
144 if ($this->isFormInViewOrEditMode()) {
145 parent
::buildQuickForm();
146 $this->addContactSearchFields();
148 CRM_Contribute_BAO_Query
::buildSearchForm($this);
151 $rows = $this->get('rows');
152 if (is_array($rows)) {
153 if (!$this->_single
) {
154 $this->addRowSelectors($rows);
157 $permission = CRM_Core_Permission
::getPermission();
159 $queryParams = $this->get('queryParams');
160 $taskParams['softCreditFiltering'] = FALSE;
161 if (!empty($queryParams)) {
162 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($queryParams);
164 $tasks = CRM_Contribute_Task
::permissionedTaskTitles($permission, $taskParams);
165 $this->addTaskMenu($tasks);
171 * Get the label for the sortName field if email searching is on.
173 * (email searching is a setting under search preferences).
177 protected function getSortNameLabelWithEmail() {
178 return ts('Contributor Name or Email');
182 * Get the label for the sortName field if email searching is off.
184 * (email searching is a setting under search preferences).
188 protected function getSortNameLabelWithOutEmail() {
189 return ts('Contributor Name');
193 * Get the label for the tag field.
195 * We do this in a function so the 'ts' wraps the whole string to allow
196 * better translation.
200 protected function getTagLabel() {
201 return ts('Contributor Tag(s)');
205 * Get the label for the group field.
209 protected function getGroupLabel() {
210 return ts('Contributor Group(s)');
214 * Get the label for the group field.
218 protected function getContactTypeLabel() {
219 return ts('Contributor Contact Type');
223 * The post processing of the form gets done here.
225 * Key things done during post processing are
226 * - check for reset or next request. if present, skip post processing.
227 * - now check if user requested running a saved search, if so, then
228 * the form values associated with the saved search are used for searching.
229 * - if user has done a submit with new values the regular post submission is
231 * The processing consists of using a Selector / Controller framework for getting the
234 public function postProcess() {
241 $this->setFormValues();
242 // @todo - stop changing formValues - respect submitted form values, change a working array.
243 $this->fixFormValues();
245 // We don't show test records in summaries or dashboards
246 if (empty($this->_formValues
['contribution_test']) && $this->_force
&& !empty($this->_context
) && $this->_context
== 'dashboard') {
247 // @todo - stop changing formValues - respect submitted form values, change a working array.
248 $this->_formValues
["contribution_test"] = 0;
252 'contribution_amount_low',
253 'contribution_amount_high',
255 if (isset($this->_formValues
[$f])) {
256 // @todo - stop changing formValues - respect submitted form values, change a working array.
257 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
261 if (!empty($_POST)) {
264 'contribution_soft_credit_type_id',
265 'contribution_status_id',
266 'contribution_trxn_id',
267 'contribution_product_id',
269 'payment_instrument_id',
270 'contribution_batch_id',
272 // @todo - stop changing formValues - respect submitted form values, change a working array.
273 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams);
275 // @todo - stop changing formValues - respect submitted form values, change a working array.
276 $tags = CRM_Utils_Array
::value('contact_tags', $this->_formValues
);
277 if ($tags && !is_array($tags)) {
278 // @todo - stop changing formValues - respect submitted form values, change a working array.
279 unset($this->_formValues
['contact_tags']);
280 $this->_formValues
['contact_tags'][$tags] = 1;
283 if ($tags && is_array($tags)) {
284 unset($this->_formValues
['contact_tags']);
285 foreach ($tags as $notImportant => $tagID) {
286 // @todo - stop changing formValues - respect submitted form values, change a working array.
287 $this->_formValues
['contact_tags'][$tagID] = 1;
291 $group = CRM_Utils_Array
::value('group', $this->_formValues
);
292 if ($group && !is_array($group)) {
293 // @todo - stop changing formValues - respect submitted form values, change a working array.
294 unset($this->_formValues
['group']);
295 $this->_formValues
['group'][$group] = 1;
298 if ($group && is_array($group)) {
299 unset($this->_formValues
['group']);
300 foreach ($group as $groupID) {
301 // @todo - stop changing formValues - respect submitted form values, change a working array.
302 $this->_formValues
['group'][$groupID] = 1;
307 // @todo - stop changing formValues - respect submitted form values, change a working array.
308 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
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 public function fixFormValues() {
365 if (!$this->_force
) {
369 $status = CRM_Utils_Request
::retrieve('status', 'String');
371 $this->_formValues
['contribution_status_id'] = [$status => 1];
372 $this->_defaults
['contribution_status_id'] = [$status => 1];
375 $pcpid = (array) CRM_Utils_Request
::retrieve('pcpid', 'String', $this);
377 // Add new pcpid to the tail of the array...
378 foreach ($pcpid as $pcpIdList) {
379 $this->_formValues
['contribution_pcp_made_through_id'][] = $pcpIdList;
381 // and avoid any duplicate
382 $this->_formValues
['contribution_pcp_made_through_id'] = array_unique($this->_formValues
['contribution_pcp_made_through_id']);
385 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
388 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
390 $this->_formValues
['contact_id'] = $cid;
391 // @todo - why do we retrieve these when they are not used?
392 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
393 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
396 // also assign individual mode to the template
397 $this->_single
= TRUE;
401 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
405 $test = CRM_Utils_Request
::retrieve('test', 'Boolean');
407 $test = CRM_Utils_Type
::escape($test, 'Boolean');
408 $this->_formValues
['contribution_test'] = $test;
411 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
413 $this->_formValues
['contribution_recur_id'] = $recur;
414 $this->_formValues
['contribution_recurring'] = 1;
417 //check for contribution page id.
418 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
419 if ($contribPageId) {
420 $this->_formValues
['contribution_page_id'] = $contribPageId;
425 * Return a descriptive name for the page, used in wizard header.
429 public function getTitle() {
430 return ts('Find Contributions');
434 * Set the metadata for the form.
436 * @throws \CiviCRM_API3_Exception
438 protected function setSearchMetadata() {
439 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query
::getSearchFieldMetadata()]);
440 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur
::getContributionRecurSearchFieldMetadata()]);
444 * Handling for url params that are deprecated.
446 * @throws \CRM_Core_Exception
448 protected function setDeprecatedDefaults() {
449 $lowReceiveDate = CRM_Utils_Request
::retrieve('start', 'Timestamp');
450 if (!empty($lowReceiveDate)) {
451 $this->_formValues
['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
452 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_low not start');
454 $highReceiveDate = CRM_Utils_Request
::retrieve('end', 'Timestamp');
455 if (!empty($highReceiveDate)) {
456 $this->_formValues
['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
457 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_high not end');
459 //check for contribution page id.
460 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
461 if ($contribPageId) {
462 CRM_Core_Error
::deprecatedFunctionWarning('pass contribution_page_id');
463 $this->_formValues
['contribution_page_id'] = $contribPageId;