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