PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/local/TM/CheckoutSuccess/Model/Observer.php

https://bitbucket.org/maconeto/proman
PHP | 186 lines | 152 code | 26 blank | 8 comment | 30 complexity | 4d8cd6226effe9c792916a8496d68ff5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class TM_CheckoutSuccess_Model_Observer
  3. {
  4. protected $_isSuccessPage = null;
  5. public function registerCurrentOrder($observer)
  6. {
  7. if (!Mage::helper('checkoutsuccess')->isEnabled()
  8. || Mage::registry('current_order')
  9. || Mage::registry('current_recurring_profile')
  10. ) {
  11. return;
  12. }
  13. // check if it is success page preview
  14. $orderToPreview = $observer->getControllerAction()->getRequest()
  15. ->getParam('previewObjectId');
  16. if ($orderToPreview) {
  17. $this->_initPreviewSuccessPage($orderToPreview);
  18. }
  19. $session = Mage::getSingleton('checkout/session');
  20. if (!$session->getLastSuccessQuoteId()) {
  21. return;
  22. }
  23. $lastQuoteId = $session->getLastQuoteId();
  24. $lastOrderId = $session->getLastOrderId();
  25. $lastRecurringProfiles = $session->getLastRecurringProfileIds();
  26. if (!$lastQuoteId || (!$lastOrderId && empty($lastRecurringProfiles))) {
  27. return;
  28. }
  29. if ($lastOrderId) {
  30. $order = Mage::getModel('sales/order')->load($lastOrderId);
  31. if ($order->getId()) {
  32. // doublecheck of successfull order loading
  33. // IWD_OrderManager cause errors (it 'hides' orders)
  34. Mage::register('current_order', Mage::getModel('sales/order')->load($lastOrderId));
  35. }
  36. } elseif ($lastRecurringProfiles) {
  37. $profileId = current($lastRecurringProfiles);
  38. Mage::register(
  39. 'current_recurring_profile',
  40. Mage::getModel('sales/recurring_profile')->load($profileId)
  41. );
  42. }
  43. }
  44. public function updateLayout($observer)
  45. {
  46. if (!Mage::helper('checkoutsuccess')->isEnabled()
  47. || !$this->isSuccessPage()) {
  48. return;
  49. }
  50. if (Mage::registry('current_order')) {
  51. Mage::app()->getLayout()->getUpdate()->addHandle('tm_checkoutsuccess_order_view');
  52. } elseif (Mage::registry('current_recurring_profile')) {
  53. Mage::app()->getLayout()->getUpdate()->addHandle('tm_checkoutsuccess_recurring_profile_view');
  54. }
  55. }
  56. public function addCustomJsCss($observer)
  57. {
  58. if (!Mage::helper('checkoutsuccess')->isEnabled()
  59. || !$this->isSuccessPage()) {
  60. return;
  61. }
  62. $head = Mage::app()->getLayout()->getBlock('head');
  63. if (!$head) {
  64. return $this;
  65. }
  66. $design = Mage::getDesign();
  67. $customFiles = array(
  68. 'skin_css' => array(
  69. 'tm/checkoutsuccess/css/custom.css'
  70. ),
  71. 'skin_js' => array(
  72. 'tm/checkoutsuccess/js/custom.js'
  73. )
  74. );
  75. foreach ($customFiles as $type => $files) {
  76. foreach ($files as $file) {
  77. $fileUrl = $design->getSkinUrl($file);
  78. // detect path to the file including package and theme
  79. preg_match('/\/skin\/frontend\/(.+)/', $fileUrl, $matches);
  80. if (empty($matches[1])) {
  81. continue;
  82. }
  83. // check is file is actually exists
  84. $baseDir = Mage::getBaseDir();
  85. $absolutePath = $baseDir . '/skin/frontend/' . $matches[1];
  86. if (is_readable($absolutePath)) {
  87. $head->addItem($type, $file, "");
  88. }
  89. }
  90. }
  91. }
  92. protected function _initPreviewSuccessPage($idToPreview)
  93. {
  94. $order = Mage::getModel('sales/order')
  95. ->loadByIncrementId($idToPreview);
  96. if (!$order->getId()) {
  97. $recurringProfile = Mage::getModel('sales/recurring_profile')
  98. ->load($idToPreview, 'reference_id');
  99. if (!$recurringProfile->getId()) {
  100. return;
  101. }
  102. }
  103. $previewAllowed = false;
  104. $switchSessionName = 'adminhtml';
  105. $currentSessionId = Mage::getSingleton('core/session')->getSessionId();
  106. $currentSessionName = Mage::getSingleton('core/session')->getSessionName();
  107. if ($currentSessionId && $currentSessionName && isset($_COOKIE[$currentSessionName])) {
  108. if (isset($_COOKIE[$switchSessionName])) {
  109. $switchSessionId = $_COOKIE[$switchSessionName];
  110. $this->_switchSession($switchSessionName, $switchSessionId);
  111. // Now you are in admin session. Get all the details you want using the below format:
  112. // $whateverData = Mage::getModel('mymodule/session')->getWhateverData();
  113. if (Mage::getModel('admin/session')->getUser()) {
  114. $previewAllowed = true;
  115. }
  116. $this->_switchSession($currentSessionName, $currentSessionId);
  117. }
  118. }
  119. if (!$previewAllowed) {
  120. return;
  121. }
  122. $session = Mage::getSingleton('checkout/session');
  123. if ($order->getId()) {
  124. $session->setLastSuccessQuoteId($order->getQuoteId());
  125. $session->setLastQuoteId($order->getQuoteId());
  126. $session->setLastOrderId($order->getId());
  127. $session->setLastRecurringProfileIds();
  128. } elseif ($recurringProfile->getId()) {
  129. $orderInfo = $recurringProfile->getOrderInfo();
  130. if (isset($orderInfo['entity_id'])) {
  131. $session->setLastSuccessQuoteId($orderInfo['entity_id']);
  132. $session->setLastQuoteId($orderInfo['entity_id']);
  133. $session->setLastOrderId('');
  134. $session->setLastRecurringProfileIds(
  135. array($recurringProfile->getId())
  136. );
  137. }
  138. }
  139. }
  140. // implamantation taken from http://stackoverflow.com/a/14359144
  141. protected function _switchSession($namespace, $id = null)
  142. {
  143. session_write_close();
  144. $session = Mage::getSingleton('core/session');
  145. if ($id) {
  146. $session->setSessionId($id);
  147. }
  148. $session
  149. ->setSkipEmptySessionCheck(true)
  150. ->start($namespace);
  151. }
  152. public function isSuccessPage()
  153. {
  154. if ($this->_isSuccessPage === null ) {
  155. $this->_isSuccessPage = Mage::helper('checkoutsuccess')
  156. ->isSuccessPage();
  157. }
  158. return $this->_isSuccessPage;
  159. }
  160. }