3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
35 * Base class for offline membership / membership type / membership renewal and membership status forms
38 class CRM_Member_Form
extends CRM_Contribute_Form_AbstractEditPayment
{
41 * The id of the object being edited / created
54 * Array of from email ids
57 protected $_fromEmails = array();
60 * Details of all enabled membership types.
64 protected $allMembershipTypeDetails = array();
67 * Array of membership type IDs and whether they permit autorenewal.
71 protected $membershipTypeRenewalStatus = array();
74 * Price set ID configured for the form.
81 * Price set details as an array.
88 * Values submitted to the form, processed along the way.
92 protected $_params = array();
94 public function preProcess() {
95 // Check for edit permission.
96 if (!CRM_Core_Permission
::checkActionPermission('CiviMember', $this->_action
)) {
97 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
99 if (!CRM_Member_BAO_Membership
::statusAvailabilty()) {
100 // all possible statuses are disabled - redirect back to contact form
101 CRM_Core_Error
::statusBounce(ts('There are no configured membership statuses. You cannot add this membership until your membership statuses are correctly configured'));
104 parent
::preProcess();
106 $params['context'] = CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'membership');
107 $params['id'] = CRM_Utils_Request
::retrieve('id', 'Positive', $this);
108 $params['mode'] = CRM_Utils_Request
::retrieve('mode', 'String', $this);
110 $this->setContextVariables($params);
112 $this->assign('context', $this->_context
);
113 $this->assign('membershipMode', $this->_mode
);
114 $this->allMembershipTypeDetails
= CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, array(), TRUE);
115 foreach ($this->allMembershipTypeDetails
as $index => $membershipType) {
116 if ($membershipType['auto_renew']) {
117 $this->_recurMembershipTypes
[$index] = $membershipType;
118 $this->membershipTypeRenewalStatus
[$index] = $membershipType['auto_renew'];
124 * Set default values for the form. MobileProvider that in edit/view mode
125 * the default values are retrieved from the database
131 public function setDefaultValues() {
133 if (isset($this->_id
)) {
134 $params = array('id' => $this->_id
);
135 CRM_Member_BAO_Membership
::retrieve($params, $defaults);
136 if (isset($defaults['minimum_fee'])) {
137 $defaults['minimum_fee'] = CRM_Utils_Money
::format($defaults['minimum_fee'], NULL, '%a');
140 if (isset($defaults['status'])) {
141 $this->assign('membershipStatus', $defaults['status']);
145 if ($this->_action
& CRM_Core_Action
::ADD
) {
146 $defaults['is_active'] = 1;
149 if (isset($defaults['member_of_contact_id']) &&
150 $defaults['member_of_contact_id']
152 $defaults['member_org'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
153 $defaults['member_of_contact_id'], 'display_name'
160 * Build the form object.
162 public function buildQuickForm() {
165 $this->add('select', 'payment_processor_id',
166 ts('Payment Processor'),
167 $this->_processors
, TRUE,
168 array('onChange' => "buildAutoRenew( null, this.value, '{$this->_mode}');")
170 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, FALSE, TRUE);
173 // Build the form for auto renew. This is displayed when in credit card mode or update mode.
174 // The reason for showing it in update mode is not that clear.
175 if ($this->_mode ||
($this->_action
& CRM_Core_Action
::UPDATE
)) {
176 if (!empty($this->_recurPaymentProcessors
)) {
177 $this->assign('allowAutoRenew', TRUE);
180 $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
181 NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
183 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
184 $autoRenewElement->freeze();
187 $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors
));
188 $this->addElement('checkbox',
190 ts('Membership renewed automatically')
194 $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus
));
196 if ($this->_action
& CRM_Core_Action
::RENEW
) {
197 $this->addButtons(array(
200 'name' => ts('Renew'),
205 'name' => ts('Cancel'),
209 elseif ($this->_action
& CRM_Core_Action
::DELETE
) {
210 $this->addButtons(array(
213 'name' => ts('Delete'),
218 'name' => ts('Cancel'),
223 $this->addButtons(array(
226 'name' => ts('Save'),
231 'name' => ts('Save and New'),
236 'name' => ts('Cancel'),
243 * Extract values from the contact create boxes on the form and assign appropriately to
245 * - $this->_contributorEmail,
246 * - $this->_memberEmail &
247 * - $this->_contributionName
248 * - $this->_memberName
249 * - $this->_contactID (effectively memberContactId but changing might have spin-off effects)
250 * - $this->_contributorContactId - id of the contributor
251 * - $this->_receiptContactId
253 * If the member & contributor are the same then the values will be the same. But if different people paid
254 * then they weill differ
256 * @param array $formValues
257 * values from form. The important values we are looking for are.
259 * - soft_credit_contact_id
261 public function storeContactFields($formValues) {
262 // in a 'standalone form' (contact id not in the url) the contact will be in the form values
263 if (!empty($formValues['contact_id'])) {
264 $this->_contactID
= $formValues['contact_id'];
267 list($this->_memberDisplayName
,
269 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
271 //CRM-10375 Where the payer differs to the member the payer should get the email.
272 // here we store details in order to do that
273 if (!empty($formValues['soft_credit_contact_id'])) {
274 $this->_receiptContactId
= $this->_contributorContactID
= $formValues['soft_credit_contact_id'];
275 list($this->_contributorDisplayName
,
276 $this->_contributorEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contributorContactID
);
279 $this->_receiptContactId
= $this->_contributorContactID
= $this->_contactID
;
280 $this->_contributorDisplayName
= $this->_memberDisplayName
;
281 $this->_contributorEmail
= $this->_memberEmail
;
286 * Set variables in a way that can be accessed from different places.
288 * This is part of refactoring for unit testability on the submit function.
290 * @param array $params
292 protected function setContextVariables($params) {
294 'action' => '_action',
295 'context' => '_context',
297 'cid' => '_contactID',
300 foreach ($variables as $paramKey => $classVar) {
301 if (isset($params[$paramKey]) && !isset($this->$classVar)) {
302 $this->$classVar = $params[$paramKey];
306 $this->assignPaymentRelatedVariables();
309 $this->_memType
= CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_id
, 'membership_type_id');
310 $this->_membershipIDs
[] = $this->_id
;
312 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
316 * Create a recurring contribution record.
318 * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard
319 * to know the downstream impacts if we keep passing around the same array.
321 * @param $paymentParams
324 * @throws \CiviCRM_API3_Exception
326 protected function processRecurringContribution($paymentParams) {
327 $membershipID = $paymentParams['membership_type_id'][1];
328 $contributionRecurParams = array(
329 'contact_id' => $paymentParams['contactID'],
330 'amount' => $paymentParams['total_amount'],
331 'contribution_status_id' => 'Pending',
332 'payment_processor_id' => $paymentParams['payment_processor_id'],
333 'campaign_id' => $paymentParams['campaign_id'],
334 'financial_type_id' => $paymentParams['financial_type_id'],
335 'is_email_receipt' => $paymentParams['is_email_receipt'],
336 'payment_instrument_id' => $paymentParams['payment_instrument_id'],
337 'invoice_id' => $paymentParams['invoice_id'],
341 'frequency_interval' => 'duration_interval',
342 'frequency_unit' => 'duration_unit',
344 $membershipType = civicrm_api3('MembershipType', 'getsingle', array(
345 'id' => $membershipID,
346 'return' => $mapping,
349 $returnParams = array('is_recur' => TRUE);
350 foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) {
351 $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
352 $returnParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
355 $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams);
356 $returnParams['contributionRecurID'] = $contributionRecur['id'];
357 return $returnParams;
361 * Ensure price parameters are set.
363 * If they are not set it means a quick config option has been chosen so we
364 * fill them in here to make the two flows the same. They look like 'price_2' => 2 etc.
366 * @param array $formValues
368 protected function ensurePriceParamsAreSet(&$formValues) {
369 foreach ($formValues as $key => $value) {
370 if ((substr($key, 0, 6) == 'price_') && is_int(substr($key, 7))) {
374 $priceFields = CRM_Member_BAO_Membership
::setQuickConfigMembershipParameters(
375 $formValues['membership_type_id'][0],
376 $formValues['membership_type_id'][1],
377 $formValues['total_amount'],
380 $formValues = array_merge($formValues, $priceFields['price_fields']);
384 * Get the details for the selected price set.
386 * @param array $params
387 * Parameters submitted to the form.
391 protected static function getPriceSetDetails($params) {
392 $priceSetID = CRM_Utils_Array
::value('price_set_id', $params);
394 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSetID);
397 $priceSet = CRM_Price_BAO_PriceSet
::getDefaultPriceSet('membership');
398 $priceSet = reset($priceSet);
399 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSet['setID']);
404 * Get the selected price set id.
406 * @param array $params
407 * Parameters submitted to the form.
411 protected static function getPriceSetID($params) {
412 $priceSetID = CRM_Utils_Array
::value('price_set_id', $params);
414 $priceSetDetails = self
::getPriceSetDetails($params);
415 return key($priceSetDetails);
421 * Store parameters relating to price sets.
423 * @param array $formValues
427 protected function setPriceSetParameters($formValues) {
428 $this->_priceSetId
= self
::getPriceSetID($formValues);
429 $priceSetDetails = self
::getPriceSetDetails($formValues);
430 $this->_priceSet
= $priceSetDetails[$this->_priceSetId
];
431 // process price set and get total amount and line items.
432 $this->ensurePriceParamsAreSet($formValues);
437 * Wrapper function for unit tests.
439 * @param array $formValues
441 public function testSubmit($formValues) {
442 $this->setContextVariables($formValues);
443 $this->_memType
= $formValues['membership_type_id'][1];
444 $this->_params
= $formValues;