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
;
26 * The id of the object being edited / created
39 * Array of from email ids
42 protected $_fromEmails = [];
45 * Details of all enabled membership types.
49 protected $allMembershipTypeDetails = [];
52 * Array of membership type IDs and whether they permit autorenewal.
56 protected $membershipTypeRenewalStatus = [];
59 * Price set ID configured for the form.
66 * Price set details as an array.
73 * Explicitly declare the entity api name.
75 public function getDefaultEntity() {
82 protected $statusMessage = [];
85 * Add to the status message.
89 protected function addStatusMessage($message) {
90 $this->statusMessage
[] = $message;
94 * Get the status message.
98 protected function getStatusMessage() {
99 return implode(' ', $this->statusMessage
);
103 * Values submitted to the form, processed along the way.
107 protected $_params = [];
110 * Fields for the entity to be assigned to the template.
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
121 * - is_freeze (field should be frozen).
125 protected $entityFields = [];
127 public function preProcess() {
128 // Check for edit permission.
129 if (!CRM_Core_Permission
::checkActionPermission('CiviMember', $this->_action
)) {
130 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
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'));
137 parent
::preProcess();
139 $params['context'] = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this, FALSE, 'membership');
140 $params['id'] = CRM_Utils_Request
::retrieve('id', 'Positive', $this);
141 $params['mode'] = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
143 $this->setContextVariables($params);
145 $this->assign('context', $this->_context
);
146 $this->assign('membershipMode', $this->_mode
);
147 $this->allMembershipTypeDetails
= CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, [], TRUE);
148 foreach ($this->allMembershipTypeDetails
as $index => $membershipType) {
149 if ($membershipType['auto_renew']) {
150 $this->_recurMembershipTypes
[$index] = $membershipType;
151 $this->membershipTypeRenewalStatus
[$index] = $membershipType['auto_renew'];
157 * Set default values for the form. MobileProvider that in edit/view mode
158 * the default values are retrieved from the database
164 public function setDefaultValues() {
166 if (isset($this->_id
)) {
167 $params = ['id' => $this->_id
];
168 CRM_Member_BAO_Membership
::retrieve($params, $defaults);
169 if (isset($defaults['minimum_fee'])) {
170 $defaults['minimum_fee'] = CRM_Utils_Money
::format($defaults['minimum_fee'], NULL, '%a');
173 if (isset($defaults['status'])) {
174 $this->assign('membershipStatus', $defaults['status']);
177 if (!empty($defaults['is_override'])) {
178 $defaults['is_override'] = CRM_Member_StatusOverrideTypes
::PERMANENT
;
180 if (!empty($defaults['status_override_end_date'])) {
181 $defaults['is_override'] = CRM_Member_StatusOverrideTypes
::UNTIL_DATE
;
185 if ($this->_action
& CRM_Core_Action
::ADD
) {
186 $defaults['is_active'] = 1;
189 if (isset($defaults['member_of_contact_id']) &&
190 $defaults['member_of_contact_id']
192 $defaults['member_org'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
193 $defaults['member_of_contact_id'], 'display_name'
196 if (!empty($defaults['membership_type_id'])) {
197 $this->_memType
= $defaults['membership_type_id'];
199 if (is_numeric($this->_memType
)) {
200 $defaults['membership_type_id'] = [];
201 $defaults['membership_type_id'][0] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
203 'member_of_contact_id',
206 $defaults['membership_type_id'][1] = $this->_memType
;
209 $defaults['membership_type_id'] = $this->_memType
;
215 * Build the form object.
217 public function buildQuickForm() {
218 $this->assignSalesTaxMetadataToTemplate();
220 $this->addPaymentProcessorSelect(TRUE, FALSE, TRUE);
221 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, FALSE, TRUE, $this->getDefaultPaymentInstrumentId());
222 $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors
));
223 // Build the form for auto renew. This is displayed when in credit card mode or update mode.
224 // The reason for showing it in update mode is not that clear.
225 if ($this->_mode ||
($this->_action
& CRM_Core_Action
::UPDATE
)) {
226 if (!empty($this->_recurPaymentProcessors
)) {
227 $this->assign('allowAutoRenew', TRUE);
230 $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
231 NULL, ['onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );"]
233 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
234 $autoRenewElement->freeze();
237 $this->addElement('checkbox',
239 ts('Membership renewed automatically')
243 $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus
));
245 if ($this->_action
& CRM_Core_Action
::RENEW
) {
249 'name' => ts('Renew'),
254 'name' => ts('Cancel'),
258 elseif ($this->_action
& CRM_Core_Action
::DELETE
) {
262 'name' => ts('Delete'),
267 'name' => ts('Cancel'),
275 'name' => ts('Save'),
280 'name' => ts('Save and New'),
285 'name' => ts('Cancel'),
292 * Extract values from the contact create boxes on the form and assign appropriately to
294 * - $this->_contributorEmail,
295 * - $this->_memberEmail &
296 * - $this->_contributionName
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
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
305 * @param array $formValues
306 * values from form. The important values we are looking for are.
308 * - soft_credit_contact_id
310 public function storeContactFields($formValues) {
311 // in a 'standalone form' (contact id not in the url) the contact will be in the form values
312 if (!empty($formValues['contact_id'])) {
313 $this->_contactID
= $formValues['contact_id'];
316 list($this->_memberDisplayName
,
318 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
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
322 if (!empty($formValues['soft_credit_contact_id'])) {
323 $this->_receiptContactId
= $this->_contributorContactID
= $formValues['soft_credit_contact_id'];
324 list($this->_contributorDisplayName
,
325 $this->_contributorEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contributorContactID
);
328 $this->_receiptContactId
= $this->_contributorContactID
= $this->_contactID
;
329 $this->_contributorDisplayName
= $this->_memberDisplayName
;
330 $this->_contributorEmail
= $this->_memberEmail
;
335 * Set variables in a way that can be accessed from different places.
337 * This is part of refactoring for unit testability on the submit function.
339 * @param array $params
341 protected function setContextVariables($params) {
343 'action' => '_action',
344 'context' => '_context',
346 'cid' => '_contactID',
349 foreach ($variables as $paramKey => $classVar) {
350 if (isset($params[$paramKey]) && !isset($this->$classVar)) {
351 $this->$classVar = $params[$paramKey];
356 $this->_memType
= CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_id
, 'membership_type_id');
357 $this->_membershipIDs
[] = $this->_id
;
359 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
363 * Create a recurring contribution record.
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.
368 * @param $paymentParams
371 * @throws \CiviCRM_API3_Exception
373 protected function processRecurringContribution($paymentParams) {
374 $membershipID = $paymentParams['membership_type_id'][1];
375 $contributionRecurParams = [
376 'contact_id' => $paymentParams['contactID'],
377 'amount' => $paymentParams['total_amount'],
378 'contribution_status_id' => 'Pending',
379 'payment_processor_id' => $paymentParams['payment_processor_id'],
380 'campaign_id' => $paymentParams['campaign_id'],
381 'financial_type_id' => $paymentParams['financial_type_id'],
382 'is_email_receipt' => $paymentParams['is_email_receipt'],
383 'payment_instrument_id' => $paymentParams['payment_instrument_id'],
384 'invoice_id' => $paymentParams['invoice_id'],
388 'frequency_interval' => 'duration_interval',
389 'frequency_unit' => 'duration_unit',
391 $membershipType = civicrm_api3('MembershipType', 'getsingle', [
392 'id' => $membershipID,
393 'return' => $mapping,
396 $returnParams = ['is_recur' => TRUE];
397 foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) {
398 $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
399 $returnParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
402 $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams);
403 $returnParams['contributionRecurID'] = $contributionRecur['id'];
404 return $returnParams;
408 * Ensure price parameters are set.
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.
413 * @param array $formValues
415 protected function ensurePriceParamsAreSet(&$formValues) {
416 foreach ($formValues as $key => $value) {
417 if ((substr($key, 0, 6) == 'price_') && is_numeric(substr($key, 6))) {
421 $priceFields = CRM_Member_BAO_Membership
::setQuickConfigMembershipParameters(
422 $formValues['membership_type_id'][0],
423 $formValues['membership_type_id'][1],
424 CRM_Utils_Array
::value('total_amount', $formValues),
427 $formValues = array_merge($formValues, $priceFields['price_fields']);
431 * Get the details for the selected price set.
433 * @param array $params
434 * Parameters submitted to the form.
438 protected static function getPriceSetDetails($params) {
439 $priceSetID = CRM_Utils_Array
::value('price_set_id', $params);
441 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSetID);
444 $priceSet = CRM_Price_BAO_PriceSet
::getDefaultPriceSet('membership');
445 $priceSet = reset($priceSet);
446 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSet['setID']);
451 * Get the selected price set id.
453 * @param array $params
454 * Parameters submitted to the form.
458 protected static function getPriceSetID($params) {
459 $priceSetID = CRM_Utils_Array
::value('price_set_id', $params);
461 $priceSetDetails = self
::getPriceSetDetails($params);
462 return key($priceSetDetails);
468 * Store parameters relating to price sets.
470 * @param array $formValues
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);
484 * Wrapper function for unit tests.
486 * @param array $formValues
488 public function testSubmit($formValues) {
489 $this->setContextVariables($formValues);
490 $this->_memType
= $formValues['membership_type_id'][1];
491 $this->_params
= $formValues;