INFRA-132 - s/true/TRUE/
[civicrm-core.git] / CRM / Contribute / Form / UpdateSubscription.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components generic to recurring contributions
38 *
39 * It delegates the work to lower level subclasses and integrates the changes
40 * back in. It also uses a lot of functionality with the CRM API's, so any change
41 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
42 *
43 */
44 class CRM_Contribute_Form_UpdateSubscription extends CRM_Core_Form {
45
46 /**
47 * The recurring contribution id, used when editing the recurring contribution
48 *
49 * @var int
50 */
51 protected $_crid = NULL;
52
53 protected $_coid = NULL;
54
55 protected $_subscriptionDetails = NULL;
56
57 protected $_selfService = FALSE;
58
59 public $_paymentProcessor = NULL;
60
61 public $_paymentProcessorObj = NULL;
62
63 /**
64 * The id of the contact associated with this recurring contribution
65 *
66 * @var int
67 */
68 public $_contactID;
69
70 public function preProcess() {
71
72 $this->_crid = CRM_Utils_Request::retrieve('crid', 'Integer', $this, FALSE);
73 if ($this->_crid) {
74 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_crid, 'recur', 'info');
75 $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_crid, 'recur', 'obj');
76 $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_crid);
77 }
78
79 $this->_coid = CRM_Utils_Request::retrieve('coid', 'Integer', $this, FALSE);
80 if ($this->_coid) {
81 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'info');
82 $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj');
83 $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_coid, 'contribution');
84 }
85
86 if ((!$this->_crid && !$this->_coid) ||
87 ($this->_subscriptionDetails == CRM_Core_DAO::$_nullObject)
88 ) {
89 CRM_Core_Error::fatal('Required information missing.');
90 }
91
92 if ($this->_subscriptionDetails->membership_id && $this->_subscriptionDetails->auto_renew) {
93 CRM_Core_Error::fatal(ts('You cannot update the subscription.'));
94 }
95
96 if (!CRM_Core_Permission::check('edit contributions')) {
97 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this, FALSE);
98 if (!CRM_Contact_BAO_Contact_Utils::validChecksum($this->_subscriptionDetails->contact_id, $userChecksum)) {
99 CRM_Core_Error::fatal(ts('You do not have permission to update subscription.'));
100 }
101 $this->_selfService = TRUE;
102 }
103 $this->assign('self_service', $this->_selfService);
104
105 if (!$this->_paymentProcessorObj->isSupported('changeSubscriptionAmount')) {
106 $userAlert = "<span class='font-red'>" . ts('Updates made using this form will change the recurring contribution information stored in your CiviCRM database, but will NOT be sent to the payment processor. You must enter the same changes using the payment processor web site.',
107 array(1 => $this->_paymentProcessorObj->_processorName)) . '</span>';
108 CRM_Core_Session::setStatus($userAlert, ts('Warning'), 'alert');
109 }
110
111 $this->assign('isChangeSupported', $this->_paymentProcessorObj->isSupported('changeSubscriptionAmount'));
112 $this->assign('paymentProcessor', $this->_paymentProcessor);
113 $this->assign('frequency_unit', $this->_subscriptionDetails->frequency_unit);
114 $this->assign('frequency_interval', $this->_subscriptionDetails->frequency_interval);
115
116 if ($this->_subscriptionDetails->contact_id) {
117 list($this->_donorDisplayName, $this->_donorEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id);
118 }
119
120 CRM_Utils_System::setTitle(ts('Update Recurring Contribution'));
121
122 // handle context redirection
123 CRM_Contribute_BAO_ContributionRecur::setSubscriptionContext();
124 }
125
126 /**
127 * Set default values for the form. Note that in edit/view mode
128 * the default values are retrieved from the database
129 *
130 *
131 * @return void
132 */
133 public function setDefaultValues() {
134
135 $this->_defaults = array();
136 $this->_defaults['amount'] = $this->_subscriptionDetails->amount;
137 $this->_defaults['installments'] = $this->_subscriptionDetails->installments;
138 $this->_defaults['is_notify'] = 1;
139
140 return $this->_defaults;
141 }
142
143 /**
144 * Actually build the components of the form
145 *
146 * @return void
147 */
148 public function buildQuickForm() {
149 // define the fields
150 $this->addMoney('amount', ts('Recurring Contribution Amount'), TRUE,
151 array(
152 'size' => 20), TRUE,
153 'currency', NULL, TRUE
154 );
155
156 $this->add('text', 'installments', ts('Number of Installments'), array('size' => 20), TRUE);
157
158 if ($this->_donorEmail) {
159 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
160 }
161
162 $type = 'next';
163 if ($this->_selfService) {
164 $type = 'submit';
165 }
166
167 // define the buttons
168 $this->addButtons(array(
169 array(
170 'type' => $type,
171 'name' => ts('Save'),
172 'isDefault' => TRUE,
173 ),
174 array(
175 'type' => 'cancel',
176 'name' => ts('Cancel'),
177 ),
178 )
179 );
180 }
181
182 /**
183 * This function is called after the user submits the form
184 *
185 *
186 * @return void
187 */
188 public function postProcess() {
189 // store the submitted values in an array
190 $params = $this->exportValues();
191
192 if ($this->_selfService && $this->_donorEmail) {
193 // for self service force notify
194 $params['is_notify'] = 1;
195 }
196
197 // if this is an update of an existing recurring contribution, pass the ID
198 $params['id'] = $this->_subscriptionDetails->recur_id;
199 $message = '';
200
201 $params['subscriptionId'] = $this->_subscriptionDetails->subscription_id;
202 $updateSubscription = TRUE;
203 if ($this->_paymentProcessorObj->isSupported('changeSubscriptionAmount')) {
204 $updateSubscription = $this->_paymentProcessorObj->changeSubscriptionAmount($message, $params);
205 }
206 if (is_a($updateSubscription, 'CRM_Core_Error')) {
207 CRM_Core_Error::displaySessionError($updateSubscription);
208 $status = ts('Could not update the Recurring contribution details');
209 $msgTitle = ts('Update Error');
210 $msgType = 'error';
211 }
212 elseif ($updateSubscription) {
213 // save the changes
214 $result = CRM_Contribute_BAO_ContributionRecur::add($params);
215 $status = ts('Recurring contribution has been updated to: %1, every %2 %3(s) for %4 installments.',
216 array(1 => CRM_Utils_Money::format($params['amount'], $this->_subscriptionDetails->currency),
217 2 => $this->_subscriptionDetails->frequency_interval,
218 3 => $this->_subscriptionDetails->frequency_unit,
219 4 => $params['installments'],
220 )
221 );
222
223 $msgTitle = ts('Update Success');
224 $msgType = 'success';
225
226 $contactID = $this->_subscriptionDetails->contact_id;
227
228 if ($this->_subscriptionDetails->amount != $params['amount']) {
229 $message .= "<br /> " . ts("Recurring contribution amount has been updated from %1 to %2 for this subscription.",
230 array(
231 1 => CRM_Utils_Money::format($this->_subscriptionDetails->amount, $this->_subscriptionDetails->currency),
232 2 => CRM_Utils_Money::format($params['amount'], $this->_subscriptionDetails->currency),
233 )) . ' ';
234 }
235
236 if ($this->_subscriptionDetails->installments != $params['installments']) {
237 $message .= "<br /> " . ts("Recurring contribution installments have been updated from %1 to %2 for this subscription.", array(1 => $this->_subscriptionDetails->installments, 2 => $params['installments'])) . ' ';
238 }
239
240 $activityParams = array(
241 'source_contact_id' => $contactID,
242 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type',
243 'Update Recurring Contribution',
244 'name'
245 ),
246 'subject' => ts('Recurring Contribution Updated'),
247 'details' => $message,
248 'activity_date_time' => date('YmdHis'),
249 'status_id' => CRM_Core_OptionGroup::getValue('activity_status',
250 'Completed',
251 'name'
252 ),
253 );
254 $session = CRM_Core_Session::singleton();
255 $cid = $session->get('userID');
256
257 if ($cid) {
258 $activityParams['target_contact_id'][] = $activityParams['source_contact_id'];
259 $activityParams['source_contact_id'] = $cid;
260 }
261 CRM_Activity_BAO_Activity::create($activityParams);
262
263 if (!empty($params['is_notify'])) {
264 // send notification
265 if ($this->_subscriptionDetails->contribution_page_id) {
266 CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id',
267 $this->_subscriptionDetails->contribution_page_id, $value, array(
268 'title',
269 'receipt_from_name',
270 'receipt_from_email',
271 )
272 );
273 $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) . '" <' . $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] . '>';
274 }
275 else {
276 $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
277 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
278 }
279
280 list($donorDisplayName, $donorEmail) = CRM_Contact_BAO_Contact::getContactDetails($contactID);
281
282 $tplParams = array(
283 'recur_frequency_interval' => $this->_subscriptionDetails->frequency_interval,
284 'recur_frequency_unit' => $this->_subscriptionDetails->frequency_unit,
285 'amount' => CRM_Utils_Money::format($params['amount']),
286 'installments' => $params['installments'],
287 );
288
289 $tplParams['contact'] = array('display_name' => $donorDisplayName);
290 $tplParams['receipt_from_email'] = $receiptFrom;
291
292 $sendTemplateParams = array(
293 'groupName' => 'msg_tpl_workflow_contribution',
294 'valueName' => 'contribution_recurring_edit',
295 'contactId' => $contactID,
296 'tplParams' => $tplParams,
297 'isTest' => $this->_subscriptionDetails->is_test,
298 'PDFFilename' => 'receipt.pdf',
299 'from' => $receiptFrom,
300 'toName' => $donorDisplayName,
301 'toEmail' => $donorEmail,
302 );
303 list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
304 }
305 }
306
307 $session = CRM_Core_Session::singleton();
308 $userID = $session->get('userID');
309 if ($userID && $status) {
310 CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
311 }
312 elseif (!$userID) {
313 if ($status)
314 CRM_Utils_System::setUFMessage($status);
315 // keep result as 1, since we not displaying anything on the redirected page anyway
316 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus',
317 "reset=1&task=update&result=1"));
318 }
319 }
320 }