Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Base class for offline membership / membership type / membership renewal and membership status forms | |
20 | * | |
21 | */ | |
cc984198 | 22 | class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { |
6a488035 | 23 | |
59c798c9 | 24 | use CRM_Core_Form_EntityFormTrait; |
6a488035 | 25 | |
a6513ad5 EM |
26 | /** |
27 | * Membership Type ID | |
971e129b | 28 | * @var int |
a6513ad5 EM |
29 | */ |
30 | protected $_memType; | |
31 | ||
32 | /** | |
33 | * Array of from email ids | |
34 | * @var array | |
35 | */ | |
be2fb01f | 36 | protected $_fromEmails = []; |
a6513ad5 | 37 | |
ab30e033 EM |
38 | /** |
39 | * Details of all enabled membership types. | |
40 | * | |
41 | * @var array | |
42 | */ | |
be2fb01f | 43 | protected $allMembershipTypeDetails = []; |
ab30e033 EM |
44 | |
45 | /** | |
46 | * Array of membership type IDs and whether they permit autorenewal. | |
47 | * | |
48 | * @var array | |
49 | */ | |
be2fb01f | 50 | protected $membershipTypeRenewalStatus = []; |
ab30e033 | 51 | |
e4a6290d | 52 | /** |
53 | * Price set ID configured for the form. | |
54 | * | |
55 | * @var int | |
56 | */ | |
57 | public $_priceSetId; | |
58 | ||
59 | /** | |
60 | * Price set details as an array. | |
61 | * | |
62 | * @var array | |
63 | */ | |
64 | public $_priceSet; | |
65 | ||
6452294d MW |
66 | /** |
67 | * Explicitly declare the entity api name. | |
68 | */ | |
69 | public function getDefaultEntity() { | |
70 | return 'Membership'; | |
71 | } | |
72 | ||
6d5b9c63 | 73 | /** |
74 | * @var array | |
75 | */ | |
76 | protected $statusMessage = []; | |
77 | ||
78 | /** | |
79 | * Add to the status message. | |
80 | * | |
81 | * @param $message | |
82 | */ | |
83 | protected function addStatusMessage($message) { | |
84 | $this->statusMessage[] = $message; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Get the status message. | |
89 | * | |
90 | * @return string | |
91 | */ | |
92 | protected function getStatusMessage() { | |
93 | return implode(' ', $this->statusMessage); | |
94 | } | |
95 | ||
09108d7d | 96 | /** |
97 | * Values submitted to the form, processed along the way. | |
98 | * | |
99 | * @var array | |
100 | */ | |
be2fb01f | 101 | protected $_params = []; |
09108d7d | 102 | |
59c798c9 | 103 | /** |
104 | * Fields for the entity to be assigned to the template. | |
105 | * | |
106 | * Fields may have keys | |
107 | * - name (required to show in tpl from the array) | |
108 | * - description (optional, will appear below the field) | |
109 | * - not-auto-addable - this class will not attempt to add the field using addField. | |
110 | * (this will be automatically set if the field does not have html in it's metadata | |
111 | * or is not a core field on the form's entity). | |
112 | * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']] | |
113 | * - template - use a field specific template to render this field | |
114 | * - required | |
115 | * - is_freeze (field should be frozen). | |
116 | * | |
117 | * @var array | |
118 | */ | |
119 | protected $entityFields = []; | |
120 | ||
00be9182 | 121 | public function preProcess() { |
42e8b05c EM |
122 | // Check for edit permission. |
123 | if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) { | |
beb414cc | 124 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
42e8b05c | 125 | } |
6a38708b | 126 | if (!CRM_Member_BAO_Membership::statusAvailabilty()) { |
127 | // all possible statuses are disabled - redirect back to contact form | |
128 | CRM_Core_Error::statusBounce(ts('There are no configured membership statuses. You cannot add this membership until your membership statuses are correctly configured')); | |
129 | } | |
130 | ||
42e8b05c | 131 | parent::preProcess(); |
be2fb01f | 132 | $params = []; |
edc80cda | 133 | $params['context'] = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'membership'); |
7865d848 | 134 | $params['id'] = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
19046166 | 135 | $params['mode'] = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this); |
7865d848 EM |
136 | |
137 | $this->setContextVariables($params); | |
a6513ad5 EM |
138 | |
139 | $this->assign('context', $this->_context); | |
140 | $this->assign('membershipMode', $this->_mode); | |
be2fb01f | 141 | $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, [], TRUE); |
ab30e033 EM |
142 | foreach ($this->allMembershipTypeDetails as $index => $membershipType) { |
143 | if ($membershipType['auto_renew']) { | |
144 | $this->_recurMembershipTypes[$index] = $membershipType; | |
145 | $this->membershipTypeRenewalStatus[$index] = $membershipType['auto_renew']; | |
146 | } | |
147 | } | |
6a488035 TO |
148 | } |
149 | ||
150 | /** | |
c490a46a | 151 | * Set default values for the form. MobileProvider that in edit/view mode |
6a488035 TO |
152 | * the default values are retrieved from the database |
153 | * | |
6a488035 | 154 | * |
a6c01b45 CW |
155 | * @return array |
156 | * defaults | |
6a488035 | 157 | */ |
00be9182 | 158 | public function setDefaultValues() { |
be2fb01f | 159 | $defaults = []; |
a6513ad5 | 160 | if (isset($this->_id)) { |
be2fb01f | 161 | $params = ['id' => $this->_id]; |
a6513ad5 | 162 | CRM_Member_BAO_Membership::retrieve($params, $defaults); |
42e8b05c EM |
163 | if (isset($defaults['minimum_fee'])) { |
164 | $defaults['minimum_fee'] = CRM_Utils_Money::format($defaults['minimum_fee'], NULL, '%a'); | |
165 | } | |
6a488035 | 166 | |
42e8b05c EM |
167 | if (isset($defaults['status'])) { |
168 | $this->assign('membershipStatus', $defaults['status']); | |
169 | } | |
e136f704 O |
170 | |
171 | if (!empty($defaults['is_override'])) { | |
172 | $defaults['is_override'] = CRM_Member_StatusOverrideTypes::PERMANENT; | |
173 | } | |
174 | if (!empty($defaults['status_override_end_date'])) { | |
175 | $defaults['is_override'] = CRM_Member_StatusOverrideTypes::UNTIL_DATE; | |
176 | } | |
6a488035 TO |
177 | } |
178 | ||
179 | if ($this->_action & CRM_Core_Action::ADD) { | |
180 | $defaults['is_active'] = 1; | |
181 | } | |
182 | ||
183 | if (isset($defaults['member_of_contact_id']) && | |
184 | $defaults['member_of_contact_id'] | |
185 | ) { | |
186 | $defaults['member_org'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
187 | $defaults['member_of_contact_id'], 'display_name' | |
188 | ); | |
189 | } | |
f525ec1f | 190 | if (!empty($defaults['membership_type_id'])) { |
191 | $this->_memType = $defaults['membership_type_id']; | |
192 | } | |
193 | if (is_numeric($this->_memType)) { | |
be2fb01f | 194 | $defaults['membership_type_id'] = []; |
f525ec1f | 195 | $defaults['membership_type_id'][0] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', |
196 | $this->_memType, | |
197 | 'member_of_contact_id', | |
198 | 'id' | |
199 | ); | |
200 | $defaults['membership_type_id'][1] = $this->_memType; | |
201 | } | |
202 | else { | |
203 | $defaults['membership_type_id'] = $this->_memType; | |
204 | } | |
6a488035 TO |
205 | return $defaults; |
206 | } | |
207 | ||
208 | /** | |
fe482240 | 209 | * Build the form object. |
6a488035 TO |
210 | */ |
211 | public function buildQuickForm() { | |
a6e29c95 | 212 | $this->assignSalesTaxMetadataToTemplate(); |
4aa7d844 | 213 | |
b15c60e1 | 214 | $this->addPaymentProcessorSelect(TRUE, FALSE, TRUE); |
0dc4ef42 | 215 | CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE, $this->getDefaultPaymentInstrumentId()); |
38f6574d | 216 | $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors)); |
4aa7d844 EM |
217 | // Build the form for auto renew. This is displayed when in credit card mode or update mode. |
218 | // The reason for showing it in update mode is not that clear. | |
4aa7d844 | 219 | if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) { |
ab30e033 EM |
220 | if (!empty($this->_recurPaymentProcessors)) { |
221 | $this->assign('allowAutoRenew', TRUE); | |
4aa7d844 EM |
222 | } |
223 | ||
ab30e033 | 224 | $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'), |
be2fb01f | 225 | NULL, ['onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );"] |
ab30e033 EM |
226 | ); |
227 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
228 | $autoRenewElement->freeze(); | |
229 | } | |
4aa7d844 | 230 | |
4aa7d844 EM |
231 | $this->addElement('checkbox', |
232 | 'auto_renew', | |
8c80f3f9 | 233 | ts('Membership renewed automatically') |
4aa7d844 EM |
234 | ); |
235 | ||
4aa7d844 | 236 | } |
ab30e033 | 237 | $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus)); |
4aa7d844 | 238 | |
6a488035 | 239 | if ($this->_action & CRM_Core_Action::RENEW) { |
be2fb01f CW |
240 | $this->addButtons([ |
241 | [ | |
c5c263ca AH |
242 | 'type' => 'upload', |
243 | 'name' => ts('Renew'), | |
244 | 'isDefault' => TRUE, | |
be2fb01f CW |
245 | ], |
246 | [ | |
c5c263ca AH |
247 | 'type' => 'cancel', |
248 | 'name' => ts('Cancel'), | |
be2fb01f CW |
249 | ], |
250 | ]); | |
6a488035 TO |
251 | } |
252 | elseif ($this->_action & CRM_Core_Action::DELETE) { | |
be2fb01f CW |
253 | $this->addButtons([ |
254 | [ | |
c5c263ca AH |
255 | 'type' => 'next', |
256 | 'name' => ts('Delete'), | |
257 | 'isDefault' => TRUE, | |
be2fb01f CW |
258 | ], |
259 | [ | |
c5c263ca AH |
260 | 'type' => 'cancel', |
261 | 'name' => ts('Cancel'), | |
be2fb01f CW |
262 | ], |
263 | ]); | |
6a488035 TO |
264 | } |
265 | else { | |
be2fb01f CW |
266 | $this->addButtons([ |
267 | [ | |
c5c263ca AH |
268 | 'type' => 'upload', |
269 | 'name' => ts('Save'), | |
270 | 'isDefault' => TRUE, | |
be2fb01f CW |
271 | ], |
272 | [ | |
c5c263ca AH |
273 | 'type' => 'upload', |
274 | 'name' => ts('Save and New'), | |
275 | 'subName' => 'new', | |
be2fb01f CW |
276 | ], |
277 | [ | |
c5c263ca AH |
278 | 'type' => 'cancel', |
279 | 'name' => ts('Cancel'), | |
be2fb01f CW |
280 | ], |
281 | ]); | |
6a488035 TO |
282 | } |
283 | } | |
284 | ||
fb3082b2 | 285 | /** |
100fef9d | 286 | * Extract values from the contact create boxes on the form and assign appropriately to |
6a488035 TO |
287 | * |
288 | * - $this->_contributorEmail, | |
289 | * - $this->_memberEmail & | |
fb3082b2 | 290 | * - $this->_contributionName |
6a488035 TO |
291 | * - $this->_memberName |
292 | * - $this->_contactID (effectively memberContactId but changing might have spin-off effects) | |
293 | * - $this->_contributorContactId - id of the contributor | |
294 | * - $this->_receiptContactId | |
295 | * | |
296 | * If the member & contributor are the same then the values will be the same. But if different people paid | |
297 | * then they weill differ | |
298 | * | |
5a4f6742 CW |
299 | * @param array $formValues |
300 | * values from form. The important values we are looking for are. | |
4c7aa1f7 CW |
301 | * - contact_id |
302 | * - soft_credit_contact_id | |
6a488035 | 303 | */ |
9b873358 | 304 | public function storeContactFields($formValues) { |
6a488035 | 305 | // in a 'standalone form' (contact id not in the url) the contact will be in the form values |
4c7aa1f7 CW |
306 | if (!empty($formValues['contact_id'])) { |
307 | $this->_contactID = $formValues['contact_id']; | |
6a488035 TO |
308 | } |
309 | ||
310 | list($this->_memberDisplayName, | |
353ffa53 TO |
311 | $this->_memberEmail |
312 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
6a488035 TO |
313 | |
314 | //CRM-10375 Where the payer differs to the member the payer should get the email. | |
315 | // here we store details in order to do that | |
4c7aa1f7 CW |
316 | if (!empty($formValues['soft_credit_contact_id'])) { |
317 | $this->_receiptContactId = $this->_contributorContactID = $formValues['soft_credit_contact_id']; | |
481a74f4 | 318 | list($this->_contributorDisplayName, |
353ffa53 | 319 | $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contributorContactID); |
6a488035 TO |
320 | } |
321 | else { | |
322 | $this->_receiptContactId = $this->_contributorContactID = $this->_contactID; | |
323 | $this->_contributorDisplayName = $this->_memberDisplayName; | |
324 | $this->_contributorEmail = $this->_memberEmail; | |
325 | } | |
326 | } | |
96025800 | 327 | |
0271af37 | 328 | /** |
329 | * Set variables in a way that can be accessed from different places. | |
330 | * | |
331 | * This is part of refactoring for unit testability on the submit function. | |
332 | * | |
333 | * @param array $params | |
334 | */ | |
7865d848 | 335 | protected function setContextVariables($params) { |
be2fb01f | 336 | $variables = [ |
7865d848 EM |
337 | 'action' => '_action', |
338 | 'context' => '_context', | |
339 | 'id' => '_id', | |
340 | 'cid' => '_contactID', | |
341 | 'mode' => '_mode', | |
be2fb01f | 342 | ]; |
7865d848 EM |
343 | foreach ($variables as $paramKey => $classVar) { |
344 | if (isset($params[$paramKey]) && !isset($this->$classVar)) { | |
345 | $this->$classVar = $params[$paramKey]; | |
346 | } | |
347 | } | |
348 | ||
7865d848 EM |
349 | if ($this->_id) { |
350 | $this->_memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'membership_type_id'); | |
351 | $this->_membershipIDs[] = $this->_id; | |
352 | } | |
353 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); | |
354 | } | |
355 | ||
a70418a7 EM |
356 | /** |
357 | * Create a recurring contribution record. | |
358 | * | |
359 | * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard | |
360 | * to know the downstream impacts if we keep passing around the same array. | |
361 | * | |
362 | * @param $paymentParams | |
363 | * | |
364 | * @return array | |
365 | * @throws \CiviCRM_API3_Exception | |
366 | */ | |
367 | protected function processRecurringContribution($paymentParams) { | |
368 | $membershipID = $paymentParams['membership_type_id'][1]; | |
be2fb01f | 369 | $contributionRecurParams = [ |
a70418a7 EM |
370 | 'contact_id' => $paymentParams['contactID'], |
371 | 'amount' => $paymentParams['total_amount'], | |
b1b2796c | 372 | 'contribution_status_id' => 'Pending', |
a70418a7 | 373 | 'payment_processor_id' => $paymentParams['payment_processor_id'], |
b1b2796c | 374 | 'campaign_id' => $paymentParams['campaign_id'], |
a70418a7 | 375 | 'financial_type_id' => $paymentParams['financial_type_id'], |
b1b2796c | 376 | 'is_email_receipt' => $paymentParams['is_email_receipt'], |
377 | 'payment_instrument_id' => $paymentParams['payment_instrument_id'], | |
378 | 'invoice_id' => $paymentParams['invoice_id'], | |
be2fb01f | 379 | ]; |
a70418a7 | 380 | |
be2fb01f | 381 | $mapping = [ |
a70418a7 EM |
382 | 'frequency_interval' => 'duration_interval', |
383 | 'frequency_unit' => 'duration_unit', | |
be2fb01f CW |
384 | ]; |
385 | $membershipType = civicrm_api3('MembershipType', 'getsingle', [ | |
a70418a7 EM |
386 | 'id' => $membershipID, |
387 | 'return' => $mapping, | |
be2fb01f | 388 | ]); |
a70418a7 | 389 | |
be2fb01f | 390 | $returnParams = ['is_recur' => TRUE]; |
a70418a7 EM |
391 | foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) { |
392 | $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName]; | |
14065266 | 393 | $returnParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName]; |
a70418a7 EM |
394 | } |
395 | ||
396 | $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams); | |
14065266 | 397 | $returnParams['contributionRecurID'] = $contributionRecur['id']; |
a70418a7 EM |
398 | return $returnParams; |
399 | } | |
400 | ||
ccb02c2d | 401 | /** |
402 | * Ensure price parameters are set. | |
403 | * | |
404 | * If they are not set it means a quick config option has been chosen so we | |
405 | * fill them in here to make the two flows the same. They look like 'price_2' => 2 etc. | |
406 | * | |
407 | * @param array $formValues | |
408 | */ | |
409 | protected function ensurePriceParamsAreSet(&$formValues) { | |
410 | foreach ($formValues as $key => $value) { | |
ea09031a | 411 | if ((substr($key, 0, 6) == 'price_') && is_numeric(substr($key, 6))) { |
ccb02c2d | 412 | return; |
413 | } | |
414 | } | |
415 | $priceFields = CRM_Member_BAO_Membership::setQuickConfigMembershipParameters( | |
416 | $formValues['membership_type_id'][0], | |
417 | $formValues['membership_type_id'][1], | |
430f2faf | 418 | CRM_Utils_Array::value('total_amount', $formValues), |
ccb02c2d | 419 | $this->_priceSetId |
420 | ); | |
421 | $formValues = array_merge($formValues, $priceFields['price_fields']); | |
422 | } | |
423 | ||
424 | /** | |
425 | * Get the details for the selected price set. | |
426 | * | |
427 | * @param array $params | |
428 | * Parameters submitted to the form. | |
429 | * | |
430 | * @return array | |
431 | */ | |
432 | protected static function getPriceSetDetails($params) { | |
433 | $priceSetID = CRM_Utils_Array::value('price_set_id', $params); | |
434 | if ($priceSetID) { | |
435 | return CRM_Price_BAO_PriceSet::getSetDetail($priceSetID); | |
436 | } | |
437 | else { | |
438 | $priceSet = CRM_Price_BAO_PriceSet::getDefaultPriceSet('membership'); | |
439 | $priceSet = reset($priceSet); | |
440 | return CRM_Price_BAO_PriceSet::getSetDetail($priceSet['setID']); | |
441 | } | |
442 | } | |
443 | ||
444 | /** | |
445 | * Get the selected price set id. | |
446 | * | |
447 | * @param array $params | |
448 | * Parameters submitted to the form. | |
449 | * | |
450 | * @return int | |
451 | */ | |
452 | protected static function getPriceSetID($params) { | |
453 | $priceSetID = CRM_Utils_Array::value('price_set_id', $params); | |
454 | if (!$priceSetID) { | |
455 | $priceSetDetails = self::getPriceSetDetails($params); | |
456 | return key($priceSetDetails); | |
457 | } | |
458 | return $priceSetID; | |
459 | } | |
460 | ||
461 | /** | |
462 | * Store parameters relating to price sets. | |
463 | * | |
464 | * @param array $formValues | |
465 | * | |
466 | * @return array | |
467 | */ | |
468 | protected function setPriceSetParameters($formValues) { | |
469 | $this->_priceSetId = self::getPriceSetID($formValues); | |
470 | $priceSetDetails = self::getPriceSetDetails($formValues); | |
471 | $this->_priceSet = $priceSetDetails[$this->_priceSetId]; | |
472 | // process price set and get total amount and line items. | |
473 | $this->ensurePriceParamsAreSet($formValues); | |
474 | return $formValues; | |
475 | } | |
476 | ||
09108d7d | 477 | /** |
478 | * Wrapper function for unit tests. | |
479 | * | |
480 | * @param array $formValues | |
481 | */ | |
482 | public function testSubmit($formValues) { | |
18135422 | 483 | $this->setContextVariables($formValues); |
09108d7d | 484 | $this->_memType = $formValues['membership_type_id'][1]; |
485 | $this->_params = $formValues; | |
486 | $this->submit(); | |
487 | } | |
488 | ||
6a488035 | 489 | } |