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 * This class provides shared contribution task functionality.
21 trait CRM_Contribute_Form_Task_TaskTrait
{
24 * Selected IDs for the action.
31 * Get the results from the BAO_Query object based search.
33 * @return CRM_Core_DAO
35 * @throws \CRM_Core_Exception
37 public function getSearchQueryResults(): CRM_Core_DAO
{
39 $queryParams = $this->getQueryParams();
40 $returnProperties = ['contribution_id' => 1];
41 $sortOrder = $sortCol = NULL;
42 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
43 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
44 //Include sort column in select clause.
45 $sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder));
46 $returnProperties[$sortCol] = 1;
49 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
50 CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
52 // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
53 // can we remove? if not why not?
54 if ($this->isQueryIncludesSoftCredits()) {
55 $query->_rowCountClause
= ' count(civicrm_contribution.id)';
56 $query->_groupByComponentClause
= ' GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ';
59 $query->_distinctComponentClause
= ' civicrm_contribution.id';
60 $query->_groupByComponentClause
= ' GROUP BY civicrm_contribution.id ';
62 return $query->searchQuery(0, 0, $sortOrder);
66 * Get the query parameters, adding test = FALSE if needed.
70 protected function getQueryParams(): ?
array {
71 $queryParams = $this->get('queryParams');
72 if (!is_array($queryParams)) {
75 foreach ($queryParams as $fields) {
76 if ($fields[0] === 'contribution_test') {
91 * Has soft credit information been requested in the query filters.
95 public function isQueryIncludesSoftCredits(): bool {
96 return (bool) CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->getQueryParams());
100 * Get ids selected for the task.
103 * @throws \CRM_Core_Exception
105 public function getIDs() {
107 $this->ids
= $this->calculateIDS();
113 * @return array|bool|string[]
114 * @throws \CRM_Core_Exception
116 protected function calculateIDS() {
117 // contact search forms use the id property to store the selected uf_group_id
118 // rather than entity (contribution) IDs, so don't use the property in that case
119 if (!$this->controller
instanceof CRM_Contact_Controller_Search
&& $this->controller
->get('id')) {
120 return explode(',', $this->controller
->get('id'));
122 $ids = $this->getSelectedIDs($this->getSearchFormValues());
124 $result = $this->getSearchQueryResults();
125 while ($result->fetch()) {
126 $ids[] = $result->contribution_id
;
133 * Get the clause to add to queries to hone the results.
135 * In practice this generally means the query to limit by selected ids.
137 * @throws \CRM_Core_Exception
139 public function getComponentClause(): string {
140 return ' civicrm_contribution.id IN ( ' . implode(',', $this->getIDs()) . ' ) ';
144 * Is only one entity being processed?
148 public function isSingle() {
149 return count($this->getIDs()) === 1;