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 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * Class to render contribution search results.
20 class CRM_Contribute_Selector_Search
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
23 * Array of action links.
27 public static $_links = NULL;
30 * We use desc to remind us what that column is, name is used in the tpl
34 public static $_columnHeaders;
37 * Properties of contact we're interested in displaying
40 public static $_properties = [
48 'contribution_source',
51 'contribution_status_id',
52 'contribution_status',
53 'contribution_cancel_date',
56 'contribution_recur_id',
60 'contribution_campaign_id',
61 'contribution_soft_credit_name',
62 'contribution_soft_credit_contact_id',
63 'contribution_soft_credit_amount',
64 'contribution_soft_credit_type',
68 * Are we restricting ourselves to a single contact
72 protected $_single = FALSE;
75 * Are we restricting ourselves to a single contact
79 protected $_limit = NULL;
82 * What context are we being invoked from
86 protected $_context = NULL;
89 * What component context are we being invoked from
93 protected $_compContext = NULL;
96 * QueryParams is the array returned by exportValues called on
97 * the HTML_QuickForm_Controller for that page.
101 public $_queryParams;
104 * Represent the type of selector
111 * The additional clause that we restrict the search with
115 protected $_contributionClause = NULL;
124 protected $_includeSoftCredits = FALSE;
129 * @param array $queryParams
130 * Array of parameters for query.
131 * @param \const|int $action - action of search basic or advanced.
132 * @param string $contributionClause
133 * If the caller wants to further restrict the search (used in contributions).
134 * @param bool $single
135 * Are we dealing only with one contact?.
137 * How many contributions do we want returned.
139 * @param string $context
140 * @param null $compContext
142 * @return \CRM_Contribute_Selector_Search
144 public function __construct(
146 $action = CRM_Core_Action
::NONE
,
147 $contributionClause = NULL,
154 // submitted form values
155 $this->_queryParams
= &$queryParams;
157 $this->_single
= $single;
158 $this->_limit
= $limit;
159 $this->_context
= $context;
160 $this->_compContext
= $compContext;
162 $this->_contributionClause
= $contributionClause;
165 $this->_action
= $action;
166 $returnProperties = CRM_Contribute_BAO_Query
::selectorReturnProperties($this->_queryParams
);
167 $this->_includeSoftCredits
= CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_queryParams
);
168 $this->_queryParams
[] = ['contribution_id', '!=', 0, 0, 0];
169 $this->_query
= new CRM_Contact_BAO_Query(
173 CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
175 // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
176 // can we remove? if not why not?
177 if ($this->_includeSoftCredits
) {
178 $this->_query
->_rowCountClause
= " count(civicrm_contribution.id)";
179 $this->_query
->_groupByComponentClause
= " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
182 $this->_query
->_distinctComponentClause
= " civicrm_contribution.id";
183 $this->_query
->_groupByComponentClause
= " GROUP BY civicrm_contribution.id ";
188 * This method returns the links that are given for each search row.
189 * currently the links added for each row are
194 * @param int $componentId
195 * @param null $componentAction
197 * @param null $compContext
201 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
204 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
207 $extraParams .= "&compContext={$compContext}";
210 $extraParams .= "&key={$key}";
213 if (!(self
::$_links)) {
215 CRM_Core_Action
::VIEW
=> [
216 'name' => ts('View'),
217 'url' => 'civicrm/contact/view/contribution',
218 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
219 'title' => ts('View Contribution'),
221 CRM_Core_Action
::UPDATE
=> [
222 'name' => ts('Edit'),
223 'url' => 'civicrm/contact/view/contribution',
224 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
225 'title' => ts('Edit Contribution'),
227 CRM_Core_Action
::DELETE
=> [
228 'name' => ts('Delete'),
229 'url' => 'civicrm/contact/view/contribution',
230 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
231 'title' => ts('Delete Contribution'),
235 return self
::$_links;
239 * Getter for array of the parameters required for creating pager.
242 * @param array $params
244 public function getPagerParams($action, &$params) {
245 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
246 $params['csvString'] = NULL;
248 $params['rowCount'] = $this->_limit
;
251 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
254 $params['buttonTop'] = 'PagerTopButton';
255 $params['buttonBottom'] = 'PagerBottomButton';
259 * Returns total number of rows for the query.
261 * @param string $action
264 * Total number of rows
266 public function getTotalCount($action) {
267 return $this->_query
->searchQuery(0, 0, NULL,
271 $this->_contributionClause
276 * Returns all the rows in the given offset and rowCount.
278 * @param string $action
279 * The action being performed.
281 * The row number to start from.
282 * @param int $rowCount
283 * The number of rows to return.
284 * @param string $sort
285 * The sql string that describes the sort order.
286 * @param string $output
287 * What should the result set include (web/email/csv).
290 * the total number of rows for this action
292 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
293 if ($this->_includeSoftCredits
) {
294 // especial sort order when rows include soft credits
295 $sort = $sort->orderBy() . ", civicrm_contribution.id, civicrm_contribution_soft.id";
297 $result = $this->_query
->searchQuery($offset, $rowCount, $sort,
301 $this->_contributionClause
303 // process the result of the query
306 //CRM-4418 check for view/edit/delete
307 $permissions = [CRM_Core_Permission
::VIEW
];
308 if (CRM_Core_Permission
::check('edit contributions')) {
309 $permissions[] = CRM_Core_Permission
::EDIT
;
311 if (CRM_Core_Permission
::check('delete in CiviContribute')) {
312 $permissions[] = CRM_Core_Permission
::DELETE
;
314 $mask = CRM_Core_Action
::mask($permissions);
316 $qfKey = $this->_key
;
317 $componentId = $componentContext = NULL;
318 if ($this->_context
!= 'contribute') {
319 // @todo explain the significance of context & why we do not get these i that context.
320 $qfKey = CRM_Utils_Request
::retrieve('key', 'String');
321 $componentId = CRM_Utils_Request
::retrieve('id', 'Positive');
322 $componentAction = CRM_Utils_Request
::retrieve('action', 'String');
323 $componentContext = CRM_Utils_Request
::retrieve('compContext', 'String');
325 if (!$componentContext &&
328 // @todo explain when this condition might occur.
329 $componentContext = $this->_compContext
;
330 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
332 // CRM-17628 for some reason qfKey is not always set when searching from contribution search.
333 // as a result if the edit link is opened using right-click + open in new tab
334 // then the browser is not returned to the search results on save.
335 // This is an effort to getting the qfKey without, sadly, understanding the intent of those who came before me.
337 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
341 // get all contribution status
342 $contributionStatuses = CRM_Core_OptionGroup
::values('contribution_status',
343 FALSE, FALSE, FALSE, NULL, 'name', FALSE
347 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
349 while ($result->fetch()) {
350 $this->_query
->convertToPseudoNames($result);
351 $links = self
::links($componentId,
356 $checkLineItem = FALSE;
358 // Now check for lineItems
359 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
360 $lineItems = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($result->id
);
361 foreach ($lineItems as $items) {
362 if (!CRM_Core_Permission
::check('view contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
363 $checkLineItem = TRUE;
366 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
367 unset($links[CRM_Core_Action
::UPDATE
]);
369 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
370 unset($links[CRM_Core_Action
::DELETE
]);
373 if ($checkLineItem) {
376 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
377 unset($links[CRM_Core_Action
::UPDATE
]);
379 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
380 unset($links[CRM_Core_Action
::DELETE
]);
383 // the columns we are interested in
384 foreach (self
::$_properties as $property) {
385 if (property_exists($result, $property)) {
386 $row[$property] = $result->$property;
390 //carry campaign on selectors.
391 // @todo - I can't find any evidence that 'carrying' the campaign on selectors actually
392 // results in it being displayed anywhere so why do we do this???
393 $row['campaign'] = $allCampaigns[$result->contribution_campaign_id
] ??
NULL;
394 $row['campaign_id'] = $result->contribution_campaign_id
;
396 // add contribution status name
397 $row['contribution_status_name'] = CRM_Utils_Array
::value($row['contribution_status_id'],
398 $contributionStatuses
402 if ($result->is_pay_later
&& CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
404 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
405 $links[CRM_Core_Action
::ADD
] = [
406 'name' => ts('Pay with Credit Card'),
407 'url' => 'civicrm/contact/view/contribution',
408 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%&mode=live',
409 'title' => ts('Pay with Credit Card'),
412 elseif (CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
413 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
416 if ($row['is_test']) {
417 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
420 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contribution_id
;
423 'id' => $result->contribution_id
,
424 'cid' => $result->contact_id
,
425 'cxt' => $this->_context
,
428 if (in_array($row['contribution_status_name'], ['Partially paid', 'Pending refund']) ||
$isPayLater) {
429 $buttonName = ts('Record Payment');
430 if ($row['contribution_status_name'] == 'Pending refund') {
431 $buttonName = ts('Record Refund');
433 elseif (CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
434 $links[CRM_Core_Action
::BASIC
] = [
435 'name' => ts('Submit Credit Card payment'),
436 'url' => 'civicrm/payment/add',
437 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution&mode=live',
438 'title' => ts('Submit Credit Card payment'),
441 $links[CRM_Core_Action
::ADD
] = [
442 'name' => $buttonName,
443 'url' => 'civicrm/payment',
444 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution',
445 'title' => $buttonName,
449 $row['action'] = CRM_Core_Action
::formLink(
454 'contribution.selector.row',
456 $result->contribution_id
459 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
, FALSE, $result->contact_id
462 if (!empty($row['amount_level'])) {
463 CRM_Event_BAO_Participant
::fixEventLevel($row['amount_level']);
475 public function getQILL() {
476 return $this->_query
->qill();
480 * Returns the column headers as an array of tuples:
481 * (name, sortName (key to the sort array))
483 * @param string $action
484 * The action being performed.
485 * @param string $output
486 * What should the result set include (web/email/csv).
489 * the column headers that need to be displayed
491 public function &getColumnHeaders($action = NULL, $output = NULL) {
493 self
::$_columnHeaders = [
495 'name' => $this->_includeSoftCredits ?
ts('Contribution Amount') : ts('Amount'),
496 'sort' => 'total_amount',
497 'direction' => CRM_Utils_Sort
::DONTCARE
,
498 'field_name' => 'total_amount',
501 if ($this->_includeSoftCredits
) {
502 self
::$_columnHeaders
504 self
::$_columnHeaders,
507 'name' => ts('Soft Credit Amount'),
508 'sort' => 'contribution_soft_credit_amount',
509 'field_name' => 'contribution_soft_credit_amount',
510 'direction' => CRM_Utils_Sort
::DONTCARE
,
515 self
::$_columnHeaders
517 self
::$_columnHeaders,
520 'name' => ts('Type'),
521 'sort' => 'financial_type',
522 'field_name' => 'financial_type',
523 'direction' => CRM_Utils_Sort
::DONTCARE
,
526 'name' => ts('Source'),
527 'sort' => 'contribution_source',
528 'field_name' => 'contribution_source',
529 'direction' => CRM_Utils_Sort
::DONTCARE
,
532 'name' => ts('Received'),
533 'sort' => 'receive_date',
534 'field_name' => 'receive_date',
536 'direction' => CRM_Utils_Sort
::DESCENDING
,
539 'name' => ts('Thank-you Sent'),
540 'sort' => 'thankyou_date',
541 'field_name' => 'thankyou_date',
543 'direction' => CRM_Utils_Sort
::DONTCARE
,
546 'name' => ts('Status'),
547 'sort' => 'contribution_status',
548 'field_name' => 'contribution_status',
549 'direction' => CRM_Utils_Sort
::DONTCARE
,
553 if (CRM_Contribute_BAO_Query
::isSiteHasProducts()) {
554 self
::$_columnHeaders[] = [
555 'name' => ts('Premium'),
556 'sort' => 'product_name',
557 'field_name' => 'product_name',
558 'direction' => CRM_Utils_Sort
::DONTCARE
,
561 if (!$this->_single
) {
564 'name' => ts('Name'),
565 'sort' => 'sort_name',
566 'direction' => CRM_Utils_Sort
::DONTCARE
,
570 self
::$_columnHeaders = array_merge($pre, self
::$_columnHeaders);
571 if ($this->_includeSoftCredits
) {
572 self
::$_columnHeaders = array_merge(
573 self
::$_columnHeaders,
576 'name' => ts('Soft Credit For'),
577 'sort' => 'contribution_soft_credit_name',
578 'direction' => CRM_Utils_Sort
::DONTCARE
,
581 'name' => ts('Soft Credit Type'),
582 'sort' => 'contribution_soft_credit_type',
583 'direction' => CRM_Utils_Sort
::ASCENDING
,
588 self
::$_columnHeaders
590 self
::$_columnHeaders, [
591 ['desc' => ts('Actions'), 'type' => 'actions'],
594 foreach (array_keys(self
::$_columnHeaders) as $index) {
595 // Add weight & space it out a bit to allow headers to be inserted.
596 self
::$_columnHeaders[$index]['weight'] = $index * 10;
599 CRM_Core_Smarty
::singleton()->assign('softCreditColumns', $this->_includeSoftCredits
);
600 return self
::$_columnHeaders;
606 public function alphabetQuery() {
607 return $this->_query
->alphabetQuery();
611 * @return \CRM_Contact_BAO_Query
613 public function &getQuery() {
614 return $this->_query
;
618 * Name of export file.
620 * @param string $output
626 public function getExportFileName($output = 'csv') {
627 return ts('CiviCRM Contribution Search');
633 public function getSummary() {
634 return $this->_query
->summaryContribution($this->_context
);