Fix editing and saving a template contribution via form
[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 use CRM_Contribute_Form_Task_TaskTrait;
25
26 /**
27 * The array that holds all the contribution ids.
28 *
29 * @var array
30 */
31 protected $_contributionIds;
32
33 /**
34 * Build all the data structures needed to build the form.
35 */
36 public function preProcess() {
37 self::preProcessCommon($this);
38 }
39
40 /**
41 * @param \CRM_Contribute_Form_Task $form
42 *
43 * @throws \CRM_Core_Exception
44 */
45 public static function preProcessCommon(&$form): void {
46 $form->_contributionIds = [];
47
48 $values = $form->getSearchFormValues();
49
50 $form->_task = $values['task'] ?? NULL;
51
52 $ids = $form->getIDs();
53 $form->_componentClause = $form->getComponentClause();
54 $form->assign('totalSelectedContributions', count($ids));
55 $form->_contributionIds = $form->_componentIds = $ids;
56 $form->set('contributionIds', $form->_contributionIds);
57 $form->setNextUrl('contribute');
58 }
59
60 /**
61 * Sets contribution Ids for unit test.
62 *
63 * @param array $contributionIds
64 */
65 public function setContributionIds(array $contributionIds): void {
66 $this->ids = $contributionIds;
67 }
68
69 /**
70 * Given the contribution id, compute the contact id
71 * since its used for things like send email
72 */
73 public function setContactIDs(): void {
74 if (!$this->isQueryIncludesSoftCredits()) {
75 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent(
76 $this->_contributionIds,
77 'civicrm_contribution'
78 );
79 }
80 }
81
82 /**
83 * Simple shell that derived classes can call to add buttons to
84 * the form with a customized title for the main Submit
85 *
86 * @param string $title
87 * Title of the main button.
88 * @param string $nextType
89 * Button type for the form after processing.
90 * @param string $backType
91 * @param bool $submitOnce
92 */
93 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
94 $this->addButtons([
95 [
96 'type' => $nextType,
97 'name' => $title,
98 'isDefault' => TRUE,
99 ],
100 [
101 'type' => $backType,
102 'name' => ts('Cancel'),
103 ],
104 ]);
105 }
106
107 /**
108 * Get the token processor schema required to list any tokens for this task.
109 *
110 * @return array
111 */
112 public function getTokenSchema(): array {
113 return ['contributionId', 'contactId'];
114 }
115
116 }