-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathObserver.php
444 lines (394 loc) · 15.9 KB
/
Observer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* package Netzarbeiter_CustomerActivation
* copyright Copyright (c) 2014 Vinai Kopp http://netzarbeiter.com/
* license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Netzarbeiter_CustomerActivation_Model_Observer
{
const XML_PATH_ALWAYS_NOTIFY_ADMIN = 'customer/customeractivation/always_send_admin_email';
/**
* Fired on customer_login event
* Check if the customer has been activated (via adminhtml)
* If not, through login error
*
* @param Varien_Event_Observer $observer
*/
public function customerLogin($observer)
{
$helper = Mage::helper('customeractivation');
if (!$helper->isModuleActive()) {
return;
}
if ($this->_isApiRequest()) {
return;
}
$customer = $observer->getEvent()->getCustomer();
$session = Mage::getSingleton('customer/session');
if (!$customer->getCustomerActivated()) {
/*
* Fake the old logout() method without deleting the session and all messages
*/
$session->setCustomer(Mage::getModel('customer/customer'))
->setId(null)
->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
if ($this->_checkRequestRoute('customer', 'account', 'createpost')) {
/*
* If this is a regular registration, simply display message
*/
$message = $helper->__('Please wait for your account to be activated');
$session->addSuccess($message);
} else {
/*
* All other types of login
*/
Mage::throwException($helper->__('This account is not activated.'));
}
}
}
/**
* Flag new accounts as such
*
* @param Varien_Event_Observer $observer
*/
public function customerSaveBefore($observer)
{
$customer = $observer->getEvent()->getCustomer();
$helper = Mage::helper('customeractivation');
$storeId = $helper->getCustomerStoreId($customer);
if (!$helper->isModuleActive($storeId)) {
return;
}
if (!$customer->getId()) {
$customer->setCustomerActivationNewAccount(true);
if ($this->shouldSetDefaultActivationStatus()) {
$groupId = $customer->getGroupId();
$defaultStatus = $helper->getDefaultActivationStatus($groupId, $storeId);
$customer->setCustomerActivated($defaultStatus);
if (!$defaultStatus) {
// Suppress the "enter your billing address for VAT validation" message.
// This setting will not be saved, its just for this request.
$helper = Mage::helper('customer/address');
if (method_exists($helper, 'isVatValidationEnabled')) {
if (is_callable(array($helper, 'isVatValidationEnabled'))) {
if (Mage::helper('customer/address')->isVatValidationEnabled($storeId)) {
Mage::app()->getStore($storeId)->setConfig(
Mage_Customer_Helper_Address::XML_PATH_VAT_VALIDATION_ENABLED, false
);
}
}
}
}
}
}
}
/**
* Send out emails
*
* @param Varien_Event_Observer $observer
*/
public function customerSaveAfter($observer)
{
/** @var Mage_Customer_Model_Customer $customer */
$customer = $observer->getEvent()->getCustomer();
$helper = Mage::helper('customeractivation');
$storeId = $helper->getCustomerStoreId($customer);
if (!$helper->isModuleActive($storeId)) {
return;
}
$groupId = $customer->getGroupId();
$defaultStatus = $helper->getDefaultActivationStatus($groupId, $storeId);
try {
if (Mage::app()->getStore()->isAdmin()) {
if (!$customer->getOrigData('customer_activated') && $customer->getCustomerActivated()) {
// Send customer email only if it isn't a new account and it isn't activated by default
if (!($customer->getCustomerActivationNewAccount() && $defaultStatus)) {
$helper->sendCustomerNotificationEmail($customer);
}
}
} else {
if ($customer->getCustomerActivationNewAccount()) {
// Only notify the admin if the default is deactivated or the "always notify" flag is configured
$alwaysNotify = Mage::getStoreConfig(self::XML_PATH_ALWAYS_NOTIFY_ADMIN, $storeId);
if (!$defaultStatus || $alwaysNotify) {
$helper->sendAdminNotificationEmail($customer);
}
}
$customer->setCustomerActivationNewAccount(false);
}
} catch (Exception $e) {
Mage::throwException($e->getMessage());
}
}
public function salesConvertQuoteAddressToOrder(Varien_Event_Observer $observer)
{
/** @var $address Mage_Sales_Model_Quote_Address */
$address = $observer->getEvent()->getAddress();
$this->_abortCheckoutRegistration($address->getQuote());
}
/**
* Abort registration during checkout if default activation status is false.
*
* Should work with: onepage checkout, multishipping checkout and custom
* checkout types, as long as they use the standard converter model
* Mage_Sales_Model_Convert_Quote.
*
* Expected state after checkout:
* - Customer saved
* - No order placed
* - Guest quote still contains items
* - Customer quote contains no items
* - Customer redirected to login page
* - Customer sees message
*
* @param Mage_Sales_Model_Quote $quote
*/
protected function _abortCheckoutRegistration(Mage_Sales_Model_Quote $quote)
{
$helper = Mage::helper('customeractivation');
if (!$helper->isModuleActive($quote->getStoreId())) {
return;
}
if ($this->_isApiRequest()) {
return;
}
if (!Mage::getSingleton('customer/session')->isLoggedIn() && !$quote->getCustomerIsGuest()) {
// Order is being created by non-activated customer
$customer = $quote->getCustomer()->save();
if (!$customer->getCustomerActivated()) {
// Abort order placement
// Exception handling can not be assumed to be useful
// Todo: merge guest quote to customer quote and save customer quote, but don't log customer in
// Add message
$message = $helper->__(
'Please wait for your account to be activated, then log in and continue with the checkout'
);
Mage::getSingleton('core/session')->addSuccess($message);
// Handle redirect to login page
$targetUrl = Mage::getUrl('customer/account/login');
$response = Mage::app()->getResponse();
if (Mage::app()->getRequest()->isAjax()) {
// Assume one page checkout
$result = array('redirect' => $targetUrl);
$response->setBody(Mage::helper('core')->jsonEncode($result));
} else {
if ($response->canSendHeaders(true)) {
// Assume multishipping checkout
$response->clearHeader('location')
->setRedirect($targetUrl);
}
}
$response->sendResponse();
/* ugly, but we need to stop the further order processing */
exit();
}
}
}
/**
* Return true if the request is made via the api
*
* @return boolean
*/
protected function _isApiRequest()
{
return Mage::app()->getRequest()->getModuleName() === 'api';
}
/**
* Check the current module, controller and action against the given values.
*
* @param string $module
* @param string $controller
* @param string $action
* @return bool
*/
protected function _checkRequestRoute($module, $controller, $action)
{
$req = Mage::app()->getRequest();
if (strtolower($req->getModuleName()) == $module
&& strtolower($req->getControllerName()) == $controller
&& strtolower($req->getActionName()) == $action
) {
return true;
}
return false;
}
/**
* Check the current controller and action match the passed names
*
* @param string $controller
* @param string $action
* @return bool
*/
protected function _checkControllerAction($controller, $action)
{
$req = Mage::app()->getRequest();
return $this->_checkRequestRoute($req->getModuleName(), $controller, $action);
}
/**
* Add customer activation option to the mass action block.
*
* This can't be done during the block abstract event
*
* @param Varien_Event_Observer $observer
*/
public function adminhtmlBlockHtmlBefore(Varien_Event_Observer $observer)
{
// Check the grid is the customer grid
if ($observer->getBlock()->getId() != 'customerGrid') {
return;
}
// Check if there is a massaction block and if yes, add the massaction for customeractivation
$massBlock = $observer->getBlock()->getMassactionBlock();
if ($massBlock) {
/** @var $helper Netzarbeiter_CustomerActivation_Helper_Data */
$helper = Mage::helper('customeractivation');
if (!$helper->isModuleActiveInAdmin()) {
return;
}
$noEmail = Netzarbeiter_CustomerActivation_Helper_Data::STATUS_ACTIVATE_WITHOUT_EMAIL;
$withEmail = Netzarbeiter_CustomerActivation_Helper_Data::STATUS_ACTIVATE_WITH_EMAIL;
$deactivate = Netzarbeiter_CustomerActivation_Helper_Data::STATUS_DEACTIVATE;
$massBlock->addItem(
'customer_activated',
array(
'label' => $helper->__('Customer Activated'),
'url' => Mage::getUrl('adminhtml/customerActivation/massActivation'),
'additional' => array(
'status' => array(
'name' => 'customer_activated',
'type' => 'select',
'class' => 'required-entry',
'label' => $helper->__('Customer Activated'),
'values' => array(
$noEmail => $helper->__('Yes (No Notification)'),
$withEmail => $helper->__('Yes (With Notification)'),
$deactivate => $helper->__('No')
)
)
)
)
);
}
}
/**
* Add the customer_activated attribute to the customer grid collection
*
* @param Varien_Event_Observer $observer
*/
public function eavCollectionAbstractLoadBefore(Varien_Event_Observer $observer)
{
if (!Mage::helper('customeractivation')->isModuleActiveInAdmin()) {
return;
}
if (Mage::app()->getRequest()->getControllerName() !== 'customer') {
return;
}
/** @var $collection Mage_Customer_Model_Resource_Customer_Collection */
$collection = $observer->getEvent()->getCollection();
// Only add attribute to customer collections
$customerTypeId = Mage::getSingleton('eav/config')->getEntityType('customer')->getId();
$collectionTypeId = $collection->getEntity()->getTypeId();
if ($customerTypeId == $collectionTypeId) {
$collection->addAttributeToSelect('customer_activated');
}
}
/**
* Add customer_activated column to CSV and XML exports
*
* @param Varien_Event_Observer $observer
*/
public function coreBlockAbstractPrepareLayoutAfter(Varien_Event_Observer $observer)
{
if (!Mage::helper('customeractivation')->isModuleActiveInAdmin()) {
return;
}
if (Mage::app()->getRequest()->getControllerName() !== 'customer') {
return;
}
$block = $observer->getBlock();
if ($block->getType() === 'adminhtml/customer_grid') {
// I don't think we need to limit applying the column by action
//$action = Mage::app()->getRequest()->getActionName();
//if (in_array($action, array('grid', 'index', 'exportCsv', 'exportXml'))) {
$this->_addActivationStatusColumn($block);
//}
}
}
/**
* Add the activation status column to the customer grid block.
*
* This is used from different events when displaying the block as well as
* during exporting the grid to CSV or XML.
*
* @param Mage_Adminhtml_Block_Widget_Grid $block
*/
protected function _addActivationStatusColumn(Mage_Adminhtml_Block_Widget_Grid $block)
{
/** @var $helper Netzarbeiter_CustomerActivation_Helper_Data */
$helper = Mage::helper('customeractivation');
// Add the attribute as a column to the grid
$block->addColumnAfter(
'customer_activated',
array(
'header' => $helper->__('Customer Activated'),
'align' => 'center',
'width' => '80px',
'type' => 'options',
'options' => array(
'0' => $helper->__('No'),
'1' => $helper->__('Yes')
),
'default' => '0',
'index' => 'customer_activated',
'renderer' => 'customeractivation/adminhtml_widget_grid_column_renderer_boolean'
),
'customer_since'
);
// Set the new columns order.. otherwise our column would be the last one
$block->sortColumnsByOrder();
}
/**
* Reportedly on Magento 1.6 customers are logged in automatically
* by the lost password functionality (must be some customization actually).
*
* This observer method removes the customer id from the customer/session,
* in effect causing a logout just in case.
*
* @param Varien_Event_Observer $observer
*/
public function controllerActionPostdispatchCustomerAccountResetPasswordPost(Varien_Event_Observer $observer)
{
if (!Mage::helper('customeractivation')->isModuleActive()) {
return;
}
if (version_compare(Mage::getVersion(), '1.7', '<')) {
$session = Mage::getSingleton('customer/session');
$customer = $session->getCustomer();
if (!$customer->getCustomerActivated() && $session->isLoggedIn()) {
$session->setCustomerId(null)->setId(null);
}
}
}
private function shouldSetDefaultActivationStatus()
{
return !$this->isAdminEditCustomerSaveAction() && !$this->_isApiRequest();
}
private function isAdminEditCustomerSaveAction()
{
return
Mage::app()->getStore()->isAdmin() &&
$this->_checkControllerAction('customer', 'save');
}
}