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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2019
34 * Class to render contribution search results.
36 class CRM_Contribute_Selector_Search
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
39 * Array of action links.
43 public static $_links = NULL;
46 * We use desc to remind us what that column is, name is used in the tpl
50 public static $_columnHeaders;
53 * Properties of contact we're interested in displaying
56 public static $_properties = [
64 'contribution_source',
67 'contribution_status_id',
68 'contribution_status',
72 'contribution_recur_id',
76 'contribution_campaign_id',
77 'contribution_soft_credit_name',
78 'contribution_soft_credit_contact_id',
79 'contribution_soft_credit_amount',
80 'contribution_soft_credit_type',
84 * Are we restricting ourselves to a single contact
88 protected $_single = FALSE;
91 * Are we restricting ourselves to a single contact
95 protected $_limit = NULL;
98 * What context are we being invoked from
102 protected $_context = NULL;
105 * What component context are we being invoked from
109 protected $_compContext = NULL;
112 * QueryParams is the array returned by exportValues called on
113 * the HTML_QuickForm_Controller for that page.
117 public $_queryParams;
120 * Represent the type of selector
127 * The additional clause that we restrict the search with
131 protected $_contributionClause = NULL;
140 protected $_includeSoftCredits = FALSE;
145 * @param array $queryParams
146 * Array of parameters for query.
147 * @param \const|int $action - action of search basic or advanced.
148 * @param string $contributionClause
149 * If the caller wants to further restrict the search (used in contributions).
150 * @param bool $single
151 * Are we dealing only with one contact?.
153 * How many contributions do we want returned.
155 * @param string $context
156 * @param null $compContext
158 * @return \CRM_Contribute_Selector_Search
160 public function __construct(
162 $action = CRM_Core_Action
::NONE
,
163 $contributionClause = NULL,
170 // submitted form values
171 $this->_queryParams
= &$queryParams;
173 $this->_single
= $single;
174 $this->_limit
= $limit;
175 $this->_context
= $context;
176 $this->_compContext
= $compContext;
178 $this->_contributionClause
= $contributionClause;
181 $this->_action
= $action;
182 $returnProperties = CRM_Contribute_BAO_Query
::selectorReturnProperties($this->_queryParams
);
183 $this->_includeSoftCredits
= CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_queryParams
);
184 $this->_queryParams
[] = ['contribution_id', '!=', 0, 0, 0];
185 $this->_query
= new CRM_Contact_BAO_Query(
189 CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
191 // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
192 // can we remove? if not why not?
193 if ($this->_includeSoftCredits
) {
194 $this->_query
->_rowCountClause
= " count(civicrm_contribution.id)";
195 $this->_query
->_groupByComponentClause
= " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
198 $this->_query
->_distinctComponentClause
= " civicrm_contribution.id";
199 $this->_query
->_groupByComponentClause
= " GROUP BY civicrm_contribution.id ";
204 * This method returns the links that are given for each search row.
205 * currently the links added for each row are
210 * @param int $componentId
211 * @param null $componentAction
213 * @param null $compContext
217 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
220 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
223 $extraParams .= "&compContext={$compContext}";
226 $extraParams .= "&key={$key}";
229 if (!(self
::$_links)) {
231 CRM_Core_Action
::VIEW
=> [
232 'name' => ts('View'),
233 'url' => 'civicrm/contact/view/contribution',
234 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
235 'title' => ts('View Contribution'),
237 CRM_Core_Action
::UPDATE
=> [
238 'name' => ts('Edit'),
239 'url' => 'civicrm/contact/view/contribution',
240 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
241 'title' => ts('Edit Contribution'),
243 CRM_Core_Action
::DELETE
=> [
244 'name' => ts('Delete'),
245 'url' => 'civicrm/contact/view/contribution',
246 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
247 'title' => ts('Delete Contribution'),
251 return self
::$_links;
255 * Getter for array of the parameters required for creating pager.
258 * @param array $params
260 public function getPagerParams($action, &$params) {
261 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
262 $params['csvString'] = NULL;
264 $params['rowCount'] = $this->_limit
;
267 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
270 $params['buttonTop'] = 'PagerTopButton';
271 $params['buttonBottom'] = 'PagerBottomButton';
275 * Returns total number of rows for the query.
277 * @param string $action
280 * Total number of rows
282 public function getTotalCount($action) {
283 return $this->_query
->searchQuery(0, 0, NULL,
287 $this->_contributionClause
292 * Returns all the rows in the given offset and rowCount.
294 * @param string $action
295 * The action being performed.
297 * The row number to start from.
298 * @param int $rowCount
299 * The number of rows to return.
300 * @param string $sort
301 * The sql string that describes the sort order.
302 * @param string $output
303 * What should the result set include (web/email/csv).
306 * the total number of rows for this action
308 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
309 if ($this->_includeSoftCredits
) {
310 // especial sort order when rows include soft credits
311 $sort = $sort->orderBy() . ", civicrm_contribution.id, civicrm_contribution_soft.id";
313 $result = $this->_query
->searchQuery($offset, $rowCount, $sort,
317 $this->_contributionClause
319 // process the result of the query
322 //CRM-4418 check for view/edit/delete
323 $permissions = [CRM_Core_Permission
::VIEW
];
324 if (CRM_Core_Permission
::check('edit contributions')) {
325 $permissions[] = CRM_Core_Permission
::EDIT
;
327 if (CRM_Core_Permission
::check('delete in CiviContribute')) {
328 $permissions[] = CRM_Core_Permission
::DELETE
;
330 $mask = CRM_Core_Action
::mask($permissions);
332 $qfKey = $this->_key
;
333 $componentId = $componentContext = NULL;
334 if ($this->_context
!= 'contribute') {
335 // @todo explain the significance of context & why we do not get these i that context.
336 $qfKey = CRM_Utils_Request
::retrieve('key', 'String');
337 $componentId = CRM_Utils_Request
::retrieve('id', 'Positive');
338 $componentAction = CRM_Utils_Request
::retrieve('action', 'String');
339 $componentContext = CRM_Utils_Request
::retrieve('compContext', 'String');
341 if (!$componentContext &&
344 // @todo explain when this condition might occur.
345 $componentContext = $this->_compContext
;
346 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
348 // CRM-17628 for some reason qfKey is not always set when searching from contribution search.
349 // as a result if the edit link is opened using right-click + open in new tab
350 // then the browser is not returned to the search results on save.
351 // This is an effort to getting the qfKey without, sadly, understanding the intent of those who came before me.
353 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
357 // get all contribution status
358 $contributionStatuses = CRM_Core_OptionGroup
::values('contribution_status',
359 FALSE, FALSE, FALSE, NULL, 'name', FALSE
363 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
365 while ($result->fetch()) {
366 $this->_query
->convertToPseudoNames($result);
367 $links = self
::links($componentId,
372 $checkLineItem = FALSE;
374 // Now check for lineItems
375 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
376 $lineItems = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($result->id
);
377 foreach ($lineItems as $items) {
378 if (!CRM_Core_Permission
::check('view contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
379 $checkLineItem = TRUE;
382 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
383 unset($links[CRM_Core_Action
::UPDATE
]);
385 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
386 unset($links[CRM_Core_Action
::DELETE
]);
389 if ($checkLineItem) {
392 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
393 unset($links[CRM_Core_Action
::UPDATE
]);
395 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
396 unset($links[CRM_Core_Action
::DELETE
]);
399 // the columns we are interested in
400 foreach (self
::$_properties as $property) {
401 if (property_exists($result, $property)) {
402 $row[$property] = $result->$property;
406 //carry campaign on selectors.
407 // @todo - I can't find any evidence that 'carrying' the campaign on selectors actually
408 // results in it being displayed anywhere so why do we do this???
409 $row['campaign'] = CRM_Utils_Array
::value($result->contribution_campaign_id
, $allCampaigns);
410 $row['campaign_id'] = $result->contribution_campaign_id
;
412 // add contribution status name
413 $row['contribution_status_name'] = CRM_Utils_Array
::value($row['contribution_status_id'],
414 $contributionStatuses
418 if ($result->is_pay_later
&& CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
420 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
421 $links[CRM_Core_Action
::ADD
] = [
422 'name' => ts('Pay with Credit Card'),
423 'url' => 'civicrm/contact/view/contribution',
424 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%&mode=live',
425 'title' => ts('Pay with Credit Card'),
428 elseif (CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
429 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
432 if ($row['is_test']) {
433 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
436 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contribution_id
;
439 'id' => $result->contribution_id
,
440 'cid' => $result->contact_id
,
441 'cxt' => $this->_context
,
444 if (in_array($row['contribution_status_name'], ['Partially paid', 'Pending refund']) ||
$isPayLater) {
445 $buttonName = ts('Record Payment');
446 if ($row['contribution_status_name'] == 'Pending refund') {
447 $buttonName = ts('Record Refund');
449 elseif (CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
450 $links[CRM_Core_Action
::BASIC
] = [
451 'name' => ts('Submit Credit Card payment'),
452 'url' => 'civicrm/payment/add',
453 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution&mode=live',
454 'title' => ts('Submit Credit Card payment'),
457 $links[CRM_Core_Action
::ADD
] = [
458 'name' => $buttonName,
459 'url' => 'civicrm/payment',
460 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution',
461 'title' => $buttonName,
465 $row['action'] = CRM_Core_Action
::formLink(
470 'contribution.selector.row',
472 $result->contribution_id
475 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
, FALSE, $result->contact_id
478 if (!empty($row['amount_level'])) {
479 CRM_Event_BAO_Participant
::fixEventLevel($row['amount_level']);
491 public function getQILL() {
492 return $this->_query
->qill();
496 * Returns the column headers as an array of tuples:
497 * (name, sortName (key to the sort array))
499 * @param string $action
500 * The action being performed.
501 * @param string $output
502 * What should the result set include (web/email/csv).
505 * the column headers that need to be displayed
507 public function &getColumnHeaders($action = NULL, $output = NULL) {
509 self
::$_columnHeaders = [
511 'name' => $this->_includeSoftCredits ?
ts('Contribution Amount') : ts('Amount'),
512 'sort' => 'total_amount',
513 'direction' => CRM_Utils_Sort
::DONTCARE
,
514 'field_name' => 'total_amount',
517 if ($this->_includeSoftCredits
) {
518 self
::$_columnHeaders
520 self
::$_columnHeaders,
523 'name' => ts('Soft Credit Amount'),
524 'sort' => 'contribution_soft_credit_amount',
525 'field_name' => 'contribution_soft_credit_amount',
526 'direction' => CRM_Utils_Sort
::DONTCARE
,
531 self
::$_columnHeaders
533 self
::$_columnHeaders,
536 'name' => ts('Type'),
537 'sort' => 'financial_type',
538 'field_name' => 'financial_type',
539 'direction' => CRM_Utils_Sort
::DONTCARE
,
542 'name' => ts('Source'),
543 'sort' => 'contribution_source',
544 'field_name' => 'contribution_source',
545 'direction' => CRM_Utils_Sort
::DONTCARE
,
548 'name' => ts('Received'),
549 'sort' => 'receive_date',
550 'field_name' => 'receive_date',
552 'direction' => CRM_Utils_Sort
::DESCENDING
,
555 'name' => ts('Thank-you Sent'),
556 'sort' => 'thankyou_date',
557 'field_name' => 'thankyou_date',
559 'direction' => CRM_Utils_Sort
::DONTCARE
,
562 'name' => ts('Status'),
563 'sort' => 'contribution_status',
564 'field_name' => 'contribution_status',
565 'direction' => CRM_Utils_Sort
::DONTCARE
,
569 if (CRM_Contribute_BAO_Query
::isSiteHasProducts()) {
570 self
::$_columnHeaders[] = [
571 'name' => ts('Premium'),
572 'sort' => 'product_name',
573 'field_name' => 'product_name',
574 'direction' => CRM_Utils_Sort
::DONTCARE
,
577 if (!$this->_single
) {
580 'name' => ts('Name'),
581 'sort' => 'sort_name',
582 'direction' => CRM_Utils_Sort
::DONTCARE
,
586 self
::$_columnHeaders = array_merge($pre, self
::$_columnHeaders);
587 if ($this->_includeSoftCredits
) {
588 self
::$_columnHeaders = array_merge(
589 self
::$_columnHeaders,
592 'name' => ts('Soft Credit For'),
593 'sort' => 'contribution_soft_credit_name',
594 'direction' => CRM_Utils_Sort
::DONTCARE
,
597 'name' => ts('Soft Credit Type'),
598 'sort' => 'contribution_soft_credit_type',
599 'direction' => CRM_Utils_Sort
::ASCENDING
,
604 self
::$_columnHeaders
606 self
::$_columnHeaders, [
607 ['desc' => ts('Actions'), 'type' => 'actions'],
610 foreach (array_keys(self
::$_columnHeaders) as $index) {
611 // Add weight & space it out a bit to allow headers to be inserted.
612 self
::$_columnHeaders[$index]['weight'] = $index * 10;
615 CRM_Core_Smarty
::singleton()->assign('softCreditColumns', $this->_includeSoftCredits
);
616 return self
::$_columnHeaders;
622 public function alphabetQuery() {
623 return $this->_query
->alphabetQuery();
629 public function &getQuery() {
630 return $this->_query
;
634 * Name of export file.
636 * @param string $output
642 public function getExportFileName($output = 'csv') {
643 return ts('CiviCRM Contribution Search');
649 public function getSummary() {
650 return $this->_query
->summaryContribution($this->_context
);