Commit | Line | Data |
---|---|---|
ca809bb6 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
ca809bb6 | 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 | | |
ca809bb6 | 9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
ca809bb6 | 16 | */ |
17 | ||
18 | /** | |
19 | * Shared parent class for recurring contribution forms. | |
20 | */ | |
21 | class CRM_Contribute_Form_ContributionRecur extends CRM_Core_Form { | |
22 | ||
379d6cd7 | 23 | use CRM_Core_Form_EntityFormTrait; |
24 | ||
ca809bb6 | 25 | /** |
5ba7d840 | 26 | * Contribution ID. |
27 | * | |
28 | * @var int | |
ca809bb6 | 29 | */ |
30 | protected $_coid = NULL; | |
31 | ||
32 | /** | |
5ba7d840 | 33 | * Contribution Recur ID. |
34 | * | |
35 | * @var int | |
ca809bb6 | 36 | */ |
37 | protected $_crid = NULL; | |
38 | ||
1f17c8ef | 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 | /** | |
5ba7d840 | 50 | * Membership ID. |
51 | * | |
52 | * @var int | |
1f17c8ef | 53 | */ |
54 | protected $_mid = NULL; | |
55 | ||
71e680bd | 56 | /** |
57 | * Payment processor object. | |
58 | * | |
59 | * @var \CRM_Core_Payment | |
60 | */ | |
61 | protected $_paymentProcessorObj = NULL; | |
62 | ||
63 | /** | |
5ba7d840 | 64 | * Current payment processor. |
71e680bd | 65 | * |
5ba7d840 | 66 | * This includes a copy of the object in 'object' key for legacy reasons. |
67 | * | |
68 | * @var array | |
71e680bd | 69 | */ |
70 | public $_paymentProcessor = []; | |
71 | ||
379d6cd7 | 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 | ||
503699d5 | 87 | /** |
88 | * Details of the subscription (recurring contribution) to be altered. | |
89 | * | |
90 | * @var array | |
91 | */ | |
92 | protected $subscriptionDetails = []; | |
93 | ||
6a719e23 | 94 | /** |
d5c59a00 | 95 | * Is the form being accessed by a front end user to update their own recurring. |
6a719e23 SL |
96 | * |
97 | * @var bool | |
98 | */ | |
99 | protected $selfService; | |
100 | ||
ca809bb6 | 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 | ||
f1105edc | 115 | /** |
116 | * Get the entity id being edited. | |
117 | * | |
118 | * @return int|null | |
119 | */ | |
120 | public function getEntityId() { | |
121 | return $this->contributionRecurID; | |
122 | } | |
123 | ||
1f17c8ef | 124 | /** |
125 | * Set variables up before form is built. | |
503699d5 | 126 | * |
127 | * @throws \CRM_Core_Exception | |
1f17c8ef | 128 | */ |
129 | public function preProcess() { | |
379d6cd7 | 130 | $this->setAction(CRM_Core_Action::UPDATE); |
1f17c8ef | 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); | |
503699d5 | 135 | $this->setSubscriptionDetails(); |
71e680bd | 136 | $this->setPaymentProcessor(); |
503699d5 | 137 | if ($this->getSubscriptionContactID()) { |
138 | $this->set('cid', $this->getSubscriptionContactID()); | |
139 | } | |
71e680bd | 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 | } | |
703235f6 | 156 | elseif ($this->_mid) { |
157 | $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_mid, 'membership', 'obj'); | |
158 | $this->_paymentProcessor = $this->_paymentProcessorObj->getPaymentProcessor(); | |
159 | } | |
1f17c8ef | 160 | } |
161 | ||
503699d5 | 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 | } | |
703235f6 | 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; | |
503699d5 | 177 | } |
178 | ||
179 | /** | |
180 | * Get details for the recurring contribution being altered. | |
181 | * | |
182 | * @return array | |
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 isset($sub->contact_id) ? $sub->contact_id : FALSE; | |
196 | } | |
197 | ||
6a719e23 SL |
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 | ||
ca809bb6 | 217 | } |