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 * Shared parent class for recurring contribution forms.
21 class CRM_Contribute_Form_ContributionRecur
extends CRM_Core_Form
{
23 use CRM_Core_Form_EntityFormTrait
;
30 protected $_coid = NULL;
33 * Contribution Recur ID.
37 protected $_crid = NULL;
40 * The recurring contribution id, used when editing the recurring contribution.
42 * For historical reasons this duplicates _crid & since the name is more meaningful
43 * we should probably deprecate $_crid.
47 protected $contributionRecurID = NULL;
54 protected $_mid = NULL;
57 * Payment processor object.
59 * @var \CRM_Core_Payment
61 protected $_paymentProcessorObj = NULL;
64 * Current payment processor.
66 * This includes a copy of the object in 'object' key for legacy reasons.
70 public $_paymentProcessor = [];
73 * Fields for the entity to be assigned to the template.
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
85 protected $entityFields = [];
88 * Details of the subscription (recurring contribution) to be altered.
92 protected $subscriptionDetails = [];
95 * Is the form being accessed by a front end user to update their own recurring.
99 protected $selfService;
102 * Explicitly declare the entity api name.
104 public function getDefaultEntity() {
105 return 'ContributionRecur';
109 * Explicitly declare the form context.
111 public function getDefaultContext() {
116 * Get the entity id being edited.
120 public function getEntityId() {
121 return $this->contributionRecurID
;
125 * Set variables up before form is built.
127 * @throws \CRM_Core_Exception
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());
143 * Set the payment processor object up.
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.
148 protected function setPaymentProcessor() {
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'));
154 $this->_paymentProcessorObj
= $this->_paymentProcessor
['object'];
156 elseif ($this->_mid
) {
157 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'obj');
158 $this->_paymentProcessor
= $this->_paymentProcessorObj
->getPaymentProcessor();
163 * Set the subscription details on the form.
165 protected function setSubscriptionDetails() {
166 if ($this->contributionRecurID
) {
167 $this->subscriptionDetails
= $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_crid
);
169 elseif ($this->_coid
) {
170 $this->subscriptionDetails
= $this->_subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_coid
, 'contribution');
172 elseif ($this->_mid
) {
173 $this->subscriptionDetails
= CRM_Contribute_BAO_ContributionRecur
::getSubscriptionDetails($this->_mid
, 'membership');
175 // This is being set temporarily - we should eventually just use the getter fn.
176 $this->_subscriptionDetails
= $this->subscriptionDetails
;
180 * Get details for the recurring contribution being altered.
182 * @return \CRM_Core_DAO
184 public function getSubscriptionDetails() {
185 return $this->subscriptionDetails
;
189 * Get the contact ID for the subscription.
193 protected function getSubscriptionContactID() {
194 $sub = $this->getSubscriptionDetails();
195 return $sub->contact_id ??
FALSE;
199 * Is this being used by a front end user to update their own recurring.
203 protected function isSelfService() {
204 if (!is_null($this->selfService
)) {
205 return $this->selfService
;
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.'));
212 $this->selfService
= TRUE;
214 return $this->selfService
;