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 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Base class for offline membership / membership type / membership renewal and membership status forms | |
38 | * | |
39 | */ | |
cc984198 | 40 | class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { |
6a488035 TO |
41 | |
42 | /** | |
43 | * The id of the object being edited / created | |
44 | * | |
45 | * @var int | |
46 | */ | |
cc984198 | 47 | public $_id; |
6a488035 | 48 | |
a6513ad5 EM |
49 | /** |
50 | * Membership Type ID | |
51 | * @var | |
52 | */ | |
53 | protected $_memType; | |
54 | ||
55 | /** | |
56 | * Array of from email ids | |
57 | * @var array | |
58 | */ | |
59 | protected $_fromEmails = array(); | |
60 | ||
ab30e033 EM |
61 | /** |
62 | * Details of all enabled membership types. | |
63 | * | |
64 | * @var array | |
65 | */ | |
66 | protected $allMembershipTypeDetails = array(); | |
67 | ||
68 | /** | |
69 | * Array of membership type IDs and whether they permit autorenewal. | |
70 | * | |
71 | * @var array | |
72 | */ | |
73 | protected $membershipTypeRenewalStatus = array(); | |
74 | ||
00be9182 | 75 | public function preProcess() { |
42e8b05c EM |
76 | // Check for edit permission. |
77 | if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) { | |
78 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); | |
79 | } | |
80 | parent::preProcess(); | |
7865d848 | 81 | $params = array(); |
7865d848 EM |
82 | $params['context'] = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'membership'); |
83 | $params['id'] = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
84 | $params['mode'] = CRM_Utils_Request::retrieve('mode', 'String', $this); | |
85 | ||
86 | $this->setContextVariables($params); | |
a6513ad5 EM |
87 | |
88 | $this->assign('context', $this->_context); | |
89 | $this->assign('membershipMode', $this->_mode); | |
ab30e033 EM |
90 | $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, NULL, TRUE); |
91 | foreach ($this->allMembershipTypeDetails as $index => $membershipType) { | |
92 | if ($membershipType['auto_renew']) { | |
93 | $this->_recurMembershipTypes[$index] = $membershipType; | |
94 | $this->membershipTypeRenewalStatus[$index] = $membershipType['auto_renew']; | |
95 | } | |
96 | } | |
6a488035 TO |
97 | } |
98 | ||
99 | /** | |
c490a46a | 100 | * Set default values for the form. MobileProvider that in edit/view mode |
6a488035 TO |
101 | * the default values are retrieved from the database |
102 | * | |
6a488035 | 103 | * |
a6c01b45 CW |
104 | * @return array |
105 | * defaults | |
6a488035 | 106 | */ |
00be9182 | 107 | public function setDefaultValues() { |
6a488035 | 108 | $defaults = array(); |
a6513ad5 EM |
109 | if (isset($this->_id)) { |
110 | $params = array('id' => $this->_id); | |
111 | CRM_Member_BAO_Membership::retrieve($params, $defaults); | |
42e8b05c EM |
112 | if (isset($defaults['minimum_fee'])) { |
113 | $defaults['minimum_fee'] = CRM_Utils_Money::format($defaults['minimum_fee'], NULL, '%a'); | |
114 | } | |
6a488035 | 115 | |
42e8b05c EM |
116 | if (isset($defaults['status'])) { |
117 | $this->assign('membershipStatus', $defaults['status']); | |
118 | } | |
6a488035 TO |
119 | } |
120 | ||
121 | if ($this->_action & CRM_Core_Action::ADD) { | |
122 | $defaults['is_active'] = 1; | |
123 | } | |
124 | ||
125 | if (isset($defaults['member_of_contact_id']) && | |
126 | $defaults['member_of_contact_id'] | |
127 | ) { | |
128 | $defaults['member_org'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
129 | $defaults['member_of_contact_id'], 'display_name' | |
130 | ); | |
131 | } | |
132 | return $defaults; | |
133 | } | |
134 | ||
135 | /** | |
fe482240 | 136 | * Build the form object. |
6a488035 TO |
137 | */ |
138 | public function buildQuickForm() { | |
4aa7d844 | 139 | |
a6513ad5 EM |
140 | if ($this->_mode) { |
141 | $this->add('select', 'payment_processor_id', | |
142 | ts('Payment Processor'), | |
143 | $this->_processors, TRUE, | |
144 | array('onChange' => "buildAutoRenew( null, this.value );") | |
145 | ); | |
dfc68e82 | 146 | CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE); |
a6513ad5 | 147 | } |
4aa7d844 EM |
148 | // Build the form for auto renew. This is displayed when in credit card mode or update mode. |
149 | // The reason for showing it in update mode is not that clear. | |
4aa7d844 | 150 | if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) { |
ab30e033 EM |
151 | if (!empty($this->_recurPaymentProcessors)) { |
152 | $this->assign('allowAutoRenew', TRUE); | |
4aa7d844 EM |
153 | } |
154 | ||
ab30e033 EM |
155 | $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'), |
156 | NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );") | |
157 | ); | |
158 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
159 | $autoRenewElement->freeze(); | |
160 | } | |
4aa7d844 | 161 | |
ab30e033 | 162 | $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors)); |
4aa7d844 EM |
163 | $this->addElement('checkbox', |
164 | 'auto_renew', | |
165 | ts('Membership renewed automatically'), | |
166 | NULL, | |
167 | array('onclick' => "buildReceiptANDNotice( );") | |
168 | ); | |
169 | ||
170 | $this->assignPaymentRelatedVariables(); | |
171 | } | |
ab30e033 | 172 | $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus)); |
4aa7d844 | 173 | |
6a488035 TO |
174 | if ($this->_action & CRM_Core_Action::RENEW) { |
175 | $this->addButtons(array( | |
176 | array( | |
177 | 'type' => 'upload', | |
178 | 'name' => ts('Renew'), | |
21dfd5f5 | 179 | 'isDefault' => TRUE, |
6a488035 TO |
180 | ), |
181 | array( | |
182 | 'type' => 'cancel', | |
21dfd5f5 TO |
183 | 'name' => ts('Cancel'), |
184 | ), | |
6a488035 TO |
185 | ) |
186 | ); | |
187 | } | |
188 | elseif ($this->_action & CRM_Core_Action::DELETE) { | |
189 | $this->addButtons(array( | |
190 | array( | |
191 | 'type' => 'next', | |
192 | 'name' => ts('Delete'), | |
21dfd5f5 | 193 | 'isDefault' => TRUE, |
6a488035 TO |
194 | ), |
195 | array( | |
196 | 'type' => 'cancel', | |
21dfd5f5 TO |
197 | 'name' => ts('Cancel'), |
198 | ), | |
6a488035 TO |
199 | ) |
200 | ); | |
201 | } | |
202 | else { | |
203 | $this->addButtons(array( | |
204 | array( | |
205 | 'type' => 'upload', | |
206 | 'name' => ts('Save'), | |
21dfd5f5 | 207 | 'isDefault' => TRUE, |
6a488035 TO |
208 | ), |
209 | array( | |
210 | 'type' => 'upload', | |
211 | 'name' => ts('Save and New'), | |
21dfd5f5 | 212 | 'subName' => 'new', |
6a488035 TO |
213 | ), |
214 | array( | |
215 | 'type' => 'cancel', | |
21dfd5f5 TO |
216 | 'name' => ts('Cancel'), |
217 | ), | |
6a488035 TO |
218 | ) |
219 | ); | |
220 | } | |
221 | } | |
222 | ||
fb3082b2 | 223 | /** |
100fef9d | 224 | * Extract values from the contact create boxes on the form and assign appropriately to |
6a488035 TO |
225 | * |
226 | * - $this->_contributorEmail, | |
227 | * - $this->_memberEmail & | |
fb3082b2 | 228 | * - $this->_contributionName |
6a488035 TO |
229 | * - $this->_memberName |
230 | * - $this->_contactID (effectively memberContactId but changing might have spin-off effects) | |
231 | * - $this->_contributorContactId - id of the contributor | |
232 | * - $this->_receiptContactId | |
233 | * | |
234 | * If the member & contributor are the same then the values will be the same. But if different people paid | |
235 | * then they weill differ | |
236 | * | |
5a4f6742 CW |
237 | * @param array $formValues |
238 | * values from form. The important values we are looking for are. | |
4c7aa1f7 CW |
239 | * - contact_id |
240 | * - soft_credit_contact_id | |
6a488035 | 241 | */ |
9b873358 | 242 | public function storeContactFields($formValues) { |
6a488035 | 243 | // in a 'standalone form' (contact id not in the url) the contact will be in the form values |
4c7aa1f7 CW |
244 | if (!empty($formValues['contact_id'])) { |
245 | $this->_contactID = $formValues['contact_id']; | |
6a488035 TO |
246 | } |
247 | ||
248 | list($this->_memberDisplayName, | |
353ffa53 TO |
249 | $this->_memberEmail |
250 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
6a488035 TO |
251 | |
252 | //CRM-10375 Where the payer differs to the member the payer should get the email. | |
253 | // here we store details in order to do that | |
4c7aa1f7 CW |
254 | if (!empty($formValues['soft_credit_contact_id'])) { |
255 | $this->_receiptContactId = $this->_contributorContactID = $formValues['soft_credit_contact_id']; | |
481a74f4 | 256 | list($this->_contributorDisplayName, |
353ffa53 | 257 | $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contributorContactID); |
6a488035 TO |
258 | } |
259 | else { | |
260 | $this->_receiptContactId = $this->_contributorContactID = $this->_contactID; | |
261 | $this->_contributorDisplayName = $this->_memberDisplayName; | |
262 | $this->_contributorEmail = $this->_memberEmail; | |
263 | } | |
264 | } | |
96025800 | 265 | |
7865d848 EM |
266 | protected function setContextVariables($params) { |
267 | $variables = array( | |
268 | 'action' => '_action', | |
269 | 'context' => '_context', | |
270 | 'id' => '_id', | |
271 | 'cid' => '_contactID', | |
272 | 'mode' => '_mode', | |
273 | ); | |
274 | foreach ($variables as $paramKey => $classVar) { | |
275 | if (isset($params[$paramKey]) && !isset($this->$classVar)) { | |
276 | $this->$classVar = $params[$paramKey]; | |
277 | } | |
278 | } | |
279 | ||
280 | if ($this->_mode) { | |
281 | $this->assignPaymentRelatedVariables(); | |
282 | } | |
283 | ||
284 | if ($this->_id) { | |
285 | $this->_memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'membership_type_id'); | |
286 | $this->_membershipIDs[] = $this->_id; | |
287 | } | |
288 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); | |
289 | } | |
290 | ||
a70418a7 EM |
291 | /** |
292 | * Create a recurring contribution record. | |
293 | * | |
294 | * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard | |
295 | * to know the downstream impacts if we keep passing around the same array. | |
296 | * | |
297 | * @param $paymentParams | |
298 | * | |
299 | * @return array | |
300 | * @throws \CiviCRM_API3_Exception | |
301 | */ | |
302 | protected function processRecurringContribution($paymentParams) { | |
303 | $membershipID = $paymentParams['membership_type_id'][1]; | |
304 | $contributionRecurParams = array( | |
305 | 'contact_id' => $paymentParams['contactID'], | |
306 | 'amount' => $paymentParams['total_amount'], | |
307 | 'payment_processor_id' => $paymentParams['payment_processor_id'], | |
438c3772 | 308 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $paymentParams), |
a70418a7 | 309 | 'financial_type_id' => $paymentParams['financial_type_id'], |
438c3772 | 310 | 'is_email_receipt' => CRM_Utils_Array::value('is_email_receipt', $paymentParams), |
a70418a7 EM |
311 | // This is not great as it could also be direct debit - but is consistent with elsewhere & all need fixing. |
312 | 'payment_instrument_id' => 1, | |
438c3772 | 313 | 'invoice_id' => CRM_Utils_Array::value('invoiceID ', $paymentParams), |
a70418a7 EM |
314 | ); |
315 | ||
316 | $mapping = array( | |
317 | 'frequency_interval' => 'duration_interval', | |
318 | 'frequency_unit' => 'duration_unit', | |
319 | ); | |
320 | $membershipType = civicrm_api3('MembershipType', 'getsingle', array( | |
321 | 'id' => $membershipID, | |
322 | 'return' => $mapping, | |
323 | )); | |
324 | ||
325 | foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) { | |
326 | $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName]; | |
327 | } | |
328 | ||
329 | $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams); | |
330 | $returnParams = array( | |
331 | 'contributionRecurID' => $contributionRecur['id'], | |
332 | 'is_recur' => TRUE, | |
333 | ); | |
334 | return $returnParams; | |
335 | } | |
336 | ||
6a488035 | 337 | } |