1: <?php
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: class Mage_Persistent_Model_Observer
36: {
37: 38: 39: 40: 41:
42: protected $_setQuotePersistent = true;
43:
44: 45: 46: 47: 48: 49:
50: public function applyPersistentData($observer)
51: {
52: if (!Mage::helper('persistent')->canProcess($observer)
53: || !$this->_getPersistentHelper()->isPersistent() || Mage::getSingleton('customer/session')->isLoggedIn()) {
54: return $this;
55: }
56: Mage::getModel('persistent/persistent_config')
57: ->setConfigFilePath(Mage::helper('persistent')->getPersistentConfigFilePath())
58: ->fire();
59: return $this;
60: }
61:
62: 63: 64: 65: 66: 67:
68: public function applyBlockPersistentData($observer)
69: {
70: if (!$this->_getPersistentHelper()->isPersistent() || Mage::getSingleton('customer/session')->isLoggedIn()) {
71: return $this;
72: }
73:
74:
75: $block = $observer->getEvent()->getBlock();
76:
77: if (!$block) {
78: return $this;
79: }
80:
81: $xPath = '//instances/blocks/*[block_type="' . get_class($block) . '"]';
82: $configFilePath = $observer->getEvent()->getConfigFilePath();
83:
84:
85: $persistentConfig = Mage::getModel('persistent/persistent_config')
86: ->setConfigFilePath(
87: $configFilePath ? $configFilePath : Mage::helper('persistent')->getPersistentConfigFilePath()
88: );
89:
90: foreach ($persistentConfig->getXmlConfig()->xpath($xPath) as $persistentConfigInfo) {
91: $persistentConfig->fireOne($persistentConfigInfo->asArray(), $block);
92: }
93:
94: return $this;
95: }
96:
97: 98: 99: 100: 101: 102:
103: public function emulateWelcomeBlock($block)
104: {
105: $block->setWelcome(
106: Mage::helper('persistent')->__('Welcome, %s!', Mage::helper('core')->escapeHtml($this->_getPersistentCustomer()->getName(), null))
107: );
108:
109: $this->_applyAccountLinksPersistentData();
110: $block->setAdditionalHtml(Mage::app()->getLayout()->getBlock('header.additional')->toHtml());
111:
112: return $this;
113: }
114:
115: 116: 117:
118: protected function _applyAccountLinksPersistentData()
119: {
120: if (!Mage::app()->getLayout()->getBlock('header.additional')) {
121: Mage::app()->getLayout()->addBlock('persistent/header_additional', 'header.additional');
122: }
123: }
124:
125: 126: 127: 128: 129:
130: public function emulateAccountLinks($block)
131: {
132: $this->_applyAccountLinksPersistentData();
133: $block->getCacheKeyInfo();
134: $block->addLink(
135: Mage::helper('persistent')->getPersistentName(),
136: Mage::helper('persistent')->getUnsetCookieUrl(),
137: Mage::helper('persistent')->getPersistentName(),
138: false,
139: array(),
140: 110
141: );
142: $block->removeLinkByUrl(Mage::helper('customer')->getRegisterUrl());
143: $block->removeLinkByUrl(Mage::helper('customer')->getLoginUrl());
144: }
145:
146: 147: 148: 149: 150:
151: public function emulateTopLinks($block)
152: {
153: $this->_applyAccountLinksPersistentData();
154: $block->removeLinkByUrl(Mage::getUrl('customer/account/login'));
155: }
156:
157: 158: 159: 160: 161:
162: public function emulateQuote($observer)
163: {
164: $stopActions = array(
165: 'persistent_index_saveMethod',
166: 'customer_account_createpost'
167: );
168:
169: if (!Mage::helper('persistent')->canProcess($observer)
170: || !$this->_getPersistentHelper()->isPersistent() || Mage::getSingleton('customer/session')->isLoggedIn()) {
171: return;
172: }
173:
174:
175: $action = $observer->getEvent()->getControllerAction();
176: $actionName = $action->getFullActionName();
177:
178: if (in_array($actionName, $stopActions)) {
179: return;
180: }
181:
182:
183: $checkoutSession = Mage::getSingleton('checkout/session');
184: if ($this->_isShoppingCartPersist()) {
185: $checkoutSession->setCustomer($this->_getPersistentCustomer());
186: if (!$checkoutSession->hasQuote()) {
187: $checkoutSession->getQuote();
188: }
189: }
190: }
191:
192: 193: 194: 195: 196:
197: public function setQuotePersistentData($observer)
198: {
199: if (!$this->_isPersistent()) {
200: return;
201: }
202:
203:
204: $quote = $observer->getEvent()->getQuote();
205: if (!$quote) {
206: return;
207: }
208:
209: if ($this->_isGuestShoppingCart() && $this->_setQuotePersistent) {
210:
211: $quote->setIsActive(false)->setIsPersistent(true);
212: }
213: }
214:
215: 216: 217: 218: 219:
220: public function setLoadPersistentQuote($observer)
221: {
222: if (!$this->_isGuestShoppingCart()) {
223: return;
224: }
225:
226:
227: $checkoutSession = $observer->getEvent()->getCheckoutSession();
228: if ($checkoutSession) {
229: $checkoutSession->setLoadInactive();
230: }
231: }
232:
233: 234: 235: 236: 237:
238: public function preventClearCheckoutSession($observer)
239: {
240: $action = $this->_checkClearCheckoutSessionNecessity($observer);
241:
242: if ($action) {
243: $action->setClearCheckoutSession(false);
244: }
245: }
246:
247: 248: 249: 250: 251:
252: public function makePersistentQuoteGuest($observer)
253: {
254: if (!$this->_checkClearCheckoutSessionNecessity($observer)) {
255: return;
256: }
257:
258: $this->setQuoteGuest(true);
259: }
260:
261: 262: 263: 264: 265: 266:
267: protected function _checkClearCheckoutSessionNecessity($observer)
268: {
269: if (!$this->_isGuestShoppingCart()) {
270: return false;
271: }
272:
273:
274: $action = $observer->getEvent()->getControllerAction();
275: if ($action instanceof Mage_Persistent_IndexController) {
276: return $action;
277: }
278:
279: return false;
280: }
281:
282: 283: 284: 285: 286:
287: public function customerAuthenticatedEvent($observer)
288: {
289:
290: $customerSession = Mage::getSingleton('customer/session');
291: $customerSession->setCustomerId(null)->setCustomerGroupId(null);
292:
293: if (Mage::app()->getRequest()->getParam('context') != 'checkout') {
294: $this->_expirePersistentSession();
295: return;
296: }
297:
298: $this->setQuoteGuest();
299: }
300:
301: 302: 303: 304: 305:
306: public function removePersistentCookie($observer)
307: {
308: if (!Mage::helper('persistent')->canProcess($observer) || !$this->_isPersistent()) {
309: return;
310: }
311:
312: $this->_getPersistentHelper()->getSession()->removePersistentCookie();
313:
314: $customerSession = Mage::getSingleton('customer/session');
315: if (!$customerSession->isLoggedIn()) {
316: $customerSession->setCustomerId(null)->setCustomerGroupId(null);
317: }
318:
319: $this->setQuoteGuest();
320: }
321:
322: 323: 324: 325: 326:
327: public function disableGuestCheckout($observer)
328: {
329: if ($this->_getPersistentHelper()->isPersistent()) {
330: $observer->getEvent()->getResult()->setIsAllowed(false);
331: }
332: }
333:
334: 335: 336: 337: 338:
339: public function preventExpressCheckout($observer)
340: {
341: if (!$this->_isLoggedOut()) {
342: return;
343: }
344:
345:
346: $controllerAction = $observer->getEvent()->getControllerAction();
347: if (is_callable(array($controllerAction, 'redirectLogin'))) {
348: Mage::getSingleton('core/session')->addNotice(
349: Mage::helper('persistent')->__('To proceed to Checkout, please log in using your email address.')
350: );
351: $controllerAction->redirectLogin();
352: if ($controllerAction instanceof Mage_GoogleCheckout_RedirectController
353: || $controllerAction instanceof Mage_Paypal_Controller_Express_Abstract
354: ) {
355: Mage::getSingleton('customer/session')
356: ->setBeforeAuthUrl(Mage::getUrl('persistent/index/expressCheckout'));
357: }
358: }
359: }
360:
361: 362: 363: 364: 365:
366: protected function _getPersistentCustomer()
367: {
368: return Mage::getModel('customer/customer')->load(
369: $this->_getPersistentHelper()->getSession()->getCustomerId()
370: );
371: }
372:
373: 374: 375: 376: 377:
378: protected function _getPersistentHelper()
379: {
380: return Mage::helper('persistent/session');
381: }
382:
383: 384: 385: 386: 387:
388: protected function _getQuote()
389: {
390: $quote = Mage::getModel('sales/quote');
391: $quote->loadByCustomer($this->_getPersistentCustomer());
392: return $quote;
393: }
394:
395: 396: 397: 398: 399:
400: protected function _isShoppingCartPersist()
401: {
402: return Mage::helper('persistent')->isShoppingCartPersist();
403: }
404:
405: 406: 407: 408: 409:
410: protected function _isPersistent()
411: {
412: return $this->_getPersistentHelper()->isPersistent();
413: }
414:
415: 416: 417: 418: 419:
420: protected function _isLoggedOut()
421: {
422: return $this->_isPersistent() && !Mage::getSingleton('customer/session')->isLoggedIn();
423: }
424:
425: 426: 427: 428: 429:
430: protected function _isGuestShoppingCart()
431: {
432: return $this->_isLoggedOut() && !Mage::helper('persistent')->isShoppingCartPersist();
433: }
434:
435: 436: 437: 438: 439:
440: public function setQuoteGuest($checkQuote = false)
441: {
442:
443: $quote = Mage::getSingleton('checkout/session')->getQuote();
444: if ($quote && $quote->getId()) {
445: if ($checkQuote && !Mage::helper('persistent')->isShoppingCartPersist() && !$quote->getIsPersistent()) {
446: Mage::getSingleton('checkout/session')->unsetAll();
447: return;
448: }
449:
450: $quote->getPaymentsCollection()->walk('delete');
451: $quote->getAddressesCollection()->walk('delete');
452: $this->_setQuotePersistent = false;
453: $quote
454: ->setIsActive(true)
455: ->setCustomerId(null)
456: ->setCustomerEmail(null)
457: ->setCustomerFirstname(null)
458: ->setCustomerLastname(null)
459: ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
460: ->setIsPersistent(false)
461: ->removeAllAddresses();
462:
463: $quote->getShippingAddress();
464: $quote->getBillingAddress();
465: $quote->collectTotals()->save();
466: }
467:
468: $this->_getPersistentHelper()->getSession()->removePersistentCookie();
469: }
470:
471: 472: 473: 474: 475:
476: public function checkExpirePersistentQuote(Varien_Event_Observer $observer)
477: {
478: if (!Mage::helper('persistent')->canProcess($observer)) {
479: return;
480: }
481:
482:
483: $customerSession = Mage::getSingleton('customer/session');
484:
485: if (Mage::helper('persistent')->isEnabled()
486: && !$this->_isPersistent()
487: && !$customerSession->isLoggedIn()
488: && Mage::getSingleton('checkout/session')->getQuoteId()
489: && !($observer->getControllerAction() instanceof Mage_Checkout_OnepageController)
490:
491: ) {
492: Mage::dispatchEvent('persistent_session_expired');
493: $this->_expirePersistentSession();
494: $customerSession->setCustomerId(null)->setCustomerGroupId(null);
495: }
496: }
497:
498: protected function _expirePersistentSession()
499: {
500:
501: $checkoutSession = Mage::getSingleton('checkout/session');
502:
503: $quote = $checkoutSession->setLoadInactive()->getQuote();
504: if ($quote->getIsActive() && $quote->getCustomerId()) {
505: $checkoutSession->setCustomer(null)->unsetAll();
506: } else {
507: $quote
508: ->setIsActive(true)
509: ->setIsPersistent(false)
510: ->setCustomerId(null)
511: ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
512: }
513: }
514:
515: 516: 517: 518: 519: 520:
521: public function clearExpiredCronJob(Mage_Cron_Model_Schedule $schedule)
522: {
523: $websiteIds = Mage::getResourceModel('core/website_collection')->getAllIds();
524: if (!is_array($websiteIds)) {
525: return $this;
526: }
527:
528: foreach ($websiteIds as $websiteId) {
529: Mage::getModel('persistent/session')->deleteExpired($websiteId);
530: }
531:
532: return $this;
533: }
534:
535: 536: 537: 538: 539:
540: public function createPersistentHandleLayout(Varien_Event_Observer $observer)
541: {
542:
543: $layout = $observer->getEvent()->getLayout();
544: if (Mage::helper('persistent')->canProcess($observer) && $layout && Mage::helper('persistent')->isEnabled()
545: && Mage::helper('persistent/session')->isPersistent()
546: ) {
547: $handle = (Mage::getSingleton('customer/session')->isLoggedIn())
548: ? Mage_Persistent_Helper_Data::LOGGED_IN_LAYOUT_HANDLE
549: : Mage_Persistent_Helper_Data::LOGGED_OUT_LAYOUT_HANDLE;
550: $layout->getUpdate()->addHandle($handle);
551: }
552: }
553:
554: 555: 556: 557: 558:
559: public function updateCustomerCookies(Varien_Event_Observer $observer)
560: {
561: if (!$this->_isPersistent()) {
562: return;
563: }
564:
565: $customerCookies = $observer->getEvent()->getCustomerCookies();
566: if ($customerCookies instanceof Varien_Object) {
567: $persistentCustomer = $this->_getPersistentCustomer();
568: $customerCookies->setCustomerId($persistentCustomer->getId());
569: $customerCookies->setCustomerGroupId($persistentCustomer->getGroupId());
570: }
571: }
572:
573: 574: 575: 576: 577: 578:
579: public function emulateCustomer($observer)
580: {
581: if (!Mage::helper('persistent')->canProcess($observer)
582: || !$this->_isShoppingCartPersist()
583: ) {
584: return $this;
585: }
586:
587: if ($this->_isLoggedOut()) {
588:
589: $customer = Mage::getModel('customer/customer')->load(
590: $this->_getPersistentHelper()->getSession()->getCustomerId()
591: );
592: Mage::getSingleton('customer/session')
593: ->setCustomerId($customer->getId())
594: ->setCustomerGroupId($customer->getGroupId());
595: }
596: return $this;
597: }
598: }
599: