Merge branch '4.6' of https://github.com/civicrm/civicrm-core
[civicrm-core.git] / CRM / Member / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * Base class for offline membership / membership type / membership renewal and membership status forms
38 *
39 */
40 class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
41
42 /**
43 * The id of the object being edited / created
44 *
45 * @var int
46 */
47 public $_id;
48
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
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
75 public function preProcess() {
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();
81 $params = array();
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);
87
88 $this->assign('context', $this->_context);
89 $this->assign('membershipMode', $this->_mode);
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 }
97 }
98
99 /**
100 * Set default values for the form. MobileProvider that in edit/view mode
101 * the default values are retrieved from the database
102 *
103 *
104 * @return array
105 * defaults
106 */
107 public function setDefaultValues() {
108 $defaults = array();
109 if (isset($this->_id)) {
110 $params = array('id' => $this->_id);
111 CRM_Member_BAO_Membership::retrieve($params, $defaults);
112 if (isset($defaults['minimum_fee'])) {
113 $defaults['minimum_fee'] = CRM_Utils_Money::format($defaults['minimum_fee'], NULL, '%a');
114 }
115
116 if (isset($defaults['status'])) {
117 $this->assign('membershipStatus', $defaults['status']);
118 }
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 /**
136 * Build the form object.
137 */
138 public function buildQuickForm() {
139
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 );
146 CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE);
147 }
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.
150 if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) {
151 if (!empty($this->_recurPaymentProcessors)) {
152 $this->assign('allowAutoRenew', TRUE);
153 }
154
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 }
161
162 $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors));
163 $this->addElement('checkbox',
164 'auto_renew',
165 ts('Membership renewed automatically'),
166 NULL,
167 array('onclick' => "buildReceiptANDNotice( );")
168 );
169
170 $this->assignPaymentRelatedVariables();
171 }
172 $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus));
173
174 if ($this->_action & CRM_Core_Action::RENEW) {
175 $this->addButtons(array(
176 array(
177 'type' => 'upload',
178 'name' => ts('Renew'),
179 'isDefault' => TRUE,
180 ),
181 array(
182 'type' => 'cancel',
183 'name' => ts('Cancel'),
184 ),
185 )
186 );
187 }
188 elseif ($this->_action & CRM_Core_Action::DELETE) {
189 $this->addButtons(array(
190 array(
191 'type' => 'next',
192 'name' => ts('Delete'),
193 'isDefault' => TRUE,
194 ),
195 array(
196 'type' => 'cancel',
197 'name' => ts('Cancel'),
198 ),
199 )
200 );
201 }
202 else {
203 $this->addButtons(array(
204 array(
205 'type' => 'upload',
206 'name' => ts('Save'),
207 'isDefault' => TRUE,
208 ),
209 array(
210 'type' => 'upload',
211 'name' => ts('Save and New'),
212 'subName' => 'new',
213 ),
214 array(
215 'type' => 'cancel',
216 'name' => ts('Cancel'),
217 ),
218 )
219 );
220 }
221 }
222
223 /**
224 * Extract values from the contact create boxes on the form and assign appropriately to
225 *
226 * - $this->_contributorEmail,
227 * - $this->_memberEmail &
228 * - $this->_contributionName
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 *
237 * @param array $formValues
238 * values from form. The important values we are looking for are.
239 * - contact_id
240 * - soft_credit_contact_id
241 */
242 public function storeContactFields($formValues) {
243 // in a 'standalone form' (contact id not in the url) the contact will be in the form values
244 if (!empty($formValues['contact_id'])) {
245 $this->_contactID = $formValues['contact_id'];
246 }
247
248 list($this->_memberDisplayName,
249 $this->_memberEmail
250 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
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
254 if (!empty($formValues['soft_credit_contact_id'])) {
255 $this->_receiptContactId = $this->_contributorContactID = $formValues['soft_credit_contact_id'];
256 list($this->_contributorDisplayName,
257 $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contributorContactID);
258 }
259 else {
260 $this->_receiptContactId = $this->_contributorContactID = $this->_contactID;
261 $this->_contributorDisplayName = $this->_memberDisplayName;
262 $this->_contributorEmail = $this->_memberEmail;
263 }
264 }
265
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
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'],
308 'campaign_id' => CRM_Utils_Array::value('campaign_id', $paymentParams),
309 'financial_type_id' => $paymentParams['financial_type_id'],
310 'is_email_receipt' => CRM_Utils_Array::value('is_email_receipt', $paymentParams),
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,
313 'invoice_id' => CRM_Utils_Array::value('invoiceID ', $paymentParams),
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
337 }