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