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