PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/DesignEditor/Model/History/Compact/Layout.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 161 lines | 79 code | 12 blank | 70 comment | 8 complexity | 28837650981f12696d04220d10357e1d MD5 | raw file
  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_DesignEditor
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * History compaction strategy to compact layout changes
  28. */
  29. class Mage_DesignEditor_Model_History_Compact_Layout extends Mage_DesignEditor_Model_History_CompactAbstract
  30. {
  31. /**
  32. * Scheduled move actions
  33. *
  34. * @var array
  35. */
  36. protected $_scheduledMoves = array();
  37. /**
  38. * Scheduled remove actions
  39. *
  40. * @var array
  41. */
  42. protected $_scheduledRemoves = array();
  43. /**
  44. * Run compact strategy on given collection
  45. *
  46. * @param Mage_DesignEditor_Model_Change_Collection $collection
  47. * @return Mage_DesignEditor_Model_History_Compact_Layout
  48. */
  49. public function compact($collection = null)
  50. {
  51. parent::compact($collection)->_scheduleActions()->_compactLayoutChanges();
  52. }
  53. /**
  54. * Schedule layout actions
  55. *
  56. * @return Mage_DesignEditor_Model_History_Compact_Layout
  57. */
  58. protected function _scheduleActions()
  59. {
  60. /** @var $change Mage_DesignEditor_Model_Change_LayoutAbstract */
  61. foreach ($this->getChangesCollection() as $changeKey => $change) {
  62. if (!$change instanceof Mage_DesignEditor_Model_Change_LayoutAbstract) {
  63. continue;
  64. }
  65. switch ($change->getData('action_name')) {
  66. case Mage_DesignEditor_Model_Change_Layout_Remove::LAYOUT_DIRECTIVE_REMOVE:
  67. $this->_scheduledRemoves[$change->getData('element_name')][] = array(
  68. 'collection_key' => $changeKey
  69. );
  70. break;
  71. case Mage_DesignEditor_Model_Change_Layout_Move::LAYOUT_DIRECTIVE_MOVE:
  72. $this->_scheduledMoves[$change->getData('element_name')][] = array(
  73. 'collection_key' => $changeKey,
  74. 'change' => $change
  75. );
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Compact layout changes
  85. *
  86. * @return Mage_DesignEditor_Model_History_Compact_Layout
  87. */
  88. protected function _compactLayoutChanges()
  89. {
  90. return $this->_compactRemoves()->_compactMoves();
  91. }
  92. /**
  93. * Compact remove layout directives
  94. *
  95. * @return Mage_DesignEditor_Model_History_Compact_Layout
  96. */
  97. protected function _compactRemoves()
  98. {
  99. foreach ($this->_scheduledRemoves as $itemName => $removeItem) {
  100. $arrayToRemove = array();
  101. if (!empty($this->_scheduledMoves[$itemName])) {
  102. $arrayToRemove = $this->_scheduledMoves[$itemName];
  103. }
  104. $this->_removeElements(array_merge($arrayToRemove, array_slice($removeItem, 0, count($removeItem) - 1)));
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Compact move layout directives
  110. *
  111. * @return Mage_DesignEditor_Model_History_Compact_Layout
  112. */
  113. protected function _compactMoves()
  114. {
  115. foreach ($this->_scheduledMoves as $moveItem) {
  116. if (count($moveItem) === 1) {
  117. continue;
  118. }
  119. $arrayToRemove = array();
  120. $lastMove = array_pop($moveItem);
  121. $lastMoveElement = $lastMove['change'];
  122. $firstMove = $moveItem[0]['change'];
  123. $originContainer = $firstMove->getData('origin_container');
  124. $originOrder = $firstMove->getData('origin_order');
  125. $hasContainerChanged = $lastMoveElement->getData('destination_container') != $originContainer;
  126. if (!$hasContainerChanged) {
  127. $hasOrderChanged = $lastMoveElement->getData('destination_order') != $originOrder;
  128. if (!$hasOrderChanged) {
  129. $arrayToRemove = array($lastMove);
  130. }
  131. }
  132. $this->_removeElements(array_merge($arrayToRemove, $moveItem));
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Remove array of elements from change collection
  138. *
  139. * @param array $elements
  140. * @return Mage_DesignEditor_Model_History_Compact_Layout
  141. */
  142. protected function _removeElements($elements)
  143. {
  144. foreach ($elements as $element) {
  145. $this->getChangesCollection()->removeItemByKey($element['collection_key']);
  146. }
  147. return $this;
  148. }
  149. }