PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/app/code/core/Mage/Persistent/Model/Observer.php

https://bitbucket.org/jit_bec/shopifine
PHP | 598 lines | 330 code | 65 blank | 203 comment | 59 complexity | 7bfaa855baa9ab3b82c581f08003caef MD5 | raw file
Possible License(s): LGPL-3.0
  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. * Persistent Observer
  28. *
  29. * @category Mage
  30. * @package Mage_Persistent
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Persistent_Model_Observer
  34. {
  35. /**
  36. * Whether set quote to be persistent in workflow
  37. *
  38. * @var bool
  39. */
  40. protected $_setQuotePersistent = true;
  41. /**
  42. * Apply persistent data
  43. *
  44. * @param Varien_Event_Observer $observer
  45. * @return Mage_Persistent_Model_Observer
  46. */
  47. public function applyPersistentData($observer)
  48. {
  49. if (!Mage::helper('persistent')->canProcess($observer)
  50. || !$this->_getPersistentHelper()->isPersistent() || Mage::getSingleton('customer/session')->isLoggedIn()) {
  51. return $this;
  52. }
  53. Mage::getModel('persistent/persistent_config')
  54. ->setConfigFilePath(Mage::helper('persistent')->getPersistentConfigFilePath())
  55. ->fire();
  56. return $this;
  57. }
  58. /**
  59. * Apply persistent data to specific block
  60. *
  61. * @param Varien_Event_Observer $observer
  62. * @return Mage_Persistent_Model_Observer
  63. */
  64. public function applyBlockPersistentData($observer)
  65. {
  66. if (!$this->_getPersistentHelper()->isPersistent() || Mage::getSingleton('customer/session')->isLoggedIn()) {
  67. return $this;
  68. }
  69. /** @var $block Mage_Core_Block_Abstract */
  70. $block = $observer->getEvent()->getBlock();
  71. if (!$block) {
  72. return $this;
  73. }
  74. $xPath = '//instances/blocks/*[block_type="' . get_class($block) . '"]';
  75. $configFilePath = $observer->getEvent()->getConfigFilePath();
  76. /** @var $persistentConfig Mage_Persistent_Model_Persistent_Config */
  77. $persistentConfig = Mage::getModel('persistent/persistent_config')
  78. ->setConfigFilePath(
  79. $configFilePath ? $configFilePath : Mage::helper('persistent')->getPersistentConfigFilePath()
  80. );
  81. foreach ($persistentConfig->getXmlConfig()->xpath($xPath) as $persistentConfigInfo) {
  82. $persistentConfig->fireOne($persistentConfigInfo->asArray(), $block);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * Emulate 'welcome' block with persistent data
  88. *
  89. * @param Mage_Core_Block_Abstract $block
  90. * @return Mage_Persistent_Model_Observer
  91. */
  92. public function emulateWelcomeBlock($block)
  93. {
  94. $block->setWelcome(
  95. Mage::helper('persistent')->__('Welcome, %s!', Mage::helper('core')->escapeHtml($this->_getPersistentCustomer()->getName(), null))
  96. );
  97. $this->_applyAccountLinksPersistentData();
  98. $block->setAdditionalHtml(Mage::app()->getLayout()->getBlock('header.additional')->toHtml());
  99. return $this;
  100. }
  101. /**
  102. * Emulate 'account links' block with persistent data
  103. */
  104. protected function _applyAccountLinksPersistentData()
  105. {
  106. if (!Mage::app()->getLayout()->getBlock('header.additional')) {
  107. Mage::app()->getLayout()->addBlock('persistent/header_additional', 'header.additional');
  108. }
  109. }
  110. /**
  111. * Emulate 'account links' block with persistent data
  112. *
  113. * @param Mage_Core_Block_Abstract $block
  114. */
  115. public function emulateAccountLinks($block)
  116. {
  117. $this->_applyAccountLinksPersistentData();
  118. $block->getCacheKeyInfo();
  119. $block->addLink(
  120. Mage::helper('persistent')->getPersistentName(),
  121. Mage::helper('persistent')->getUnsetCookieUrl(),
  122. Mage::helper('persistent')->getPersistentName(),
  123. false,
  124. array(),
  125. 110
  126. );
  127. $block->removeLinkByUrl(Mage::helper('customer')->getRegisterUrl());
  128. $block->removeLinkByUrl(Mage::helper('customer')->getLoginUrl());
  129. }
  130. /**
  131. * Emulate 'top links' block with persistent data
  132. *
  133. * @param Mage_Core_Block_Abstract $block
  134. */
  135. public function emulateTopLinks($block)
  136. {
  137. $this->_applyAccountLinksPersistentData();
  138. $block->removeLinkByUrl(Mage::getUrl('customer/account/login'));
  139. }
  140. /**
  141. * Emulate quote by persistent data
  142. *
  143. * @param Varien_Event_Observer $observer
  144. */
  145. public function emulateQuote($observer)
  146. {
  147. $stopActions = array(
  148. 'persistent_index_saveMethod',
  149. 'customer_account_createpost'
  150. );
  151. if (!Mage::helper('persistent')->canProcess($observer)
  152. || !$this->_getPersistentHelper()->isPersistent() || Mage::getSingleton('customer/session')->isLoggedIn()) {
  153. return;
  154. }
  155. /** @var $action Mage_Checkout_OnepageController */
  156. $action = $observer->getEvent()->getControllerAction();
  157. $actionName = $action->getFullActionName();
  158. if (in_array($actionName, $stopActions)) {
  159. return;
  160. }
  161. /** @var $checkoutSession Mage_Checkout_Model_Session */
  162. $checkoutSession = Mage::getSingleton('checkout/session');
  163. if ($this->_isShoppingCartPersist()) {
  164. $checkoutSession->setCustomer($this->_getPersistentCustomer());
  165. if (!$checkoutSession->hasQuote()) {
  166. $checkoutSession->getQuote();
  167. }
  168. }
  169. }
  170. /**
  171. * Set persistent data into quote
  172. *
  173. * @param Varien_Event_Observer $observer
  174. */
  175. public function setQuotePersistentData($observer)
  176. {
  177. if (!$this->_isPersistent()) {
  178. return;
  179. }
  180. /** @var $quote Mage_Sales_Model_Quote */
  181. $quote = $observer->getEvent()->getQuote();
  182. if (!$quote) {
  183. return;
  184. }
  185. if ($this->_isGuestShoppingCart() && $this->_setQuotePersistent) {
  186. //Quote is not actual customer's quote, just persistent
  187. $quote->setIsActive(false)->setIsPersistent(true);
  188. }
  189. }
  190. /**
  191. * Set quote to be loaded even if not active
  192. *
  193. * @param Varien_Event_Observer $observer
  194. */
  195. public function setLoadPersistentQuote($observer)
  196. {
  197. if (!$this->_isGuestShoppingCart()) {
  198. return;
  199. }
  200. /** @var $checkoutSession Mage_Checkout_Model_Session */
  201. $checkoutSession = $observer->getEvent()->getCheckoutSession();
  202. if ($checkoutSession) {
  203. $checkoutSession->setLoadInactive();
  204. }
  205. }
  206. /**
  207. * Prevent clear checkout session
  208. *
  209. * @param Varien_Event_Observer $observer
  210. */
  211. public function preventClearCheckoutSession($observer)
  212. {
  213. $action = $this->_checkClearCheckoutSessionNecessity($observer);
  214. if ($action) {
  215. $action->setClearCheckoutSession(false);
  216. }
  217. }
  218. /**
  219. * Make persistent quote to be guest
  220. *
  221. * @param Varien_Event_Observer $observer
  222. */
  223. public function makePersistentQuoteGuest($observer)
  224. {
  225. if (!$this->_checkClearCheckoutSessionNecessity($observer)) {
  226. return;
  227. }
  228. $this->setQuoteGuest(true);
  229. }
  230. /**
  231. * Check if checkout session should NOT be cleared
  232. *
  233. * @param Varien_Event_Observer $observer
  234. * @return bool|Mage_Persistent_IndexController
  235. */
  236. protected function _checkClearCheckoutSessionNecessity($observer)
  237. {
  238. if (!$this->_isGuestShoppingCart()) {
  239. return false;
  240. }
  241. /** @var $action Mage_Persistent_IndexController */
  242. $action = $observer->getEvent()->getControllerAction();
  243. if ($action instanceof Mage_Persistent_IndexController) {
  244. return $action;
  245. }
  246. return false;
  247. }
  248. /**
  249. * Reset session data when customer re-authenticates
  250. *
  251. * @param Varien_Event_Observer $observer
  252. */
  253. public function customerAuthenticatedEvent($observer)
  254. {
  255. /** @var $customerSession Mage_Customer_Model_Session */
  256. $customerSession = Mage::getSingleton('customer/session');
  257. $customerSession->setCustomerId(null)->setCustomerGroupId(null);
  258. if (Mage::app()->getRequest()->getParam('context') != 'checkout') {
  259. $this->_expirePersistentSession();
  260. return;
  261. }
  262. $this->setQuoteGuest();
  263. }
  264. /**
  265. * Unset persistent cookie and make customer's quote as a guest
  266. *
  267. * @param Varien_Event_Observer $observer
  268. */
  269. public function removePersistentCookie($observer)
  270. {
  271. if (!Mage::helper('persistent')->canProcess($observer) || !$this->_isPersistent()) {
  272. return;
  273. }
  274. $this->_getPersistentHelper()->getSession()->removePersistentCookie();
  275. /** @var $customerSession Mage_Customer_Model_Session */
  276. $customerSession = Mage::getSingleton('customer/session');
  277. if (!$customerSession->isLoggedIn()) {
  278. $customerSession->setCustomerId(null)->setCustomerGroupId(null);
  279. }
  280. $this->setQuoteGuest();
  281. }
  282. /**
  283. * Disable guest checkout if we are in persistent mode
  284. *
  285. * @param Varien_Event_Observer $observer
  286. */
  287. public function disableGuestCheckout($observer)
  288. {
  289. if ($this->_getPersistentHelper()->isPersistent()) {
  290. $observer->getEvent()->getResult()->setIsAllowed(false);
  291. }
  292. }
  293. /**
  294. * Prevent express checkout with Google checkout and PayPal Express checkout
  295. *
  296. * @param Varien_Event_Observer $observer
  297. */
  298. public function preventExpressCheckout($observer)
  299. {
  300. if (!$this->_isLoggedOut()) {
  301. return;
  302. }
  303. /** @var $controllerAction Mage_Core_Controller_Front_Action */
  304. $controllerAction = $observer->getEvent()->getControllerAction();
  305. if (is_callable(array($controllerAction, 'redirectLogin'))) {
  306. Mage::getSingleton('core/session')->addNotice(
  307. Mage::helper('persistent')->__('To proceed to Checkout, please log in using your email address.')
  308. );
  309. $controllerAction->redirectLogin();
  310. if ($controllerAction instanceof Mage_GoogleCheckout_RedirectController
  311. || $controllerAction instanceof Mage_Paypal_Controller_Express_Abstract
  312. ) {
  313. Mage::getSingleton('customer/session')
  314. ->setBeforeAuthUrl(Mage::getUrl('persistent/index/expressCheckout'));
  315. }
  316. }
  317. }
  318. /**
  319. * Retrieve persistent customer instance
  320. *
  321. * @return Mage_Customer_Model_Customer
  322. */
  323. protected function _getPersistentCustomer()
  324. {
  325. return Mage::getModel('customer/customer')->load(
  326. $this->_getPersistentHelper()->getSession()->getCustomerId()
  327. );
  328. }
  329. /**
  330. * Retrieve persistent helper
  331. *
  332. * @return Mage_Persistent_Helper_Session
  333. */
  334. protected function _getPersistentHelper()
  335. {
  336. return Mage::helper('persistent/session');
  337. }
  338. /**
  339. * Return current active quote for persistent customer
  340. *
  341. * @return Mage_Sales_Model_Quote
  342. */
  343. protected function _getQuote()
  344. {
  345. $quote = Mage::getModel('sales/quote');
  346. $quote->loadByCustomer($this->_getPersistentCustomer());
  347. return $quote;
  348. }
  349. /**
  350. * Check whether shopping cart is persistent
  351. *
  352. * @return bool
  353. */
  354. protected function _isShoppingCartPersist()
  355. {
  356. return Mage::helper('persistent')->isShoppingCartPersist();
  357. }
  358. /**
  359. * Check whether persistent mode is running
  360. *
  361. * @return bool
  362. */
  363. protected function _isPersistent()
  364. {
  365. return $this->_getPersistentHelper()->isPersistent();
  366. }
  367. /**
  368. * Check if persistent mode is running and customer is logged out
  369. *
  370. * @return bool
  371. */
  372. protected function _isLoggedOut()
  373. {
  374. return $this->_isPersistent() && !Mage::getSingleton('customer/session')->isLoggedIn();
  375. }
  376. /**
  377. * Check if shopping cart is guest while persistent session and user is logged out
  378. *
  379. * @return bool
  380. */
  381. protected function _isGuestShoppingCart()
  382. {
  383. return $this->_isLoggedOut() && !Mage::helper('persistent')->isShoppingCartPersist();
  384. }
  385. /**
  386. * Make quote to be guest
  387. *
  388. * @param bool $checkQuote Check quote to be persistent (not stolen)
  389. */
  390. public function setQuoteGuest($checkQuote = false)
  391. {
  392. /** @var $quote Mage_Sales_Model_Quote */
  393. $quote = Mage::getSingleton('checkout/session')->getQuote();
  394. if ($quote && $quote->getId()) {
  395. if ($checkQuote && !Mage::helper('persistent')->isShoppingCartPersist() && !$quote->getIsPersistent()) {
  396. Mage::getSingleton('checkout/session')->unsetAll();
  397. return;
  398. }
  399. $quote->getPaymentsCollection()->walk('delete');
  400. $quote->getAddressesCollection()->walk('delete');
  401. $this->_setQuotePersistent = false;
  402. $quote
  403. ->setIsActive(true)
  404. ->setCustomerId(null)
  405. ->setCustomerEmail(null)
  406. ->setCustomerFirstname(null)
  407. ->setCustomerLastname(null)
  408. ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
  409. ->setIsPersistent(false)
  410. ->removeAllAddresses();
  411. //Create guest addresses
  412. $quote->getShippingAddress();
  413. $quote->getBillingAddress();
  414. $quote->collectTotals()->save();
  415. }
  416. $this->_getPersistentHelper()->getSession()->removePersistentCookie();
  417. }
  418. /**
  419. * Check and clear session data if persistent session expired
  420. *
  421. * @param Varien_Event_Observer $observer
  422. */
  423. public function checkExpirePersistentQuote(Varien_Event_Observer $observer)
  424. {
  425. if (!Mage::helper('persistent')->canProcess($observer)) {
  426. return;
  427. }
  428. /** @var $customerSession Mage_Customer_Model_Session */
  429. $customerSession = Mage::getSingleton('customer/session');
  430. if (Mage::helper('persistent')->isEnabled()
  431. && !$this->_isPersistent()
  432. && !$customerSession->isLoggedIn()
  433. && Mage::getSingleton('checkout/session')->getQuoteId()
  434. && !($observer->getControllerAction() instanceof Mage_Checkout_OnepageController)
  435. // persistent session does not expire on onepage checkout page to not spoil customer group id
  436. ) {
  437. Mage::dispatchEvent('persistent_session_expired');
  438. $this->_expirePersistentSession();
  439. $customerSession->setCustomerId(null)->setCustomerGroupId(null);
  440. }
  441. }
  442. protected function _expirePersistentSession()
  443. {
  444. /** @var $checkoutSession Mage_Checkout_Model_Session */
  445. $checkoutSession = Mage::getSingleton('checkout/session');
  446. $quote = $checkoutSession->setLoadInactive()->getQuote();
  447. if ($quote->getIsActive() && $quote->getCustomerId()) {
  448. $checkoutSession->setCustomer(null)->unsetAll();
  449. } else {
  450. $quote
  451. ->setIsActive(true)
  452. ->setIsPersistent(false)
  453. ->setCustomerId(null)
  454. ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
  455. }
  456. }
  457. /**
  458. * Clear expired persistent sessions
  459. *
  460. * @param Mage_Cron_Model_Schedule $schedule
  461. * @return Mage_Persistent_Model_Observer_Cron
  462. */
  463. public function clearExpiredCronJob(Mage_Cron_Model_Schedule $schedule)
  464. {
  465. $websiteIds = Mage::getResourceModel('core/website_collection')->getAllIds();
  466. if (!is_array($websiteIds)) {
  467. return $this;
  468. }
  469. foreach ($websiteIds as $websiteId) {
  470. Mage::getModel('persistent/session')->deleteExpired($websiteId);
  471. }
  472. return $this;
  473. }
  474. /**
  475. * Create handle for persistent session if persistent cookie and customer not logged in
  476. *
  477. * @param Varien_Event_Observer $observer
  478. */
  479. public function createPersistentHandleLayout(Varien_Event_Observer $observer)
  480. {
  481. /** @var $layout Mage_Core_Model_Layout */
  482. $layout = $observer->getEvent()->getLayout();
  483. if (Mage::helper('persistent')->canProcess($observer) && $layout && Mage::helper('persistent')->isEnabled()
  484. && Mage::helper('persistent/session')->isPersistent()
  485. ) {
  486. $handle = (Mage::getSingleton('customer/session')->isLoggedIn())
  487. ? Mage_Persistent_Helper_Data::LOGGED_IN_LAYOUT_HANDLE
  488. : Mage_Persistent_Helper_Data::LOGGED_OUT_LAYOUT_HANDLE;
  489. $layout->getUpdate()->addHandle($handle);
  490. }
  491. }
  492. /**
  493. * Update customer id and customer group id if user is in persistent session
  494. *
  495. * @param Varien_Event_Observer $observer
  496. */
  497. public function updateCustomerCookies(Varien_Event_Observer $observer)
  498. {
  499. if (!$this->_isPersistent()) {
  500. return;
  501. }
  502. $customerCookies = $observer->getEvent()->getCustomerCookies();
  503. if ($customerCookies instanceof Varien_Object) {
  504. $persistentCustomer = $this->_getPersistentCustomer();
  505. $customerCookies->setCustomerId($persistentCustomer->getId());
  506. $customerCookies->setCustomerGroupId($persistentCustomer->getGroupId());
  507. }
  508. }
  509. /**
  510. * Set persistent data to customer session
  511. *
  512. * @param Varien_Event_Observer $observer
  513. * @return Mage_Persistent_Model_Observer
  514. */
  515. public function emulateCustomer($observer)
  516. {
  517. if (!Mage::helper('persistent')->canProcess($observer)
  518. || !$this->_isShoppingCartPersist()
  519. ) {
  520. return $this;
  521. }
  522. if ($this->_isLoggedOut()) {
  523. /** @var $customer Mage_Customer_Model_Customer */
  524. $customer = Mage::getModel('customer/customer')->load(
  525. $this->_getPersistentHelper()->getSession()->getCustomerId()
  526. );
  527. Mage::getSingleton('customer/session')
  528. ->setCustomerId($customer->getId())
  529. ->setCustomerGroupId($customer->getGroupId());
  530. }
  531. return $this;
  532. }
  533. }