Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
31aaf096 MW |
19 | * Class for contribute form task actions. |
20 | * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions. | |
6a488035 | 21 | */ |
31aaf096 | 22 | class CRM_Contribute_Form_Task extends CRM_Core_Form_Task { |
6a488035 TO |
23 | |
24 | /** | |
fe482240 | 25 | * The array that holds all the contribution ids. |
6a488035 TO |
26 | * |
27 | * @var array | |
28 | */ | |
29 | protected $_contributionIds; | |
30 | ||
34eec215 | 31 | /** |
fe482240 | 32 | * The array that holds all the mapping contribution and contact ids. |
34eec215 DS |
33 | * |
34 | * @var array | |
35 | */ | |
be2fb01f | 36 | protected $_contributionContactIds = []; |
34eec215 DS |
37 | |
38 | /** | |
fe482240 | 39 | * The flag to tell if there are soft credits included. |
34eec215 | 40 | * |
b67daa72 | 41 | * @var bool |
34eec215 DS |
42 | */ |
43 | public $_includesSoftCredits = FALSE; | |
44 | ||
6a488035 | 45 | /** |
fe482240 | 46 | * Build all the data structures needed to build the form. |
6a488035 | 47 | */ |
00be9182 | 48 | public function preProcess() { |
6a488035 TO |
49 | self::preProcessCommon($this); |
50 | } | |
51 | ||
186c9c17 | 52 | /** |
c490a46a | 53 | * @param CRM_Core_Form $form |
186c9c17 | 54 | */ |
2b089ce1 | 55 | public static function preProcessCommon(&$form) { |
be2fb01f | 56 | $form->_contributionIds = []; |
6a488035 TO |
57 | |
58 | $values = $form->controller->exportValues($form->get('searchFormName')); | |
59 | ||
9c1bc317 | 60 | $form->_task = $values['task'] ?? NULL; |
6a488035 | 61 | $contributeTasks = CRM_Contribute_Task::tasks(); |
7f20f38e | 62 | $form->assign('taskName', CRM_Utils_Array::value($form->_task, $contributeTasks)); |
6a488035 | 63 | |
be2fb01f | 64 | $ids = []; |
7f20f38e | 65 | if (isset($values['radio_ts']) && $values['radio_ts'] == 'ts_sel') { |
6a488035 TO |
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'); | |
593e0d0b | 74 | $isTest = FALSE; |
85b68a11 CR |
75 | if (is_array($queryParams)) { |
76 | foreach ($queryParams as $fields) { | |
77 | if ($fields[0] == 'contribution_test') { | |
78 | $isTest = TRUE; | |
79 | break; | |
80 | } | |
593e0d0b E |
81 | } |
82 | } | |
83 | if (!$isTest) { | |
be2fb01f | 84 | $queryParams[] = [ |
593e0d0b E |
85 | 'contribution_test', |
86 | '=', | |
87 | 0, | |
88 | 0, | |
89 | 0, | |
be2fb01f | 90 | ]; |
593e0d0b | 91 | } |
be2fb01f | 92 | $returnProperties = ['contribution_id' => 1]; |
7f20f38e | 93 | $sortOrder = $sortCol = NULL; |
353ffa53 | 94 | if ($form->get(CRM_Utils_Sort::SORT_ORDER)) { |
481a74f4 | 95 | $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER); |
7f20f38e | 96 | //Include sort column in select clause. |
be2fb01f | 97 | $sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder)); |
7f20f38e | 98 | $returnProperties[$sortCol] = 1; |
6a488035 | 99 | } |
665e5ec7 | 100 | |
34eec215 | 101 | $form->_includesSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams); |
7f20f38e | 102 | $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE, |
6a488035 TO |
103 | CRM_Contact_BAO_Query::MODE_CONTRIBUTE |
104 | ); | |
17caeafe | 105 | // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this |
106 | // can we remove? if not why not? | |
34eec215 | 107 | if ($form->_includesSoftCredits) { |
be2fb01f | 108 | $contactIds = $contributionContactIds = []; |
34eec215 DS |
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 "; | |
0db6c3e1 TO |
111 | } |
112 | else { | |
34eec215 DS |
113 | $query->_distinctComponentClause = ' civicrm_contribution.id'; |
114 | $query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id '; | |
115 | } | |
6a488035 TO |
116 | $result = $query->searchQuery(0, 0, $sortOrder); |
117 | while ($result->fetch()) { | |
118 | $ids[] = $result->contribution_id; | |
34eec215 DS |
119 | if ($form->_includesSoftCredits) { |
120 | $contactIds[$result->contact_id] = $result->contact_id; | |
121 | $contributionContactIds["{$result->contact_id}-{$result->contribution_id}"] = $result->contribution_id; | |
122 | } | |
6a488035 TO |
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 | } | |
b9884d60 | 132 | if (!empty($form->_includesSoftCredits) && !empty($contactIds)) { |
34eec215 DS |
133 | $form->_contactIds = $contactIds; |
134 | $form->_contributionContactIds = $contributionContactIds; | |
135 | } | |
6a488035 TO |
136 | |
137 | $form->_contributionIds = $form->_componentIds = $ids; | |
7f20f38e | 138 | $form->set('contributionIds', $form->_contributionIds); |
6a488035 TO |
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", | |
353ffa53 TO |
155 | $urlParams |
156 | )); | |
6a488035 TO |
157 | } |
158 | } | |
159 | ||
59d861cb | 160 | /** |
161 | * Sets contribution Ids for unit test. | |
bc854509 | 162 | * |
163 | * @param array $contributionIds | |
59d861cb | 164 | */ |
165 | public function setContributionIds($contributionIds) { | |
166 | $this->_contributionIds = $contributionIds; | |
167 | } | |
168 | ||
6a488035 TO |
169 | /** |
170 | * Given the contribution id, compute the contact id | |
171 | * since its used for things like send email | |
172 | */ | |
173 | public function setContactIDs() { | |
34eec215 | 174 | if (!$this->_includesSoftCredits) { |
797ddfb4 | 175 | $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent( |
34eec215 DS |
176 | $this->_contributionIds, |
177 | 'civicrm_contribution' | |
178 | ); | |
179 | } | |
6a488035 TO |
180 | } |
181 | ||
182 | /** | |
100fef9d | 183 | * Simple shell that derived classes can call to add buttons to |
6a488035 TO |
184 | * the form with a customized title for the main Submit |
185 | * | |
014c4014 TO |
186 | * @param string $title |
187 | * Title of the main button. | |
188 | * @param string $nextType | |
189 | * Button type for the form after processing. | |
da6b46f4 EM |
190 | * @param string $backType |
191 | * @param bool $submitOnce | |
6a488035 | 192 | */ |
00be9182 | 193 | public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) { |
be2fb01f | 194 | $this->addButtons([ |
1330f57a SL |
195 | [ |
196 | 'type' => $nextType, | |
197 | 'name' => $title, | |
198 | 'isDefault' => TRUE, | |
199 | ], | |
200 | [ | |
201 | 'type' => $backType, | |
202 | 'name' => ts('Cancel'), | |
203 | ], | |
204 | ]); | |
6a488035 | 205 | } |
96025800 | 206 | |
6a488035 | 207 | } |