3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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.
87 public function preProcess() {
88 // Check for edit permission.
89 if (!CRM_Core_Permission
::checkActionPermission('CiviMember', $this->_action
)) {
90 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
94 $params['context'] = CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'membership');
95 $params['id'] = CRM_Utils_Request
::retrieve('id', 'Positive', $this);
96 $params['mode'] = CRM_Utils_Request
::retrieve('mode', 'String', $this);
98 $this->setContextVariables($params);
100 $this->assign('context', $this->_context
);
101 $this->assign('membershipMode', $this->_mode
);
102 $this->allMembershipTypeDetails
= CRM_Member_BAO_Membership
::buildMembershipTypeValues($this, array(), TRUE);
103 foreach ($this->allMembershipTypeDetails
as $index => $membershipType) {
104 if ($membershipType['auto_renew']) {
105 $this->_recurMembershipTypes
[$index] = $membershipType;
106 $this->membershipTypeRenewalStatus
[$index] = $membershipType['auto_renew'];
112 * Set default values for the form. MobileProvider that in edit/view mode
113 * the default values are retrieved from the database
119 public function setDefaultValues() {
121 if (isset($this->_id
)) {
122 $params = array('id' => $this->_id
);
123 CRM_Member_BAO_Membership
::retrieve($params, $defaults);
124 if (isset($defaults['minimum_fee'])) {
125 $defaults['minimum_fee'] = CRM_Utils_Money
::format($defaults['minimum_fee'], NULL, '%a');
128 if (isset($defaults['status'])) {
129 $this->assign('membershipStatus', $defaults['status']);
133 if ($this->_action
& CRM_Core_Action
::ADD
) {
134 $defaults['is_active'] = 1;
137 if (isset($defaults['member_of_contact_id']) &&
138 $defaults['member_of_contact_id']
140 $defaults['member_org'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
141 $defaults['member_of_contact_id'], 'display_name'
148 * Build the form object.
150 public function buildQuickForm() {
153 $this->add('select', 'payment_processor_id',
154 ts('Payment Processor'),
155 $this->_processors
, TRUE,
156 array('onChange' => "buildAutoRenew( null, this.value );")
158 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, FALSE, TRUE);
160 // Build the form for auto renew. This is displayed when in credit card mode or update mode.
161 // The reason for showing it in update mode is not that clear.
162 if ($this->_mode ||
($this->_action
& CRM_Core_Action
::UPDATE
)) {
163 if (!empty($this->_recurPaymentProcessors
)) {
164 $this->assign('allowAutoRenew', TRUE);
167 $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
168 NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
170 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
171 $autoRenewElement->freeze();
174 $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors
));
175 $this->addElement('checkbox',
177 ts('Membership renewed automatically'),
179 array('onclick' => "buildReceiptANDNotice( );")
182 $this->assignPaymentRelatedVariables();
184 $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus
));
186 if ($this->_action
& CRM_Core_Action
::RENEW
) {
187 $this->addButtons(array(
190 'name' => ts('Renew'),
195 'name' => ts('Cancel'),
200 elseif ($this->_action
& CRM_Core_Action
::DELETE
) {
201 $this->addButtons(array(
204 'name' => ts('Delete'),
209 'name' => ts('Cancel'),
215 $this->addButtons(array(
218 'name' => ts('Save'),
223 'name' => ts('Save and New'),
228 'name' => ts('Cancel'),
236 * Extract values from the contact create boxes on the form and assign appropriately to
238 * - $this->_contributorEmail,
239 * - $this->_memberEmail &
240 * - $this->_contributionName
241 * - $this->_memberName
242 * - $this->_contactID (effectively memberContactId but changing might have spin-off effects)
243 * - $this->_contributorContactId - id of the contributor
244 * - $this->_receiptContactId
246 * If the member & contributor are the same then the values will be the same. But if different people paid
247 * then they weill differ
249 * @param array $formValues
250 * values from form. The important values we are looking for are.
252 * - soft_credit_contact_id
254 public function storeContactFields($formValues) {
255 // in a 'standalone form' (contact id not in the url) the contact will be in the form values
256 if (!empty($formValues['contact_id'])) {
257 $this->_contactID
= $formValues['contact_id'];
260 list($this->_memberDisplayName
,
262 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
264 //CRM-10375 Where the payer differs to the member the payer should get the email.
265 // here we store details in order to do that
266 if (!empty($formValues['soft_credit_contact_id'])) {
267 $this->_receiptContactId
= $this->_contributorContactID
= $formValues['soft_credit_contact_id'];
268 list($this->_contributorDisplayName
,
269 $this->_contributorEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contributorContactID
);
272 $this->_receiptContactId
= $this->_contributorContactID
= $this->_contactID
;
273 $this->_contributorDisplayName
= $this->_memberDisplayName
;
274 $this->_contributorEmail
= $this->_memberEmail
;
279 * Set variables in a way that can be accessed from different places.
281 * This is part of refactoring for unit testability on the submit function.
283 * @param array $params
285 protected function setContextVariables($params) {
287 'action' => '_action',
288 'context' => '_context',
290 'cid' => '_contactID',
293 foreach ($variables as $paramKey => $classVar) {
294 if (isset($params[$paramKey]) && !isset($this->$classVar)) {
295 $this->$classVar = $params[$paramKey];
300 $this->assignPaymentRelatedVariables();
304 $this->_memType
= CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_id
, 'membership_type_id');
305 $this->_membershipIDs
[] = $this->_id
;
307 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
311 * Create a recurring contribution record.
313 * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard
314 * to know the downstream impacts if we keep passing around the same array.
316 * @param $paymentParams
319 * @throws \CiviCRM_API3_Exception
321 protected function processRecurringContribution($paymentParams) {
322 $membershipID = $paymentParams['membership_type_id'][1];
323 $contributionRecurParams = array(
324 'contact_id' => $paymentParams['contactID'],
325 'amount' => $paymentParams['total_amount'],
326 'payment_processor_id' => $paymentParams['payment_processor_id'],
327 'campaign_id' => CRM_Utils_Array
::value('campaign_id', $paymentParams),
328 'financial_type_id' => $paymentParams['financial_type_id'],
329 'is_email_receipt' => CRM_Utils_Array
::value('is_email_receipt', $paymentParams),
330 // This is not great as it could also be direct debit - but is consistent with elsewhere & all need fixing.
331 'payment_instrument_id' => 1,
332 'invoice_id' => CRM_Utils_Array
::value('invoiceID ', $paymentParams),
336 'frequency_interval' => 'duration_interval',
337 'frequency_unit' => 'duration_unit',
339 $membershipType = civicrm_api3('MembershipType', 'getsingle', array(
340 'id' => $membershipID,
341 'return' => $mapping,
344 foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) {
345 $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
348 $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams);
349 $returnParams = array(
350 'contributionRecurID' => $contributionRecur['id'],
353 return $returnParams;
357 * Ensure price parameters are set.
359 * If they are not set it means a quick config option has been chosen so we
360 * fill them in here to make the two flows the same. They look like 'price_2' => 2 etc.
362 * @param array $formValues
364 protected function ensurePriceParamsAreSet(&$formValues) {
365 foreach ($formValues as $key => $value) {
366 if ((substr($key, 0, 6) == 'price_') && is_int(substr($key, 7))) {
370 $priceFields = CRM_Member_BAO_Membership
::setQuickConfigMembershipParameters(
371 $formValues['membership_type_id'][0],
372 $formValues['membership_type_id'][1],
373 $formValues['total_amount'],
376 $formValues = array_merge($formValues, $priceFields['price_fields']);
380 * Get the details for the selected price set.
382 * @param array $params
383 * Parameters submitted to the form.
387 protected static function getPriceSetDetails($params) {
388 $priceSetID = CRM_Utils_Array
::value('price_set_id', $params);
390 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSetID);
393 $priceSet = CRM_Price_BAO_PriceSet
::getDefaultPriceSet('membership');
394 $priceSet = reset($priceSet);
395 return CRM_Price_BAO_PriceSet
::getSetDetail($priceSet['setID']);
400 * Get the selected price set id.
402 * @param array $params
403 * Parameters submitted to the form.
407 protected static function getPriceSetID($params) {
408 $priceSetID = CRM_Utils_Array
::value('price_set_id', $params);
410 $priceSetDetails = self
::getPriceSetDetails($params);
411 return key($priceSetDetails);
417 * Store parameters relating to price sets.
419 * @param array $formValues
423 protected function setPriceSetParameters($formValues) {
424 $this->_priceSetId
= self
::getPriceSetID($formValues);
425 $priceSetDetails = self
::getPriceSetDetails($formValues);
426 $this->_priceSet
= $priceSetDetails[$this->_priceSetId
];
427 // process price set and get total amount and line items.
428 $this->ensurePriceParamsAreSet($formValues);