Merge pull request #5729 from mlutfy/47-crm14588
[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 public function preProcess() {
62 // Check for edit permission.
63 if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) {
64 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
65 }
66 parent::preProcess();
67 $params = array();
68 $params['context'] = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'membership');
69 $params['id'] = CRM_Utils_Request::retrieve('id', 'Positive', $this);
70 $params['mode'] = CRM_Utils_Request::retrieve('mode', 'String', $this);
71
72 $this->setContextVariables($params);
73
74 $this->assign('context', $this->_context);
75 $this->assign('membershipMode', $this->_mode);
76 }
77
78 /**
79 * Set default values for the form. MobileProvider that in edit/view mode
80 * the default values are retrieved from the database
81 *
82 *
83 * @return array
84 * defaults
85 */
86 public function setDefaultValues() {
87 $defaults = array();
88 if (isset($this->_id)) {
89 $params = array('id' => $this->_id);
90 CRM_Member_BAO_Membership::retrieve($params, $defaults);
91 if (isset($defaults['minimum_fee'])) {
92 $defaults['minimum_fee'] = CRM_Utils_Money::format($defaults['minimum_fee'], NULL, '%a');
93 }
94
95 if (isset($defaults['status'])) {
96 $this->assign('membershipStatus', $defaults['status']);
97 }
98 }
99
100 if ($this->_action & CRM_Core_Action::ADD) {
101 $defaults['is_active'] = 1;
102 }
103
104 if (isset($defaults['member_of_contact_id']) &&
105 $defaults['member_of_contact_id']
106 ) {
107 $defaults['member_org'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
108 $defaults['member_of_contact_id'], 'display_name'
109 );
110 }
111 return $defaults;
112 }
113
114 /**
115 * Build the form object.
116 */
117 public function buildQuickForm() {
118
119 if ($this->_mode) {
120 $this->add('select', 'payment_processor_id',
121 ts('Payment Processor'),
122 $this->_processors, TRUE,
123 array('onChange' => "buildAutoRenew( null, this.value );")
124 );
125 CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE);
126 }
127 // Build the form for auto renew. This is displayed when in credit card mode or update mode.
128 // The reason for showing it in update mode is not that clear.
129 $autoRenew = array();
130 $recurProcessor = array();
131 if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) {
132 if (!empty($recurProcessor)) {
133 $autoRenew = array();
134 if (!empty($membershipType)) {
135 $sql = '
136 SELECT id,
137 auto_renew,
138 duration_unit,
139 duration_interval
140 FROM civicrm_membership_type
141 WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
142 $recurMembershipTypes = CRM_Core_DAO::executeQuery($sql);
143 while ($recurMembershipTypes->fetch()) {
144 $autoRenew[$recurMembershipTypes->id] = $recurMembershipTypes->auto_renew;
145 foreach (array(
146 'id',
147 'auto_renew',
148 'duration_unit',
149 'duration_interval',
150 ) as $fld) {
151 $this->_recurMembershipTypes[$recurMembershipTypes->id][$fld] = $recurMembershipTypes->$fld;
152 }
153 }
154 }
155
156 if ($this->_mode) {
157 if (!empty($this->_recurPaymentProcessors)) {
158 $this->assign('allowAutoRenew', TRUE);
159 }
160 }
161
162 $this->assign('autoRenew', json_encode($autoRenew));
163 $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
164 NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
165 );
166 if ($this->_action & CRM_Core_Action::UPDATE) {
167 $autoRenewElement->freeze();
168 }
169 }
170
171 }
172 $this->assign('recurProcessor', json_encode($recurProcessor));
173
174 if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) {
175 $this->addElement('checkbox',
176 'auto_renew',
177 ts('Membership renewed automatically'),
178 NULL,
179 array('onclick' => "buildReceiptANDNotice( );")
180 );
181
182 $this->assignPaymentRelatedVariables();
183 }
184 $this->assign('autoRenewOptions', json_encode($autoRenew));
185
186 if ($this->_action & CRM_Core_Action::RENEW) {
187 $this->addButtons(array(
188 array(
189 'type' => 'upload',
190 'name' => ts('Renew'),
191 'isDefault' => TRUE,
192 ),
193 array(
194 'type' => 'cancel',
195 'name' => ts('Cancel'),
196 ),
197 )
198 );
199 }
200 elseif ($this->_action & CRM_Core_Action::DELETE) {
201 $this->addButtons(array(
202 array(
203 'type' => 'next',
204 'name' => ts('Delete'),
205 'isDefault' => TRUE,
206 ),
207 array(
208 'type' => 'cancel',
209 'name' => ts('Cancel'),
210 ),
211 )
212 );
213 }
214 else {
215 $this->addButtons(array(
216 array(
217 'type' => 'upload',
218 'name' => ts('Save'),
219 'isDefault' => TRUE,
220 ),
221 array(
222 'type' => 'upload',
223 'name' => ts('Save and New'),
224 'subName' => 'new',
225 ),
226 array(
227 'type' => 'cancel',
228 'name' => ts('Cancel'),
229 ),
230 )
231 );
232 }
233 }
234
235 /**
236 * Extract values from the contact create boxes on the form and assign appropriately to
237 *
238 * - $this->_contributorEmail,
239 * - $this->_memberEmail &
240 * - $this->_contributionName
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 *
249 * @param array $formValues
250 * values from form. The important values we are looking for are.
251 * - contact_id
252 * - soft_credit_contact_id
253 */
254 public function storeContactFields($formValues) {
255 // in a 'standalone form' (contact id not in the url) the contact will be in the form values
256 if (!empty($formValues['contact_id'])) {
257 $this->_contactID = $formValues['contact_id'];
258 }
259
260 list($this->_memberDisplayName,
261 $this->_memberEmail
262 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
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
266 if (!empty($formValues['soft_credit_contact_id'])) {
267 $this->_receiptContactId = $this->_contributorContactID = $formValues['soft_credit_contact_id'];
268 list($this->_contributorDisplayName,
269 $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contributorContactID);
270 }
271 else {
272 $this->_receiptContactId = $this->_contributorContactID = $this->_contactID;
273 $this->_contributorDisplayName = $this->_memberDisplayName;
274 $this->_contributorEmail = $this->_memberEmail;
275 }
276 }
277
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
303 }