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 * Base class for offline membership / membership type / membership renewal and membership status forms
22 class CRM_Member_Form
extends CRM_Contribute_Form_AbstractEditPayment
{
24 use CRM_Core_Form_EntityFormTrait
;
33 * Array of from email ids
36 protected $_fromEmails = [];
39 * Details of all enabled membership types.
43 protected $allMembershipTypeDetails = [];
46 * Array of membership type IDs and whether they permit autorenewal.
50 protected $membershipTypeRenewalStatus = [];
53 * Price set ID configured for the form.
60 * Price set details as an array.
67 * Explicitly declare the entity api name.
69 public function getDefaultEntity() {
76 protected $statusMessage = [];
79 * Add to the status message.
83 protected function addStatusMessage($message) {
84 $this->statusMessage
[] = $message;
88 * Get the status message.
92 protected function getStatusMessage() {
93 return implode(' ', $this->statusMessage
);
97 * Values submitted to the form, processed along the way.
101 protected $_params = [];
104 * Fields for the entity to be assigned to the template.
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
115 * - is_freeze (field should be frozen).
119 protected $entityFields = [];
121 public function preProcess() {
122 // Check for edit permission.
123 if (!CRM_Core_Permission
::checkActionPermission('CiviMember', $this->_action
)) {
124 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
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'));
131 parent
::preProcess();
133 $params['context'] = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this, FALSE, 'membership');
134 $params['id'] = CRM_Utils_Request
::retrieve('id', 'Positive', $this);
135 $params['mode'] = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
137 $this->setContextVariables($params);
139 $this->assign('context', $this->_context
);
140 $this->assign('membershipMode', $this->_mode
);
141 $this->allMembershipTypeDetails
= CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, [], TRUE);
142 foreach ($this->allMembershipTypeDetails
as $index => $membershipType) {
143 if ($membershipType['auto_renew']) {
144 $this->_recurMembershipTypes
[$index] = $membershipType;
145 $this->membershipTypeRenewalStatus
[$index] = $membershipType['auto_renew'];
151 * Set default values for the form. MobileProvider that in edit/view mode
152 * the default values are retrieved from the database
158 public function setDefaultValues() {
160 if (isset($this->_id
)) {
161 $params = ['id' => $this->_id
];
162 CRM_Member_BAO_Membership
::retrieve($params, $defaults);
163 if (isset($defaults['minimum_fee'])) {
164 $defaults['minimum_fee'] = CRM_Utils_Money
::format($defaults['minimum_fee'], NULL, '%a');
167 if (isset($defaults['status'])) {
168 $this->assign('membershipStatus', $defaults['status']);
171 if (!empty($defaults['is_override'])) {
172 $defaults['is_override'] = CRM_Member_StatusOverrideTypes
::PERMANENT
;
174 if (!empty($defaults['status_override_end_date'])) {
175 $defaults['is_override'] = CRM_Member_StatusOverrideTypes
::UNTIL_DATE
;
179 if ($this->_action
& CRM_Core_Action
::ADD
) {
180 $defaults['is_active'] = 1;
183 if (isset($defaults['member_of_contact_id']) &&
184 $defaults['member_of_contact_id']
186 $defaults['member_org'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
187 $defaults['member_of_contact_id'], 'display_name'
190 if (!empty($defaults['membership_type_id'])) {
191 $this->_memType
= $defaults['membership_type_id'];
193 if (is_numeric($this->_memType
)) {
194 $defaults['membership_type_id'] = [];
195 $defaults['membership_type_id'][0] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
197 'member_of_contact_id',
200 $defaults['membership_type_id'][1] = $this->_memType
;
203 $defaults['membership_type_id'] = $this->_memType
;
209 * Build the form object.
211 public function buildQuickForm() {
212 $this->assignSalesTaxMetadataToTemplate();
214 $this->addPaymentProcessorSelect(TRUE, FALSE, TRUE);
215 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, FALSE, TRUE, $this->getDefaultPaymentInstrumentId());
216 $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors
));
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.
219 if ($this->_mode ||
($this->_action
& CRM_Core_Action
::UPDATE
)) {
220 if (!empty($this->_recurPaymentProcessors
)) {
221 $this->assign('allowAutoRenew', TRUE);
224 $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
225 NULL, ['onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );"]
227 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
228 $autoRenewElement->freeze();
231 $this->addElement('checkbox',
233 ts('Membership renewed automatically')
237 $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus
));
239 if ($this->_action
& CRM_Core_Action
::RENEW
) {
243 'name' => ts('Renew'),
248 'name' => ts('Cancel'),
252 elseif ($this->_action
& CRM_Core_Action
::DELETE
) {
256 'name' => ts('Delete'),
261 'name' => ts('Cancel'),
269 'name' => ts('Save'),
274 'name' => ts('Save and New'),
279 'name' => ts('Cancel'),
286 * Extract values from the contact create boxes on the form and assign appropriately to
288 * - $this->_contributorEmail,
289 * - $this->_memberEmail &
290 * - $this->_contributionName
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
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
299 * @param array $formValues
300 * values from form. The important values we are looking for are.
302 * - soft_credit_contact_id
304 public function storeContactFields($formValues) {
305 // in a 'standalone form' (contact id not in the url) the contact will be in the form values
306 if (!empty($formValues['contact_id'])) {
307 $this->_contactID
= $formValues['contact_id'];
310 list($this->_memberDisplayName
,
312 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
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
316 if (!empty($formValues['soft_credit_contact_id'])) {
317 $this->_receiptContactId
= $this->_contributorContactID
= $formValues['soft_credit_contact_id'];
318 list($this->_contributorDisplayName
,
319 $this->_contributorEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contributorContactID
);
322 $this->_receiptContactId
= $this->_contributorContactID
= $this->_contactID
;
323 $this->_contributorDisplayName
= $this->_memberDisplayName
;
324 $this->_contributorEmail
= $this->_memberEmail
;
329 * Set variables in a way that can be accessed from different places.
331 * This is part of refactoring for unit testability on the submit function.
333 * @param array $params
335 protected function setContextVariables($params) {
337 'action' => '_action',
338 'context' => '_context',
340 'cid' => '_contactID',
343 foreach ($variables as $paramKey => $classVar) {
344 if (isset($params[$paramKey]) && !isset($this->$classVar)) {
345 $this->$classVar = $params[$paramKey];
350 $this->_memType
= CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_id
, 'membership_type_id');
351 $this->_membershipIDs
[] = $this->_id
;
353 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
357 * Create a recurring contribution record.
359 * @param array $contributionRecurParams
361 * @param int $membershipID
364 * @throws \CiviCRM_API3_Exception
366 protected function processRecurringContribution($contributionRecurParams, $membershipID) {
369 'frequency_interval' => 'duration_interval',
370 'frequency_unit' => 'duration_unit',
372 $membershipType = civicrm_api3('MembershipType', 'getsingle', [
373 'id' => $membershipID,
374 'return' => $mapping,
377 $returnParams = ['is_recur' => TRUE];
378 foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) {
379 $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
380 $returnParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
383 $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams);
384 $returnParams['contributionRecurID'] = $contributionRecur['id'];
385 return $returnParams;
389 * Ensure price parameters are set.
391 * If they are not set it means a quick config option has been chosen so we
392 * fill them in here to make the two flows the same. They look like 'price_2' => 2 etc.
394 * @param array $formValues
396 protected function ensurePriceParamsAreSet(&$formValues) {
397 foreach ($formValues as $key => $value) {
398 if ((substr($key, 0, 6) == 'price_') && is_numeric(substr($key, 6))) {
402 $priceFields = CRM_Member_BAO_Membership
::setQuickConfigMembershipParameters(
403 $formValues['membership_type_id'][0],
404 $formValues['membership_type_id'][1],
405 CRM_Utils_Array
::value('total_amount', $formValues),
408 $formValues = array_merge($formValues, $priceFields['price_fields']);
412 * Get the details for the selected price set.
414 * @param array $params
415 * Parameters submitted to the form.
419 protected static function getPriceSetDetails($params) {
420 $priceSetID = $params['price_set_id'] ??
NULL;
422 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSetID);
425 $priceSet = CRM_Price_BAO_PriceSet
::getDefaultPriceSet('membership');
426 $priceSet = reset($priceSet);
427 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSet['setID']);
432 * Get the selected price set id.
434 * @param array $params
435 * Parameters submitted to the form.
439 protected static function getPriceSetID($params) {
440 $priceSetID = $params['price_set_id'] ??
NULL;
442 $priceSetDetails = self
::getPriceSetDetails($params);
443 return (int) key($priceSetDetails);
445 return (int) $priceSetID;
449 * Store parameters relating to price sets.
451 * @param array $formValues
455 protected function setPriceSetParameters($formValues) {
456 $this->_priceSetId
= self
::getPriceSetID($formValues);
457 $priceSetDetails = self
::getPriceSetDetails($formValues);
458 $this->_priceSet
= $priceSetDetails[$this->_priceSetId
];
459 // process price set and get total amount and line items.
460 $this->ensurePriceParamsAreSet($formValues);
465 * Wrapper function for unit tests.
467 * @param array $formValues
469 public function testSubmit($formValues) {
470 $this->setContextVariables($formValues);
471 $this->_memType
= $formValues['membership_type_id'][1];
472 $this->_params
= $formValues;