-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8693243
commit 1368014
Showing
7 changed files
with
283 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,27 @@ | ||
mage-sorted-db-layouts | ||
magento-sorted-db-layouts | ||
====================== | ||
|
||
Modifies (fixes?) Magento 1.x core when fetching layout updates from core_layout. They are now applied in correct sort_order. | ||
|
||
Useful when you want to specify sort order for widgets. Check [this stackexchange thread](http://magento.stackexchange.com/questions/14744/widgets-sort-order-in-different-design-packages) for an error description. | ||
|
||
Install with composer | ||
--------------------- | ||
|
||
``` | ||
"require": { | ||
"gruenspar/magento-sorted-db-layouts":"1.*" | ||
}, | ||
"repositories": [ | ||
{ | ||
"url": "https://github.com/gruenspar/magento-sorted-db-layouts", | ||
"type": "git" | ||
} | ||
] | ||
``` | ||
|
||
Rewrites | ||
-------- | ||
|
||
- `Mage_Core_Model_Layout_Update` | ||
- `Mage_Core_Model_Resource_Layout` |
117 changes: 117 additions & 0 deletions
117
app/code/community/Gruenspar/SortedDbLayouts/Model/Layout/Update.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Gruenspar_SortedDbLayouts. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Gruenspar | ||
* @package Gruenspar_SortedDbLayouts | ||
* @author Jan Papenbrock <j.papenbrock@gruenspar.de> | ||
* @copyright 2014 Gruenspar IT (http://www.gruenspar.de) | ||
* @license MIT | ||
* @link http://www.gruenspar.de | ||
* @since 1.0.0 | ||
*/ | ||
|
||
/** | ||
* Layout update model. | ||
* | ||
* @category Gruenspar | ||
* @package Gruenspar_SortedDbLayouts | ||
* @author Jan Papenbrock <j.papenbrock@gruenspar.de> | ||
* @license MIT | ||
*/ | ||
class Gruenspar_SortedDbLayouts_Model_Layout_Update extends Mage_Core_Model_Layout_Update | ||
{ | ||
/** | ||
* Add layout update in a sort order. | ||
* | ||
* @param string $update Update XML string. | ||
* @param int $sortOrder Sort order | ||
* | ||
* @return $this | ||
*/ | ||
public function addSortedUpdate($update, $sortOrder) | ||
{ | ||
$veryLargeIndex = 1000000; | ||
$sortOrder = $sortOrder * 100; // allows 100 elements with the same sort order | ||
|
||
$index = $veryLargeIndex + $sortOrder - 1; | ||
|
||
do { | ||
$index++; | ||
} while (isset($this->_updates[$index])); | ||
|
||
|
||
$this->_updates[$index] = $update; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Fetch layout updates from DB for given handle. | ||
* | ||
* @param string $handle Handle string. | ||
* | ||
* @return bool | ||
*/ | ||
public function fetchDbLayoutUpdates($handle) | ||
{ | ||
$_profilerKey = 'layout/db_update: '.$handle; | ||
Varien_Profiler::start($_profilerKey); | ||
$updates = $this->_getUpdates($handle); | ||
if (!count($updates)) { | ||
return false; | ||
} | ||
|
||
foreach ($updates as $update) { | ||
$updateStr = $update['xml']; | ||
$sortOrder = $update['sort_order']; | ||
|
||
$updateStr = '<update_xml>' . $updateStr . '</update_xml>'; | ||
$updateStr = str_replace($this->_subst['from'], $this->_subst['to'], $updateStr); | ||
$updateXml = simplexml_load_string($updateStr, $this->getElementClass()); | ||
$this->fetchRecursiveUpdates($updateXml); | ||
$this->addSortedUpdate($updateXml->innerXml(), $sortOrder); | ||
} | ||
|
||
ksort($this->_updates); | ||
|
||
Varien_Profiler::stop($_profilerKey); | ||
return true; | ||
} | ||
|
||
/** | ||
* Get updates by handle | ||
* | ||
* @param string $handle | ||
* | ||
* @return array | ||
*/ | ||
protected function _getUpdates($handle) | ||
{ | ||
return Mage::getResourceModel('core/layout')->fetchUpdatesByHandle($handle); | ||
} | ||
|
||
/** | ||
* Get update string | ||
* | ||
* @deprecated | ||
* | ||
* @param string $handle | ||
* | ||
* @return string | ||
*/ | ||
protected function _getUpdateString($handle) | ||
{ | ||
$updates = $this->_getUpdates($handle); | ||
$result = array(); | ||
|
||
foreach ($updates as $update) { | ||
$result[] = $update['xml']; | ||
} | ||
|
||
return implode("", $result); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
app/code/community/Gruenspar/SortedDbLayouts/Model/Resource/Layout.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Gruenspar_SortedDbLayouts. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Gruenspar | ||
* @package Gruenspar_SortedDbLayouts | ||
* @author Jan Papenbrock <j.papenbrock@gruenspar.de> | ||
* @copyright 2014 Gruenspar IT (http://www.gruenspar.de) | ||
* @license MIT | ||
* @link http://www.gruenspar.de | ||
* @since 1.0.0 | ||
*/ | ||
|
||
/** | ||
* Layout resource model. | ||
* | ||
* @category Gruenspar | ||
* @package Gruenspar_SortedDbLayouts | ||
* @author Jan Papenbrock <j.papenbrock@gruenspar.de> | ||
* @license MIT | ||
*/ | ||
class Gruenspar_SortedDbLayouts_Model_Resource_Layout extends Mage_Core_Model_Resource_Layout | ||
{ | ||
|
||
/** | ||
* Retrieve layout updates by handle. | ||
* | ||
* Overridden to load layout updates for all templates in the design | ||
* hierarchy, not only for exact template match. | ||
* | ||
* @param string $handle Handle. | ||
* @param array $params Params. | ||
* | ||
* @return string | ||
*/ | ||
public function fetchUpdatesByHandle($handle, $params = array()) | ||
{ | ||
$bind = array( | ||
'store_id' => Mage::app()->getStore()->getId(), | ||
'area' => Mage::getSingleton('core/design_package')->getArea(), | ||
'package' => Mage::getSingleton('core/design_package')->getPackageName(), | ||
'theme' => Mage::getSingleton('core/design_package')->getTheme('layout') | ||
); | ||
|
||
foreach ($params as $key => $value) { | ||
if (isset($bind[$key])) { | ||
$bind[$key] = $value; | ||
} | ||
} | ||
$bind['layout_update_handle'] = $handle; | ||
$result = ''; | ||
|
||
$readAdapter = $this->_getReadAdapter(); | ||
if ($readAdapter) { | ||
$select = $readAdapter->select() | ||
->from( | ||
array('layout_update' => $this->getMainTable()), | ||
array($this->getIdFieldName(), 'xml', 'sort_order') | ||
) | ||
->join( | ||
array( | ||
'link'=>$this->getTable('core/layout_link') | ||
), | ||
'link.layout_update_id=layout_update.layout_update_id', | ||
'' | ||
) | ||
->where('link.store_id IN (0, :store_id)') | ||
->where('link.area = :area') | ||
->where('link.package = :package') | ||
->where('link.theme = :theme') | ||
->where('layout_update.handle = :layout_update_handle') | ||
->order('layout_update.sort_order ' . Varien_Db_Select::SQL_ASC); | ||
|
||
$result = $readAdapter->fetchAssoc($select, $bind); | ||
} | ||
return $result; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/code/community/Gruenspar/SortedDbLayouts/etc/config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<config> | ||
<modules> | ||
<Gruenspar_SortedDbLayouts> | ||
<version>1.0.0</version> | ||
</Gruenspar_SortedDbLayouts> | ||
</modules> | ||
<global> | ||
<models> | ||
<core> | ||
<rewrite> | ||
<layout_update>Gruenspar_SortedDbLayouts_Model_Layout_Update</layout_update> | ||
</rewrite> | ||
</core> | ||
<core_resource> | ||
<rewrite> | ||
<layout>Gruenspar_SortedDbLayouts_Model_Resource_Layout</layout> | ||
</rewrite> | ||
</core_resource> | ||
</models> | ||
</global> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0"?> | ||
<config> | ||
<modules> | ||
<Gruenspar_SortedDbLayouts> | ||
<active>true</active> | ||
<codePool>community</codePool> | ||
</Gruenspar_SortedDbLayouts> | ||
</modules> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "gruenspar/magento-sorted-db-layouts", | ||
"type": "magento-module", | ||
"license": "MIT", | ||
"description": "Magento: Sorted DB layouts.", | ||
"homepage": "https://github.com/gruenspar/magento-sorted-db-layouts", | ||
"authors": [ | ||
{ | ||
"name": "Jan Papenbrock", | ||
"email": "j.papenbrock@gruenspar.de", | ||
"homepage": "https://www.gruenspar.de", | ||
"role": "Developer" | ||
} | ||
], | ||
"extra": { | ||
"map": [ | ||
["app/code/community/Gruenspar/SortedDbLayouts", "app/code/community/Gruenspar/SortedDbLayouts"], | ||
["app/etc/modules/Gruenspar_SortedDbLayouts.xml", "app/etc/modules/Gruenspar_SortedDbLayouts.xml"] | ||
] | ||
}, | ||
"require": { | ||
"php": ">=5.3.0" | ||
}, | ||
"suggest": { | ||
"magento-hackathon/magento-composer-installer": "*" | ||
} | ||
} |