PageRenderTime 175ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/JackoPlane/magento2
PHP | 167 lines | 84 code | 13 blank | 70 comment | 11 complexity | e253e9a7b69458c5ae13c20397021770 MD5 | raw file
Possible License(s): CC-BY-SA-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_DesignEditor
  23. * @copyright Copyright (c) 2013 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 = array_shift($moveItem);
  123. $firstMoveElement = $firstMove['change'];
  124. $originContainer = $firstMoveElement->getData('origin_container');
  125. $originOrder = $firstMoveElement->getData('origin_order');
  126. $hasContainerChanged = $lastMoveElement->getData('destination_container') != $originContainer;
  127. if (!$hasContainerChanged) {
  128. $hasOrderChanged = $lastMoveElement->getData('destination_order') != $originOrder;
  129. if (!$hasOrderChanged) {
  130. $arrayToRemove = array($lastMove);
  131. }
  132. }
  133. if (empty($arrayToRemove)
  134. || $firstMoveElement->getData(Mage_DesignEditor_Model_History::SYSTEM_LAYOUT_UPDATE_FLAG) != true) {
  135. array_unshift($moveItem, $firstMove);
  136. }
  137. $this->_removeElements(array_merge($arrayToRemove, $moveItem));
  138. }
  139. return $this;
  140. }
  141. /**
  142. * Remove array of elements from change collection
  143. *
  144. * @param array $elements
  145. * @return Mage_DesignEditor_Model_History_Compact_Layout
  146. */
  147. protected function _removeElements($elements)
  148. {
  149. foreach ($elements as $element) {
  150. $this->getChangesCollection()->removeItemByKey($element['collection_key']);
  151. }
  152. return $this;
  153. }
  154. }