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