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