Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | 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 | ||
00be9182 | 87 | public function preProcess() { |
42e8b05c EM |
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.')); | |
91 | } | |
92 | parent::preProcess(); | |
7865d848 | 93 | $params = array(); |
7865d848 EM |
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); | |
97 | ||
98 | $this->setContextVariables($params); | |
a6513ad5 EM |
99 | |
100 | $this->assign('context', $this->_context); | |
101 | $this->assign('membershipMode', $this->_mode); | |
1a00ea3c | 102 | $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, array(), TRUE); |
ab30e033 EM |
103 | foreach ($this->allMembershipTypeDetails as $index => $membershipType) { |
104 | if ($membershipType['auto_renew']) { | |
105 | $this->_recurMembershipTypes[$index] = $membershipType; | |
106 | $this->membershipTypeRenewalStatus[$index] = $membershipType['auto_renew']; | |
107 | } | |
108 | } | |
6a488035 TO |
109 | } |
110 | ||
111 | /** | |
c490a46a | 112 | * Set default values for the form. MobileProvider that in edit/view mode |
6a488035 TO |
113 | * the default values are retrieved from the database |
114 | * | |
6a488035 | 115 | * |
a6c01b45 CW |
116 | * @return array |
117 | * defaults | |
6a488035 | 118 | */ |
00be9182 | 119 | public function setDefaultValues() { |
6a488035 | 120 | $defaults = array(); |
a6513ad5 EM |
121 | if (isset($this->_id)) { |
122 | $params = array('id' => $this->_id); | |
123 | CRM_Member_BAO_Membership::retrieve($params, $defaults); | |
42e8b05c EM |
124 | if (isset($defaults['minimum_fee'])) { |
125 | $defaults['minimum_fee'] = CRM_Utils_Money::format($defaults['minimum_fee'], NULL, '%a'); | |
126 | } | |
6a488035 | 127 | |
42e8b05c EM |
128 | if (isset($defaults['status'])) { |
129 | $this->assign('membershipStatus', $defaults['status']); | |
130 | } | |
6a488035 TO |
131 | } |
132 | ||
133 | if ($this->_action & CRM_Core_Action::ADD) { | |
134 | $defaults['is_active'] = 1; | |
135 | } | |
136 | ||
137 | if (isset($defaults['member_of_contact_id']) && | |
138 | $defaults['member_of_contact_id'] | |
139 | ) { | |
140 | $defaults['member_org'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
141 | $defaults['member_of_contact_id'], 'display_name' | |
142 | ); | |
143 | } | |
144 | return $defaults; | |
145 | } | |
146 | ||
147 | /** | |
fe482240 | 148 | * Build the form object. |
6a488035 TO |
149 | */ |
150 | public function buildQuickForm() { | |
4aa7d844 | 151 | |
a6513ad5 EM |
152 | if ($this->_mode) { |
153 | $this->add('select', 'payment_processor_id', | |
154 | ts('Payment Processor'), | |
155 | $this->_processors, TRUE, | |
156 | array('onChange' => "buildAutoRenew( null, this.value );") | |
157 | ); | |
dfc68e82 | 158 | CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE); |
a6513ad5 | 159 | } |
4aa7d844 EM |
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. | |
4aa7d844 | 162 | if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) { |
ab30e033 EM |
163 | if (!empty($this->_recurPaymentProcessors)) { |
164 | $this->assign('allowAutoRenew', TRUE); | |
4aa7d844 EM |
165 | } |
166 | ||
ab30e033 EM |
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( );") | |
169 | ); | |
170 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
171 | $autoRenewElement->freeze(); | |
172 | } | |
4aa7d844 | 173 | |
ab30e033 | 174 | $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors)); |
4aa7d844 EM |
175 | $this->addElement('checkbox', |
176 | 'auto_renew', | |
177 | ts('Membership renewed automatically'), | |
178 | NULL, | |
179 | array('onclick' => "buildReceiptANDNotice( );") | |
180 | ); | |
181 | ||
182 | $this->assignPaymentRelatedVariables(); | |
183 | } | |
ab30e033 | 184 | $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus)); |
4aa7d844 | 185 | |
6a488035 TO |
186 | if ($this->_action & CRM_Core_Action::RENEW) { |
187 | $this->addButtons(array( | |
188 | array( | |
189 | 'type' => 'upload', | |
190 | 'name' => ts('Renew'), | |
21dfd5f5 | 191 | 'isDefault' => TRUE, |
6a488035 TO |
192 | ), |
193 | array( | |
194 | 'type' => 'cancel', | |
21dfd5f5 TO |
195 | 'name' => ts('Cancel'), |
196 | ), | |
6a488035 TO |
197 | ) |
198 | ); | |
199 | } | |
200 | elseif ($this->_action & CRM_Core_Action::DELETE) { | |
201 | $this->addButtons(array( | |
202 | array( | |
203 | 'type' => 'next', | |
204 | 'name' => ts('Delete'), | |
21dfd5f5 | 205 | 'isDefault' => TRUE, |
6a488035 TO |
206 | ), |
207 | array( | |
208 | 'type' => 'cancel', | |
21dfd5f5 TO |
209 | 'name' => ts('Cancel'), |
210 | ), | |
6a488035 TO |
211 | ) |
212 | ); | |
213 | } | |
214 | else { | |
215 | $this->addButtons(array( | |
216 | array( | |
217 | 'type' => 'upload', | |
218 | 'name' => ts('Save'), | |
21dfd5f5 | 219 | 'isDefault' => TRUE, |
6a488035 TO |
220 | ), |
221 | array( | |
222 | 'type' => 'upload', | |
223 | 'name' => ts('Save and New'), | |
21dfd5f5 | 224 | 'subName' => 'new', |
6a488035 TO |
225 | ), |
226 | array( | |
227 | 'type' => 'cancel', | |
21dfd5f5 TO |
228 | 'name' => ts('Cancel'), |
229 | ), | |
6a488035 TO |
230 | ) |
231 | ); | |
232 | } | |
233 | } | |
234 | ||
fb3082b2 | 235 | /** |
100fef9d | 236 | * Extract values from the contact create boxes on the form and assign appropriately to |
6a488035 TO |
237 | * |
238 | * - $this->_contributorEmail, | |
239 | * - $this->_memberEmail & | |
fb3082b2 | 240 | * - $this->_contributionName |
6a488035 TO |
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 | |
245 | * | |
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 | |
248 | * | |
5a4f6742 CW |
249 | * @param array $formValues |
250 | * values from form. The important values we are looking for are. | |
4c7aa1f7 CW |
251 | * - contact_id |
252 | * - soft_credit_contact_id | |
6a488035 | 253 | */ |
9b873358 | 254 | public function storeContactFields($formValues) { |
6a488035 | 255 | // in a 'standalone form' (contact id not in the url) the contact will be in the form values |
4c7aa1f7 CW |
256 | if (!empty($formValues['contact_id'])) { |
257 | $this->_contactID = $formValues['contact_id']; | |
6a488035 TO |
258 | } |
259 | ||
260 | list($this->_memberDisplayName, | |
353ffa53 TO |
261 | $this->_memberEmail |
262 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
6a488035 TO |
263 | |
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 | |
4c7aa1f7 CW |
266 | if (!empty($formValues['soft_credit_contact_id'])) { |
267 | $this->_receiptContactId = $this->_contributorContactID = $formValues['soft_credit_contact_id']; | |
481a74f4 | 268 | list($this->_contributorDisplayName, |
353ffa53 | 269 | $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contributorContactID); |
6a488035 TO |
270 | } |
271 | else { | |
272 | $this->_receiptContactId = $this->_contributorContactID = $this->_contactID; | |
273 | $this->_contributorDisplayName = $this->_memberDisplayName; | |
274 | $this->_contributorEmail = $this->_memberEmail; | |
275 | } | |
276 | } | |
96025800 | 277 | |
7865d848 EM |
278 | protected function setContextVariables($params) { |
279 | $variables = array( | |
280 | 'action' => '_action', | |
281 | 'context' => '_context', | |
282 | 'id' => '_id', | |
283 | 'cid' => '_contactID', | |
284 | 'mode' => '_mode', | |
285 | ); | |
286 | foreach ($variables as $paramKey => $classVar) { | |
287 | if (isset($params[$paramKey]) && !isset($this->$classVar)) { | |
288 | $this->$classVar = $params[$paramKey]; | |
289 | } | |
290 | } | |
291 | ||
292 | if ($this->_mode) { | |
293 | $this->assignPaymentRelatedVariables(); | |
294 | } | |
295 | ||
296 | if ($this->_id) { | |
297 | $this->_memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'membership_type_id'); | |
298 | $this->_membershipIDs[] = $this->_id; | |
299 | } | |
300 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); | |
301 | } | |
302 | ||
a70418a7 EM |
303 | /** |
304 | * Create a recurring contribution record. | |
305 | * | |
306 | * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard | |
307 | * to know the downstream impacts if we keep passing around the same array. | |
308 | * | |
309 | * @param $paymentParams | |
310 | * | |
311 | * @return array | |
312 | * @throws \CiviCRM_API3_Exception | |
313 | */ | |
314 | protected function processRecurringContribution($paymentParams) { | |
315 | $membershipID = $paymentParams['membership_type_id'][1]; | |
316 | $contributionRecurParams = array( | |
317 | 'contact_id' => $paymentParams['contactID'], | |
318 | 'amount' => $paymentParams['total_amount'], | |
319 | 'payment_processor_id' => $paymentParams['payment_processor_id'], | |
438c3772 | 320 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $paymentParams), |
a70418a7 | 321 | 'financial_type_id' => $paymentParams['financial_type_id'], |
438c3772 | 322 | 'is_email_receipt' => CRM_Utils_Array::value('is_email_receipt', $paymentParams), |
a70418a7 EM |
323 | // This is not great as it could also be direct debit - but is consistent with elsewhere & all need fixing. |
324 | 'payment_instrument_id' => 1, | |
438c3772 | 325 | 'invoice_id' => CRM_Utils_Array::value('invoiceID ', $paymentParams), |
a70418a7 EM |
326 | ); |
327 | ||
328 | $mapping = array( | |
329 | 'frequency_interval' => 'duration_interval', | |
330 | 'frequency_unit' => 'duration_unit', | |
331 | ); | |
332 | $membershipType = civicrm_api3('MembershipType', 'getsingle', array( | |
333 | 'id' => $membershipID, | |
334 | 'return' => $mapping, | |
335 | )); | |
336 | ||
337 | foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) { | |
338 | $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName]; | |
339 | } | |
340 | ||
341 | $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams); | |
342 | $returnParams = array( | |
343 | 'contributionRecurID' => $contributionRecur['id'], | |
344 | 'is_recur' => TRUE, | |
345 | ); | |
346 | return $returnParams; | |
347 | } | |
348 | ||
6a488035 | 349 | } |