Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * Base class for offline membership / membership type / membership renewal and membership status forms | |
36 | * | |
37 | */ | |
cc984198 | 38 | class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { |
6a488035 TO |
39 | |
40 | /** | |
41 | * The id of the object being edited / created | |
42 | * | |
43 | * @var int | |
44 | */ | |
cc984198 | 45 | public $_id; |
6a488035 | 46 | |
a6513ad5 EM |
47 | /** |
48 | * Membership Type ID | |
49 | * @var | |
50 | */ | |
51 | protected $_memType; | |
52 | ||
53 | /** | |
54 | * Array of from email ids | |
55 | * @var array | |
56 | */ | |
57 | protected $_fromEmails = array(); | |
58 | ||
ab30e033 EM |
59 | /** |
60 | * Details of all enabled membership types. | |
61 | * | |
62 | * @var array | |
63 | */ | |
64 | protected $allMembershipTypeDetails = array(); | |
65 | ||
66 | /** | |
67 | * Array of membership type IDs and whether they permit autorenewal. | |
68 | * | |
69 | * @var array | |
70 | */ | |
71 | protected $membershipTypeRenewalStatus = array(); | |
72 | ||
e4a6290d | 73 | /** |
74 | * Price set ID configured for the form. | |
75 | * | |
76 | * @var int | |
77 | */ | |
78 | public $_priceSetId; | |
79 | ||
80 | /** | |
81 | * Price set details as an array. | |
82 | * | |
83 | * @var array | |
84 | */ | |
85 | public $_priceSet; | |
86 | ||
6452294d MW |
87 | /** |
88 | * Explicitly declare the entity api name. | |
89 | */ | |
90 | public function getDefaultEntity() { | |
91 | return 'Membership'; | |
92 | } | |
93 | ||
09108d7d | 94 | /** |
95 | * Values submitted to the form, processed along the way. | |
96 | * | |
97 | * @var array | |
98 | */ | |
99 | protected $_params = array(); | |
100 | ||
00be9182 | 101 | public function preProcess() { |
42e8b05c EM |
102 | // Check for edit permission. |
103 | if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) { | |
104 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); | |
105 | } | |
6a38708b | 106 | if (!CRM_Member_BAO_Membership::statusAvailabilty()) { |
107 | // all possible statuses are disabled - redirect back to contact form | |
108 | CRM_Core_Error::statusBounce(ts('There are no configured membership statuses. You cannot add this membership until your membership statuses are correctly configured')); | |
109 | } | |
110 | ||
42e8b05c | 111 | parent::preProcess(); |
7865d848 | 112 | $params = array(); |
edc80cda | 113 | $params['context'] = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'membership'); |
7865d848 | 114 | $params['id'] = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
19046166 | 115 | $params['mode'] = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this); |
7865d848 EM |
116 | |
117 | $this->setContextVariables($params); | |
a6513ad5 EM |
118 | |
119 | $this->assign('context', $this->_context); | |
120 | $this->assign('membershipMode', $this->_mode); | |
1a00ea3c | 121 | $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, array(), TRUE); |
ab30e033 EM |
122 | foreach ($this->allMembershipTypeDetails as $index => $membershipType) { |
123 | if ($membershipType['auto_renew']) { | |
124 | $this->_recurMembershipTypes[$index] = $membershipType; | |
125 | $this->membershipTypeRenewalStatus[$index] = $membershipType['auto_renew']; | |
126 | } | |
127 | } | |
6a488035 TO |
128 | } |
129 | ||
130 | /** | |
c490a46a | 131 | * Set default values for the form. MobileProvider that in edit/view mode |
6a488035 TO |
132 | * the default values are retrieved from the database |
133 | * | |
6a488035 | 134 | * |
a6c01b45 CW |
135 | * @return array |
136 | * defaults | |
6a488035 | 137 | */ |
00be9182 | 138 | public function setDefaultValues() { |
6a488035 | 139 | $defaults = array(); |
a6513ad5 EM |
140 | if (isset($this->_id)) { |
141 | $params = array('id' => $this->_id); | |
142 | CRM_Member_BAO_Membership::retrieve($params, $defaults); | |
42e8b05c EM |
143 | if (isset($defaults['minimum_fee'])) { |
144 | $defaults['minimum_fee'] = CRM_Utils_Money::format($defaults['minimum_fee'], NULL, '%a'); | |
145 | } | |
6a488035 | 146 | |
42e8b05c EM |
147 | if (isset($defaults['status'])) { |
148 | $this->assign('membershipStatus', $defaults['status']); | |
149 | } | |
e136f704 O |
150 | |
151 | if (!empty($defaults['is_override'])) { | |
152 | $defaults['is_override'] = CRM_Member_StatusOverrideTypes::PERMANENT; | |
153 | } | |
154 | if (!empty($defaults['status_override_end_date'])) { | |
155 | $defaults['is_override'] = CRM_Member_StatusOverrideTypes::UNTIL_DATE; | |
156 | } | |
6a488035 TO |
157 | } |
158 | ||
159 | if ($this->_action & CRM_Core_Action::ADD) { | |
160 | $defaults['is_active'] = 1; | |
161 | } | |
162 | ||
163 | if (isset($defaults['member_of_contact_id']) && | |
164 | $defaults['member_of_contact_id'] | |
165 | ) { | |
166 | $defaults['member_org'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
167 | $defaults['member_of_contact_id'], 'display_name' | |
168 | ); | |
169 | } | |
f525ec1f | 170 | if (!empty($defaults['membership_type_id'])) { |
171 | $this->_memType = $defaults['membership_type_id']; | |
172 | } | |
173 | if (is_numeric($this->_memType)) { | |
174 | $defaults['membership_type_id'] = array(); | |
175 | $defaults['membership_type_id'][0] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
176 | $this->_memType, | |
177 | 'member_of_contact_id', | |
178 | 'id' | |
179 | ); | |
180 | $defaults['membership_type_id'][1] = $this->_memType; | |
181 | } | |
182 | else { | |
183 | $defaults['membership_type_id'] = $this->_memType; | |
184 | } | |
6a488035 TO |
185 | return $defaults; |
186 | } | |
187 | ||
188 | /** | |
fe482240 | 189 | * Build the form object. |
6a488035 TO |
190 | */ |
191 | public function buildQuickForm() { | |
a6e29c95 | 192 | $this->assignSalesTaxMetadataToTemplate(); |
4aa7d844 | 193 | |
b15c60e1 | 194 | $this->addPaymentProcessorSelect(TRUE, FALSE, TRUE); |
0dc4ef42 | 195 | CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE, $this->getDefaultPaymentInstrumentId()); |
4aa7d844 EM |
196 | // Build the form for auto renew. This is displayed when in credit card mode or update mode. |
197 | // The reason for showing it in update mode is not that clear. | |
4aa7d844 | 198 | if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) { |
ab30e033 EM |
199 | if (!empty($this->_recurPaymentProcessors)) { |
200 | $this->assign('allowAutoRenew', TRUE); | |
4aa7d844 EM |
201 | } |
202 | ||
ab30e033 EM |
203 | $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'), |
204 | NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );") | |
205 | ); | |
206 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
207 | $autoRenewElement->freeze(); | |
208 | } | |
4aa7d844 | 209 | |
ab30e033 | 210 | $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors)); |
4aa7d844 EM |
211 | $this->addElement('checkbox', |
212 | 'auto_renew', | |
8c80f3f9 | 213 | ts('Membership renewed automatically') |
4aa7d844 EM |
214 | ); |
215 | ||
4aa7d844 | 216 | } |
ab30e033 | 217 | $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus)); |
4aa7d844 | 218 | |
6a488035 TO |
219 | if ($this->_action & CRM_Core_Action::RENEW) { |
220 | $this->addButtons(array( | |
c5c263ca AH |
221 | array( |
222 | 'type' => 'upload', | |
223 | 'name' => ts('Renew'), | |
224 | 'isDefault' => TRUE, | |
225 | ), | |
226 | array( | |
227 | 'type' => 'cancel', | |
228 | 'name' => ts('Cancel'), | |
229 | ), | |
230 | )); | |
6a488035 TO |
231 | } |
232 | elseif ($this->_action & CRM_Core_Action::DELETE) { | |
233 | $this->addButtons(array( | |
c5c263ca AH |
234 | array( |
235 | 'type' => 'next', | |
236 | 'name' => ts('Delete'), | |
237 | 'isDefault' => TRUE, | |
238 | ), | |
239 | array( | |
240 | 'type' => 'cancel', | |
241 | 'name' => ts('Cancel'), | |
242 | ), | |
243 | )); | |
6a488035 TO |
244 | } |
245 | else { | |
246 | $this->addButtons(array( | |
c5c263ca AH |
247 | array( |
248 | 'type' => 'upload', | |
249 | 'name' => ts('Save'), | |
250 | 'isDefault' => TRUE, | |
251 | ), | |
252 | array( | |
253 | 'type' => 'upload', | |
254 | 'name' => ts('Save and New'), | |
255 | 'subName' => 'new', | |
256 | ), | |
257 | array( | |
258 | 'type' => 'cancel', | |
259 | 'name' => ts('Cancel'), | |
260 | ), | |
261 | )); | |
6a488035 TO |
262 | } |
263 | } | |
264 | ||
fb3082b2 | 265 | /** |
100fef9d | 266 | * Extract values from the contact create boxes on the form and assign appropriately to |
6a488035 TO |
267 | * |
268 | * - $this->_contributorEmail, | |
269 | * - $this->_memberEmail & | |
fb3082b2 | 270 | * - $this->_contributionName |
6a488035 TO |
271 | * - $this->_memberName |
272 | * - $this->_contactID (effectively memberContactId but changing might have spin-off effects) | |
273 | * - $this->_contributorContactId - id of the contributor | |
274 | * - $this->_receiptContactId | |
275 | * | |
276 | * If the member & contributor are the same then the values will be the same. But if different people paid | |
277 | * then they weill differ | |
278 | * | |
5a4f6742 CW |
279 | * @param array $formValues |
280 | * values from form. The important values we are looking for are. | |
4c7aa1f7 CW |
281 | * - contact_id |
282 | * - soft_credit_contact_id | |
6a488035 | 283 | */ |
9b873358 | 284 | public function storeContactFields($formValues) { |
6a488035 | 285 | // in a 'standalone form' (contact id not in the url) the contact will be in the form values |
4c7aa1f7 CW |
286 | if (!empty($formValues['contact_id'])) { |
287 | $this->_contactID = $formValues['contact_id']; | |
6a488035 TO |
288 | } |
289 | ||
290 | list($this->_memberDisplayName, | |
353ffa53 TO |
291 | $this->_memberEmail |
292 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
6a488035 TO |
293 | |
294 | //CRM-10375 Where the payer differs to the member the payer should get the email. | |
295 | // here we store details in order to do that | |
4c7aa1f7 CW |
296 | if (!empty($formValues['soft_credit_contact_id'])) { |
297 | $this->_receiptContactId = $this->_contributorContactID = $formValues['soft_credit_contact_id']; | |
481a74f4 | 298 | list($this->_contributorDisplayName, |
353ffa53 | 299 | $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contributorContactID); |
6a488035 TO |
300 | } |
301 | else { | |
302 | $this->_receiptContactId = $this->_contributorContactID = $this->_contactID; | |
303 | $this->_contributorDisplayName = $this->_memberDisplayName; | |
304 | $this->_contributorEmail = $this->_memberEmail; | |
305 | } | |
306 | } | |
96025800 | 307 | |
0271af37 | 308 | /** |
309 | * Set variables in a way that can be accessed from different places. | |
310 | * | |
311 | * This is part of refactoring for unit testability on the submit function. | |
312 | * | |
313 | * @param array $params | |
314 | */ | |
7865d848 EM |
315 | protected function setContextVariables($params) { |
316 | $variables = array( | |
317 | 'action' => '_action', | |
318 | 'context' => '_context', | |
319 | 'id' => '_id', | |
320 | 'cid' => '_contactID', | |
321 | 'mode' => '_mode', | |
322 | ); | |
323 | foreach ($variables as $paramKey => $classVar) { | |
324 | if (isset($params[$paramKey]) && !isset($this->$classVar)) { | |
325 | $this->$classVar = $params[$paramKey]; | |
326 | } | |
327 | } | |
328 | ||
7865d848 EM |
329 | if ($this->_id) { |
330 | $this->_memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'membership_type_id'); | |
331 | $this->_membershipIDs[] = $this->_id; | |
332 | } | |
333 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); | |
334 | } | |
335 | ||
a70418a7 EM |
336 | /** |
337 | * Create a recurring contribution record. | |
338 | * | |
339 | * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard | |
340 | * to know the downstream impacts if we keep passing around the same array. | |
341 | * | |
342 | * @param $paymentParams | |
343 | * | |
344 | * @return array | |
345 | * @throws \CiviCRM_API3_Exception | |
346 | */ | |
347 | protected function processRecurringContribution($paymentParams) { | |
348 | $membershipID = $paymentParams['membership_type_id'][1]; | |
349 | $contributionRecurParams = array( | |
350 | 'contact_id' => $paymentParams['contactID'], | |
351 | 'amount' => $paymentParams['total_amount'], | |
b1b2796c | 352 | 'contribution_status_id' => 'Pending', |
a70418a7 | 353 | 'payment_processor_id' => $paymentParams['payment_processor_id'], |
b1b2796c | 354 | 'campaign_id' => $paymentParams['campaign_id'], |
a70418a7 | 355 | 'financial_type_id' => $paymentParams['financial_type_id'], |
b1b2796c | 356 | 'is_email_receipt' => $paymentParams['is_email_receipt'], |
357 | 'payment_instrument_id' => $paymentParams['payment_instrument_id'], | |
358 | 'invoice_id' => $paymentParams['invoice_id'], | |
a70418a7 EM |
359 | ); |
360 | ||
361 | $mapping = array( | |
362 | 'frequency_interval' => 'duration_interval', | |
363 | 'frequency_unit' => 'duration_unit', | |
364 | ); | |
365 | $membershipType = civicrm_api3('MembershipType', 'getsingle', array( | |
366 | 'id' => $membershipID, | |
367 | 'return' => $mapping, | |
368 | )); | |
369 | ||
14065266 | 370 | $returnParams = array('is_recur' => TRUE); |
a70418a7 EM |
371 | foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) { |
372 | $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName]; | |
14065266 | 373 | $returnParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName]; |
a70418a7 EM |
374 | } |
375 | ||
376 | $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams); | |
14065266 | 377 | $returnParams['contributionRecurID'] = $contributionRecur['id']; |
a70418a7 EM |
378 | return $returnParams; |
379 | } | |
380 | ||
ccb02c2d | 381 | /** |
382 | * Ensure price parameters are set. | |
383 | * | |
384 | * If they are not set it means a quick config option has been chosen so we | |
385 | * fill them in here to make the two flows the same. They look like 'price_2' => 2 etc. | |
386 | * | |
387 | * @param array $formValues | |
388 | */ | |
389 | protected function ensurePriceParamsAreSet(&$formValues) { | |
390 | foreach ($formValues as $key => $value) { | |
ea09031a | 391 | if ((substr($key, 0, 6) == 'price_') && is_numeric(substr($key, 6))) { |
ccb02c2d | 392 | return; |
393 | } | |
394 | } | |
395 | $priceFields = CRM_Member_BAO_Membership::setQuickConfigMembershipParameters( | |
396 | $formValues['membership_type_id'][0], | |
397 | $formValues['membership_type_id'][1], | |
430f2faf | 398 | CRM_Utils_Array::value('total_amount', $formValues), |
ccb02c2d | 399 | $this->_priceSetId |
400 | ); | |
401 | $formValues = array_merge($formValues, $priceFields['price_fields']); | |
402 | } | |
403 | ||
404 | /** | |
405 | * Get the details for the selected price set. | |
406 | * | |
407 | * @param array $params | |
408 | * Parameters submitted to the form. | |
409 | * | |
410 | * @return array | |
411 | */ | |
412 | protected static function getPriceSetDetails($params) { | |
413 | $priceSetID = CRM_Utils_Array::value('price_set_id', $params); | |
414 | if ($priceSetID) { | |
415 | return CRM_Price_BAO_PriceSet::getSetDetail($priceSetID); | |
416 | } | |
417 | else { | |
418 | $priceSet = CRM_Price_BAO_PriceSet::getDefaultPriceSet('membership'); | |
419 | $priceSet = reset($priceSet); | |
420 | return CRM_Price_BAO_PriceSet::getSetDetail($priceSet['setID']); | |
421 | } | |
422 | } | |
423 | ||
424 | /** | |
425 | * Get the selected price set id. | |
426 | * | |
427 | * @param array $params | |
428 | * Parameters submitted to the form. | |
429 | * | |
430 | * @return int | |
431 | */ | |
432 | protected static function getPriceSetID($params) { | |
433 | $priceSetID = CRM_Utils_Array::value('price_set_id', $params); | |
434 | if (!$priceSetID) { | |
435 | $priceSetDetails = self::getPriceSetDetails($params); | |
436 | return key($priceSetDetails); | |
437 | } | |
438 | return $priceSetID; | |
439 | } | |
440 | ||
441 | /** | |
442 | * Store parameters relating to price sets. | |
443 | * | |
444 | * @param array $formValues | |
445 | * | |
446 | * @return array | |
447 | */ | |
448 | protected function setPriceSetParameters($formValues) { | |
449 | $this->_priceSetId = self::getPriceSetID($formValues); | |
450 | $priceSetDetails = self::getPriceSetDetails($formValues); | |
451 | $this->_priceSet = $priceSetDetails[$this->_priceSetId]; | |
452 | // process price set and get total amount and line items. | |
453 | $this->ensurePriceParamsAreSet($formValues); | |
454 | return $formValues; | |
455 | } | |
456 | ||
09108d7d | 457 | /** |
458 | * Wrapper function for unit tests. | |
459 | * | |
460 | * @param array $formValues | |
461 | */ | |
462 | public function testSubmit($formValues) { | |
18135422 | 463 | $this->setContextVariables($formValues); |
09108d7d | 464 | $this->_memType = $formValues['membership_type_id'][1]; |
465 | $this->_params = $formValues; | |
466 | $this->submit(); | |
467 | } | |
468 | ||
6a488035 | 469 | } |