Merge pull request #23210 from eileenmcnaughton/cancel
[civicrm-core.git] / CRM / Contribute / Form / Task / TaskTrait.php
CommitLineData
5b43eb5a 1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18/**
19 * This class provides shared contribution task functionality.
20 */
21trait CRM_Contribute_Form_Task_TaskTrait {
22
12732415 23 /**
06dec048 24 * Selected IDs for the action.
12732415 25 *
06dec048 26 * @var array
12732415 27 */
06dec048 28 protected $ids;
12732415 29
5b43eb5a 30 /**
31 * Get the results from the BAO_Query object based search.
32 *
33 * @return CRM_Core_DAO
34 *
35 * @throws \CRM_Core_Exception
36 */
37 public function getSearchQueryResults(): CRM_Core_DAO {
f0c2d258 38 $form = $this;
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;
47 }
5b43eb5a 48
f0c2d258 49 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
50 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
51 );
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 ';
57 }
58 else {
59 $query->_distinctComponentClause = ' civicrm_contribution.id';
60 $query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id ';
5b43eb5a 61 }
f0c2d258 62 return $query->searchQuery(0, 0, $sortOrder);
5b43eb5a 63 }
64
3ccafc03 65 /**
66 * Get the query parameters, adding test = FALSE if needed.
67 *
68 * @return array|null
69 */
70 protected function getQueryParams(): ?array {
71 $queryParams = $this->get('queryParams');
72 if (!is_array($queryParams)) {
73 return NULL;
74 }
75 foreach ($queryParams as $fields) {
76 if ($fields[0] === 'contribution_test') {
77 return $queryParams;
78 }
79 }
80 $queryParams[] = [
81 'contribution_test',
82 '=',
83 0,
84 0,
85 0,
86 ];
87 return $queryParams;
88 }
89
102279e2 90 /**
91 * Has soft credit information been requested in the query filters.
92 *
93 * @return bool
94 */
95 public function isQueryIncludesSoftCredits(): bool {
96 return (bool) CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->getQueryParams());
97 }
98
06dec048 99 /**
100 * Get ids selected for the task.
101 *
102 * @return array|bool
103 * @throws \CRM_Core_Exception
104 */
105 public function getIDs() {
106 if (!$this->ids) {
107 $this->ids = $this->calculateIDS();
108 }
109 return $this->ids;
110 }
111
112 /**
113 * @return array|bool|string[]
114 * @throws \CRM_Core_Exception
115 */
116 protected function calculateIDS() {
7656ed42
PF
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')) {
06dec048 120 return explode(',', $this->controller->get('id'));
121 }
122 $ids = $this->getSelectedIDs($this->getSearchFormValues());
123 if (!$ids) {
124 $result = $this->getSearchQueryResults();
125 while ($result->fetch()) {
126 $ids[] = $result->contribution_id;
127 }
128 }
129 return $ids;
130 }
131
132 /**
133 * Get the clause to add to queries to hone the results.
134 *
135 * In practice this generally means the query to limit by selected ids.
136 *
137 * @throws \CRM_Core_Exception
138 */
139 public function getComponentClause(): string {
140 return ' civicrm_contribution.id IN ( ' . implode(',', $this->getIDs()) . ' ) ';
141 }
142
1a90603e 143 /**
144 * Is only one entity being processed?
145 *
004f7adf 146 * @return bool
1a90603e 147 */
148 public function isSingle() {
2c2cf700 149 return count($this->getIDs()) === 1;
1a90603e 150 }
151
5b43eb5a 152}