Merge pull request #16057 from eileenmcnaughton/act_perm521
[civicrm-core.git] / CRM / Contribute / Form / Task.php
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 * Class for contribute form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
21 */
22 class CRM_Contribute_Form_Task extends CRM_Core_Form_Task {
23
24 /**
25 * The array that holds all the contribution ids.
26 *
27 * @var array
28 */
29 protected $_contributionIds;
30
31 /**
32 * The array that holds all the mapping contribution and contact ids.
33 *
34 * @var array
35 */
36 protected $_contributionContactIds = [];
37
38 /**
39 * The flag to tell if there are soft credits included.
40 *
41 * @var bool
42 */
43 public $_includesSoftCredits = FALSE;
44
45 /**
46 * Build all the data structures needed to build the form.
47 */
48 public function preProcess() {
49 self::preProcessCommon($this);
50 }
51
52 /**
53 * @param CRM_Core_Form $form
54 */
55 public static function preProcessCommon(&$form) {
56 $form->_contributionIds = [];
57
58 $values = $form->controller->exportValues($form->get('searchFormName'));
59
60 $form->_task = CRM_Utils_Array::value('task', $values);
61 $contributeTasks = CRM_Contribute_Task::tasks();
62 $form->assign('taskName', CRM_Utils_Array::value($form->_task, $contributeTasks));
63
64 $ids = [];
65 if (isset($values['radio_ts']) && $values['radio_ts'] == 'ts_sel') {
66 foreach ($values as $name => $value) {
67 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
68 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
69 }
70 }
71 }
72 else {
73 $queryParams = $form->get('queryParams');
74 $isTest = FALSE;
75 if (is_array($queryParams)) {
76 foreach ($queryParams as $fields) {
77 if ($fields[0] == 'contribution_test') {
78 $isTest = TRUE;
79 break;
80 }
81 }
82 }
83 if (!$isTest) {
84 $queryParams[] = [
85 'contribution_test',
86 '=',
87 0,
88 0,
89 0,
90 ];
91 }
92 $returnProperties = ['contribution_id' => 1];
93 $sortOrder = $sortCol = NULL;
94 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
95 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
96 //Include sort column in select clause.
97 $sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder));
98 $returnProperties[$sortCol] = 1;
99 }
100
101 $form->_includesSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams);
102 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
103 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
104 );
105 // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
106 // can we remove? if not why not?
107 if ($form->_includesSoftCredits) {
108 $contactIds = $contributionContactIds = [];
109 $query->_rowCountClause = " count(civicrm_contribution.id)";
110 $query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
111 }
112 else {
113 $query->_distinctComponentClause = ' civicrm_contribution.id';
114 $query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id ';
115 }
116 $result = $query->searchQuery(0, 0, $sortOrder);
117 while ($result->fetch()) {
118 $ids[] = $result->contribution_id;
119 if ($form->_includesSoftCredits) {
120 $contactIds[$result->contact_id] = $result->contact_id;
121 $contributionContactIds["{$result->contact_id}-{$result->contribution_id}"] = $result->contribution_id;
122 }
123 }
124 $form->assign('totalSelectedContributions', $form->get('rowCount'));
125 }
126
127 if (!empty($ids)) {
128 $form->_componentClause = ' civicrm_contribution.id IN ( ' . implode(',', $ids) . ' ) ';
129
130 $form->assign('totalSelectedContributions', count($ids));
131 }
132 if (!empty($form->_includesSoftCredits) && !empty($contactIds)) {
133 $form->_contactIds = $contactIds;
134 $form->_contributionContactIds = $contributionContactIds;
135 }
136
137 $form->_contributionIds = $form->_componentIds = $ids;
138 $form->set('contributionIds', $form->_contributionIds);
139
140 //set the context for redirection for any task actions
141 $session = CRM_Core_Session::singleton();
142
143 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
144 $urlParams = 'force=1';
145 if (CRM_Utils_Rule::qfKey($qfKey)) {
146 $urlParams .= "&qfKey=$qfKey";
147 }
148
149 $searchFormName = strtolower($form->get('searchFormName'));
150 if ($searchFormName == 'search') {
151 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contribute/search', $urlParams));
152 }
153 else {
154 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
155 $urlParams
156 ));
157 }
158 }
159
160 /**
161 * Sets contribution Ids for unit test.
162 *
163 * @param array $contributionIds
164 */
165 public function setContributionIds($contributionIds) {
166 $this->_contributionIds = $contributionIds;
167 }
168
169 /**
170 * Given the contribution id, compute the contact id
171 * since its used for things like send email
172 */
173 public function setContactIDs() {
174 if (!$this->_includesSoftCredits) {
175 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent(
176 $this->_contributionIds,
177 'civicrm_contribution'
178 );
179 }
180 }
181
182 /**
183 * Simple shell that derived classes can call to add buttons to
184 * the form with a customized title for the main Submit
185 *
186 * @param string $title
187 * Title of the main button.
188 * @param string $nextType
189 * Button type for the form after processing.
190 * @param string $backType
191 * @param bool $submitOnce
192 */
193 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
194 $this->addButtons([
195 [
196 'type' => $nextType,
197 'name' => $title,
198 'isDefault' => TRUE,
199 ],
200 [
201 'type' => $backType,
202 'name' => ts('Cancel'),
203 ],
204 ]);
205 }
206
207 }