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