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 * Class for contribute form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
22 class CRM_Contribute_Form_Task
extends CRM_Core_Form_Task
{
25 * The array that holds all the contribution ids.
29 protected $_contributionIds;
32 * The array that holds all the mapping contribution and contact ids.
36 protected $_contributionContactIds = [];
39 * The flag to tell if there are soft credits included.
43 public $_includesSoftCredits = FALSE;
46 * Build all the data structures needed to build the form.
48 public function preProcess() {
49 self
::preProcessCommon($this);
53 * @param CRM_Core_Form $form
55 public static function preProcessCommon(&$form) {
56 $form->_contributionIds
= [];
58 $values = $form->controller
->exportValues($form->get('searchFormName'));
60 $form->_task
= $values['task'] ??
NULL;
61 $contributeTasks = CRM_Contribute_Task
::tasks();
62 $form->assign('taskName', CRM_Utils_Array
::value($form->_task
, $contributeTasks));
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
);
73 $queryParams = $form->get('queryParams');
75 if (is_array($queryParams)) {
76 foreach ($queryParams as $fields) {
77 if ($fields[0] == 'contribution_test') {
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;
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
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 ";
113 $query->_distinctComponentClause
= ' civicrm_contribution.id';
114 $query->_groupByComponentClause
= ' GROUP BY civicrm_contribution.id ';
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
;
124 $form->assign('totalSelectedContributions', $form->get('rowCount'));
128 $form->_componentClause
= ' civicrm_contribution.id IN ( ' . implode(',', $ids) . ' ) ';
130 $form->assign('totalSelectedContributions', count($ids));
132 if (!empty($form->_includesSoftCredits
) && !empty($contactIds)) {
133 $form->_contactIds
= $contactIds;
134 $form->_contributionContactIds
= $contributionContactIds;
137 $form->_contributionIds
= $form->_componentIds
= $ids;
138 $form->set('contributionIds', $form->_contributionIds
);
140 //set the context for redirection for any task actions
141 $session = CRM_Core_Session
::singleton();
143 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $form);
144 $urlParams = 'force=1';
145 if (CRM_Utils_Rule
::qfKey($qfKey)) {
146 $urlParams .= "&qfKey=$qfKey";
149 $searchFormName = strtolower($form->get('searchFormName'));
150 if ($searchFormName == 'search') {
151 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contribute/search', $urlParams));
154 $session->replaceUserContext(CRM_Utils_System
::url("civicrm/contact/search/$searchFormName",
161 * Sets contribution Ids for unit test.
163 * @param array $contributionIds
165 public function setContributionIds($contributionIds) {
166 $this->_contributionIds
= $contributionIds;
170 * Given the contribution id, compute the contact id
171 * since its used for things like send email
173 public function setContactIDs() {
174 if (!$this->_includesSoftCredits
) {
175 $this->_contactIds
= CRM_Core_DAO
::getContactIDsFromComponent(
176 $this->_contributionIds
,
177 'civicrm_contribution'
183 * Simple shell that derived classes can call to add buttons to
184 * the form with a customized title for the main Submit
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
193 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
202 'name' => ts('Cancel'),