Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Persistent_Block_Form_Remember
  • Mage_Persistent_Block_Header_Additional
  • Mage_Persistent_Helper_Data
  • Mage_Persistent_Helper_Session
  • Mage_Persistent_IndexController
  • Mage_Persistent_Model_Observer
  • Mage_Persistent_Model_Observer_Session
  • Mage_Persistent_Model_Persistent_Config
  • Mage_Persistent_Model_Resource_Session
  • Mage_Persistent_Model_Session
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Persistent
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: 
 28: /**
 29:  * Persistent Observer
 30:  *
 31:  * @category   Mage
 32:  * @package    Mage_Persistent
 33:  * @author     Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_Persistent_Model_Observer
 36: {
 37:     /**
 38:      * Whether set quote to be persistent in workflow
 39:      *
 40:      * @var bool
 41:      */
 42:     protected $_setQuotePersistent = true;
 43: 
 44:     /**
 45:      * Apply persistent data
 46:      *
 47:      * @param Varien_Event_Observer $observer
 48:      * @return Mage_Persistent_Model_Observer
 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:      * Apply persistent data to specific block
 64:      *
 65:      * @param Varien_Event_Observer $observer
 66:      * @return Mage_Persistent_Model_Observer
 67:      */
 68:     public function applyBlockPersistentData($observer)
 69:     {
 70:         if (!$this->_getPersistentHelper()->isPersistent() || Mage::getSingleton('customer/session')->isLoggedIn()) {
 71:             return $this;
 72:         }
 73: 
 74:         /** @var $block Mage_Core_Block_Abstract */
 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:         /** @var $persistentConfig Mage_Persistent_Model_Persistent_Config */
 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:      * Emulate 'welcome' block with persistent data
 99:      *
100:      * @param Mage_Core_Block_Abstract $block
101:      * @return Mage_Persistent_Model_Observer
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:      * Emulate 'account links' block with persistent data
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:      * Emulate 'account links' block with persistent data
127:      *
128:      * @param Mage_Core_Block_Abstract $block
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:      * Emulate 'top links' block with persistent data
148:      *
149:      * @param Mage_Core_Block_Abstract $block
150:      */
151:     public function emulateTopLinks($block)
152:     {
153:         $this->_applyAccountLinksPersistentData();
154:         $block->removeLinkByUrl(Mage::getUrl('customer/account/login'));
155:     }
156: 
157:     /**
158:      * Emulate quote by persistent data
159:      *
160:      * @param Varien_Event_Observer $observer
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:         /** @var $action Mage_Checkout_OnepageController */
175:         $action = $observer->getEvent()->getControllerAction();
176:         $actionName = $action->getFullActionName();
177: 
178:         if (in_array($actionName, $stopActions)) {
179:             return;
180:         }
181: 
182:         /** @var $checkoutSession Mage_Checkout_Model_Session */
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:      * Set persistent data into quote
194:      *
195:      * @param Varien_Event_Observer $observer
196:      */
197:     public function setQuotePersistentData($observer)
198:     {
199:         if (!$this->_isPersistent()) {
200:             return;
201:         }
202: 
203:         /** @var $quote Mage_Sales_Model_Quote */
204:         $quote = $observer->getEvent()->getQuote();
205:         if (!$quote) {
206:             return;
207:         }
208: 
209:         if ($this->_isGuestShoppingCart() && $this->_setQuotePersistent) {
210:             //Quote is not actual customer's quote, just persistent
211:             $quote->setIsActive(false)->setIsPersistent(true);
212:         }
213:     }
214: 
215:     /**
216:      * Set quote to be loaded even if not active
217:      *
218:      * @param Varien_Event_Observer $observer
219:      */
220:     public function setLoadPersistentQuote($observer)
221:     {
222:         if (!$this->_isGuestShoppingCart()) {
223:             return;
224:         }
225: 
226:         /** @var $checkoutSession Mage_Checkout_Model_Session */
227:         $checkoutSession = $observer->getEvent()->getCheckoutSession();
228:         if ($checkoutSession) {
229:             $checkoutSession->setLoadInactive();
230:         }
231:     }
232: 
233:     /**
234:      * Prevent clear checkout session
235:      *
236:      * @param Varien_Event_Observer $observer
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:      * Make persistent quote to be guest
249:      *
250:      * @param Varien_Event_Observer $observer
251:      */
252:     public function makePersistentQuoteGuest($observer)
253:     {
254:         if (!$this->_checkClearCheckoutSessionNecessity($observer)) {
255:             return;
256:         }
257: 
258:         $this->setQuoteGuest(true);
259:     }
260: 
261:     /**
262:      * Check if checkout session should NOT be cleared
263:      *
264:      * @param Varien_Event_Observer $observer
265:      * @return bool|Mage_Persistent_IndexController
266:      */
267:     protected function _checkClearCheckoutSessionNecessity($observer)
268:     {
269:         if (!$this->_isGuestShoppingCart()) {
270:             return false;
271:         }
272: 
273:         /** @var $action Mage_Persistent_IndexController */
274:         $action = $observer->getEvent()->getControllerAction();
275:         if ($action instanceof Mage_Persistent_IndexController) {
276:             return $action;
277:         }
278: 
279:         return false;
280:     }
281: 
282:     /**
283:      * Reset session data when customer re-authenticates
284:      *
285:      * @param Varien_Event_Observer $observer
286:      */
287:     public function customerAuthenticatedEvent($observer)
288:     {
289:         /** @var $customerSession Mage_Customer_Model_Session */
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:      * Unset persistent cookie and make customer's quote as a guest
303:      *
304:      * @param Varien_Event_Observer $observer
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:         /** @var $customerSession Mage_Customer_Model_Session */
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:      * Disable guest checkout if we are in persistent mode
324:      *
325:      * @param Varien_Event_Observer $observer
326:      */
327:     public function disableGuestCheckout($observer)
328:     {
329:         if ($this->_getPersistentHelper()->isPersistent()) {
330:             $observer->getEvent()->getResult()->setIsAllowed(false);
331:         }
332:     }
333: 
334:     /**
335:      * Prevent express checkout with Google checkout and PayPal Express checkout
336:      *
337:      * @param Varien_Event_Observer $observer
338:      */
339:     public function preventExpressCheckout($observer)
340:     {
341:         if (!$this->_isLoggedOut()) {
342:             return;
343:         }
344: 
345:         /** @var $controllerAction Mage_Core_Controller_Front_Action */
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:      * Retrieve persistent customer instance
363:      *
364:      * @return Mage_Customer_Model_Customer
365:      */
366:     protected function _getPersistentCustomer()
367:     {
368:         return Mage::getModel('customer/customer')->load(
369:             $this->_getPersistentHelper()->getSession()->getCustomerId()
370:         );
371:     }
372: 
373:     /**
374:      * Retrieve persistent helper
375:      *
376:      * @return Mage_Persistent_Helper_Session
377:      */
378:     protected function _getPersistentHelper()
379:     {
380:         return Mage::helper('persistent/session');
381:     }
382: 
383:     /**
384:      * Return current active quote for persistent customer
385:      *
386:      * @return Mage_Sales_Model_Quote
387:      */
388:     protected function _getQuote()
389:     {
390:         $quote = Mage::getModel('sales/quote');
391:         $quote->loadByCustomer($this->_getPersistentCustomer());
392:         return $quote;
393:     }
394: 
395:     /**
396:      * Check whether shopping cart is persistent
397:      *
398:      * @return bool
399:      */
400:     protected function _isShoppingCartPersist()
401:     {
402:         return Mage::helper('persistent')->isShoppingCartPersist();
403:     }
404: 
405:     /**
406:      * Check whether persistent mode is running
407:      *
408:      * @return bool
409:      */
410:     protected function _isPersistent()
411:     {
412:         return $this->_getPersistentHelper()->isPersistent();
413:     }
414: 
415:     /**
416:      * Check if persistent mode is running and customer is logged out
417:      *
418:      * @return bool
419:      */
420:     protected function _isLoggedOut()
421:     {
422:         return $this->_isPersistent() && !Mage::getSingleton('customer/session')->isLoggedIn();
423:     }
424: 
425:     /**
426:      * Check if shopping cart is guest while persistent session and user is logged out
427:      *
428:      * @return bool
429:      */
430:     protected function _isGuestShoppingCart()
431:     {
432:         return $this->_isLoggedOut() && !Mage::helper('persistent')->isShoppingCartPersist();
433:     }
434: 
435:     /**
436:      * Make quote to be guest
437:      *
438:      * @param bool $checkQuote Check quote to be persistent (not stolen)
439:      */
440:     public function setQuoteGuest($checkQuote = false)
441:     {
442:         /** @var $quote Mage_Sales_Model_Quote */
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:             //Create guest addresses
463:             $quote->getShippingAddress();
464:             $quote->getBillingAddress();
465:             $quote->collectTotals()->save();
466:         }
467: 
468:         $this->_getPersistentHelper()->getSession()->removePersistentCookie();
469:     }
470: 
471:     /**
472:      * Check and clear session data if persistent session expired
473:      *
474:      * @param Varien_Event_Observer $observer
475:      */
476:     public function checkExpirePersistentQuote(Varien_Event_Observer $observer)
477:     {
478:         if (!Mage::helper('persistent')->canProcess($observer)) {
479:             return;
480:         }
481: 
482:         /** @var $customerSession Mage_Customer_Model_Session */
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:             // persistent session does not expire on onepage checkout page to not spoil customer group id
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:         /** @var $checkoutSession Mage_Checkout_Model_Session */
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:      * Clear expired persistent sessions
517:      *
518:      * @param Mage_Cron_Model_Schedule $schedule
519:      * @return Mage_Persistent_Model_Observer_Cron
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:      * Create handle for persistent session if persistent cookie and customer not logged in
537:      *
538:      * @param Varien_Event_Observer $observer
539:      */
540:     public function createPersistentHandleLayout(Varien_Event_Observer $observer)
541:     {
542:         /** @var $layout Mage_Core_Model_Layout */
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:      * Update customer id and customer group id if user is in persistent session
556:      *
557:      * @param Varien_Event_Observer $observer
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:      * Set persistent data to customer session
575:      *
576:      * @param Varien_Event_Observer $observer
577:      * @return Mage_Persistent_Model_Observer
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:             /** @var $customer Mage_Customer_Model_Customer */
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: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0