[REF] Follow up cleanup
[civicrm-core.git] / CRM / Contribute / Form / ContributionRecur.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 * Shared parent class for recurring contribution forms.
20 */
21 class CRM_Contribute_Form_ContributionRecur extends CRM_Core_Form {
22
23 use CRM_Core_Form_EntityFormTrait;
24
25 /**
26 * Contribution ID.
27 *
28 * @var int
29 */
30 protected $_coid = NULL;
31
32 /**
33 * Contribution Recur ID.
34 *
35 * @var int
36 */
37 protected $_crid = NULL;
38
39 /**
40 * The recurring contribution id, used when editing the recurring contribution.
41 *
42 * For historical reasons this duplicates _crid & since the name is more meaningful
43 * we should probably deprecate $_crid.
44 *
45 * @var int
46 */
47 protected $contributionRecurID = NULL;
48
49 /**
50 * Membership ID.
51 *
52 * @var int
53 */
54 protected $_mid = NULL;
55
56 /**
57 * Payment processor object.
58 *
59 * @var \CRM_Core_Payment
60 */
61 protected $_paymentProcessorObj = NULL;
62
63 /**
64 * Current payment processor.
65 *
66 * This includes a copy of the object in 'object' key for legacy reasons.
67 *
68 * @var array
69 */
70 public $_paymentProcessor = [];
71
72 /**
73 * Fields for the entity to be assigned to the template.
74 *
75 * Fields may have keys
76 * - name (required to show in tpl from the array)
77 * - description (optional, will appear below the field)
78 * - not-auto-addable - this class will not attempt to add the field using addField.
79 * (this will be automatically set if the field does not have html in it's metadata
80 * or is not a core field on the form's entity).
81 * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
82 * - template - use a field specific template to render this field
83 * @var array
84 */
85 protected $entityFields = [];
86
87 /**
88 * Details of the subscription (recurring contribution) to be altered.
89 *
90 * @var \CRM_Core_DAO
91 */
92 protected $subscriptionDetails = [];
93
94 /**
95 * Is the form being accessed by a front end user to update their own recurring.
96 *
97 * @var bool
98 */
99 protected $selfService;
100
101 /**
102 * Explicitly declare the entity api name.
103 */
104 public function getDefaultEntity() {
105 return 'ContributionRecur';
106 }
107
108 /**
109 * Explicitly declare the form context.
110 */
111 public function getDefaultContext() {
112 return 'create';
113 }
114
115 /**
116 * Get the entity id being edited.
117 *
118 * @return int|null
119 */
120 public function getEntityId() {
121 return $this->contributionRecurID;
122 }
123
124 /**
125 * Set variables up before form is built.
126 *
127 * @throws \CRM_Core_Exception
128 */
129 public function preProcess() {
130 $this->setAction(CRM_Core_Action::UPDATE);
131 $this->_mid = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE);
132 $this->_crid = CRM_Utils_Request::retrieve('crid', 'Integer', $this, FALSE);
133 $this->contributionRecurID = $this->_crid;
134 $this->_coid = CRM_Utils_Request::retrieve('coid', 'Integer', $this, FALSE);
135 $this->setSubscriptionDetails();
136 $this->setPaymentProcessor();
137 if ($this->getSubscriptionContactID()) {
138 $this->set('cid', $this->getSubscriptionContactID());
139 }
140 }
141
142 /**
143 * Set the payment processor object up.
144 *
145 * This is a function that needs to be better consolidated between the inheriting forms
146 * but this is good choice of function to call.
147 */
148 protected function setPaymentProcessor() {
149 if ($this->_crid) {
150 $this->_paymentProcessor = CRM_Contribute_BAO_ContributionRecur::getPaymentProcessor($this->contributionRecurID);
151 if (!$this->_paymentProcessor) {
152 CRM_Core_Error::statusBounce(ts('There is no valid processor for this subscription so it cannot be updated'));
153 }
154 $this->_paymentProcessorObj = $this->_paymentProcessor['object'];
155 }
156 elseif ($this->_mid) {
157 $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_mid, 'membership', 'obj');
158 $this->_paymentProcessor = $this->_paymentProcessorObj->getPaymentProcessor();
159 }
160 }
161
162 /**
163 * Set the subscription details on the form.
164 */
165 protected function setSubscriptionDetails() {
166 if ($this->contributionRecurID) {
167 $this->subscriptionDetails = $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_crid);
168 }
169 elseif ($this->_coid) {
170 $this->subscriptionDetails = $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_coid, 'contribution');
171 }
172 elseif ($this->_mid) {
173 $this->subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_mid, 'membership');
174 }
175 // This is being set temporarily - we should eventually just use the getter fn.
176 $this->_subscriptionDetails = $this->subscriptionDetails;
177 }
178
179 /**
180 * Get details for the recurring contribution being altered.
181 *
182 * @return \CRM_Core_DAO
183 */
184 public function getSubscriptionDetails() {
185 return $this->subscriptionDetails;
186 }
187
188 /**
189 * Get the contact ID for the subscription.
190 *
191 * @return int|false
192 */
193 protected function getSubscriptionContactID() {
194 $sub = $this->getSubscriptionDetails();
195 return $sub->contact_id ?? FALSE;
196 }
197
198 /**
199 * Is this being used by a front end user to update their own recurring.
200 *
201 * @return bool
202 */
203 protected function isSelfService() {
204 if (!is_null($this->selfService)) {
205 return $this->selfService;
206 }
207 $this->selfService = FALSE;
208 if (!CRM_Core_Permission::check('edit contributions')) {
209 if ($this->_subscriptionDetails->contact_id != $this->getContactID()) {
210 CRM_Core_Error::statusBounce(ts('You do not have permission to cancel this recurring contribution.'));
211 }
212 $this->selfService = TRUE;
213 }
214 return $this->selfService;
215 }
216
217 }