3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Base class for offline membership / membership type / membership renewal and membership status forms
38 class CRM_Member_Form
extends CRM_Contribute_Form_AbstractEditPayment
{
40 use CRM_Core_Form_EntityFormTrait
;
42 * The id of the object being edited / created
55 * Array of from email ids
58 protected $_fromEmails = array();
61 * Details of all enabled membership types.
65 protected $allMembershipTypeDetails = array();
68 * Array of membership type IDs and whether they permit autorenewal.
72 protected $membershipTypeRenewalStatus = array();
75 * Price set ID configured for the form.
82 * Price set details as an array.
89 * Explicitly declare the entity api name.
91 public function getDefaultEntity() {
98 protected $statusMessage = [];
101 * Add to the status message.
105 protected function addStatusMessage($message) {
106 $this->statusMessage
[] = $message;
110 * Get the status message.
114 protected function getStatusMessage() {
115 return implode(' ', $this->statusMessage
);
119 * Values submitted to the form, processed along the way.
123 protected $_params = array();
126 * Fields for the entity to be assigned to the template.
128 * Fields may have keys
129 * - name (required to show in tpl from the array)
130 * - description (optional, will appear below the field)
131 * - not-auto-addable - this class will not attempt to add the field using addField.
132 * (this will be automatically set if the field does not have html in it's metadata
133 * or is not a core field on the form's entity).
134 * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
135 * - template - use a field specific template to render this field
137 * - is_freeze (field should be frozen).
141 protected $entityFields = [];
143 public function preProcess() {
144 // Check for edit permission.
145 if (!CRM_Core_Permission
::checkActionPermission('CiviMember', $this->_action
)) {
146 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
148 if (!CRM_Member_BAO_Membership
::statusAvailabilty()) {
149 // all possible statuses are disabled - redirect back to contact form
150 CRM_Core_Error
::statusBounce(ts('There are no configured membership statuses. You cannot add this membership until your membership statuses are correctly configured'));
153 parent
::preProcess();
155 $params['context'] = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this, FALSE, 'membership');
156 $params['id'] = CRM_Utils_Request
::retrieve('id', 'Positive', $this);
157 $params['mode'] = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
159 $this->setContextVariables($params);
161 $this->assign('context', $this->_context
);
162 $this->assign('membershipMode', $this->_mode
);
163 $this->allMembershipTypeDetails
= CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, array(), TRUE);
164 foreach ($this->allMembershipTypeDetails
as $index => $membershipType) {
165 if ($membershipType['auto_renew']) {
166 $this->_recurMembershipTypes
[$index] = $membershipType;
167 $this->membershipTypeRenewalStatus
[$index] = $membershipType['auto_renew'];
173 * Set default values for the form. MobileProvider that in edit/view mode
174 * the default values are retrieved from the database
180 public function setDefaultValues() {
182 if (isset($this->_id
)) {
183 $params = array('id' => $this->_id
);
184 CRM_Member_BAO_Membership
::retrieve($params, $defaults);
185 if (isset($defaults['minimum_fee'])) {
186 $defaults['minimum_fee'] = CRM_Utils_Money
::format($defaults['minimum_fee'], NULL, '%a');
189 if (isset($defaults['status'])) {
190 $this->assign('membershipStatus', $defaults['status']);
193 if (!empty($defaults['is_override'])) {
194 $defaults['is_override'] = CRM_Member_StatusOverrideTypes
::PERMANENT
;
196 if (!empty($defaults['status_override_end_date'])) {
197 $defaults['is_override'] = CRM_Member_StatusOverrideTypes
::UNTIL_DATE
;
201 if ($this->_action
& CRM_Core_Action
::ADD
) {
202 $defaults['is_active'] = 1;
205 if (isset($defaults['member_of_contact_id']) &&
206 $defaults['member_of_contact_id']
208 $defaults['member_org'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
209 $defaults['member_of_contact_id'], 'display_name'
212 if (!empty($defaults['membership_type_id'])) {
213 $this->_memType
= $defaults['membership_type_id'];
215 if (is_numeric($this->_memType
)) {
216 $defaults['membership_type_id'] = array();
217 $defaults['membership_type_id'][0] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
219 'member_of_contact_id',
222 $defaults['membership_type_id'][1] = $this->_memType
;
225 $defaults['membership_type_id'] = $this->_memType
;
231 * Build the form object.
233 public function buildQuickForm() {
234 $this->assignSalesTaxMetadataToTemplate();
236 $this->addPaymentProcessorSelect(TRUE, FALSE, TRUE);
237 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, FALSE, TRUE, $this->getDefaultPaymentInstrumentId());
238 // Build the form for auto renew. This is displayed when in credit card mode or update mode.
239 // The reason for showing it in update mode is not that clear.
240 if ($this->_mode ||
($this->_action
& CRM_Core_Action
::UPDATE
)) {
241 if (!empty($this->_recurPaymentProcessors
)) {
242 $this->assign('allowAutoRenew', TRUE);
245 $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
246 NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
248 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
249 $autoRenewElement->freeze();
252 $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors
));
253 $this->addElement('checkbox',
255 ts('Membership renewed automatically')
259 $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus
));
261 if ($this->_action
& CRM_Core_Action
::RENEW
) {
262 $this->addButtons(array(
265 'name' => ts('Renew'),
270 'name' => ts('Cancel'),
274 elseif ($this->_action
& CRM_Core_Action
::DELETE
) {
275 $this->addButtons(array(
278 'name' => ts('Delete'),
283 'name' => ts('Cancel'),
288 $this->addButtons(array(
291 'name' => ts('Save'),
296 'name' => ts('Save and New'),
301 'name' => ts('Cancel'),
308 * Extract values from the contact create boxes on the form and assign appropriately to
310 * - $this->_contributorEmail,
311 * - $this->_memberEmail &
312 * - $this->_contributionName
313 * - $this->_memberName
314 * - $this->_contactID (effectively memberContactId but changing might have spin-off effects)
315 * - $this->_contributorContactId - id of the contributor
316 * - $this->_receiptContactId
318 * If the member & contributor are the same then the values will be the same. But if different people paid
319 * then they weill differ
321 * @param array $formValues
322 * values from form. The important values we are looking for are.
324 * - soft_credit_contact_id
326 public function storeContactFields($formValues) {
327 // in a 'standalone form' (contact id not in the url) the contact will be in the form values
328 if (!empty($formValues['contact_id'])) {
329 $this->_contactID
= $formValues['contact_id'];
332 list($this->_memberDisplayName
,
334 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
336 //CRM-10375 Where the payer differs to the member the payer should get the email.
337 // here we store details in order to do that
338 if (!empty($formValues['soft_credit_contact_id'])) {
339 $this->_receiptContactId
= $this->_contributorContactID
= $formValues['soft_credit_contact_id'];
340 list($this->_contributorDisplayName
,
341 $this->_contributorEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contributorContactID
);
344 $this->_receiptContactId
= $this->_contributorContactID
= $this->_contactID
;
345 $this->_contributorDisplayName
= $this->_memberDisplayName
;
346 $this->_contributorEmail
= $this->_memberEmail
;
351 * Set variables in a way that can be accessed from different places.
353 * This is part of refactoring for unit testability on the submit function.
355 * @param array $params
357 protected function setContextVariables($params) {
359 'action' => '_action',
360 'context' => '_context',
362 'cid' => '_contactID',
365 foreach ($variables as $paramKey => $classVar) {
366 if (isset($params[$paramKey]) && !isset($this->$classVar)) {
367 $this->$classVar = $params[$paramKey];
372 $this->_memType
= CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_id
, 'membership_type_id');
373 $this->_membershipIDs
[] = $this->_id
;
375 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
379 * Create a recurring contribution record.
381 * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard
382 * to know the downstream impacts if we keep passing around the same array.
384 * @param $paymentParams
387 * @throws \CiviCRM_API3_Exception
389 protected function processRecurringContribution($paymentParams) {
390 $membershipID = $paymentParams['membership_type_id'][1];
391 $contributionRecurParams = array(
392 'contact_id' => $paymentParams['contactID'],
393 'amount' => $paymentParams['total_amount'],
394 'contribution_status_id' => 'Pending',
395 'payment_processor_id' => $paymentParams['payment_processor_id'],
396 'campaign_id' => $paymentParams['campaign_id'],
397 'financial_type_id' => $paymentParams['financial_type_id'],
398 'is_email_receipt' => $paymentParams['is_email_receipt'],
399 'payment_instrument_id' => $paymentParams['payment_instrument_id'],
400 'invoice_id' => $paymentParams['invoice_id'],
404 'frequency_interval' => 'duration_interval',
405 'frequency_unit' => 'duration_unit',
407 $membershipType = civicrm_api3('MembershipType', 'getsingle', array(
408 'id' => $membershipID,
409 'return' => $mapping,
412 $returnParams = array('is_recur' => TRUE);
413 foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) {
414 $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
415 $returnParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
418 $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams);
419 $returnParams['contributionRecurID'] = $contributionRecur['id'];
420 return $returnParams;
424 * Ensure price parameters are set.
426 * If they are not set it means a quick config option has been chosen so we
427 * fill them in here to make the two flows the same. They look like 'price_2' => 2 etc.
429 * @param array $formValues
431 protected function ensurePriceParamsAreSet(&$formValues) {
432 foreach ($formValues as $key => $value) {
433 if ((substr($key, 0, 6) == 'price_') && is_numeric(substr($key, 6))) {
437 $priceFields = CRM_Member_BAO_Membership
::setQuickConfigMembershipParameters(
438 $formValues['membership_type_id'][0],
439 $formValues['membership_type_id'][1],
440 CRM_Utils_Array
::value('total_amount', $formValues),
443 $formValues = array_merge($formValues, $priceFields['price_fields']);
447 * Get the details for the selected price set.
449 * @param array $params
450 * Parameters submitted to the form.
454 protected static function getPriceSetDetails($params) {
455 $priceSetID = CRM_Utils_Array
::value('price_set_id', $params);
457 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSetID);
460 $priceSet = CRM_Price_BAO_PriceSet
::getDefaultPriceSet('membership');
461 $priceSet = reset($priceSet);
462 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSet['setID']);
467 * Get the selected price set id.
469 * @param array $params
470 * Parameters submitted to the form.
474 protected static function getPriceSetID($params) {
475 $priceSetID = CRM_Utils_Array
::value('price_set_id', $params);
477 $priceSetDetails = self
::getPriceSetDetails($params);
478 return key($priceSetDetails);
484 * Store parameters relating to price sets.
486 * @param array $formValues
490 protected function setPriceSetParameters($formValues) {
491 $this->_priceSetId
= self
::getPriceSetID($formValues);
492 $priceSetDetails = self
::getPriceSetDetails($formValues);
493 $this->_priceSet
= $priceSetDetails[$this->_priceSetId
];
494 // process price set and get total amount and line items.
495 $this->ensurePriceParamsAreSet($formValues);
500 * Wrapper function for unit tests.
502 * @param array $formValues
504 public function testSubmit($formValues) {
505 $this->setContextVariables($formValues);
506 $this->_memType
= $formValues['membership_type_id'][1];
507 $this->_params
= $formValues;