3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Advanced search, extends basic search.
37 class CRM_Contribute_Form_Search
extends CRM_Core_Form_Search
{
40 * The params that are sent to the query.
44 protected $_queryParams;
47 * Are we restricting ourselves to a single contact.
51 protected $_single = FALSE;
54 * Are we restricting ourselves to a single contact.
58 protected $_limit = NULL;
61 * Prefix for the controller.
64 protected $_prefix = "contribute_";
67 * Explicitly declare the entity api name.
69 public function getDefaultEntity() {
70 return 'Contribution';
74 * Processing needed for buildForm and later.
76 * @throws \CiviCRM_API3_Exception
77 * @throws \CRM_Core_Exception
79 public function preProcess() {
80 $this->set('searchFormName', 'Search');
82 $this->_searchButtonName
= $this->getButtonName('refresh');
83 $this->_actionButtonName
= $this->getButtonName('next', 'action');
87 $this->loadStandardSearchOptionsFromUrl();
89 $this->_formValues
= $this->getFormValues();
92 $memberShipId = CRM_Utils_Request
::retrieve('memberId', 'Positive', $this);
93 if (isset($memberShipId)) {
94 $this->_formValues
['contribution_membership_id'] = $memberShipId;
96 $participantId = CRM_Utils_Request
::retrieve('participantId', 'Positive', $this);
97 if (isset($participantId)) {
98 $this->_formValues
['contribution_participant_id'] = $participantId;
102 $this->handleForcedSearch();
106 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
107 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
108 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
112 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
113 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
121 if ($this->_context
== 'user') {
122 $prefix = $this->_prefix
;
125 $this->assign("{$prefix}limit", $this->_limit
);
126 $this->assign("{$prefix}single", $this->_single
);
128 $controller = new CRM_Core_Selector_Controller($selector,
129 $this->get(CRM_Utils_Pager
::PAGE_ID
),
131 CRM_Core_Action
::VIEW
,
133 CRM_Core_Selector_Controller
::TRANSFER
,
137 $controller->setEmbedded(TRUE);
138 $controller->moveFromSessionToTemplate();
140 $this->assign('contributionSummary', $this->get('summary'));
149 public function setDefaultValues() {
150 $lowReceiveDate = CRM_Utils_Request
::retrieve('start', 'Timestamp');
151 if (!empty($lowReceiveDate)) {
152 $this->_formValues
['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate));
153 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_low not start');
155 $highReceiveDate = CRM_Utils_Request
::retrieve('end', 'Timestamp');
156 if (!empty($highReceiveDate)) {
157 $this->_formValues
['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate));
158 CRM_Core_Error
::deprecatedFunctionWarning('pass receive_date_high not end');
160 $this->_defaults
= parent
::setDefaultValues();
162 $this->_defaults
= array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults
);
164 if (empty($this->_defaults
['contribution_status_id']) && !$this->_force
) {
165 // In force mode only parameters from the url will be used. When visible/ explicit this is a useful default.
166 $this->_defaults
['contribution_status_id'][1] = CRM_Core_PseudoConstant
::getKey(
167 'CRM_Contribute_BAO_Contribution',
168 'contribution_status_id',
172 return $this->_defaults
;
176 * Build the form object.
178 * @throws \CRM_Core_Exception
179 * @throws \CiviCRM_API3_Exception
181 public function buildQuickForm() {
182 if ($this->isFormInViewOrEditMode()) {
183 parent
::buildQuickForm();
184 $this->addContactSearchFields();
186 CRM_Contribute_BAO_Query
::buildSearchForm($this);
189 $rows = $this->get('rows');
190 if (is_array($rows)) {
191 if (!$this->_single
) {
192 $this->addRowSelectors($rows);
195 $permission = CRM_Core_Permission
::getPermission();
197 $queryParams = $this->get('queryParams');
198 $taskParams['softCreditFiltering'] = FALSE;
199 if (!empty($queryParams)) {
200 $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($queryParams);
202 $tasks = CRM_Contribute_Task
::permissionedTaskTitles($permission, $taskParams);
203 $this->addTaskMenu($tasks);
209 * Get the label for the sortName field if email searching is on.
211 * (email searching is a setting under search preferences).
215 protected function getSortNameLabelWithEmail() {
216 return ts('Contributor Name or Email');
220 * Get the label for the sortName field if email searching is off.
222 * (email searching is a setting under search preferences).
226 protected function getSortNameLabelWithOutEmail() {
227 return ts('Contributor Name');
231 * Get the label for the tag field.
233 * We do this in a function so the 'ts' wraps the whole string to allow
234 * better translation.
238 protected function getTagLabel() {
239 return ts('Contributor Tag(s)');
243 * Get the label for the group field.
247 protected function getGroupLabel() {
248 return ts('Contributor Group(s)');
252 * Get the label for the group field.
256 protected function getContactTypeLabel() {
257 return ts('Contributor Contact Type');
261 * The post processing of the form gets done here.
263 * Key things done during post processing are
264 * - check for reset or next request. if present, skip post processing.
265 * - now check if user requested running a saved search, if so, then
266 * the form values associated with the saved search are used for searching.
267 * - if user has done a submit with new values the regular post submission is
269 * The processing consists of using a Selector / Controller framework for getting the
272 public function postProcess() {
279 $this->setFormValues();
280 $this->fixFormValues();
282 // We don't show test records in summaries or dashboards
283 if (empty($this->_formValues
['contribution_test']) && $this->_force
&& !empty($this->_context
) && $this->_context
== 'dashboard') {
284 $this->_formValues
["contribution_test"] = 0;
288 'contribution_amount_low',
289 'contribution_amount_high',
291 if (isset($this->_formValues
[$f])) {
292 $this->_formValues
[$f] = CRM_Utils_Rule
::cleanMoney($this->_formValues
[$f]);
296 $config = CRM_Core_Config
::singleton();
297 if (!empty($_POST)) {
300 'contribution_soft_credit_type_id',
301 'contribution_status_id',
302 'contribution_trxn_id',
303 'contribution_page_id',
304 'contribution_product_id',
306 'payment_instrument_id',
307 'contribution_batch_id',
309 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams);
311 $tags = CRM_Utils_Array
::value('contact_tags', $this->_formValues
);
312 if ($tags && !is_array($tags)) {
313 unset($this->_formValues
['contact_tags']);
314 $this->_formValues
['contact_tags'][$tags] = 1;
317 if ($tags && is_array($tags)) {
318 unset($this->_formValues
['contact_tags']);
319 foreach ($tags as $notImportant => $tagID) {
320 $this->_formValues
['contact_tags'][$tagID] = 1;
324 $group = CRM_Utils_Array
::value('group', $this->_formValues
);
325 if ($group && !is_array($group)) {
326 unset($this->_formValues
['group']);
327 $this->_formValues
['group'][$group] = 1;
330 if ($group && is_array($group)) {
331 unset($this->_formValues
['group']);
332 foreach ($group as $groupID) {
333 $this->_formValues
['group'][$groupID] = 1;
338 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
340 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
342 $this->set('formValues', $this->_formValues
);
343 $this->set('queryParams', $this->_queryParams
);
345 $buttonName = $this->controller
->getButtonName();
346 if ($buttonName == $this->_actionButtonName
) {
347 // check actionName and if next, then do not repeat a search, since we are going to the next page
349 // hack, make sure we reset the task values
350 $stateMachine = $this->controller
->getStateMachine();
351 $formName = $stateMachine->getTaskFormName();
352 $this->controller
->resetPage($formName);
357 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
358 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
359 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
363 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
364 $selector = new CRM_Contribute_Selector_Search($this->_queryParams
,
371 $selector->setKey($this->controller
->_key
);
374 if ($this->_context
== 'basic' ||
$this->_context
== 'user') {
375 $prefix = $this->_prefix
;
378 $controller = new CRM_Core_Selector_Controller($selector,
379 $this->get(CRM_Utils_Pager
::PAGE_ID
),
381 CRM_Core_Action
::VIEW
,
383 CRM_Core_Selector_Controller
::SESSION
,
386 $controller->setEmbedded(TRUE);
388 $query = &$selector->getQuery();
389 if ($this->_context
== 'user') {
390 $query->setSkipPermission(TRUE);
397 * Use values from $_GET if force is set to TRUE.
399 * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained.
401 public function fixFormValues() {
402 if (!$this->_force
) {
406 $status = CRM_Utils_Request
::retrieve('status', 'String');
408 $this->_formValues
['contribution_status_id'] = [$status => 1];
409 $this->_defaults
['contribution_status_id'] = [$status => 1];
412 $pcpid = (array) CRM_Utils_Request
::retrieve('pcpid', 'String', $this);
414 // Add new pcpid to the tail of the array...
415 foreach ($pcpid as $pcpIdList) {
416 $this->_formValues
['contribution_pcp_made_through_id'][] = $pcpIdList;
418 // and avoid any duplicate
419 $this->_formValues
['contribution_pcp_made_through_id'] = array_unique($this->_formValues
['contribution_pcp_made_through_id']);
422 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
425 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
427 $this->_formValues
['contact_id'] = $cid;
428 // @todo - why do we retrieve these when they are not used?
429 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
430 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
433 // also assign individual mode to the template
434 $this->_single
= TRUE;
438 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
442 $test = CRM_Utils_Request
::retrieve('test', 'Boolean');
444 $test = CRM_Utils_Type
::escape($test, 'Boolean');
445 $this->_formValues
['contribution_test'] = $test;
448 $recur = CRM_Utils_Request
::retrieve('recur', 'Positive', $this, FALSE);
450 $this->_formValues
['contribution_recur_id'] = $recur;
451 $this->_formValues
['contribution_recurring'] = 1;
454 //check for contribution page id.
455 $contribPageId = CRM_Utils_Request
::retrieve('pid', 'Positive', $this);
456 if ($contribPageId) {
457 $this->_formValues
['contribution_page_id'] = $contribPageId;
460 //give values to default.
461 $this->_defaults
= $this->_formValues
;
465 * Return a descriptive name for the page, used in wizard header.
469 public function getTitle() {
470 return ts('Find Contributions');
474 * Set the metadata for the form.
476 * @throws \CiviCRM_API3_Exception
478 protected function setSearchMetadata() {
479 $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query
::getSearchFieldMetadata()]);
480 $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur
::getContributionRecurSearchFieldMetadata()]);